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

@@ -10,27 +10,50 @@
#include <Sema/Environment.hpp>
#include <Sema/Type.hpp>
#include <Ast/Ast.hpp>
#include <Deps/Deps.hpp>
namespace Fig
{
class Analyzer
{
private:
Environment env;
Environment env;
SourceManager &manager;
SourceLocation makeSourceLocation(AstNode *ast, std::source_location loc = std::source_location::current())
TypeContext typeCtx;
TypeInfo *currentReturnType = nullptr; // 正在分析的函数,预期返回类型
struct ReturnTypeProtector
{
return SourceLocation(
ast->location.sp,
Analyzer *analyzer;
TypeInfo *prevCurrentReturnType;
[[nodiscard]]
ReturnTypeProtector(Analyzer *_analyzer, TypeInfo *current) :
analyzer(_analyzer), prevCurrentReturnType(_analyzer->currentReturnType)
{
analyzer->currentReturnType = current;
}
~ReturnTypeProtector()
{
analyzer->currentReturnType = prevCurrentReturnType;
}
ReturnTypeProtector(const ReturnTypeProtector &) = delete;
ReturnTypeProtector &operator=(const ReturnTypeProtector &) = delete;
};
SourceLocation makeSourceLocation(
AstNode *ast, std::source_location loc = std::source_location::current())
{
return SourceLocation(ast->location.sp,
ast->location.fileName,
"[internal analyzer]",
loc.function_name()
);
loc.function_name());
}
bool isValidLvalue(Expr *expr)
@@ -54,18 +77,23 @@ namespace Fig
return false;
}
Result<TypeInfo *, Error> resolveType(TypeExpr *);
Result<void, Error> analyzeVarDecl(VarDecl *);
Result<void, Error> analyzeIfStmt(IfStmt *);
Result<void, Error> analyzeWhileStmt(WhileStmt *);
Result<void, Error> analyzeFnDefStmt(FnDefStmt *);
Result<void, Error> analyzeReturnStmt(ReturnStmt *);
Result<void, Error> analyzeIdentiExpr(IdentiExpr *);
Result<void, Error> analyzeInfixExpr(InfixExpr *);
Result<void, Error> analyzeStmt(Stmt *);
Result<void, Error> analyzeExpr(Expr *);
public:
Result<void, Error> Analyze(Program *);
Analyzer(SourceManager &_manager) : manager(_manager) {}
Analyzer(SourceManager &_manager) : manager(_manager), typeCtx() {}
};
}; // namespace Fig