diff --git a/.clang-format b/.clang-format index 09e9f78..0935b82 100644 --- a/.clang-format +++ b/.clang-format @@ -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 diff --git a/src/Ast/Base.hpp b/src/Ast/Base.hpp new file mode 100644 index 0000000..26af312 --- /dev/null +++ b/src/Ast/Base.hpp @@ -0,0 +1,50 @@ +/*! + @file src/Ast/Base.hpp + @brief AstNode基类定义 + @author PuqiAR (im@puqiar.top) + @date 2026-02-14 +*/ + +#pragma once +#include +#include + +#include + +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 \ No newline at end of file diff --git a/src/Ast/Expr/IdentiExpr.hpp b/src/Ast/Expr/IdentiExpr.hpp new file mode 100644 index 0000000..f8b1d51 --- /dev/null +++ b/src/Ast/Expr/IdentiExpr.hpp @@ -0,0 +1,29 @@ +/*! + @file src/Ast/Expr/IdentiExpr.hpp + @brief IdentiExpr定义 + @author PuqiAR (im@puqiar.top) + @date 2026-02-14 +*/ + +#pragma once +#include + +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); + } + }; +}; \ No newline at end of file diff --git a/src/Ast/Expr/LiteralExpr.hpp b/src/Ast/Expr/LiteralExpr.hpp new file mode 100644 index 0000000..af23f1c --- /dev/null +++ b/src/Ast/Expr/LiteralExpr.hpp @@ -0,0 +1,28 @@ +/*! + @file src/Ast/Expr/LiteralExpr.hpp + @brief 字面量表达式定义 + @author PuqiAR (im@puqiar.top) + @date 2026-02-14 +*/ + +#pragma once + +#include +#include + +namespace Fig +{ + struct LiteralExpr final : Expr + { + Token token; + + LiteralExpr() + { + type = AstType::LiteralExpr; + } + LiteralExpr(const Token& token) : token(token) + { + type = AstType::LiteralExpr; + } + }; +}; // namespace Fig \ No newline at end of file diff --git a/src/Ast/Operator.cpp b/src/Ast/Operator.cpp new file mode 100644 index 0000000..db9e501 --- /dev/null +++ b/src/Ast/Operator.cpp @@ -0,0 +1,106 @@ +/*! + @file src/Ast/Operator.cpp + @brief 运算符定义内函数实现 + @author PuqiAR (im@puqiar.top) + @date 2026-02-14 +*/ + +#include + +namespace Fig +{ + HashMap &GetUnaryOpMap() + { + static HashMap unaryOpMap{ + {TokenType::Tilde, UnaryOperator::BitNot}, + {TokenType::Minus, UnaryOperator::Negate}, + {TokenType::Not, UnaryOperator::Not}, + {TokenType::Ampersand, UnaryOperator::AddressOf}, + }; + return unaryOpMap; + } + + HashMap &GetBinaryOpMap() + { + static HashMap 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 &GetUnaryOpBindingPowerMap() + { + static HashMap unbpm{ + {UnaryOperator::BitNot, 10000}, + {UnaryOperator::Negate, 10000}, + {UnaryOperator::Not, 10000}, + {UnaryOperator::AddressOf, 10000}, + }; + return unbpm; + } + + HashMap &GetBinaryOpBindingPowerMap() + { + static HashMap 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 \ No newline at end of file diff --git a/src/Ast/Operator.hpp b/src/Ast/Operator.hpp new file mode 100644 index 0000000..a612a08 --- /dev/null +++ b/src/Ast/Operator.hpp @@ -0,0 +1,65 @@ +/*! + @file src/Ast/Operator.hpp + @brief 运算符定义 + @author PuqiAR (im@puqiar.top) + @date 2026-02-14 +*/ + +#pragma once + +#include + +#include +#include + +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 &GetUnaryOpMap(); + HashMap &GetBinaryOpMap(); + + HashMap &GetUnaryOpBindingPowerMap(); + HashMap &GetBinaryOpBindingPowerMap(); + + bool IsTokenOp(TokenType type, bool binary = true); +}; // namespace Fig \ No newline at end of file diff --git a/src/Deps/Deps.hpp b/src/Deps/Deps.hpp index 57033d7..f2386b0 100644 --- a/src/Deps/Deps.hpp +++ b/src/Deps/Deps.hpp @@ -8,20 +8,22 @@ #pragma once #include +#include #include -#include #include +#include #include namespace Fig { - #ifdef __FCORE_LINK_DEPS - using Deps::String; - using Deps::HashMap; - using Deps::CharUtils; +#ifdef __FCORE_LINK_DEPS + using Deps::String; + using Deps::HashMap; + using Deps::CharUtils; + using Deps::DynArray; - template - using Result = std::expected<_Tp, _Err>; - #endif -}; \ No newline at end of file + template + using Result = std::expected<_Tp, _Err>; +#endif +}; // namespace Fig \ No newline at end of file diff --git a/src/Deps/DynArray/DynArray.hpp b/src/Deps/DynArray/DynArray.hpp new file mode 100644 index 0000000..fe08cf5 --- /dev/null +++ b/src/Deps/DynArray/DynArray.hpp @@ -0,0 +1,14 @@ +/*! + @file src/Deps/DynArray/DynArray + @brief 依赖库DynArray定义 + @author PuqiAR (im@puqiar.top) + @date 2026-02-14 +*/ + +#include + +namespace Fig::Deps +{ + template> + using DynArray = std::vector<_Tp, _Allocator>; +}; \ No newline at end of file