forked from PuqiAR/Fig-TreeWalker
v0.3.1
This commit is contained in:
14
include/Ast/AccessModifier.hpp
Normal file
14
include/Ast/AccessModifier.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
namespace Fig
|
||||
{
|
||||
enum class AccessModifier
|
||||
{
|
||||
Normal,
|
||||
Const,
|
||||
Final,
|
||||
Public,
|
||||
PublicConst,
|
||||
PublicFinal,
|
||||
};
|
||||
};
|
||||
29
include/Ast/BinaryExpr.hpp
Normal file
29
include/Ast/BinaryExpr.hpp
Normal 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
|
||||
65
include/Ast/ContainerInitExprs.hpp
Normal file
65
include/Ast/ContainerInitExprs.hpp
Normal 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
|
||||
46
include/Ast/ControlSt.hpp
Normal file
46
include/Ast/ControlSt.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class ReturnSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
Expression retValue;
|
||||
|
||||
ReturnSt()
|
||||
{
|
||||
type = AstType::ReturnSt;
|
||||
}
|
||||
ReturnSt(Expression _retValue) :
|
||||
retValue(_retValue)
|
||||
{
|
||||
type = AstType::ReturnSt;
|
||||
}
|
||||
};
|
||||
|
||||
using Return = std::shared_ptr<ReturnSt>;
|
||||
|
||||
class BreakSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
BreakSt()
|
||||
{
|
||||
type = AstType::BreakSt;
|
||||
}
|
||||
};
|
||||
|
||||
using Break = std::shared_ptr<BreakSt>;
|
||||
|
||||
class ContinueSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
ContinueSt()
|
||||
{
|
||||
type = AstType::ContinueSt;
|
||||
}
|
||||
};
|
||||
|
||||
using Continue = std::shared_ptr<ContinueSt>;
|
||||
};
|
||||
21
include/Ast/ExpressionStmt.hpp
Normal file
21
include/Ast/ExpressionStmt.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class ExpressionStmtAst final : public StatementAst
|
||||
{
|
||||
public:
|
||||
Expression exp;
|
||||
ExpressionStmtAst()
|
||||
{
|
||||
type = AstType::ExpressionStmt;
|
||||
}
|
||||
ExpressionStmtAst(Expression _exp) : exp(std::move(_exp))
|
||||
{
|
||||
type = AstType::ExpressionStmt;
|
||||
}
|
||||
};
|
||||
using ExpressionStmt = std::shared_ptr<ExpressionStmtAst>;
|
||||
}
|
||||
54
include/Ast/FunctionCall.hpp
Normal file
54
include/Ast/FunctionCall.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// include/Ast/FunctionCall.hpp
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <value.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
struct FunctionArguments
|
||||
{
|
||||
std::vector<Expression> argv;
|
||||
size_t getLength() const { return argv.size(); }
|
||||
};
|
||||
|
||||
struct FunctionCallArgs final
|
||||
{
|
||||
std::vector<Value> argv;
|
||||
size_t getLength() const { return argv.size(); }
|
||||
};
|
||||
|
||||
class FunctionCallExpr final : public ExpressionAst
|
||||
{
|
||||
public:
|
||||
FString name;
|
||||
FunctionArguments arg;
|
||||
|
||||
FunctionCallExpr()
|
||||
{
|
||||
type = AstType::FunctionCall;
|
||||
}
|
||||
|
||||
FunctionCallExpr(FString _name, FunctionArguments _arg) :
|
||||
name(std::move(_name)), arg(std::move(_arg))
|
||||
{
|
||||
type = AstType::FunctionCall;
|
||||
}
|
||||
|
||||
virtual FString toString() override
|
||||
{
|
||||
FString s = name;
|
||||
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
|
||||
46
include/Ast/FunctionDefSt.hpp
Normal file
46
include/Ast/FunctionDefSt.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <Ast/functionParameters.hpp>
|
||||
#include <fig_string.hpp>
|
||||
|
||||
#include <value.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
/*
|
||||
fun greet(greeting, name:String, age:Int, split:String=":") public -> Null
|
||||
{
|
||||
io.println("{}, {}{}{}", greeting, name, split, age);
|
||||
}
|
||||
|
||||
`greeting`, `name`, `age` -> positional parameters
|
||||
`split` -> default parameter
|
||||
*/
|
||||
|
||||
class FunctionDefSt final : public StatementAst // for define
|
||||
{
|
||||
public:
|
||||
FString name;
|
||||
FunctionParameters paras;
|
||||
bool isPublic;
|
||||
FString retType;
|
||||
BlockStatement body;
|
||||
FunctionDefSt() :
|
||||
retType(ValueType::Null.name)
|
||||
{
|
||||
type = AstType::FunctionDefSt;
|
||||
}
|
||||
FunctionDefSt(FString _name, FunctionParameters _paras, bool _isPublic, FString _retType, BlockStatement _body)
|
||||
{
|
||||
type = AstType::FunctionDefSt;
|
||||
|
||||
name = std::move(_name);
|
||||
paras = std::move(_paras);
|
||||
isPublic = _isPublic;
|
||||
retType = std::move(_retType);
|
||||
body = std::move(_body);
|
||||
}
|
||||
};
|
||||
using FunctionDef = std::shared_ptr<FunctionDefSt>;
|
||||
}; // namespace Fig
|
||||
68
include/Ast/IfSt.hpp
Normal file
68
include/Ast/IfSt.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class ElseSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
BlockStatement body;
|
||||
ElseSt()
|
||||
{
|
||||
type = AstType::ElseSt;
|
||||
}
|
||||
ElseSt(BlockStatement _body) :
|
||||
body(_body)
|
||||
{
|
||||
type = AstType::ElseSt;
|
||||
}
|
||||
virtual FString toString() override
|
||||
{
|
||||
return FString(std::format("<Else Ast at {}:{}>", aai.line, aai.column));
|
||||
}
|
||||
};
|
||||
using Else = std::shared_ptr<ElseSt>;
|
||||
class ElseIfSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
Expression condition;
|
||||
BlockStatement body;
|
||||
ElseIfSt()
|
||||
{
|
||||
type = AstType::ElseIfSt;
|
||||
}
|
||||
ElseIfSt(Expression _condition,
|
||||
BlockStatement _body) :
|
||||
condition(_condition), body(_body)
|
||||
{
|
||||
type = AstType::ElseIfSt;
|
||||
}
|
||||
virtual FString toString() override
|
||||
{
|
||||
return FString(std::format("<ElseIf Ast at {}:{}>", aai.line, aai.column));
|
||||
}
|
||||
};
|
||||
using ElseIf = std::shared_ptr<ElseIfSt>;
|
||||
class IfSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
Expression condition;
|
||||
BlockStatement body;
|
||||
std::vector<ElseIf> elifs;
|
||||
Else els;
|
||||
IfSt()
|
||||
{
|
||||
type = AstType::IfSt;
|
||||
}
|
||||
IfSt(Expression _condition,
|
||||
BlockStatement _body,
|
||||
std::vector<ElseIf> _elifs,
|
||||
Else _els) :
|
||||
condition(_condition), body(_body), elifs(_elifs), els(_els)
|
||||
{
|
||||
type = AstType::IfSt;
|
||||
}
|
||||
};
|
||||
using If = std::shared_ptr<IfSt>;
|
||||
}; // namespace Fig
|
||||
0
include/Ast/ImplementSt.hpp
Normal file
0
include/Ast/ImplementSt.hpp
Normal file
28
include/Ast/InitExpr.hpp
Normal file
28
include/Ast/InitExpr.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class InitExprAst final : public ExpressionAst
|
||||
{
|
||||
public:
|
||||
FString structName;
|
||||
|
||||
std::vector<std::pair<FString, Expression>> args;
|
||||
|
||||
InitExprAst()
|
||||
{
|
||||
type = AstType::InitExpr;
|
||||
}
|
||||
|
||||
InitExprAst(FString _structName, std::vector<std::pair<FString, Expression>> _args) :
|
||||
structName(std::move(_structName)), args(std::move(_args))
|
||||
{
|
||||
type = AstType::InitExpr;
|
||||
}
|
||||
};
|
||||
|
||||
using InitExpr = std::shared_ptr<InitExprAst>;
|
||||
|
||||
}; // namespace Fig::Ast
|
||||
43
include/Ast/LambdaExpr.hpp
Normal file
43
include/Ast/LambdaExpr.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <Ast/functionParameters.hpp>
|
||||
|
||||
#include <Value/Type.hpp>
|
||||
#include <fig_string.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class LambdaExprAst : public ExpressionAst
|
||||
{
|
||||
public:
|
||||
/*
|
||||
Lambda:
|
||||
fun (greeting) -> Null {}
|
||||
*/
|
||||
|
||||
FunctionParameters paras;
|
||||
FString retType;
|
||||
BlockStatement body;
|
||||
LambdaExprAst() :
|
||||
retType(ValueType::Null.name)
|
||||
{
|
||||
type = AstType::LambdaExpr;
|
||||
}
|
||||
LambdaExprAst(FunctionParameters _paras, FString _retType, BlockStatement _body) :
|
||||
retType(ValueType::Null.name)
|
||||
{
|
||||
paras = std::move(_paras);
|
||||
retType = std::move(_retType);
|
||||
body = std::move(_body);
|
||||
}
|
||||
|
||||
virtual FString typeName() override
|
||||
{
|
||||
return FString(std::format("LambdaExprAst<{}>", retType.toBasicString()));
|
||||
}
|
||||
virtual ~LambdaExprAst() = default;
|
||||
};
|
||||
|
||||
using LambdaExpr = std::shared_ptr<LambdaExprAst>;
|
||||
}; // namespace Fig
|
||||
45
include/Ast/StructDefSt.hpp
Normal file
45
include/Ast/StructDefSt.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <fig_string.hpp>
|
||||
#include <Ast/AccessModifier.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
|
||||
struct StructDefField
|
||||
{
|
||||
AccessModifier am;
|
||||
FString fieldName;
|
||||
FString tiName;
|
||||
Expression defaultValueExpr;
|
||||
|
||||
StructDefField() {}
|
||||
StructDefField(AccessModifier _am, FString _fieldName, FString _tiName, Expression _defaultValueExpr) :
|
||||
am(std::move(_am)), fieldName(std::move(_fieldName)), tiName(std::move(_tiName)), defaultValueExpr(std::move(_defaultValueExpr))
|
||||
{
|
||||
}
|
||||
};
|
||||
class StructDefSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
bool isPublic;
|
||||
const FString name;
|
||||
const std::vector<StructDefField> fields; // field name (:type name = default value expression)
|
||||
// name / name: String / name: String = "Fig"
|
||||
const BlockStatement body;
|
||||
StructDefSt()
|
||||
{
|
||||
type = AstType::StructSt;
|
||||
}
|
||||
StructDefSt(bool _isPublic, FString _name, std::vector<StructDefField> _fields, BlockStatement _body) :
|
||||
isPublic(std::move(_isPublic)), name(std::move(_name)), fields(std::move(_fields)), body(std::move(_body))
|
||||
{
|
||||
type = AstType::StructSt;
|
||||
}
|
||||
};
|
||||
|
||||
using StructDef = std::shared_ptr<StructDefSt>;
|
||||
}; // namespace Fig
|
||||
29
include/Ast/TernaryExpr.hpp
Normal file
29
include/Ast/TernaryExpr.hpp
Normal 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
|
||||
27
include/Ast/UnaryExpr.hpp
Normal file
27
include/Ast/UnaryExpr.hpp
Normal 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
|
||||
26
include/Ast/ValueExpr.hpp
Normal file
26
include/Ast/ValueExpr.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
#include <value.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class ValueExprAst final : public ExpressionAst
|
||||
{
|
||||
public:
|
||||
Value val;
|
||||
|
||||
ValueExprAst()
|
||||
{
|
||||
type = AstType::ValueExpr;
|
||||
}
|
||||
ValueExprAst(Value _val)
|
||||
{
|
||||
type = AstType::ValueExpr;
|
||||
val = std::move(_val);
|
||||
}
|
||||
};
|
||||
|
||||
using ValueExpr = std::shared_ptr<ValueExprAst>;
|
||||
};
|
||||
26
include/Ast/VarAssignSt.hpp
Normal file
26
include/Ast/VarAssignSt.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#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
|
||||
34
include/Ast/VarDef.hpp
Normal file
34
include/Ast/VarDef.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <Value/Type.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class VarDefAst final : public StatementAst
|
||||
{
|
||||
public:
|
||||
bool isPublic;
|
||||
bool isConst;
|
||||
FString name;
|
||||
FString typeName;
|
||||
Expression expr;
|
||||
|
||||
VarDefAst() :
|
||||
typeName(ValueType::Any.name)
|
||||
{
|
||||
type = AstType::VarDefSt;
|
||||
}
|
||||
VarDefAst(bool _isPublic, bool _isConst, FString _name, FString _info, Expression _expr) :
|
||||
typeName(std::move(_info))
|
||||
{
|
||||
type = AstType::VarDefSt;
|
||||
isPublic = _isPublic;
|
||||
isConst = _isConst;
|
||||
name = std::move(_name);
|
||||
expr = std::move(_expr);
|
||||
}
|
||||
};
|
||||
|
||||
using VarDef = std::shared_ptr<VarDefAst>;
|
||||
} // namespace Fig
|
||||
24
include/Ast/VarExpr.hpp
Normal file
24
include/Ast/VarExpr.hpp
Normal 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
|
||||
26
include/Ast/WhileSt.hpp
Normal file
26
include/Ast/WhileSt.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
class WhileSt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
Expression condition;
|
||||
BlockStatement body;
|
||||
|
||||
WhileSt()
|
||||
{
|
||||
type = AstType::WhileSt;
|
||||
}
|
||||
|
||||
WhileSt(Expression _condition, BlockStatement _body)
|
||||
: condition(_condition), body(_body)
|
||||
{
|
||||
type = AstType::WhileSt;
|
||||
}
|
||||
};
|
||||
|
||||
using While = std::shared_ptr<WhileSt>;
|
||||
};
|
||||
338
include/Ast/astBase.hpp
Normal file
338
include/Ast/astBase.hpp
Normal file
@@ -0,0 +1,338 @@
|
||||
#pragma once
|
||||
|
||||
#include <token.hpp>
|
||||
#include <fig_string.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
enum class AstType : uint8_t
|
||||
{
|
||||
/* Base Class */
|
||||
_AstBase,
|
||||
StatementBase,
|
||||
ExpressionBase,
|
||||
/* Expression */
|
||||
ValueExpr,
|
||||
VarExpr,
|
||||
FunctionCall,
|
||||
LambdaExpr,
|
||||
UnaryExpr,
|
||||
BinaryExpr,
|
||||
TernaryExpr,
|
||||
|
||||
ListExpr, // []
|
||||
TupleExpr, // ()
|
||||
MapExpr, // {}
|
||||
InitExpr, // struct{}
|
||||
|
||||
/* Statement */
|
||||
BlockStatement,
|
||||
ExpressionStmt,
|
||||
|
||||
VarDefSt,
|
||||
FunctionDefSt,
|
||||
StructSt,
|
||||
ImplementSt,
|
||||
|
||||
IfSt,
|
||||
ElseSt,
|
||||
ElseIfSt,
|
||||
|
||||
VarAssignSt,
|
||||
WhileSt,
|
||||
ReturnSt,
|
||||
BreakSt,
|
||||
ContinueSt,
|
||||
};
|
||||
|
||||
static const std::unordered_map<AstType, FString> astTypeToString{
|
||||
/* Base Class */
|
||||
{AstType::_AstBase, FString(u8"Ast")},
|
||||
{AstType::StatementBase, FString(u8"Statement")},
|
||||
{AstType::ExpressionBase, FString(u8"Expression")},
|
||||
/* Expression */
|
||||
{AstType::ValueExpr, FString(u8"ValueExpr")},
|
||||
{AstType::LambdaExpr, FString(u8"LambdaExpr")},
|
||||
{AstType::UnaryExpr, FString(u8"UnaryExpr")},
|
||||
{AstType::BinaryExpr, FString(u8"BinaryExpr")},
|
||||
{AstType::TernaryExpr, FString(u8"TernaryExpr")},
|
||||
|
||||
{AstType::InitExpr, FString(u8"InitExpr")},
|
||||
|
||||
/* Statement */
|
||||
{AstType::BlockStatement, FString(u8"BlockStatement")},
|
||||
|
||||
{AstType::VarDefSt, FString(u8"VarSt")},
|
||||
{AstType::FunctionDefSt, FString(u8"FunctionDefSt")},
|
||||
{AstType::StructSt, FString(u8"StructSt")},
|
||||
{AstType::ImplementSt, FString(u8"ImplementSt")},
|
||||
|
||||
{AstType::IfSt, FString(u8"IfSt")},
|
||||
{AstType::ElseSt, FString(u8"ElseSt")},
|
||||
{AstType::ElseIfSt, FString(u8"ElseIfSt")},
|
||||
{AstType::VarAssignSt, FString(u8"VarAssignSt")},
|
||||
{AstType::WhileSt, FString(u8"WhileSt")},
|
||||
{AstType::ReturnSt, FString(u8"ReturnSt")},
|
||||
{AstType::BreakSt, FString(u8"BreakSt")},
|
||||
{AstType::ContinueSt, FString(u8"ContinueSt")},
|
||||
};
|
||||
|
||||
struct AstAddressInfo
|
||||
{
|
||||
size_t line, column;
|
||||
};
|
||||
|
||||
class _AstBase
|
||||
{
|
||||
protected:
|
||||
AstType type;
|
||||
AstAddressInfo aai;
|
||||
|
||||
public:
|
||||
_AstBase(const _AstBase &) = default;
|
||||
_AstBase(_AstBase &&) = default;
|
||||
|
||||
_AstBase &operator=(const _AstBase &) = default;
|
||||
_AstBase &operator=(_AstBase &&) = default;
|
||||
|
||||
_AstBase() {}
|
||||
|
||||
void setAAI(AstAddressInfo _aai)
|
||||
{
|
||||
aai = std::move(_aai);
|
||||
}
|
||||
|
||||
virtual FString typeName()
|
||||
{
|
||||
return astTypeToString.at(type);
|
||||
}
|
||||
virtual FString toString()
|
||||
{
|
||||
return FString(std::format("<Base Ast '{}' at {}:{}>", typeName().toBasicString(), aai.line, aai.column));
|
||||
}
|
||||
|
||||
AstAddressInfo getAAI()
|
||||
{
|
||||
return aai;
|
||||
}
|
||||
|
||||
AstType getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
};
|
||||
|
||||
class StatementAst : public _AstBase
|
||||
{
|
||||
public:
|
||||
using _AstBase::_AstBase;
|
||||
using _AstBase::operator=;
|
||||
StatementAst()
|
||||
{
|
||||
type = AstType::StatementBase;
|
||||
}
|
||||
|
||||
virtual FString toString() override
|
||||
{
|
||||
return FString(std::format("<Stmt Ast '{}' at {}:{}>", typeName().toBasicString(), aai.line, aai.column));
|
||||
}
|
||||
};
|
||||
|
||||
class EofStmt final : public StatementAst
|
||||
{
|
||||
public:
|
||||
EofStmt()
|
||||
{
|
||||
type = AstType::StatementBase;
|
||||
}
|
||||
|
||||
virtual FString toString() override
|
||||
{
|
||||
return FString(std::format("<EOF Stmt at {}:{}>", aai.line, aai.column));
|
||||
}
|
||||
};
|
||||
|
||||
class ExpressionAst : public _AstBase
|
||||
{
|
||||
public:
|
||||
using _AstBase::_AstBase;
|
||||
using _AstBase::operator=;
|
||||
ExpressionAst()
|
||||
{
|
||||
type = AstType::ExpressionBase;
|
||||
}
|
||||
|
||||
virtual FString toString() override
|
||||
{
|
||||
return FString(std::format("<Expr Ast '{}' at {}:{}>", typeName().toBasicString(), aai.line, aai.column));
|
||||
}
|
||||
};
|
||||
enum class Operator : uint8_t
|
||||
{
|
||||
LeftParen,
|
||||
RightParen,
|
||||
|
||||
// 算术
|
||||
Add, // +
|
||||
Subtract, // -
|
||||
Multiply, // *
|
||||
Divide, // /
|
||||
Modulo, // %
|
||||
Power, // **
|
||||
|
||||
// 逻辑
|
||||
And, // and / &&
|
||||
Or, // or / ||
|
||||
Not, // not / !
|
||||
|
||||
// 比较
|
||||
Equal, // ==
|
||||
NotEqual, // !=
|
||||
Less, // <
|
||||
LessEqual, // <=
|
||||
Greater, // >
|
||||
GreaterEqual, // >=
|
||||
|
||||
// 三目
|
||||
TernaryCond,
|
||||
|
||||
// 位运算
|
||||
BitAnd, // &
|
||||
BitOr, // |
|
||||
BitXor, // ^
|
||||
BitNot, // ~
|
||||
ShiftLeft, // <<
|
||||
ShiftRight, // >>
|
||||
|
||||
// 赋值表达式
|
||||
Walrus, // :=
|
||||
|
||||
// 点运算符 .
|
||||
Dot,
|
||||
};
|
||||
|
||||
static const std::unordered_set<Operator> unaryOps{
|
||||
Operator::Not,
|
||||
Operator::Subtract,
|
||||
Operator::BitNot,
|
||||
};
|
||||
static const std::unordered_set<Operator> binaryOps{
|
||||
Operator::Add,
|
||||
Operator::Subtract,
|
||||
Operator::Multiply,
|
||||
Operator::Divide,
|
||||
Operator::Modulo,
|
||||
Operator::Power,
|
||||
Operator::And,
|
||||
Operator::Or,
|
||||
|
||||
Operator::Equal,
|
||||
Operator::NotEqual,
|
||||
Operator::Less,
|
||||
Operator::LessEqual,
|
||||
Operator::Greater,
|
||||
Operator::GreaterEqual,
|
||||
|
||||
Operator::BitAnd,
|
||||
Operator::BitOr,
|
||||
Operator::BitXor,
|
||||
Operator::BitNot,
|
||||
Operator::ShiftLeft,
|
||||
Operator::ShiftRight,
|
||||
|
||||
Operator::Walrus,
|
||||
Operator::Dot};
|
||||
static const std::unordered_set<Operator> ternaryOps{Operator::TernaryCond};
|
||||
|
||||
static const std::unordered_map<TokenType, Operator> TokenToOp{
|
||||
// 算术
|
||||
{TokenType::Plus, Operator::Add},
|
||||
{TokenType::Minus, Operator::Subtract},
|
||||
{TokenType::Asterisk, Operator::Multiply},
|
||||
{TokenType::Slash, Operator::Divide},
|
||||
{TokenType::Percent, Operator::Modulo},
|
||||
{TokenType::Power, Operator::Power},
|
||||
|
||||
// 逻辑
|
||||
{TokenType::And, Operator::And},
|
||||
{TokenType::DoubleAmpersand, Operator::And},
|
||||
{TokenType::Or, Operator::Or},
|
||||
{TokenType::DoublePipe, Operator::Or},
|
||||
{TokenType::Not, Operator::Not},
|
||||
|
||||
// 比较
|
||||
{TokenType::Equal, Operator::Equal},
|
||||
{TokenType::NotEqual, Operator::NotEqual},
|
||||
{TokenType::Less, Operator::Less},
|
||||
{TokenType::LessEqual, Operator::LessEqual},
|
||||
{TokenType::Greater, Operator::Greater},
|
||||
{TokenType::GreaterEqual, Operator::GreaterEqual},
|
||||
|
||||
// 三目
|
||||
{TokenType::Question, Operator::TernaryCond},
|
||||
|
||||
// 位运算
|
||||
{TokenType::Ampersand, Operator::BitAnd},
|
||||
{TokenType::Pipe, Operator::BitOr},
|
||||
{TokenType::Caret, Operator::BitXor},
|
||||
{TokenType::Tilde, Operator::BitNot},
|
||||
{TokenType::ShiftLeft, Operator::ShiftLeft},
|
||||
{TokenType::ShiftRight, Operator::ShiftRight},
|
||||
|
||||
// 赋值表达式
|
||||
{TokenType::Walrus, Operator::Walrus},
|
||||
// 点运算符
|
||||
{TokenType::Dot, Operator::Dot},
|
||||
}; // :=
|
||||
|
||||
inline bool isOpUnary(Operator op)
|
||||
{
|
||||
return unaryOps.contains(op);
|
||||
}
|
||||
inline bool isOpBinary(Operator op)
|
||||
{
|
||||
return binaryOps.contains(op);
|
||||
}
|
||||
inline bool isOpTernary(Operator op)
|
||||
{
|
||||
return ternaryOps.contains(op);
|
||||
}
|
||||
|
||||
using AstBase = std::shared_ptr<_AstBase>;
|
||||
using Statement = std::shared_ptr<StatementAst>;
|
||||
using Expression = std::shared_ptr<ExpressionAst>;
|
||||
using Eof = std::shared_ptr<EofStmt>;
|
||||
|
||||
class BlockStatementAst : public StatementAst
|
||||
{
|
||||
public:
|
||||
const std::vector<Statement> stmts;
|
||||
BlockStatementAst()
|
||||
{
|
||||
type = AstType::BlockStatement;
|
||||
}
|
||||
BlockStatementAst(std::vector<Statement> _stmts) :
|
||||
stmts(std::move(_stmts))
|
||||
{
|
||||
type = AstType::BlockStatement;
|
||||
}
|
||||
virtual FString typeName() override
|
||||
{
|
||||
return FString(u8"BlockStatement");
|
||||
}
|
||||
virtual FString toString() override
|
||||
{
|
||||
return FString(std::format("<StmtAst '{}' at {}:{}>", typeName().toBasicString(), aai.line, aai.column));
|
||||
}
|
||||
virtual ~BlockStatementAst() = default;
|
||||
};
|
||||
|
||||
using BlockStatement = std::shared_ptr<BlockStatementAst>;
|
||||
// static BlockStatement builtinEmptyBlockSt(new BlockStatementAst());
|
||||
}; // namespace Fig::Ast
|
||||
39
include/Ast/functionParameters.hpp
Normal file
39
include/Ast/functionParameters.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <Value/Type.hpp>
|
||||
#include <fig_string.hpp>
|
||||
|
||||
namespace Fig::Ast
|
||||
{
|
||||
struct FunctionParameters // for define
|
||||
{
|
||||
/*
|
||||
Positional Parameters:
|
||||
fun test(pp1, pp2: Int)
|
||||
Default Parameters:
|
||||
fun test2(dp1 = 10, dp2:String = "default parameter 2")
|
||||
*/
|
||||
|
||||
using PosParasType = std::vector<std::pair<FString, FString>>;
|
||||
using DefParasType = std::vector<std::pair<FString, std::pair<FString, Expression>>>;
|
||||
|
||||
PosParasType posParas;
|
||||
DefParasType defParas; // default parameters
|
||||
|
||||
FunctionParameters()
|
||||
{
|
||||
|
||||
}
|
||||
FunctionParameters(PosParasType _posParas, DefParasType _defParas)
|
||||
{
|
||||
posParas = std::move(_posParas);
|
||||
defParas = std::move(_defParas);
|
||||
}
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
return posParas.size() + defParas.size();
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user