feat: 实现控制流语句并优化类型解析
- 新增了 ReturnStmt、BreakStmt 和 ContinueStmt 结构,以支持 AST 中的控制流。 - 引入了 TypeInfo 和 TypeContext 以实现更好的类型管理和解析。 - 对分析器进行了增强,能够处理函数定义和返回语句,包括类型检查和错误处理。 - 更新了编译器和解析器以适应新的控制流语句和类型解析逻辑。 - 重构了现有代码以提高清晰度和可维护性,包括对表达式和语句中的类型处理的更改。 - 删除了过时的 String 和 Struct 定义,代之以 StringObject 和 StructObject 以实现更好的对象表示。
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <Ast/Expr/LiteralExpr.hpp>
|
||||
#include <Ast/Expr/PrefixExpr.hpp>
|
||||
|
||||
#include <Ast/Stmt/ControlFlowStmts.hpp>
|
||||
#include <Ast/Stmt/ExprStmt.hpp>
|
||||
#include <Ast/Stmt/FnDefStmt.hpp>
|
||||
#include <Ast/Stmt/IfStmt.hpp>
|
||||
|
||||
@@ -33,12 +33,15 @@ namespace Fig
|
||||
CallExpr, // 后缀表达式,函数调用
|
||||
|
||||
/* Statements */
|
||||
ExprStmt, // 表达式语句,如 println(1)
|
||||
VarDecl, // 变量声明
|
||||
IfStmt, // If语句
|
||||
ElseIfStmt, // ElseIf语句,不准悬空,平铺式else if
|
||||
WhileStmt, // while语句
|
||||
FnDefStmt, // func函数定义语句
|
||||
ExprStmt, // 表达式语句,如 println(1)
|
||||
VarDecl, // 变量声明
|
||||
IfStmt, // If语句
|
||||
ElseIfStmt, // ElseIf语句,不准悬空,平铺式else if
|
||||
WhileStmt, // while语句
|
||||
FnDefStmt, // func函数定义语句
|
||||
ReturnStmt, // 返回语句
|
||||
BreakStmt, // break语句
|
||||
ContinueStmt, // continue语句
|
||||
|
||||
/* Type Expressions */
|
||||
TypeExpr, // 基类
|
||||
@@ -67,7 +70,7 @@ namespace Fig
|
||||
|
||||
struct Expr : public AstNode
|
||||
{
|
||||
TypeTag resolvedType = TypeTag::Any;
|
||||
TypeInfo *resolvedType = nullptr;
|
||||
// TODO: 可选的常量折叠
|
||||
// 拓展 isConstExpr和 constValue槽位
|
||||
|
||||
|
||||
72
src/Ast/Stmt/ControlFlowStmts.hpp
Normal file
72
src/Ast/Stmt/ControlFlowStmts.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*!
|
||||
@file src/Ast/Stmt/ControlFlowStmts
|
||||
@brief 控制流语句 return, break, continue 定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-27
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Ast/Base.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct ReturnStmt final : public Stmt
|
||||
{
|
||||
Expr *value;
|
||||
|
||||
ReturnStmt()
|
||||
{
|
||||
type = AstType::ReturnStmt;
|
||||
}
|
||||
|
||||
ReturnStmt(Expr *_value, SourceLocation _location) : value(_value)
|
||||
{
|
||||
type = AstType::ReturnStmt;
|
||||
location = std::move(_location);
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
return std::format("<ReturnStmt '{}'>", value->toString());
|
||||
}
|
||||
};
|
||||
|
||||
struct BreakStmt final : public Stmt
|
||||
{
|
||||
BreakStmt()
|
||||
{
|
||||
type = AstType::BreakStmt;
|
||||
}
|
||||
|
||||
BreakStmt(SourceLocation _location)
|
||||
{
|
||||
type = AstType::BreakStmt;
|
||||
location = std::move(_location);
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
return "<BreakStmt>";
|
||||
}
|
||||
};
|
||||
|
||||
struct ContinueStmt final : public Stmt
|
||||
{
|
||||
ContinueStmt()
|
||||
{
|
||||
type = AstType::ContinueStmt;
|
||||
}
|
||||
|
||||
ContinueStmt(SourceLocation _location)
|
||||
{
|
||||
type = AstType::ContinueStmt;
|
||||
location = std::move(_location);
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
return "<ContinueStmt>";
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user