forked from PuqiAR/Fig-TreeWalker
support function literal, set builtins to global context. great!
This commit is contained in:
@@ -21,7 +21,7 @@ namespace Fig::Ast
|
||||
class FunctionCallExpr final : public ExpressionAst
|
||||
{
|
||||
public:
|
||||
FString name;
|
||||
Expression callee; // 不是 name 了!!
|
||||
FunctionArguments arg;
|
||||
|
||||
FunctionCallExpr()
|
||||
@@ -29,15 +29,15 @@ namespace Fig::Ast
|
||||
type = AstType::FunctionCall;
|
||||
}
|
||||
|
||||
FunctionCallExpr(FString _name, FunctionArguments _arg) :
|
||||
name(std::move(_name)), arg(std::move(_arg))
|
||||
FunctionCallExpr(Expression _callee, FunctionArguments _arg) :
|
||||
callee(std::move(_callee)), arg(std::move(_arg))
|
||||
{
|
||||
type = AstType::FunctionCall;
|
||||
}
|
||||
|
||||
virtual FString toString() override
|
||||
virtual FString toString() override
|
||||
{
|
||||
FString s = name;
|
||||
FString s = callee->toString();
|
||||
s += u8"(";
|
||||
for (size_t i = 0; i < arg.argv.size(); ++i)
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace Fig::Ast
|
||||
{
|
||||
/*
|
||||
fun greet(greeting, name:String, age:Int, split:String=":") public -> Null
|
||||
func greet(greeting, name:String, age:Int, split:String=":") -> Null
|
||||
{
|
||||
io.println("{}, {}{}{}", greeting, name, split, age);
|
||||
}
|
||||
@@ -18,7 +18,7 @@ namespace Fig::Ast
|
||||
`split` -> default parameter
|
||||
*/
|
||||
|
||||
class FunctionDefSt final : public StatementAst // for define
|
||||
class FunctionDefSt final : public StatementAst // for definition
|
||||
{
|
||||
public:
|
||||
FString name;
|
||||
|
||||
47
include/Ast/FunctionLiteralExpr.hpp
Normal file
47
include/Ast/FunctionLiteralExpr.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <Ast/astBase.hpp>
|
||||
#include <Ast/functionParameters.hpp>
|
||||
#include <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
|
||||
@@ -1,43 +0,0 @@
|
||||
#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
|
||||
@@ -30,6 +30,7 @@ namespace Fig::Ast
|
||||
TupleExpr, // ()
|
||||
MapExpr, // {}
|
||||
InitExpr, // struct{}
|
||||
FunctionLiteralExpr,
|
||||
|
||||
/* Statement */
|
||||
BlockStatement,
|
||||
@@ -312,7 +313,7 @@ namespace Fig::Ast
|
||||
class BlockStatementAst : public StatementAst
|
||||
{
|
||||
public:
|
||||
const std::vector<Statement> stmts;
|
||||
std::vector<Statement> stmts;
|
||||
BlockStatementAst()
|
||||
{
|
||||
type = AstType::BlockStatement;
|
||||
|
||||
Reference in New Issue
Block a user