feat: 实现控制流语句并优化类型解析

- 新增了 ReturnStmt、BreakStmt 和 ContinueStmt 结构,以支持 AST 中的控制流。
- 引入了 TypeInfo 和 TypeContext 以实现更好的类型管理和解析。
- 对分析器进行了增强,能够处理函数定义和返回语句,包括类型检查和错误处理。
- 更新了编译器和解析器以适应新的控制流语句和类型解析逻辑。
- 重构了现有代码以提高清晰度和可维护性,包括对表达式和语句中的类型处理的更改。
- 删除了过时的 String 和 Struct 定义,代之以 StringObject 和 StructObject 以实现更好的对象表示。
This commit is contained in:
2026-02-28 20:42:15 +08:00
parent bb23ddf9fa
commit 1fe9ccf7ea
18 changed files with 642 additions and 137 deletions

View File

@@ -34,8 +34,8 @@ namespace Fig
int depth; // 物理作用域深度(用于 EndScope 释放寄存器)
};
static constexpr int MAX_LOCALS = 250;
static constexpr int MAX_CONSTANTS = UINT16_MAX;
inline constexpr int MAX_LOCALS = 250;
inline constexpr int MAX_CONSTANTS = UINT16_MAX + 1;
// 任何跨函数、跨模块的编译,都压入弹出这个 State
struct FuncState
@@ -66,6 +66,24 @@ namespace Fig
SourceManager &manager;
FuncState *current = nullptr; // 永远指向当前正在编译的上下文
public:
struct FuncStateProtector
{
Compiler *compiler;
FuncState *prevState;
[[nodiscard]]
FuncStateProtector(Compiler *comp, FuncState *newState) :
compiler(comp), prevState(comp->current)
{
compiler->current = newState;
}
~FuncStateProtector()
{
compiler->current = prevState;
}
};
Compiler(String _fileName, SourceManager &_manager) :
fileName(std::move(_fileName)), manager(_manager)
{
@@ -85,7 +103,6 @@ namespace Fig
}
Result<Proto *, Error> Compile(Program *program);
private:
void PushState(String _name)
{