完成表达式Ast定义。修改了format文件
This commit is contained in:
@@ -6,13 +6,13 @@ Language: Cpp
|
||||
AccessModifierOffset: -4
|
||||
|
||||
# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
|
||||
# 连续赋值时,对齐所有等号
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveAssignments: true
|
||||
|
||||
# 连续声明时,对齐所有声明的变量名
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignConsecutiveDeclarations: true
|
||||
|
||||
# 右对齐逃脱换行(使用反斜杠换行)的反斜杠
|
||||
AlignEscapedNewlines: Right
|
||||
@@ -109,7 +109,6 @@ BreakStringLiterals: false
|
||||
|
||||
# 每行字符的限制,0表示没有限制
|
||||
ColumnLimit: 120
|
||||
|
||||
CompactNamespaces: true
|
||||
|
||||
# 构造函数的初始化列表要么都在同一行,要么都各自一行
|
||||
@@ -157,7 +156,7 @@ PointerAlignment: Right
|
||||
ReflowComments: true
|
||||
|
||||
# 允许排序#include
|
||||
SortIncludes: false
|
||||
SortIncludes: true
|
||||
|
||||
# 允许排序 using 声明
|
||||
SortUsingDeclarations: false
|
||||
|
||||
50
src/Ast/Base.hpp
Normal file
50
src/Ast/Base.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
/*!
|
||||
@file src/Ast/Base.hpp
|
||||
@brief AstNode基类定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <Core/SourceLocations.hpp>
|
||||
#include <Deps/Deps.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
enum class AstType : std::uint8_t
|
||||
{
|
||||
AstNode, // 基类
|
||||
Expr, // 表达式
|
||||
Stmt, // 语句
|
||||
|
||||
IdentiExpr, // 标识符表达式
|
||||
LiteralExpr, // 字面量表达式
|
||||
UnaryExpr, // 一元表达式
|
||||
BinaryExpr, // 二元表达式
|
||||
TernaryExpr, // 三元表达式
|
||||
};
|
||||
struct AstNode
|
||||
{
|
||||
AstType type = AstType::AstNode;
|
||||
SourceLocation location;
|
||||
};
|
||||
|
||||
struct Expr : public AstNode
|
||||
{
|
||||
Expr()
|
||||
{
|
||||
type = AstType::Expr;
|
||||
}
|
||||
};
|
||||
|
||||
struct Stmt : public AstNode
|
||||
{
|
||||
Stmt()
|
||||
{
|
||||
type = AstType::Stmt;
|
||||
}
|
||||
};
|
||||
|
||||
}; // namespace Fig
|
||||
29
src/Ast/Expr/IdentiExpr.hpp
Normal file
29
src/Ast/Expr/IdentiExpr.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
/*!
|
||||
@file src/Ast/Expr/IdentiExpr.hpp
|
||||
@brief IdentiExpr定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <Ast/Base.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct IdentiExpr final : Expr
|
||||
{
|
||||
String name;
|
||||
|
||||
IdentiExpr()
|
||||
{
|
||||
type = AstType::IdentiExpr;
|
||||
}
|
||||
|
||||
IdentiExpr(String _name, SourceLocation _loc)
|
||||
{
|
||||
type = AstType::IdentiExpr;
|
||||
name = std::move(_name);
|
||||
location = std::move(_loc);
|
||||
}
|
||||
};
|
||||
};
|
||||
28
src/Ast/Expr/LiteralExpr.hpp
Normal file
28
src/Ast/Expr/LiteralExpr.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/*!
|
||||
@file src/Ast/Expr/LiteralExpr.hpp
|
||||
@brief 字面量表达式定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Ast/Base.hpp>
|
||||
#include <Token/Token.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
struct LiteralExpr final : Expr
|
||||
{
|
||||
Token token;
|
||||
|
||||
LiteralExpr()
|
||||
{
|
||||
type = AstType::LiteralExpr;
|
||||
}
|
||||
LiteralExpr(const Token& token) : token(token)
|
||||
{
|
||||
type = AstType::LiteralExpr;
|
||||
}
|
||||
};
|
||||
}; // namespace Fig
|
||||
106
src/Ast/Operator.cpp
Normal file
106
src/Ast/Operator.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*!
|
||||
@file src/Ast/Operator.cpp
|
||||
@brief 运算符定义内函数实现
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
*/
|
||||
|
||||
#include <Ast/Operator.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
HashMap<TokenType, UnaryOperator> &GetUnaryOpMap()
|
||||
{
|
||||
static HashMap<TokenType, UnaryOperator> unaryOpMap{
|
||||
{TokenType::Tilde, UnaryOperator::BitNot},
|
||||
{TokenType::Minus, UnaryOperator::Negate},
|
||||
{TokenType::Not, UnaryOperator::Not},
|
||||
{TokenType::Ampersand, UnaryOperator::AddressOf},
|
||||
};
|
||||
return unaryOpMap;
|
||||
}
|
||||
|
||||
HashMap<TokenType, BinaryOperator> &GetBinaryOpMap()
|
||||
{
|
||||
static HashMap<TokenType, BinaryOperator> binaryOpMap{{TokenType::Plus, BinaryOperator::Add},
|
||||
{TokenType::Minus, BinaryOperator::Subtract},
|
||||
{TokenType::Slash, BinaryOperator::Divide},
|
||||
{TokenType::Percent, BinaryOperator::Modulo},
|
||||
|
||||
{TokenType::Equal, BinaryOperator::Equal},
|
||||
{TokenType::NotEqual, BinaryOperator::NotEqual},
|
||||
{TokenType::Less, BinaryOperator::Less},
|
||||
{TokenType::Greater, BinaryOperator::Greater},
|
||||
{TokenType::LessEqual, BinaryOperator::LessEqual},
|
||||
{TokenType::GreaterEqual, BinaryOperator::GreaterEqual},
|
||||
|
||||
{TokenType::Is, BinaryOperator::Is},
|
||||
|
||||
{TokenType::And, BinaryOperator::LogicalAnd},
|
||||
{TokenType::Or, BinaryOperator::LogicalOr},
|
||||
|
||||
{TokenType::Power, BinaryOperator::Power},
|
||||
|
||||
{TokenType::Assign, BinaryOperator::Assign},
|
||||
|
||||
{TokenType::Pipe, BinaryOperator::BitAnd},
|
||||
{TokenType::Ampersand, BinaryOperator::BitAnd},
|
||||
{TokenType::ShiftLeft, BinaryOperator::ShiftLeft},
|
||||
{TokenType::ShiftRight, BinaryOperator::ShiftRight}};
|
||||
return binaryOpMap;
|
||||
}
|
||||
|
||||
// 赋值 < 三元 < 逻辑或 < 逻辑与 < 位运算 < 比较 < 位移 < 加减 < 乘除 < 幂 < 一元
|
||||
|
||||
HashMap<UnaryOperator, BindingPower> &GetUnaryOpBindingPowerMap()
|
||||
{
|
||||
static HashMap<UnaryOperator, BindingPower> unbpm{
|
||||
{UnaryOperator::BitNot, 10000},
|
||||
{UnaryOperator::Negate, 10000},
|
||||
{UnaryOperator::Not, 10000},
|
||||
{UnaryOperator::AddressOf, 10000},
|
||||
};
|
||||
return unbpm;
|
||||
}
|
||||
|
||||
HashMap<BinaryOperator, BindingPower> &GetBinaryOpBindingPowerMap()
|
||||
{
|
||||
static HashMap<BinaryOperator, BindingPower> bnbpm{{BinaryOperator::Assign, 100},
|
||||
|
||||
{BinaryOperator::LogicalOr, 500},
|
||||
{BinaryOperator::LogicalAnd, 550},
|
||||
|
||||
{BinaryOperator::BitOr, 1000},
|
||||
{BinaryOperator::BitXor, 1100},
|
||||
{BinaryOperator::BitAnd, 1200},
|
||||
|
||||
{BinaryOperator::Equal, 2000},
|
||||
{BinaryOperator::NotEqual, 2000},
|
||||
|
||||
{BinaryOperator::Less, 2100},
|
||||
{BinaryOperator::LessEqual, 2100},
|
||||
{BinaryOperator::Greater, 2100},
|
||||
{BinaryOperator::GreaterEqual, 2100},
|
||||
|
||||
{BinaryOperator::Is, 2100},
|
||||
|
||||
{BinaryOperator::ShiftLeft, 3000},
|
||||
{BinaryOperator::ShiftRight, 3000},
|
||||
|
||||
{BinaryOperator::Add, 4000},
|
||||
{BinaryOperator::Subtract, 4000},
|
||||
{BinaryOperator::Multiply, 4500},
|
||||
{BinaryOperator::Divide, 4500},
|
||||
|
||||
{BinaryOperator::Power, 5000},
|
||||
};
|
||||
}
|
||||
bool IsTokenOp(TokenType type, bool binary /* = true*/)
|
||||
{
|
||||
if (binary)
|
||||
{
|
||||
return GetBinaryOpMap().contains(type);
|
||||
}
|
||||
return GetUnaryOpMap().contains(type);
|
||||
}
|
||||
}; // namespace Fig
|
||||
65
src/Ast/Operator.hpp
Normal file
65
src/Ast/Operator.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*!
|
||||
@file src/Ast/Operator.hpp
|
||||
@brief 运算符定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <Deps/Deps.hpp>
|
||||
#include <Token/Token.hpp>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
enum class UnaryOperator : std::uint8_t
|
||||
{
|
||||
BitNot, // 位运算取反 ~
|
||||
Negate, // 取反 -
|
||||
Not, // 逻辑非 ! / not
|
||||
AddressOf, // 取引用 &
|
||||
};
|
||||
enum class BinaryOperator : std::uint8_t
|
||||
{
|
||||
Add, // 加 +
|
||||
Subtract, // 减 -
|
||||
Multiply, // 乘 *
|
||||
Divide, // 除 /
|
||||
Modulo, // 取模 %
|
||||
|
||||
Equal, // 等于 ==
|
||||
NotEqual, // 不等于 !=
|
||||
Less, // 小于 <
|
||||
Greater, // 大于 >
|
||||
LessEqual, // 小于等于 <=
|
||||
GreaterEqual, // 大于等于 >=
|
||||
|
||||
Is, // is操作符
|
||||
|
||||
LogicalAnd, // 逻辑与 && / and
|
||||
LogicalOr, // 逻辑或 || / or
|
||||
|
||||
Power, // 幂运算 **
|
||||
|
||||
Assign, // 赋值(修改) =
|
||||
|
||||
// 位运算
|
||||
BitAnd, // 按位与 &
|
||||
BitOr, // 按位或 |
|
||||
BitXor, // 异或 ^
|
||||
ShiftLeft, // 左移
|
||||
ShiftRight, // 右移
|
||||
};
|
||||
|
||||
using BindingPower = unsigned int;
|
||||
|
||||
HashMap<TokenType, UnaryOperator> &GetUnaryOpMap();
|
||||
HashMap<TokenType, BinaryOperator> &GetBinaryOpMap();
|
||||
|
||||
HashMap<UnaryOperator, BindingPower> &GetUnaryOpBindingPowerMap();
|
||||
HashMap<BinaryOperator, BindingPower> &GetBinaryOpBindingPowerMap();
|
||||
|
||||
bool IsTokenOp(TokenType type, bool binary = true);
|
||||
}; // namespace Fig
|
||||
@@ -8,9 +8,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <Core/CoreInfos.hpp>
|
||||
#include <Deps/DynArray/DynArray.hpp>
|
||||
#include <Deps/HashMap/HashMap.hpp>
|
||||
#include <Deps/String/String.hpp>
|
||||
#include <Deps/String/CharUtils.hpp>
|
||||
#include <Deps/String/String.hpp>
|
||||
|
||||
#include <expected>
|
||||
|
||||
@@ -20,8 +21,9 @@ namespace Fig
|
||||
using Deps::String;
|
||||
using Deps::HashMap;
|
||||
using Deps::CharUtils;
|
||||
using Deps::DynArray;
|
||||
|
||||
template <class _Tp, class _Err>
|
||||
using Result = std::expected<_Tp, _Err>;
|
||||
#endif
|
||||
};
|
||||
}; // namespace Fig
|
||||
14
src/Deps/DynArray/DynArray.hpp
Normal file
14
src/Deps/DynArray/DynArray.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
/*!
|
||||
@file src/Deps/DynArray/DynArray
|
||||
@brief 依赖库DynArray定义
|
||||
@author PuqiAR (im@puqiar.top)
|
||||
@date 2026-02-14
|
||||
*/
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Fig::Deps
|
||||
{
|
||||
template<class _Tp, class _Allocator = std::allocator<_Tp>>
|
||||
using DynArray = std::vector<_Tp, _Allocator>;
|
||||
};
|
||||
Reference in New Issue
Block a user