重构类型系统并改进诊断功能

- 更新了类型系统,新增了类型并优化了结构。
- 引入了基类型和派生类,用于函数、结构体和接口类型。
- 实现了类型上下文,用于管理内置类型和类型解析。
- 添加了诊断类,用于收集和报告警告和错误。
- 通过改进错误处理增强了虚拟机执行,以应对递归限制问题。
- 实现了反汇编器,将字节码转换为代码,以改善调试和分析。
- 添加了新的抽象语法树节点,用于成员表达式、对象初始化、接口和结构体定义。
- 引入了语义错误测试,包括重定义、未声明的变量和无效的结构字段。
This commit is contained in:
2026-03-10 12:33:17 +08:00
parent 90448006ff
commit 0f635ccf2b
47 changed files with 2365 additions and 2541 deletions

View File

@@ -2,52 +2,55 @@
@file src/Ast/Base.hpp
@brief AstNode基类定义
@author PuqiAR (im@puqiar.top)
@date 2026-02-14
@date 2026-03-08
*/
#pragma once
#include <Core/SourceLocations.hpp>
#include <Deps/Deps.hpp>
#include <Sema/Type.hpp>
#include <cstdint>
namespace Fig
{
enum class AstType : std::uint8_t
{
AstNode, // 基类
Program, // 程序
Expr, // 表达式
Stmt, // 语句
BlockStmt, // 块语句
AstNode,
Program,
Expr,
Stmt,
BlockStmt,
/* Expressions */
IdentiExpr, // 标识符表达式
LiteralExpr, // 字面量表达式
PrefixExpr, // 一元 前缀表达式
InfixExpr, // 二元 中缀表达式
IndexExpr, // 后缀表达式,索引
CallExpr, // 后缀表达式,函数调用
IdentiExpr,
LiteralExpr,
PrefixExpr,
InfixExpr,
IndexExpr,
CallExpr,
MemberExpr, // obj.prop
ObjectInitExpr, // new Point{}
/* Statements */
ExprStmt, // 表达式语句,如 println(1)
VarDecl, // 变量声明
IfStmt, // If语句
ElseIfStmt, // ElseIf语句不准悬空平铺式else if
WhileStmt, // while语句
FnDefStmt, // func函数定义语句
ReturnStmt, // 返回语句
BreakStmt, // break语句
ContinueStmt, // continue语句
ExprStmt,
VarDecl,
IfStmt,
ElseIfStmt,
WhileStmt,
FnDefStmt,
StructDefStmt,
InterfaceDefStmt,
ImplStmt, // impl Document for File {}
ReturnStmt,
BreakStmt,
ContinueStmt,
/* Type Expressions */
TypeExpr, // 基类
NamedTypeExpr, // 命名类型支持namespace, std.file这样的
// 泛型等...
TypeExpr,
NamedTypeExpr,
NullableTypeExpr
};
struct AstNode
{
AstType type = AstType::AstNode;
@@ -66,13 +69,10 @@ namespace Fig
virtual ~TypeExpr() = default;
};
struct Program;
struct Expr : public AstNode
{
TypeInfo *resolvedType = nullptr;
// TODO: 可选的常量折叠
// 拓展 isConstExpr和 constValue槽位
// 语义分析后填充
Type resolvedType;
Expr()
{
@@ -82,7 +82,7 @@ namespace Fig
struct Stmt : public AstNode
{
bool isPublic;
bool isPublic = false;
Stmt()
{
type = AstType::Stmt;
@@ -92,22 +92,10 @@ namespace Fig
struct Program final : public AstNode
{
DynArray<Stmt *> nodes;
Program()
{
type = AstType::Program;
}
Program(DynArray<Stmt *> _nodes)
{
type = AstType::Program;
nodes = std::move(_nodes);
if (!_nodes.empty())
{
location = std::move(_nodes.back()->location);
}
}
virtual String toString() const override
{
return "<Program>";
@@ -121,36 +109,9 @@ namespace Fig
{
type = AstType::BlockStmt;
}
BlockStmt(DynArray<Stmt *> _nodes)
{
type = AstType::BlockStmt;
nodes = std::move(_nodes);
if (!_nodes.empty())
{
location = std::move(_nodes.back()->location);
}
}
virtual String toString() const override
{
return "<BlockStmt>";
}
};
}; // namespace Fig
namespace std
{
template <>
struct std::formatter<Fig::AstNode *, char>
{
constexpr auto parse(std::format_parse_context &ctx)
{
return ctx.begin();
}
template <typename FormatContext>
auto format(const Fig::AstNode *_node, FormatContext &ctx) const
{
return std::format_to(ctx.out(), "{}", _node->toString().toStdString());
}
};
}; // namespace std
} // namespace Fig