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

@@ -16,6 +16,9 @@ namespace Fig
String name;
SourceLocation location;
TypeInfo *resolvedType = nullptr;
int localId = -1;
virtual String toString() const = 0;
};
@@ -28,18 +31,16 @@ namespace Fig
PosParam(String _name, TypeExpr *_type, Expr *_defaultValue, SourceLocation _location) :
type(_type), defaultValue(_defaultValue)
{
name = std::move(_name);
name = std::move(_name);
location = std::move(_location);
}
virtual String toString() const override
{
return std::format(
"<Pos {}: {}{}>",
return std::format("<Pos {}: {}{}>",
name,
(type ? type->toString() : "Any"),
(defaultValue ? " =" + defaultValue->toString() : "")
);
(defaultValue ? " =" + defaultValue->toString() : ""));
}
};
@@ -58,6 +59,9 @@ namespace Fig
TypeExpr *returnType;
BlockStmt *body;
TypeInfo *resolvedReturnType = nullptr;
int localId = -1;
FnDefStmt()
{
type = AstType::FnDefStmt;
@@ -87,14 +91,12 @@ namespace Fig
}
pStr += p->toString();
}
return std::format(
"<FnDefStmt {}{}({}) -> {} {{{}}}>",
return std::format("<FnDefStmt {}{}({}) -> {} {{{}}}>",
(isPublic ? "public " : ""),
name,
pStr,
(returnType ? returnType->toString() : "Any"),
body->toString()
);
body->toString());
}
};
}; // namespace Fig