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

@@ -1,6 +1,6 @@
/*!
@file src/Compiler/StmtCompiler.cpp
@brief 语句编译器实现:实装水位线机制,彻底消灭硬编码寄存器释放
@brief 语句编译
*/
#include <Ast/Stmt/FnDefStmt.hpp>
@@ -35,7 +35,7 @@ namespace Fig
auto *v = static_cast<VarDecl *>(stmt);
if (current->enclosing == nullptr) // 处理全局变量
{
Register mark = current->freereg; // 记录水位线
Register mark = current->freereg; // mark
auto regRes = compileExpr(v->initExpr);
if (!regRes)
return std::unexpected(regRes.error());
@@ -74,7 +74,7 @@ namespace Fig
Proto *newProto = new Proto();
newProto->name = f->name;
newProto->numParams = static_cast<uint8_t>(f->params.size());
newProto->maxRegisters = newProto->numParams; // 同步水位线
newProto->maxRegisters = newProto->numParams; // sync
f->protoIndex = static_cast<int>(module->protos.size());
module->protos.push_back(newProto);
@@ -141,7 +141,7 @@ namespace Fig
auto *i = static_cast<IfStmt *>(stmt);
DynArray<int> exitJumps;
Register mark = current->freereg; // 记录水位线
Register mark = current->freereg; // mark
auto r_cond = compileExpr(i->cond);
if (!r_cond)
return std::unexpected(r_cond.error());
@@ -204,7 +204,7 @@ namespace Fig
auto *w = static_cast<WhileStmt *>(stmt);
int startIdx = static_cast<int>(current->proto->code.size());
Register mark = current->freereg; // 记录水位线
Register mark = current->freereg; // mark
auto r_cond = compileExpr(w->cond);
if (!r_cond)
return std::unexpected(r_cond.error());
@@ -228,7 +228,7 @@ namespace Fig
case AstType::ReturnStmt: {
auto *rs = static_cast<ReturnStmt *>(stmt);
Register mark = current->freereg; // 记录水位线
Register mark = current->freereg; // mark
Register retReg;
if (rs->value)
@@ -253,7 +253,7 @@ namespace Fig
}
case AstType::ExprStmt: {
Register mark = current->freereg; // 记录水位线
Register mark = current->freereg; // mark
auto reg = compileExpr(static_cast<ExprStmt *>(stmt)->expr);
if (!reg)
return std::unexpected(reg.error());