forked from PuqiAR/Fig-TreeWalker
[feat]类初始化
This commit is contained in:
@@ -11,13 +11,27 @@ namespace Fig::Ast
|
||||
|
||||
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) :
|
||||
structName(std::move(_structName)), args(std::move(_args))
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class VarAssignSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
const FString varName;
|
||||
const Expression valueExpr;
|
||||
|
||||
VarAssignSt()
|
||||
{
|
||||
type = AstType::VarAssignSt;
|
||||
}
|
||||
|
||||
VarAssignSt(FString _varName, Expression _valueExpr) :
|
||||
varName(std::move(_varName)), valueExpr(std::move(_valueExpr))
|
||||
{
|
||||
type = AstType::VarAssignSt;
|
||||
}
|
||||
};
|
||||
|
||||
using VarAssign = std::shared_ptr<VarAssignSt>;
|
||||
}; // namespace Fig
|
||||
@@ -25,7 +25,6 @@ namespace Fig::Ast
|
||||
UnaryExpr,
|
||||
BinaryExpr,
|
||||
TernaryExpr,
|
||||
|
||||
ListExpr, // []
|
||||
TupleExpr, // ()
|
||||
MapExpr, // {}
|
||||
@@ -215,7 +214,8 @@ namespace Fig::Ast
|
||||
ShiftRight, // >>
|
||||
|
||||
// 赋值表达式
|
||||
Walrus, // :=
|
||||
Assign, // =
|
||||
// Walrus, // :=
|
||||
|
||||
// 点运算符 .
|
||||
Dot,
|
||||
@@ -250,7 +250,7 @@ namespace Fig::Ast
|
||||
Operator::ShiftLeft,
|
||||
Operator::ShiftRight,
|
||||
|
||||
Operator::Walrus,
|
||||
// Operator::Walrus,
|
||||
Operator::Dot};
|
||||
static const std::unordered_set<Operator> ternaryOps{Operator::TernaryCond};
|
||||
|
||||
@@ -290,7 +290,7 @@ namespace Fig::Ast
|
||||
{TokenType::ShiftRight, Operator::ShiftRight},
|
||||
|
||||
// 赋值表达式
|
||||
{TokenType::Walrus, Operator::Walrus},
|
||||
// {TokenType::Walrus, Operator::Walrus},
|
||||
// 点运算符
|
||||
{TokenType::Dot, Operator::Dot},
|
||||
}; // :=
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
|
||||
#include <variant>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
|
||||
@@ -8,14 +8,14 @@ namespace Fig
|
||||
{
|
||||
struct StructInstanceT final
|
||||
{
|
||||
FString structName; // 类的名字 (StructType), 非变量名。用于获取所属类
|
||||
size_t parentId;
|
||||
ContextPtr localContext;
|
||||
|
||||
StructInstanceT(FString _structName, ContextPtr _localContext) :
|
||||
structName(std::move(_structName)), localContext(std::move(_localContext)) {}
|
||||
StructInstanceT(size_t _parentId, ContextPtr _localContext) :
|
||||
parentId(std::move(_parentId)), localContext(std::move(_localContext)) {}
|
||||
|
||||
StructInstanceT(const StructInstanceT &other) :
|
||||
structName(other.structName), localContext(other.localContext) {}
|
||||
parentId(other.parentId), localContext(other.localContext) {}
|
||||
StructInstanceT &operator=(const StructInstanceT &) = default;
|
||||
StructInstanceT(StructInstanceT &&) = default;
|
||||
StructInstanceT &operator=(StructInstanceT &&) = default;
|
||||
@@ -31,10 +31,10 @@ namespace Fig
|
||||
{
|
||||
data = std::make_unique<StructInstanceT>(x);
|
||||
}
|
||||
StructInstance(FString _structName, ContextPtr _localContext) :
|
||||
StructInstance(size_t _parentId, ContextPtr _localContext) :
|
||||
__ValueWrapper(ValueType::StructInstance)
|
||||
{
|
||||
data = std::make_unique<StructInstanceT>(std::move(_structName), std::move(_localContext));
|
||||
data = std::make_unique<StructInstanceT>(std::move(_parentId), std::move(_localContext));
|
||||
}
|
||||
|
||||
bool operator==(const StructInstance &other) const noexcept
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
#include <Ast/AccessModifier.hpp>
|
||||
#include <Ast/BinaryExpr.hpp>
|
||||
#include <Ast/ContainerInitExprs.hpp>
|
||||
@@ -18,7 +19,6 @@
|
||||
#include <Ast/TernaryExpr.hpp>
|
||||
#include <Ast/UnaryExpr.hpp>
|
||||
#include <Ast/ValueExpr.hpp>
|
||||
#include <Ast/VarAssignSt.hpp>
|
||||
#include <Ast/VarDef.hpp>
|
||||
#include <Ast/VarExpr.hpp>
|
||||
#include <Ast/WhileSt.hpp>
|
||||
@@ -20,6 +20,8 @@ namespace Fig
|
||||
|
||||
std::unordered_map<std::size_t, FunctionStruct> functions;
|
||||
std::unordered_map<std::size_t, FString> functionNames;
|
||||
|
||||
std::unordered_map<std::size_t, FString> structTypeNames;
|
||||
public:
|
||||
ContextPtr parent;
|
||||
|
||||
@@ -129,6 +131,11 @@ namespace Fig
|
||||
functions[fn.id] = fn;
|
||||
functionNames[fn.id] = name;
|
||||
}
|
||||
if (ti == ValueType::StructType)
|
||||
{
|
||||
auto &st = value.as<StructType>().getValue();
|
||||
structTypeNames[st.id] = name;
|
||||
}
|
||||
}
|
||||
std::optional<FunctionStruct> getFunction(std::size_t id)
|
||||
{
|
||||
@@ -162,6 +169,22 @@ namespace Fig
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
std::optional<FString> getStructName(std::size_t id)
|
||||
{
|
||||
auto it = structTypeNames.find(id);
|
||||
if (it != structTypeNames.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
else if (parent)
|
||||
{
|
||||
return parent->getFunctionName(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
bool contains(const FString &name)
|
||||
{
|
||||
if (variables.contains(name))
|
||||
|
||||
@@ -302,7 +302,6 @@ namespace Fig
|
||||
Ast::ValueExpr __parseValueExpr();
|
||||
Ast::FunctionParameters __parseFunctionParameters(); // entry: current is Token::LeftParen
|
||||
Ast::BlockStatement __parseBlockStatement(); // entry: current is Token::LeftBrace
|
||||
Ast::VarAssign __parseVarAssign(FString); // entry: current is Token::Assign, para1 is var name
|
||||
Ast::If __parseIf(); // entry: current is Token::If
|
||||
Ast::While __parseWhile(); // entry: current is Token::While
|
||||
Ast::Statement __parseIncrementStatement(); // only allowed in __parseFor function
|
||||
|
||||
@@ -126,12 +126,16 @@ namespace Fig
|
||||
line = _line;
|
||||
column = _column;
|
||||
}
|
||||
Token setPos(size_t _line, size_t _column)
|
||||
const Token& setPos(size_t _line, size_t _column)
|
||||
{
|
||||
line = _line;
|
||||
column = _column;
|
||||
return *this;
|
||||
}
|
||||
size_t getLength()
|
||||
{
|
||||
return value.length();
|
||||
}
|
||||
const FString& getValue() const
|
||||
{
|
||||
return value;
|
||||
|
||||
@@ -182,7 +182,7 @@ namespace Fig
|
||||
if (is<StructInstance>())
|
||||
{
|
||||
return FString(std::format("<Struct Instance('{}') at {:p}",
|
||||
as<StructInstance>().getValue().structName.toBasicString(),
|
||||
as<StructInstance>().getValue().parentId,
|
||||
static_cast<const void *>(as<StructInstance>().data.get())));
|
||||
}
|
||||
return FString(u8"<error>");
|
||||
|
||||
Reference in New Issue
Block a user