[Feat] 详细区分左值(LvObject)与右值(RvObject -> ObjectPtr)

[Impl] 重构evaluator.cpp + hpp 全部
[Feat] 增加对于IndexExpr的解析
[Fix][Impl] 现在点运算符不由BinaryExpr负责,增加MemberExpr,单独实现解析
[Impl] 项目目录全部翻修, src/目录下单独文件夹放置每一个模块
This commit is contained in:
2025-12-24 17:51:49 +08:00
parent 3227230aa2
commit fc35368d85
70 changed files with 1558 additions and 1233 deletions

View File

@@ -0,0 +1,29 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
class BinaryExprAst final : public ExpressionAst
{
public:
Operator op;
Expression lexp, rexp;
BinaryExprAst()
{
type = AstType::BinaryExpr;
}
BinaryExprAst(Expression _lexp, Operator _op, Expression _rexp)
{
type = AstType::BinaryExpr;
lexp = _lexp;
op = _op;
rexp = _rexp;
}
};
using BinaryExpr = std::shared_ptr<BinaryExprAst>;
}; // namespace Fig

View File

@@ -0,0 +1,65 @@
// Container Data Types --- Tuple/List/Map...
#pragma once
#include <Ast/astBase.hpp>
#include <map>
namespace Fig::Ast
{
class ListExprAst final : public ExpressionAst
{
public:
std::vector<Expression> val;
ListExprAst()
{
type = AstType::ListExpr;
}
ListExprAst(std::vector<Expression> _val) :
val(std::move(_val))
{
type = AstType::ListExpr;
}
};
using ListExpr = std::shared_ptr<ListExprAst>;
class TupleExprAst final : public ExpressionAst
{
public:
std::vector<Expression> val;
TupleExprAst()
{
type = AstType::TupleExpr;
}
TupleExprAst(std::vector<Expression> _val) :
val(std::move(_val))
{
type = AstType::TupleExpr;
}
};
using TupleExpr = std::shared_ptr<TupleExprAst>;
class MapExprAst final : public ExpressionAst
{
public:
std::map<FString, Expression> val;
MapExprAst()
{
type = AstType::MapExpr;
}
MapExprAst(std::map<FString, Expression> _val) :
val(std::move(_val))
{
type = AstType::MapExpr;
}
};
using MapExpr = std::shared_ptr<MapExprAst>;
}; // namespace Fig::Ast

View File

@@ -0,0 +1,54 @@
// include/Ast/FunctionCall.hpp
#pragma once
#include <Ast/astBase.hpp>
#include <Value/value.hpp>
namespace Fig::Ast
{
struct FunctionArguments
{
std::vector<Expression> argv;
size_t getLength() const { return argv.size(); }
};
struct FunctionCallArgs final
{
std::vector<ObjectPtr> argv;
size_t getLength() const { return argv.size(); }
};
class FunctionCallExpr final : public ExpressionAst
{
public:
Expression callee;
FunctionArguments arg;
FunctionCallExpr()
{
type = AstType::FunctionCall;
}
FunctionCallExpr(Expression _callee, FunctionArguments _arg) :
callee(std::move(_callee)), arg(std::move(_arg))
{
type = AstType::FunctionCall;
}
virtual FString toString() override
{
FString s = callee->toString();
s += u8"(";
for (size_t i = 0; i < arg.argv.size(); ++i)
{
s += arg.argv[i]->toString();
if (i + 1 < arg.argv.size())
s += u8", ";
}
s += u8")";
return s;
}
};
using FunctionCall = std::shared_ptr<FunctionCallExpr>;
}; // namespace Fig

View File

@@ -0,0 +1,47 @@
#pragma once
#include <Ast/astBase.hpp>
#include <Ast/functionParameters.hpp>
#include <Core/fig_string.hpp>
#include <variant>
namespace Fig::Ast
{
class FunctionLiteralExprAst final : public ExpressionAst
{
public:
FunctionParameters paras;
std::variant<BlockStatement, Expression> body;
FunctionLiteralExprAst(FunctionParameters _paras, BlockStatement _body) :
paras(std::move(_paras)), body(std::move(_body))
{
type = AstType::FunctionLiteralExpr;
}
FunctionLiteralExprAst(FunctionParameters _paras, Expression _exprBody) :
paras(std::move(_paras)), body(std::move(_exprBody))
{
type = AstType::FunctionLiteralExpr;
}
bool isExprMode() const
{
return std::holds_alternative<Expression>(body);
}
BlockStatement &getBlockBody()
{
return std::get<BlockStatement>(body);
}
Expression &getExprBody()
{
return std::get<Expression>(body);
}
~FunctionLiteralExprAst() = default;
};
using FunctionLiteralExpr = std::shared_ptr<FunctionLiteralExprAst>;
} // namespace Fig::Ast

View File

@@ -0,0 +1,42 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
class InitExprAst final : public ExpressionAst
{
public:
FString structName;
std::vector<std::pair<FString, Expression>> args;
enum class InitMode
{
Positional = 1,
Named,
Shorthand
} initMode;
/*
3 ways of calling constructor
.1 Person {"Fig", 1, "IDK"};
.2 Person {name: "Fig", age: 1, sex: "IDK"}; // can be unordered
.3 Person {name, age, sex};
*/
InitExprAst()
{
type = AstType::InitExpr;
}
InitExprAst(FString _structName, std::vector<std::pair<FString, Expression>> _args, InitMode _initMode) :
structName(std::move(_structName)), args(std::move(_args)), initMode(_initMode)
{
type = AstType::InitExpr;
}
};
using InitExpr = std::shared_ptr<InitExprAst>;
}; // namespace Fig::Ast

View File

@@ -0,0 +1,49 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
// actually, function call is postfix, too
// but it's too long, so use a single file (FunctionCall.hpp)
class MemberExprAst final : public ExpressionAst
{
public:
Expression base;
FString member;
MemberExprAst()
{
type = AstType::MemberExpr;
}
MemberExprAst(Expression _base, FString _member) :
base(std::move(_base)), member(std::move(_member))
{
type = AstType::MemberExpr;
}
};
using MemberExpr = std::shared_ptr<MemberExprAst>;
class IndexExprAst final : public ExpressionAst
{
public:
Expression base;
Expression index;
IndexExprAst()
{
type = AstType::IndexExpr;
}
IndexExprAst(Expression _base, Expression _index) :
base(std::move(_base)), index(std::move(_index))
{
type = AstType::IndexExpr;
}
};
using IndexExpr = std::shared_ptr<IndexExprAst>;
}; // namespace Fig::Ast

View File

@@ -0,0 +1,29 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
// condition ? val_true : val_false
class TernaryExprAst final : public ExpressionAst
{
public:
Expression condition;
Expression valueT;
Expression valueF;
TernaryExprAst()
{
type = AstType::TernaryExpr;
}
TernaryExprAst(Expression _condition, Expression _valueT, Expression _valueF)
{
type = AstType::TernaryExpr;
condition = std::move(_condition);
valueT = std::move(_valueT);
valueF = std::move(_valueF);
}
};
using TernaryExpr = std::shared_ptr<TernaryExprAst>;
} // namespace Fig

View File

@@ -0,0 +1,27 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
class UnaryExprAst final : public ExpressionAst
{
public:
Operator op;
Expression exp;
UnaryExprAst()
{
type = AstType::UnaryExpr;
}
UnaryExprAst(Operator _op, Expression _exp)
{
type = AstType::UnaryExpr;
op = _op;
exp = std::move(_exp);
}
};
using UnaryExpr = std::shared_ptr<UnaryExprAst>;
} // namespace Fig

View File

@@ -0,0 +1,26 @@
#pragma once
#include <Ast/astBase.hpp>
#include <Value/value.hpp>
namespace Fig::Ast
{
class ValueExprAst final : public ExpressionAst
{
public:
ObjectPtr val;
ValueExprAst()
{
type = AstType::ValueExpr;
}
ValueExprAst(ObjectPtr _val)
{
type = AstType::ValueExpr;
val = std::move(_val);
}
};
using ValueExpr = std::shared_ptr<ValueExprAst>;
};

View File

@@ -0,0 +1,24 @@
#pragma once
#include <Ast/astBase.hpp>
namespace Fig::Ast
{
class VarExprAst final : public ExpressionAst
{
public:
const FString name;
VarExprAst() :
name(u8"")
{
type = AstType::VarExpr;
}
VarExprAst(FString _name) :
name(std::move(_name))
{
type = AstType::VarExpr;
}
};
using VarExpr = std::shared_ptr<VarExprAst>;
}; // namespace Fig