- 更新了 ParserTest,以改进文件路径处理和输出格式。 - 在 StmtParser 中新增了 parseConstDecl 和 parseForStmt 方法,用于处理常量声明和 for 循环。 - TypeExpr现归类为Expr。TypeExpr属于Expr,语义阶段视为Expr - 添加了新的 AST 节点:PostfixExpr、TernaryExpr、ForStmt 和 ImportStmt,用于表示新的语法结构。
52 lines
1.6 KiB
C++
52 lines
1.6 KiB
C++
/*!
|
|
@file src/Ast/Stmt/FnDefStmt.hpp
|
|
@brief 函数定义 AST 节点
|
|
*/
|
|
|
|
#pragma once
|
|
#include <Ast/Base.hpp>
|
|
#include <Sema/Environment.hpp>
|
|
#include <Bytecode/Bytecode.hpp>
|
|
|
|
namespace Fig
|
|
{
|
|
struct Param : public AstNode {
|
|
String name;
|
|
Expr *typeSpecifier;
|
|
Expr *defaultValue;
|
|
Type resolvedType;
|
|
Param() { type = AstType::AstNode; }
|
|
virtual ~Param() = default;
|
|
};
|
|
|
|
struct PosParam final : public Param {
|
|
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;
|
|
DynArray<Param *> params;
|
|
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, 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);
|
|
}
|
|
|
|
virtual String toString() const override {
|
|
return std::format("<FnDefStmt '{}'>", name);
|
|
}
|
|
};
|
|
}
|