add: complete AST generation
This commit is contained in:
parent
4e9cae13ff
commit
266273963c
27
AST.h
27
AST.h
|
|
@ -23,6 +23,7 @@ enum ASTType {
|
||||||
T_VarOpAST,
|
T_VarOpAST,
|
||||||
T_LValOpAST,
|
T_LValOpAST,
|
||||||
T_ReturnAST,
|
T_ReturnAST,
|
||||||
|
T_LiteralAST,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// BaseAST - Base class for all AST nodes.
|
/// BaseAST - Base class for all AST nodes.
|
||||||
|
|
@ -90,6 +91,10 @@ public:
|
||||||
inline ASTType getType() const override final {
|
inline ASTType getType() const override final {
|
||||||
return T_FuncDefAST;
|
return T_FuncDefAST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline const std::string& getName() const {
|
||||||
|
return this->name;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class FuncCallAST : public ExprAST {
|
class FuncCallAST : public ExprAST {
|
||||||
|
|
@ -236,6 +241,28 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LiteralAST : public ExprAST {
|
||||||
|
private:
|
||||||
|
std::shared_ptr<Value> val;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit LiteralAST(bool val) : val(std::make_shared<SingleValue>(val)) {}
|
||||||
|
|
||||||
|
explicit LiteralAST(std::int64_t val) : val(std::make_shared<SingleValue>(val)) {}
|
||||||
|
|
||||||
|
explicit LiteralAST(double val) : val(std::make_shared<SingleValue>(val)) {}
|
||||||
|
|
||||||
|
explicit LiteralAST(std::string val) : val(std::make_shared<SingleValue>(std::move(val))) {}
|
||||||
|
|
||||||
|
inline ASTType getType() const override final {
|
||||||
|
return T_LiteralAST;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Value> eval(Context* parent) override final {
|
||||||
|
return val->copy();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // __DRAGON_LISP_AST_H__
|
#endif // __DRAGON_LISP_AST_H__
|
||||||
|
|
|
||||||
126
DragonLisp.y
126
DragonLisp.y
|
|
@ -104,7 +104,33 @@ namespace DragonLisp {
|
||||||
%type <DragonLisp::Token> binary-tokens
|
%type <DragonLisp::Token> binary-tokens
|
||||||
%type <DragonLisp::Token> list-tokens
|
%type <DragonLisp::Token> list-tokens
|
||||||
|
|
||||||
%type <std::unique_ptr<DragonLisp::ExprAST>> R-Value
|
%type <std::shared_ptr<DragonLisp::LValueAST>> L-Value
|
||||||
|
%type <std::shared_ptr<DragonLisp::LValueAST>> array-ref
|
||||||
|
%type <std::shared_ptr<DragonLisp::FuncDefAST>> func-def
|
||||||
|
|
||||||
|
%type <std::shared_ptr<DragonLisp::ExprAST>> R-Value
|
||||||
|
%type <std::shared_ptr<DragonLisp::ExprAST>> S-Expr
|
||||||
|
%type <std::shared_ptr<DragonLisp::ExprAST>> S-Expr-helper
|
||||||
|
|
||||||
|
%type <std::vector<std::shared_ptr<DragonLisp::ExprAST>>> R-Value-list
|
||||||
|
|
||||||
|
%type <std::vector<std::string>> identifier-list
|
||||||
|
%type <std::vector<std::string>> func-arg-list
|
||||||
|
%type <std::vector<std::shared_ptr<DragonLisp::ExprAST>>> func-body
|
||||||
|
%type <std::shared_ptr<DragonLisp::ExprAST>> func-body-expr
|
||||||
|
|
||||||
|
%type <std::shared_ptr<DragonLisp::ReturnAST>> return-expr
|
||||||
|
%type <std::shared_ptr<DragonLisp::BinaryAST>> S-Expr-binary
|
||||||
|
%type <std::shared_ptr<DragonLisp::UnaryAST>> S-Expr-unary
|
||||||
|
%type <std::shared_ptr<DragonLisp::ListAST>> S-Expr-list
|
||||||
|
%type <std::shared_ptr<DragonLisp::IfAST>> S-Expr-if
|
||||||
|
%type <std::shared_ptr<DragonLisp::VarOpAST>> S-Expr-var-op
|
||||||
|
%type <std::shared_ptr<DragonLisp::LValOpAST>> S-Expr-Lval-op
|
||||||
|
%type <std::shared_ptr<DragonLisp::LoopAST>> S-Expr-loop
|
||||||
|
%type <std::shared_ptr<DragonLisp::FuncCallAST>> S-Expr-func-call
|
||||||
|
|
||||||
|
|
||||||
|
%type <std::variant<std::shared_ptr<DragonLisp::ExprAST>, std::shared_ptr<DragonLisp::FuncDefAST>>> statement
|
||||||
|
|
||||||
%define parse.error verbose
|
%define parse.error verbose
|
||||||
|
|
||||||
|
|
@ -118,72 +144,72 @@ S
|
||||||
;
|
;
|
||||||
|
|
||||||
statements
|
statements
|
||||||
: statement { std::printf("Parsed statements -> statement\n"); }
|
: statement { std::printf("Parsed statements -> statement\n"); drv.execute($1); }
|
||||||
| statements statement { std::printf("Parsed statements -> statements statement\n"); }
|
| statements statement { std::printf("Parsed statements -> statements statement\n"); drv.execute($2); }
|
||||||
;
|
;
|
||||||
|
|
||||||
statement
|
statement
|
||||||
: S-Expr { std::printf("Parsed statement -> S-Expr\n"); }
|
: R-Value { std::printf("Parsed statement -> R-Value\n"); $$ = $1; }
|
||||||
| func-def { std::printf("Parsed statement -> func-def\n"); }
|
| func-def { std::printf("Parsed statement -> func-def\n"); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
array-ref
|
array-ref
|
||||||
: LPAREN AREF IDENTIFIER R-Value RPAREN { std::printf("Parsed array-ref -> ( AREF IDENTIFIER R-Value )\n"); }
|
: LPAREN AREF IDENTIFIER R-Value RPAREN { std::printf("Parsed array-ref -> ( AREF IDENTIFIER R-Value )\n"); $$ = drv.constructLValueAST($3, $4); }
|
||||||
;
|
;
|
||||||
|
|
||||||
return-expr
|
return-expr
|
||||||
: LPAREN RETURN R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN R-Value )\n"); }
|
: LPAREN RETURN R-Value RPAREN { std::printf("Parsed return-expr -> ( RETURN R-Value )\n"); $$ = drv.constructReturnAST($3); }
|
||||||
;
|
;
|
||||||
|
|
||||||
func-body-expr
|
func-body-expr
|
||||||
: return-expr { std::printf("Parsed func-body -> return-expr\n"); }
|
: return-expr { std::printf("Parsed func-body -> return-expr\n"); $$ = $1; }
|
||||||
| R-Value { std::printf("Parsed func-body -> R-Value\n"); }
|
| R-Value { std::printf("Parsed func-body -> R-Value\n"); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
func-body
|
func-body
|
||||||
: func-body-expr { std::printf("Parsed func-body -> func-body-expr\n"); }
|
: func-body-expr { std::printf("Parsed func-body -> func-body-expr\n"); $$ = { $1 }; }
|
||||||
| func-body func-body-expr { std::printf("Parsed func-body -> func-body func-body-expr\n"); }
|
| func-body func-body-expr { std::printf("Parsed func-body -> func-body func-body-expr\n"); $1.push_back($2); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
L-Value
|
L-Value
|
||||||
: IDENTIFIER { std::printf("Parsed L-Value -> IDENTIFIER\n"); }
|
: IDENTIFIER { std::printf("Parsed L-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); }
|
||||||
| array-ref { std::printf("Parsed L-Value -> array-ref\n"); }
|
| array-ref { std::printf("Parsed L-Value -> array-ref\n"); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
R-Value
|
R-Value
|
||||||
: IDENTIFIER { std::printf("Parsed R-Value -> IDENTIFIER\n"); }
|
: IDENTIFIER { std::printf("Parsed R-Value -> IDENTIFIER\n"); $$ = drv.constructLValueAST($1); }
|
||||||
| S-Expr { std::printf("Parsed R-Value -> S-Expr\n"); }
|
| S-Expr { std::printf("Parsed R-Value -> S-Expr\n"); $$ = $1; }
|
||||||
| INTEGER { std::printf("Parsed R-Value -> INTEGER\n"); }
|
| INTEGER { std::printf("Parsed R-Value -> INTEGER\n"); $$ = drv.constructLiteralAST($1); }
|
||||||
| FLOAT { std::printf("Parsed R-Value -> FLOAT\n"); }
|
| FLOAT { std::printf("Parsed R-Value -> FLOAT\n"); $$ = drv.constructLiteralAST($1); }
|
||||||
| STRING { std::printf("Parsed R-Value -> STRING\n"); }
|
| STRING { std::printf("Parsed R-Value -> STRING\n"); $$ = drv.constructLiteralAST($1); }
|
||||||
| array-ref { std::printf("Parsed R-Value -> array-ref\n"); }
|
| array-ref { std::printf("Parsed R-Value -> array-ref\n"); $$ = $1; }
|
||||||
| NIL { std::printf("Parsed R-Value -> NIL\n"); }
|
| NIL { std::printf("Parsed R-Value -> NIL\n"); $$ = drv.constructLiteralAST(false); }
|
||||||
| T { std::printf("Parsed R-Value -> T\n"); }
|
| T { std::printf("Parsed R-Value -> T\n"); $$ = drv.constructLiteralAST(true); }
|
||||||
;
|
;
|
||||||
|
|
||||||
R-Value-list
|
R-Value-list
|
||||||
: R-Value { std::printf("Parsed R-Value-list -> R-Value\n"); }
|
: R-Value { std::printf("Parsed R-Value-list -> R-Value\n"); $$ = { $1 }; }
|
||||||
| R-Value-list R-Value { std::printf("Parsed R-Value-list -> R-Value-list R-Value\n"); }
|
| R-Value-list R-Value { std::printf("Parsed R-Value-list -> R-Value-list R-Value\n"); $1.push_back($2); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr
|
S-Expr
|
||||||
: LPAREN S-Expr-helper RPAREN { std::printf("Parsed S-Expr -> ( S-Expr-helper )\n"); }
|
: LPAREN S-Expr-helper RPAREN { std::printf("Parsed S-Expr -> ( S-Expr-helper )\n"); $$ = $2; }
|
||||||
| LPAREN RPAREN { std::printf("Parsed S-Expr -> ()\n"); }
|
| LPAREN RPAREN { std::printf("Parsed S-Expr -> ()\n"); $$ = drv.constructLiteralAST(false); }
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-helper
|
S-Expr-helper
|
||||||
: S-Expr-var-op { std::printf("Parsed S-Expr-helper -> S-Expr-var-op\n"); }
|
: S-Expr-var-op { std::printf("Parsed S-Expr-helper -> S-Expr-var-op\n"); $$ = $1; }
|
||||||
| S-Expr-Lval-op { std::printf("Parsed S-Expr-helper -> S-Expr-Lval-op\n"); }
|
| S-Expr-Lval-op { std::printf("Parsed S-Expr-helper -> S-Expr-Lval-op\n"); $$ = $1; }
|
||||||
| S-Expr-unary { std::printf("Parsed S-Expr-helper -> S-Expr-unary\n"); }
|
| S-Expr-unary { std::printf("Parsed S-Expr-helper -> S-Expr-unary\n"); $$ = $1; }
|
||||||
| S-Expr-binary { std::printf("Parsed S-Expr-helper -> S-Expr-binary\n"); }
|
| S-Expr-binary { std::printf("Parsed S-Expr-helper -> S-Expr-binary\n"); $$ = $1; }
|
||||||
| S-Expr-list { std::printf("Parsed S-Expr-helper -> S-Expr-list\n"); }
|
| S-Expr-list { std::printf("Parsed S-Expr-helper -> S-Expr-list\n"); $$ = $1; }
|
||||||
| S-Expr-if { std::printf("Parsed S-Expr-helper -> S-Expr-if\n"); }
|
| S-Expr-if { std::printf("Parsed S-Expr-helper -> S-Expr-if\n"); $$ = $1; }
|
||||||
| S-Expr-loop { std::printf("Parsed S-Expr-helper -> S-Expr-loop\n"); }
|
| S-Expr-loop { std::printf("Parsed S-Expr-helper -> S-Expr-loop\n"); $$ = $1; }
|
||||||
| S-Expr-func-call { std::printf("Parsed S-Expr-helper -> S-Expr-func-call\n"); }
|
| S-Expr-func-call { std::printf("Parsed S-Expr-helper -> S-Expr-func-call\n"); $$ = $1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-var-op
|
S-Expr-var-op
|
||||||
: var-op-tokens IDENTIFIER R-Value { std::printf("Parsed S-Expr-var-op -> var-op-tokens IDENTIFIER R-Value\n"); }
|
: var-op-tokens IDENTIFIER R-Value { std::printf("Parsed S-Expr-var-op -> var-op-tokens IDENTIFIER R-Value\n"); $$ = drv.constructVarOpAST($2, $3, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
var-op-tokens
|
var-op-tokens
|
||||||
|
|
@ -192,7 +218,7 @@ var-op-tokens
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-Lval-op
|
S-Expr-Lval-op
|
||||||
: lval-op-tokens L-Value R-Value { std::printf("Parsed S-Expr-Lval-op -> lval-op-tokens L-Value R-Value\n"); }
|
: lval-op-tokens L-Value R-Value { std::printf("Parsed S-Expr-Lval-op -> lval-op-tokens L-Value R-Value\n"); $$ = drv.constructLValOpAST($2, $3, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
lval-op-tokens
|
lval-op-tokens
|
||||||
|
|
@ -202,7 +228,7 @@ lval-op-tokens
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-unary
|
S-Expr-unary
|
||||||
: unary-tokens R-Value { std::printf("Parsed S-Expr-unary -> unary-tokens R-Value\n"); }
|
: unary-tokens R-Value { std::printf("Parsed S-Expr-unary -> unary-tokens R-Value\n"); $$ = drv.constructUnaryExprAST($2, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
unary-tokens
|
unary-tokens
|
||||||
|
|
@ -212,7 +238,7 @@ unary-tokens
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-binary
|
S-Expr-binary
|
||||||
: binary-tokens R-Value R-Value { std::printf("Parsed S-Expr-binary -> binary-tokens R-Value R-Value\n"); }
|
: binary-tokens R-Value R-Value { std::printf("Parsed S-Expr-binary -> binary-tokens R-Value R-Value\n"); $$ = drv.constructBinaryExprAST($2, $3, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
binary-tokens
|
binary-tokens
|
||||||
|
|
@ -226,7 +252,7 @@ binary-tokens
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-list
|
S-Expr-list
|
||||||
: list-tokens R-Value-list { std::printf("Parsed S-Expr-list -> list-tokens R-Value-list\n"); }
|
: list-tokens R-Value-list { std::printf("Parsed S-Expr-list -> list-tokens R-Value-list\n"); $$ = drv.constructListExprAST($2, $1); }
|
||||||
;
|
;
|
||||||
|
|
||||||
list-tokens
|
list-tokens
|
||||||
|
|
@ -247,32 +273,32 @@ list-tokens
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-if
|
S-Expr-if
|
||||||
: IF R-Value func-body-expr func-body-expr { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr func-body-expr\n"); }
|
: IF R-Value func-body-expr func-body-expr { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr func-body-expr\n"); $$ = drv.constructIfAST($2, $3, $4); }
|
||||||
| IF R-Value func-body-expr { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr\n"); }
|
| IF R-Value func-body-expr { std::printf("Parsed S-Expr-if -> IF R-Value func-body-expr\n"); $$ = drv.constructIfAST($2, $3, nullptr); }
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-loop
|
S-Expr-loop
|
||||||
: LOOP func-body { std::printf("Parsed S-Expr-loop -> LOOP func-body\n"); }
|
: LOOP func-body { std::printf("Parsed S-Expr-loop -> LOOP func-body\n"); $$ = drv.constructLoopAST($2); }
|
||||||
| LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body { std::printf("Parsed S-Expr-loop -> LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body\n"); }
|
| LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body { std::printf("Parsed S-Expr-loop -> LOOP FOR IDENTIFIER FROM R-Value TO R-Value DO func-body\n"); $$ = drv.constructLoopAST($3, $5, $7, $9); }
|
||||||
| DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body { std::printf("Parsed S-Expr-loop -> DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body\n"); }
|
| DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body { std::printf("Parsed S-Expr-loop -> DOTIMES LPAREN IDENTIFIER R-Value RPAREN func-body\n"); $$ = drv.constructLoopAST($3, $4, $6); }
|
||||||
;
|
;
|
||||||
|
|
||||||
func-def
|
func-def
|
||||||
: LPAREN DEFUN IDENTIFIER func-arg-list func-body RPAREN { std::printf("Parsed func-def -> ( DEFUN IDENTIFIER func-arg-list func-body )\n"); }
|
: LPAREN DEFUN IDENTIFIER func-arg-list func-body RPAREN { std::printf("Parsed func-def -> ( DEFUN IDENTIFIER func-arg-list func-body )\n"); $$ = drv.constructFuncDefAST($3, $4, $5); }
|
||||||
;
|
;
|
||||||
|
|
||||||
func-arg-list
|
func-arg-list
|
||||||
: LPAREN RPAREN { std::printf("Parsed func-arg-list -> ( )\n"); }
|
: LPAREN RPAREN { std::printf("Parsed func-arg-list -> ( )\n"); $$ = {}; }
|
||||||
| LPAREN identifier-list RPAREN { std::printf("Parsed func-arg-list -> ( identifier-list )\n"); }
|
| LPAREN identifier-list RPAREN { std::printf("Parsed func-arg-list -> ( identifier-list )\n"); $$ = $2; }
|
||||||
|
|
||||||
identifier-list
|
identifier-list
|
||||||
: identifier-list IDENTIFIER { std::printf("Parsed identifier-list -> identifier-list IDENTIFIER\n"); }
|
: identifier-list IDENTIFIER { std::printf("Parsed identifier-list -> identifier-list IDENTIFIER\n"); $1.push_back($2); $$ = $1; }
|
||||||
| IDENTIFIER { std::printf("Parsed identifier-list -> IDENTIFIER\n"); }
|
| IDENTIFIER { std::printf("Parsed identifier-list -> IDENTIFIER\n"); $$ = { $1 }; }
|
||||||
;
|
;
|
||||||
|
|
||||||
S-Expr-func-call
|
S-Expr-func-call
|
||||||
: IDENTIFIER R-Value-list { std::printf("Parsed S-Expr-func-call -> IDENTIFIER R-Value-list\n"); }
|
: IDENTIFIER R-Value-list { std::printf("Parsed S-Expr-func-call -> IDENTIFIER R-Value-list\n"); $$ = drv.constructFuncCallAST($1, $2); }
|
||||||
| IDENTIFIER { std::printf("Parsed S-Expr-func-call -> IDENTIFIER\n"); }
|
| IDENTIFIER { std::printf("Parsed S-Expr-func-call -> IDENTIFIER\n"); $$ = drv.constructFuncCallAST($1, {}); }
|
||||||
;
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,109 @@ int DLDriver::parse(std::istream& in, const std::string& s) {
|
||||||
delete this->parser;
|
delete this->parser;
|
||||||
this->parser = new DLParser(*this->scanner, *this);
|
this->parser = new DLParser(*this->scanner, *this);
|
||||||
|
|
||||||
|
// Execution Context
|
||||||
|
delete this->context;
|
||||||
|
this->context = new Context(nullptr);
|
||||||
|
|
||||||
this->parser->set_debug_level(1);
|
this->parser->set_debug_level(1);
|
||||||
return this->parser->parse();
|
return this->parser->parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<LValueAST> DLDriver::constructLValueAST(std::string name) {
|
||||||
|
return std::make_shared<IdentifierAST>(std::move(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<LValueAST> DLDriver::constructLValueAST(std::string name, std::shared_ptr<ExprAST> index) {
|
||||||
|
return std::make_shared<ArrayRefAST>(std::move(name), std::move(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<FuncDefAST> DLDriver::constructFuncDefAST(std::string name, std::vector<std::string> args, std::vector<std::shared_ptr<ExprAST>> body) {
|
||||||
|
return std::make_shared<FuncDefAST>(std::move(name), std::move(args), std::move(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ExprAST> DLDriver::constructLiteralAST(bool value) {
|
||||||
|
return std::make_shared<LiteralAST>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ExprAST> DLDriver::constructLiteralAST(std::int64_t value) {
|
||||||
|
return std::make_shared<LiteralAST>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ExprAST> DLDriver::constructLiteralAST(double value) {
|
||||||
|
return std::make_shared<LiteralAST>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ExprAST> DLDriver::constructLiteralAST(std::string value) {
|
||||||
|
return std::make_shared<LiteralAST>(std::move(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<BinaryAST> DLDriver::constructBinaryExprAST(std::shared_ptr<ExprAST> lhs, std::shared_ptr<ExprAST> rhs, Token op) {
|
||||||
|
return std::make_shared<BinaryAST>(std::move(lhs), std::move(rhs), op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<UnaryAST> DLDriver::constructUnaryExprAST(std::shared_ptr<ExprAST> expr, Token op) {
|
||||||
|
return std::make_shared<UnaryAST>(std::move(expr), op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ListAST> DLDriver::constructListExprAST(std::vector<std::shared_ptr<ExprAST>> exprs, Token op) {
|
||||||
|
return std::make_shared<ListAST>(std::move(exprs), op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<IfAST> DLDriver::constructIfAST(std::shared_ptr<ExprAST> cond, std::shared_ptr<ExprAST> then, std::shared_ptr<ExprAST> els) {
|
||||||
|
return std::make_shared<IfAST>(std::move(cond), std::move(then), std::move(els));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<FuncCallAST> DLDriver::constructFuncCallAST(std::string name, std::vector<std::shared_ptr<ExprAST>> args) {
|
||||||
|
return std::make_shared<FuncCallAST>(std::move(name), std::move(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<VarOpAST> DLDriver::constructVarOpAST(std::string name, std::shared_ptr<ExprAST> value, Token op) {
|
||||||
|
return std::make_shared<VarOpAST>(std::move(name), std::move(value), op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<LValOpAST> DLDriver::constructLValOpAST(std::shared_ptr<LValueAST> lval, std::shared_ptr<ExprAST> value, Token op) {
|
||||||
|
return std::make_shared<LValOpAST>(std::move(lval), std::move(value), op);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ReturnAST> DLDriver::constructReturnAST(std::shared_ptr<ExprAST> value) {
|
||||||
|
return std::make_shared<ReturnAST>(std::move(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<LoopAST> DLDriver::constructLoopAST(std::vector<std::shared_ptr<ExprAST>> body) {
|
||||||
|
return std::make_shared<LoopAST>(
|
||||||
|
"",
|
||||||
|
std::make_shared<LiteralAST>(true),
|
||||||
|
std::make_shared<LiteralAST>(true),
|
||||||
|
std::move(body)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<LoopAST> DLDriver::constructLoopAST(std::string id, std::shared_ptr<ExprAST> to, std::vector<std::shared_ptr<ExprAST>> body) {
|
||||||
|
return std::make_shared<LoopAST>(
|
||||||
|
std::move(id),
|
||||||
|
std::make_shared<LiteralAST>(std::int64_t(0)),
|
||||||
|
std::move(to),
|
||||||
|
std::move(body)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<LoopAST> DLDriver::constructLoopAST(std::string id, std::shared_ptr<ExprAST> from, std::shared_ptr<ExprAST> to, std::vector<std::shared_ptr<ExprAST>> body) {
|
||||||
|
return std::make_shared<LoopAST>(
|
||||||
|
std::move(id),
|
||||||
|
std::move(from),
|
||||||
|
std::move(to),
|
||||||
|
std::move(body)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DLDriver::execute(std::variant <std::shared_ptr<DragonLisp::ExprAST>, std::shared_ptr<DragonLisp::FuncDefAST>> ast) {
|
||||||
|
if (ast.index() == 0) { // ExprAST
|
||||||
|
auto expr = std::get<0>(ast);
|
||||||
|
expr->eval(this->context);
|
||||||
|
} else { // ast.index() == 1, FuncDefAST
|
||||||
|
auto func = std::get<1>(ast);
|
||||||
|
this->context->setFunc(func->getName(), func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // end namespace DragonLisp
|
} // end namespace DragonLisp
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,18 @@
|
||||||
|
|
||||||
#include "DragonLispScanner.h"
|
#include "DragonLispScanner.h"
|
||||||
#include "DragonLisp.tab.hh"
|
#include "DragonLisp.tab.hh"
|
||||||
|
#include "AST.h"
|
||||||
|
|
||||||
namespace DragonLisp {
|
namespace DragonLisp {
|
||||||
|
|
||||||
class DLDriver {
|
class DLDriver {
|
||||||
|
private:
|
||||||
|
DLParser* parser = nullptr;
|
||||||
|
DLScanner* scanner = nullptr;
|
||||||
|
DragonLisp::location location;
|
||||||
|
|
||||||
|
Context* context = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DLDriver() = default;
|
DLDriver() = default;
|
||||||
virtual ~DLDriver();
|
virtual ~DLDriver();
|
||||||
|
|
@ -20,10 +28,51 @@ public:
|
||||||
void error(const DLParser::location_type& l, const std::string& m);
|
void error(const DLParser::location_type& l, const std::string& m);
|
||||||
void error(const std::string& m);
|
void error(const std::string& m);
|
||||||
|
|
||||||
private:
|
void execute(std::variant<std::shared_ptr<DragonLisp::ExprAST>, std::shared_ptr<DragonLisp::FuncDefAST>> ast);
|
||||||
DLParser* parser = nullptr;
|
|
||||||
DLScanner* scanner = nullptr;
|
// Identifier AST
|
||||||
DragonLisp::location location;
|
static std::shared_ptr<LValueAST> constructLValueAST(std::string name);
|
||||||
|
|
||||||
|
// ArrayRef AST
|
||||||
|
static std::shared_ptr<LValueAST> constructLValueAST(std::string name, std::shared_ptr<ExprAST> index);
|
||||||
|
|
||||||
|
// FuncDef AST
|
||||||
|
static std::shared_ptr<FuncDefAST> constructFuncDefAST(std::string name, std::vector<std::string> args, std::vector<std::shared_ptr<ExprAST>> body);
|
||||||
|
|
||||||
|
// Literal AST
|
||||||
|
static std::shared_ptr<ExprAST> constructLiteralAST(bool value);
|
||||||
|
static std::shared_ptr<ExprAST> constructLiteralAST(std::int64_t value);
|
||||||
|
static std::shared_ptr<ExprAST> constructLiteralAST(double value);
|
||||||
|
static std::shared_ptr<ExprAST> constructLiteralAST(std::string value);
|
||||||
|
|
||||||
|
// BinaryExpr AST
|
||||||
|
static std::shared_ptr<BinaryAST> constructBinaryExprAST(std::shared_ptr<ExprAST> lhs, std::shared_ptr<ExprAST> rhs, Token op);
|
||||||
|
|
||||||
|
// UnaryExpr AST
|
||||||
|
static std::shared_ptr<UnaryAST> constructUnaryExprAST(std::shared_ptr<ExprAST> expr, Token op);
|
||||||
|
|
||||||
|
// ListExpr AST
|
||||||
|
static std::shared_ptr<ListAST> constructListExprAST(std::vector<std::shared_ptr<ExprAST>> exprs, Token op);
|
||||||
|
|
||||||
|
// If AST
|
||||||
|
static std::shared_ptr<IfAST> constructIfAST(std::shared_ptr<ExprAST> cond, std::shared_ptr<ExprAST> then, std::shared_ptr<ExprAST> els);
|
||||||
|
|
||||||
|
// Func Call AST
|
||||||
|
static std::shared_ptr<FuncCallAST> constructFuncCallAST(std::string name, std::vector<std::shared_ptr<ExprAST>> args);
|
||||||
|
|
||||||
|
// Var Op AST
|
||||||
|
static std::shared_ptr<VarOpAST> constructVarOpAST(std::string name, std::shared_ptr<ExprAST> value, Token op);
|
||||||
|
|
||||||
|
// LVal Op AST
|
||||||
|
static std::shared_ptr<LValOpAST> constructLValOpAST(std::shared_ptr<LValueAST> lval, std::shared_ptr<ExprAST> value, Token op);
|
||||||
|
|
||||||
|
// Return AST
|
||||||
|
static std::shared_ptr<ReturnAST> constructReturnAST(std::shared_ptr<ExprAST> value);
|
||||||
|
|
||||||
|
// Loop AST
|
||||||
|
static std::shared_ptr<LoopAST> constructLoopAST(std::vector<std::shared_ptr<ExprAST>> body);
|
||||||
|
static std::shared_ptr<LoopAST> constructLoopAST(std::string id, std::shared_ptr<ExprAST> from, std::shared_ptr<ExprAST> to, std::vector<std::shared_ptr<ExprAST>> body);
|
||||||
|
static std::shared_ptr<LoopAST> constructLoopAST(std::string id, std::shared_ptr<ExprAST> to, std::vector<std::shared_ptr<ExprAST>> body);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end namespace DragonLisp
|
} // end namespace DragonLisp
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue