修复EOF飘逸(去除末尾\n)以及其他修复...
This commit is contained in:
@@ -2,13 +2,16 @@
|
||||
@file src/Ast/Ast.hpp
|
||||
@brief Ast总链接
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
@date 2026-02-17
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Ast/Expr/CallExpr.hpp>
|
||||
#include <Ast/Expr/IdentiExpr.hpp>
|
||||
#include <Ast/Expr/IndexExpr.hpp>
|
||||
#include <Ast/Expr/InfixExpr.hpp>
|
||||
#include <Ast/Expr/LiteralExpr.hpp>
|
||||
#include <Ast/Expr/PrefixExpr.hpp>
|
||||
|
||||
#include <Ast/Stmt/VarDecl.hpp>
|
||||
@@ -19,10 +19,17 @@ namespace Fig
|
||||
Expr, // 表达式
|
||||
Stmt, // 语句
|
||||
|
||||
/* Expressions */
|
||||
IdentiExpr, // 标识符表达式
|
||||
LiteralExpr, // 字面量表达式
|
||||
PrefixExpr, // 一元 前缀表达式
|
||||
InfixExpr, // 二元 中缀表达式
|
||||
|
||||
IndexExpr, // 后缀表达式,索引
|
||||
CallExpr, // 后缀表达式,函数调用
|
||||
|
||||
/* Statements */
|
||||
VarDecl,
|
||||
};
|
||||
struct AstNode
|
||||
{
|
||||
@@ -42,10 +49,28 @@ namespace Fig
|
||||
|
||||
struct Stmt : public AstNode
|
||||
{
|
||||
bool isPublic;
|
||||
Stmt()
|
||||
{
|
||||
type = AstType::Stmt;
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
|
||||
}; // namespace Fig
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct std::formatter<Fig::AstNode *, char>
|
||||
{
|
||||
constexpr auto parse(std::format_parse_context &ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
template <typename FormatContext>
|
||||
auto format(const Fig::AstNode *_node, FormatContext &ctx) const
|
||||
{
|
||||
return std::format_to(ctx.out(), "{}", _node->toString().toStdString());
|
||||
}
|
||||
};
|
||||
};
|
||||
60
src/Ast/Expr/CallExpr.hpp
Normal file
60
src/Ast/Expr/CallExpr.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*!
|
||||
@file src/Ast/Expr/CallExpr.hpp
|
||||
@brief CallExpr等定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-17
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Ast/Base.hpp>
|
||||
#include <Deps/Deps.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct FnCallArgs
|
||||
{
|
||||
DynArray<Expr *> args;
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
return args.size();
|
||||
}
|
||||
|
||||
String toString() const
|
||||
{
|
||||
String str = "(";
|
||||
for (const Expr *expr : args)
|
||||
{
|
||||
if (expr != args.front())
|
||||
{
|
||||
str += ", ";
|
||||
}
|
||||
str += expr->toString();
|
||||
}
|
||||
str += ")";
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
struct CallExpr final : public Expr
|
||||
{
|
||||
Expr *callee;
|
||||
FnCallArgs args;
|
||||
|
||||
CallExpr()
|
||||
{
|
||||
type = AstType::CallExpr;
|
||||
}
|
||||
|
||||
CallExpr(Expr *_callee, FnCallArgs _args) : callee(_callee), args(std::move(_args))
|
||||
{
|
||||
type = AstType::CallExpr;
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
return std::format("<CallExpr: '{}{}'>", callee->toString(), args.toString());
|
||||
}
|
||||
};
|
||||
} // namespace Fig
|
||||
34
src/Ast/Expr/IndexExpr.hpp
Normal file
34
src/Ast/Expr/IndexExpr.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*!
|
||||
@file src/Ast/Expr/IndexExpr.hpp
|
||||
@brief IndexExpr定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-17
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Ast/Base.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct IndexExpr final : public Expr
|
||||
{
|
||||
Expr *base;
|
||||
Expr *index;
|
||||
|
||||
IndexExpr()
|
||||
{
|
||||
type = AstType::IndexExpr;
|
||||
}
|
||||
|
||||
IndexExpr(Expr *_base, Expr *_index) : base(_base), index(_index)
|
||||
{
|
||||
location = base->location;
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
return std::format("<IndexExpr: '{}[{}]'>", base->toString(), index->toString());
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
42
src/Ast/Stmt/VarDecl.hpp
Normal file
42
src/Ast/Stmt/VarDecl.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*!
|
||||
@file src/Ast/Stmt/VarDecl.hpp
|
||||
@brief VarDecl定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-17
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Ast/Base.hpp>
|
||||
#include <Deps/Deps.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct VarDecl final : public Stmt
|
||||
{
|
||||
String name;
|
||||
Expr *typeSpecifier;
|
||||
Expr *initExpr;
|
||||
|
||||
VarDecl()
|
||||
{
|
||||
type = AstType::VarDecl;
|
||||
}
|
||||
|
||||
VarDecl(String _name, Expr *_typeSpecifier, Expr *_initExpr, SourceLocation _location) :
|
||||
name(std::move(_name)),
|
||||
typeSpecifier(_typeSpecifier),
|
||||
initExpr(_initExpr) // location 指向关键字 var/const位置
|
||||
{
|
||||
type = AstType::VarDecl;
|
||||
location = std::move(_location);
|
||||
}
|
||||
|
||||
virtual String toString() const override
|
||||
{
|
||||
const String &typeSpecifierString = (typeSpecifier ? typeSpecifier->toString() : "Any");
|
||||
const String &initExprString = (initExpr ? initExpr->toString() : "(disprovided)");
|
||||
return std::format("<VarDecl {}: {} = {}>", name, typeSpecifierString, initExprString);
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
Reference in New Issue
Block a user