feat: 添加函数定义和类型表达式支持,重构解析器以处理新语法(函数定义)

This commit is contained in:
2026-02-26 14:41:41 +08:00
parent 12dc31a6c0
commit bb23ddf9fa
9 changed files with 376 additions and 11 deletions

View File

@@ -15,6 +15,8 @@
#include <Ast/Expr/PrefixExpr.hpp>
#include <Ast/Stmt/ExprStmt.hpp>
#include <Ast/Stmt/FnDefStmt.hpp>
#include <Ast/Stmt/IfStmt.hpp>
#include <Ast/Stmt/VarDecl.hpp>
#include <Ast/Stmt/WhileStmt.hpp>
#include <Ast/Stmt/WhileStmt.hpp>
#include <Ast/TypeExpr.hpp>

View File

@@ -38,6 +38,12 @@ namespace Fig
IfStmt, // If语句
ElseIfStmt, // ElseIf语句不准悬空平铺式else if
WhileStmt, // while语句
FnDefStmt, // func函数定义语句
/* Type Expressions */
TypeExpr, // 基类
NamedTypeExpr, // 命名类型支持namespace, std.file这样的
// 泛型等...
};
struct AstNode
{
@@ -45,7 +51,16 @@ namespace Fig
SourceLocation location;
virtual String toString() const = 0;
virtual ~AstNode(){};
virtual ~AstNode() {};
};
struct TypeExpr : public AstNode
{
TypeExpr()
{
type = AstType::TypeExpr;
}
virtual ~TypeExpr() = default;
};
struct Program;

100
src/Ast/Stmt/FnDefStmt.hpp Normal file
View File

@@ -0,0 +1,100 @@
/*!
@file src/Ast/Stmt/FnDefStmt.hpp
@brief FnDefStmt定义
@author PuqiAR (im@puqiar.top)
@date 2026-02-25
*/
#pragma once
#include <Ast/Base.hpp>
namespace Fig
{
struct Param
{
String name;
SourceLocation location;
virtual String toString() const = 0;
};
struct PosParam final : public Param
{
TypeExpr *type;
Expr *defaultValue;
PosParam() {}
PosParam(String _name, TypeExpr *_type, Expr *_defaultValue, SourceLocation _location) :
type(_type), defaultValue(_defaultValue)
{
name = std::move(_name);
location = std::move(_location);
}
virtual String toString() const override
{
return std::format(
"<Pos {}: {}{}>",
name,
(type ? type->toString() : "Any"),
(defaultValue ? " =" + defaultValue->toString() : "")
);
}
};
/*
(public) func foo([name: (type) (= default value)]) (-> return type)
{
...
}
*/
struct FnDefStmt final : public Stmt
{
String name;
DynArray<Param *> params;
TypeExpr *returnType;
BlockStmt *body;
FnDefStmt()
{
type = AstType::FnDefStmt;
}
FnDefStmt(bool _isPublic,
String _name,
DynArray<Param *> _params,
TypeExpr *_returnType,
BlockStmt *_body,
SourceLocation _location) :
name(std::move(_name)), params(std::move(_params)), returnType(_returnType), body(_body)
{
type = AstType::FnDefStmt;
isPublic = _isPublic;
location = std::move(_location);
}
virtual String toString() const override
{
String pStr;
for (const Param *p : params)
{
if (p != *params.begin())
{
pStr += ", ";
}
pStr += p->toString();
}
return std::format(
"<FnDefStmt {}{}({}) -> {} {{{}}}>",
(isPublic ? "public " : ""),
name,
pStr,
(returnType ? returnType->toString() : "Any"),
body->toString()
);
}
};
}; // namespace Fig

View File

@@ -15,7 +15,7 @@ namespace Fig
struct VarDecl final : public Stmt
{
String name;
Expr *typeSpecifier;
TypeExpr *typeSpecifier;
bool isInfer; // 是否用了 := 类型推断
Expr *initExpr;
@@ -26,7 +26,7 @@ namespace Fig
type = AstType::VarDecl;
}
VarDecl(bool _isPublic, String _name, Expr *_typeSpecifier, bool _isInfer, Expr *_initExpr, SourceLocation _location) :
VarDecl(bool _isPublic, String _name, TypeExpr *_typeSpecifier, bool _isInfer, Expr *_initExpr, SourceLocation _location) :
name(std::move(_name)),
typeSpecifier(_typeSpecifier),
isInfer(_isInfer),

36
src/Ast/TypeExpr.hpp Normal file
View File

@@ -0,0 +1,36 @@
/*!
@file src/Ast/TypeExpr.hpp
@brief TypeExpr定义
@author PuqiAR (im@puqiar.top)
@date 2026-02-25
*/
#pragma once
#include <Ast/Base.hpp>
namespace Fig
{
struct NamedTypeExpr final : public TypeExpr
{
DynArray<String> path; // {"std", "file"} etc.
NamedTypeExpr()
{
type = AstType::NamedTypeExpr;
}
NamedTypeExpr(DynArray<String> _path, SourceLocation _location) :
path(std::move(_path))
{
type = AstType::NamedTypeExpr;
location = std::move(_location);
}
virtual String toString() const override
{
return std::format("<NamedTypeExpr '{}'>", path);
}
};
};