diff --git a/ast/nasal_new_ast.h b/ast/nasal_new_ast.h index e39b78f..ab38ba4 100644 --- a/ast/nasal_new_ast.h +++ b/ast/nasal_new_ast.h @@ -96,7 +96,7 @@ public: null_expr(const span& location): expr(location, expr_type::ast_null) {} ~null_expr() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class nil_expr:public expr { @@ -104,7 +104,7 @@ public: nil_expr(const span& location): expr(location, expr_type::ast_nil) {} ~nil_expr() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class number_literal:public expr { @@ -115,7 +115,7 @@ public: number_literal(const span& location, const f64 num): expr(location, expr_type::ast_num), number(num) {} ~number_literal() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class string_literal:public expr { @@ -126,7 +126,7 @@ public: string_literal(const span& location, const std::string& str): expr(location, expr_type::ast_str), content(str) {} ~string_literal() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class identifier:public expr { @@ -137,7 +137,7 @@ public: identifier(const span& location, const std::string& str): expr(location, expr_type::ast_id), name(str) {} ~identifier() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class bool_literal:public expr { @@ -148,7 +148,7 @@ public: bool_literal(const span& location, const bool bool_flag): expr(location, expr_type::ast_bool), flag(bool_flag) {} ~bool_literal() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class vector_expr:public expr { @@ -161,7 +161,7 @@ public: ~vector_expr(); void add_element(expr* node) {elements.push_back(node);} std::vector& get_elements() {return elements;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class hash_expr:public expr { @@ -174,7 +174,7 @@ public: ~hash_expr(); void add_member(hash_pair* node) {members.push_back(node);} std::vector& get_members() {return members;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class hash_pair:public expr { @@ -190,7 +190,7 @@ public: void set_element(expr* node) {element = node;} const std::string& get_name() const {return name;} expr* get_element() {return element;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class function:public expr { @@ -207,7 +207,7 @@ public: void set_code_block(code_block* node) {block = node;} std::vector& get_parameter_list() {return parameter_list;} code_block* get_code_block() {return block;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class code_block:public expr { @@ -220,7 +220,7 @@ public: ~code_block(); void add_expression(expr* node) {expressions.push_back(node);} std::vector& get_expressions() {return expressions;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class parameter:public expr { @@ -244,7 +244,7 @@ public: void set_parameter_type(param_type pt) {type = pt;} void set_parameter_name(identifier* node) {name = node;} void set_default_value(expr* node) {default_value = node;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class ternary_operator:public expr { @@ -258,7 +258,10 @@ public: expr(location, expr_type::ast_ternary), condition(nullptr), left(nullptr), right(nullptr) {} ~ternary_operator(); - virtual void accept(ast_visitor*) override; + void set_condition(expr* node) {condition = node;} + void set_left(expr* node) {left = node;} + void set_right(expr* node) {right = node;} + void accept(ast_visitor*) override; }; class binary_operator:public expr { @@ -277,7 +280,9 @@ public: geq, bitwise_or, bitwise_xor, - bitwise_and + bitwise_and, + condition_and, + condition_or }; private: @@ -293,7 +298,7 @@ public: void set_type(binary_type operator_type) {type = operator_type;} void set_left(expr* node) {left = node;} void set_right(expr* node) {right = node;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class unary_operator:public expr { @@ -315,7 +320,7 @@ public: ~unary_operator(); void set_type(unary_type operator_type) {type = operator_type;} void set_value(expr* node) {value = node;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class call_expr:public expr { @@ -330,7 +335,7 @@ public: ~call_expr(); void set_first(expr* node) {first = node;} void add_call(expr* node) {calls.push_back(node);} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class call_hash:public expr { @@ -342,7 +347,7 @@ public: expr(location, expr_type::ast_callh), field(name) {} ~call_hash(); - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class call_vector:public expr { @@ -355,7 +360,7 @@ public: ~call_vector(); void add_slice(slice_vector* node) {calls.push_back(node);} std::vector& get_slices() {return calls;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class call_function:public expr { @@ -368,7 +373,7 @@ public: ~call_function(); void add_argument(expr* node) {args.push_back(node);} std::vector& get_argument() {return args;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class slice_vector:public expr { @@ -383,33 +388,39 @@ public: ~slice_vector(); void set_begin(expr* node) {begin = node;} void set_end(expr* node) {end = node;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class definition_expr:public expr { private: - identifier* variable; + identifier* variable_name; multi_define* variables; expr* value; public: definition_expr(const span& location): expr(location, expr_type::ast_def), - variable(nullptr), variables(nullptr), value(nullptr) {} + variable_name(nullptr), variables(nullptr), value(nullptr) {} ~definition_expr(); - virtual void accept(ast_visitor*) override; + void set_identifier(identifier* node) {variable_name = node;} + void set_multi_define(multi_define* node) {variables = node;} + void set_value(expr* node) {value = node;} + void accept(ast_visitor*) override; }; class multi_define:public expr { private: std::vector variables; + expr* value; public: multi_define(const span& location): - expr(location, expr_type::ast_multi_id) {} + expr(location, expr_type::ast_multi_id), + value(nullptr) {} ~multi_define(); void add_var(expr* node) {variables.push_back(node);} - virtual void accept(ast_visitor*) override; + void set_value(expr* node) {value = node;} + void accept(ast_visitor*) override; }; class tuple_expr:public expr { @@ -421,7 +432,7 @@ public: expr(location, expr_type::ast_tuple) {} ~tuple_expr(); void add_element(expr* node) {elements.push_back(node);} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class multi_assign:public expr { @@ -435,7 +446,7 @@ public: ~multi_assign(); void set_left(tuple_expr* node) {left = node;} void set_right(expr* node) {right = node;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class while_expr:public expr { @@ -452,7 +463,7 @@ public: void set_code_block (code_block* node) {block = node;} expr* get_condition() {return condition;} code_block* get_code_block() {return block;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class for_expr:public expr { @@ -476,7 +487,7 @@ public: expr* get_condition() {return condition;} expr* get_step() {return step;} code_block* get_code_block() {return block;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class iter_expr:public expr { @@ -493,7 +504,7 @@ public: void set_call(expr* node) {call = node;} identifier* get_name() {return name;} expr* get_call() {return call;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class forei_expr:public expr { @@ -522,7 +533,7 @@ public: iter_expr* get_iterator() {return iterator;} expr* get_value() {return vector_node;} code_block* get_code_block() {return block;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class condition_expr:public expr { @@ -542,7 +553,7 @@ public: if_expr* get_if_statement() {return if_stmt;} std::vector& get_elsif_stataments() {return elsif_stmt;} if_expr* get_else_statement() {return else_stmt;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class if_expr:public expr { @@ -559,7 +570,7 @@ public: void set_code_block(code_block* node) {block = node;} expr* get_condition() {return condition;} code_block* get_code_block() {return block;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class continue_expr:public expr { @@ -567,7 +578,7 @@ public: continue_expr(const span& location): expr(location, expr_type::ast_continue) {} ~continue_expr() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class break_expr:public expr { @@ -575,7 +586,7 @@ public: break_expr(const span& location): expr(location, expr_type::ast_break) {} ~break_expr() = default; - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; class return_expr:public expr { @@ -589,5 +600,5 @@ public: ~return_expr(); void set_value(expr* node) {value = node;} expr* get_value() {return value;} - virtual void accept(ast_visitor*) override; + void accept(ast_visitor*) override; }; diff --git a/ast/nasal_new_parse.cpp b/ast/nasal_new_parse.cpp index 1cd9dbe..ffa7c13 100644 --- a/ast/nasal_new_parse.cpp +++ b/ast/nasal_new_parse.cpp @@ -408,131 +408,145 @@ expr* parse::calc() { auto node = bitwise_or(); if (lookahead(tok::quesmark)) { // trinocular calculation - ast tmp(toks[ptr].loc, ast_trino); + auto tmp = new ternary_operator(toks[ptr].loc); match(tok::quesmark); - tmp.add(std::move(node)); - tmp.add(calc()); + tmp->set_condition(node); + tmp->set_left(calc()); match(tok::colon); - tmp.add(calc()); - node=std::move(tmp); + tmp->set_right(calc()); + node = tmp; } else if (tok::eq<=toks[ptr].type && toks[ptr].type<=tok::lnkeq) { // tok::eq~tok::lnkeq is 37 to 42,ast_equal~ast_lnkeq is 21~26 ast tmp(toks[ptr].loc, (u32)toks[ptr].type-(u32)tok::eq+ast_equal); tmp.add(std::move(node)); match(toks[ptr].type); tmp.add(calc()); - node=std::move(tmp); + node = tmp; } else if (toks[ptr].type==tok::btandeq || toks[ptr].type==tok::btoreq || toks[ptr].type==tok::btxoreq) { ast tmp(toks[ptr].loc, (u32)toks[ptr].type-(u32)tok::btandeq+ast_btandeq); tmp.add(std::move(node)); match(toks[ptr].type); tmp.add(calc()); - node=std::move(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::bitwise_or() { - ast node=bitwise_xor(); +expr* parse::bitwise_or() { + auto node = bitwise_xor(); while(lookahead(tok::btor)) { - ast tmp(toks[ptr].loc, ast_bitor); - tmp.add(std::move(node)); + auto tmp = new binary_operator(toks[ptr].loc); + tmp->set_type(binary_operator::binary_type::bitwise_or); + tmp->set_left(node); match(tok::btor); - tmp.add(bitwise_xor()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(bitwise_xor()); + update_location(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::bitwise_xor() { - ast node=bitwise_and(); +expr* parse::bitwise_xor() { + auto node = bitwise_and(); while(lookahead(tok::btxor)) { - ast tmp(toks[ptr].loc, ast_bitxor); - tmp.add(std::move(node)); + auto tmp = new binary_operator(toks[ptr].loc); + tmp->set_type(binary_operator::binary_type::bitwise_xor); + tmp->set_left(node); match(tok::btxor); - tmp.add(bitwise_and()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(bitwise_and()); + update_location(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::bitwise_and() { - ast node=or_expr(); +expr* parse::bitwise_and() { + auto node = or_expr(); while(lookahead(tok::btand)) { - ast tmp(toks[ptr].loc, ast_bitand); - tmp.add(std::move(node)); + auto tmp = new binary_operator(toks[ptr].loc); + tmp->set_type(binary_operator::binary_type::bitwise_and); + tmp->set_left(node); match(tok::btand); - tmp.add(or_expr()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(or_expr()); + update_location(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::or_expr() { - ast node=and_expr(); +expr* parse::or_expr() { + auto node = and_expr(); while(lookahead(tok::opor)) { - ast tmp(toks[ptr].loc, ast_or); - tmp.add(std::move(node)); + auto tmp = new binary_operator(toks[ptr].loc); + tmp->set_type(binary_operator::binary_type::condition_or); + tmp->set_left(node); match(tok::opor); - tmp.add(and_expr()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(and_expr()); + update_location(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::and_expr() { - ast node=cmp_expr(); +expr* parse::and_expr() { + auto node = cmp_expr(); while(lookahead(tok::opand)) { - ast tmp(toks[ptr].loc, ast_and); - tmp.add(std::move(node)); + auto tmp = new binary_operator(toks[ptr].loc); + tmp->set_type(binary_operator::binary_type::condition_and); + tmp->set_left(node); match(tok::opand); - tmp.add(cmp_expr()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(cmp_expr()); + update_location(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::cmp_expr() { - ast node=additive_expr(); +expr* parse::cmp_expr() { + auto node = additive_expr(); while(tok::cmpeq<=toks[ptr].type && toks[ptr].type<=tok::geq) { // tok::cmpeq~tok::geq is 43~48,ast_cmpeq~ast_geq is 27~32 - ast tmp(toks[ptr].loc, (u32)toks[ptr].type-(u32)tok::cmpeq+ast_cmpeq); - tmp.add(std::move(node)); + auto tmp = new binary_operator(toks[ptr].loc); + switch(toks[ptr].type) { + case tok::cmpeq: tmp->set_type(binary_operator::binary_type::cmpeq); break; + case tok::neq: tmp->set_type(binary_operator::binary_type::cmpneq); break; + case tok::less: tmp->set_type(binary_operator::binary_type::less); break; + case tok::leq: tmp->set_type(binary_operator::binary_type::leq); break; + case tok::grt: tmp->set_type(binary_operator::binary_type::grt); break; + case tok::geq: tmp->set_type(binary_operator::binary_type::geq); break; + default: break; + } + tmp->set_left(node); match(toks[ptr].type); - tmp.add(additive_expr()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(additive_expr()); + update_location(tmp); + node = tmp; } update_location(node); return node; } -binary_operator* parse::additive_expr() { - ast node=multive_expr(); +expr* parse::additive_expr() { + auto node = multive_expr(); while(lookahead(tok::add) || lookahead(tok::sub) || lookahead(tok::floater)) { - ast tmp(toks[ptr].loc, ast_null); + auto tmp = new binary_operator(toks[ptr].loc); switch(toks[ptr].type) { - case tok::add: tmp.set_type(ast_add); break; - case tok::sub: tmp.set_type(ast_sub); break; - case tok::floater: tmp.set_type(ast_link); break; + case tok::add: tmp->set_type(binary_operator::binary_type::add); break; + case tok::sub: tmp->set_type(binary_operator::binary_type::sub); break; + case tok::floater: tmp->set_type(binary_operator::binary_type::concat); break; default: break; } - tmp.add(std::move(node)); + tmp->set_left(node); match(toks[ptr].type); - tmp.add(multive_expr()); - tmp.update_span(); - node=std::move(tmp); + tmp->set_right(multive_expr()); + update_location(tmp); + node = tmp; } update_location(node); return node; @@ -582,47 +596,48 @@ unary_operator* parse::unary() { expr* parse::scalar() { expr* node = nullptr; if (lookahead(tok::tknil)) { - node=nil(); + node = nil(); match(tok::tknil); } else if (lookahead(tok::num)) { - node=num(); + node = num(); } else if (lookahead(tok::str)) { - node=str(); + node = str(); } else if (lookahead(tok::id)) { - node=id(); + node = id(); } else if (lookahead(tok::tktrue) || lookahead(tok::tkfalse)) { - node=bools(); + node = bools(); } else if (lookahead(tok::func)) { - node=func(); + node = func(); } else if (lookahead(tok::lbracket)) { - node=vec(); + node = vec(); } else if (lookahead(tok::lbrace)) { - node=hash(); + node = hash(); } else if (lookahead(tok::lcurve)) { const auto& loc=toks[ptr].loc; match(tok::lcurve); - node=calc(); + node = calc(); node->set_begin(loc.begin_line, loc.begin_column); update_location(node); match(tok::rcurve); } else if (lookahead(tok::var)) { match(tok::var); - node.set_type(ast_def); - node.add(id()); + auto def_node = new definition_expr(toks[ptr].loc); + def_node->set_identifier(id()); match(tok::eq); - node.add(calc()); + def_node->set_value(calc()); + node = def_node; } else { die(thisspan, "expected scalar"); return node; } // check call and avoid ambiguous syntax if (is_call(toks[ptr].type) && !(lookahead(tok::lcurve) && toks[ptr+1].type==tok::var)) { - ast tmp=std::move(node); - node={toks[ptr].loc, ast_call}; - node.add(std::move(tmp)); + auto call_node = new call_expr(toks[ptr].loc); + call_node->set_first(node); while(is_call(toks[ptr].type)) { - node.add(call_scalar()); + call_node->add_call(call_scalar()); } + node = call_node; } update_location(node); return node; @@ -721,18 +736,18 @@ expr* parse::definition() { if (lookahead(tok::var)) { match(tok::var); switch(toks[ptr].type) { - case tok::id: node.add(id());break; - case tok::lcurve: node.add(outcurve_def());break; + case tok::id: node->set_identifier(id());break; + case tok::lcurve: node->set_multi_define(outcurve_def());break; default: die(thisspan, "expected identifier");break; } } else if (lookahead(tok::lcurve)) { - node.add(incurve_def()); + node->set_multi_define(incurve_def()); } match(tok::eq); if (lookahead(tok::lcurve)) { - node.add(check_tuple()?multi_scalar():calc()); + node->set_value(check_tuple()?multi_scalar():calc()); } else { - node.add(calc()); + node->set_value(calc()); } update_location(node); return node; @@ -759,7 +774,7 @@ multi_define* parse::outcurve_def() { return node; } -expr* parse::multi_id() { +multi_define* parse::multi_id() { auto node = new multi_define(toks[ptr].loc); while(!lookahead(tok::eof)) { // only identifier is allowed here diff --git a/ast/nasal_new_parse.h b/ast/nasal_new_parse.h index 8addbe3..9753bc5 100644 --- a/ast/nasal_new_parse.h +++ b/ast/nasal_new_parse.h @@ -105,13 +105,13 @@ private: expr* expression(); code_block* expression_block(); expr* calc(); - binary_operator* bitwise_or(); - binary_operator* bitwise_xor(); - binary_operator* bitwise_and(); - binary_operator* or_expr(); - binary_operator* and_expr(); - binary_operator* cmp_expr(); - binary_operator* additive_expr(); + expr* bitwise_or(); + expr* bitwise_xor(); + expr* bitwise_and(); + expr* or_expr(); + expr* and_expr(); + expr* cmp_expr(); + expr* additive_expr(); expr* multive_expr(); unary_operator* unary(); expr* scalar();