Refactor: 重构Parser和AST结构,以支持新的语言特性

- 更新了 ParserTest,以改进文件路径处理和输出格式。
- 在 StmtParser 中新增了 parseConstDecl 和 parseForStmt 方法,用于处理常量声明和 for 循环。
- TypeExpr现归类为Expr。TypeExpr属于Expr,语义阶段视为Expr
- 添加了新的 AST 节点:PostfixExpr、TernaryExpr、ForStmt 和 ImportStmt,用于表示新的语法结构。
This commit is contained in:
2026-06-06 22:12:04 +08:00
parent 4f87078a87
commit 680197aafe
27 changed files with 1299 additions and 225 deletions

View File

@@ -11,34 +11,34 @@
namespace Fig
{
struct Param : public AstNode {
String name;
TypeExpr *typeSpecifier;
Expr *defaultValue;
Type resolvedType;
String name;
Expr *typeSpecifier;
Expr *defaultValue;
Type resolvedType;
Param() { type = AstType::AstNode; }
virtual ~Param() = default;
};
struct PosParam final : public Param {
PosParam(String _n, TypeExpr *_ts, Expr *_dv, SourceLocation _loc) {
PosParam(String _n, Expr *_ts, Expr *_dv, SourceLocation _loc) {
name = std::move(_n); typeSpecifier = _ts; defaultValue = _dv; location = std::move(_loc);
}
virtual String toString() const override { return name; }
};
struct FnDefStmt final : public Stmt {
String name;
String name;
DynArray<Param *> params;
TypeExpr *returnTypeSpecifier;
BlockStmt *body;
Type resolvedReturnType;
Symbol *resolvedSymbol = nullptr; // 连接物理符号
Expr *returnTypeSpecifier;
BlockStmt *body;
Type resolvedReturnType;
Symbol *resolvedSymbol = nullptr; // 连接物理符号
int protoIndex = -1; // 在CompiledModule扁平化protos的下标
DynArray<UpvalueInfo> upvalues;
FnDefStmt() { type = AstType::FnDefStmt; }
FnDefStmt(bool _p, String _n, DynArray<Param *> _pa, TypeExpr *_rt, BlockStmt *_b, SourceLocation _loc)
FnDefStmt(bool _p, String _n, DynArray<Param *> _pa, Expr *_rt, BlockStmt *_b, SourceLocation _loc)
: name(std::move(_n)), params(std::move(_pa)), returnTypeSpecifier(_rt), body(_b)
{
type = AstType::FnDefStmt; isPublic = _p; location = std::move(_loc);