update

This commit is contained in:
ValKmjolnir 2023-06-24 00:30:50 +08:00
parent ac49c0d676
commit 0132ca0870
3 changed files with 151 additions and 125 deletions

View File

@ -96,7 +96,7 @@ public:
null_expr(const span& location): null_expr(const span& location):
expr(location, expr_type::ast_null) {} expr(location, expr_type::ast_null) {}
~null_expr() = default; ~null_expr() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class nil_expr:public expr { class nil_expr:public expr {
@ -104,7 +104,7 @@ public:
nil_expr(const span& location): nil_expr(const span& location):
expr(location, expr_type::ast_nil) {} expr(location, expr_type::ast_nil) {}
~nil_expr() = default; ~nil_expr() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class number_literal:public expr { class number_literal:public expr {
@ -115,7 +115,7 @@ public:
number_literal(const span& location, const f64 num): number_literal(const span& location, const f64 num):
expr(location, expr_type::ast_num), number(num) {} expr(location, expr_type::ast_num), number(num) {}
~number_literal() = default; ~number_literal() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class string_literal:public expr { class string_literal:public expr {
@ -126,7 +126,7 @@ public:
string_literal(const span& location, const std::string& str): string_literal(const span& location, const std::string& str):
expr(location, expr_type::ast_str), content(str) {} expr(location, expr_type::ast_str), content(str) {}
~string_literal() = default; ~string_literal() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class identifier:public expr { class identifier:public expr {
@ -137,7 +137,7 @@ public:
identifier(const span& location, const std::string& str): identifier(const span& location, const std::string& str):
expr(location, expr_type::ast_id), name(str) {} expr(location, expr_type::ast_id), name(str) {}
~identifier() = default; ~identifier() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class bool_literal:public expr { class bool_literal:public expr {
@ -148,7 +148,7 @@ public:
bool_literal(const span& location, const bool bool_flag): bool_literal(const span& location, const bool bool_flag):
expr(location, expr_type::ast_bool), flag(bool_flag) {} expr(location, expr_type::ast_bool), flag(bool_flag) {}
~bool_literal() = default; ~bool_literal() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class vector_expr:public expr { class vector_expr:public expr {
@ -161,7 +161,7 @@ public:
~vector_expr(); ~vector_expr();
void add_element(expr* node) {elements.push_back(node);} void add_element(expr* node) {elements.push_back(node);}
std::vector<expr*>& get_elements() {return elements;} std::vector<expr*>& get_elements() {return elements;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class hash_expr:public expr { class hash_expr:public expr {
@ -174,7 +174,7 @@ public:
~hash_expr(); ~hash_expr();
void add_member(hash_pair* node) {members.push_back(node);} void add_member(hash_pair* node) {members.push_back(node);}
std::vector<hash_pair*>& get_members() {return members;} std::vector<hash_pair*>& get_members() {return members;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class hash_pair:public expr { class hash_pair:public expr {
@ -190,7 +190,7 @@ public:
void set_element(expr* node) {element = node;} void set_element(expr* node) {element = node;}
const std::string& get_name() const {return name;} const std::string& get_name() const {return name;}
expr* get_element() {return element;} expr* get_element() {return element;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class function:public expr { class function:public expr {
@ -207,7 +207,7 @@ public:
void set_code_block(code_block* node) {block = node;} void set_code_block(code_block* node) {block = node;}
std::vector<parameter*>& get_parameter_list() {return parameter_list;} std::vector<parameter*>& get_parameter_list() {return parameter_list;}
code_block* get_code_block() {return block;} code_block* get_code_block() {return block;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class code_block:public expr { class code_block:public expr {
@ -220,7 +220,7 @@ public:
~code_block(); ~code_block();
void add_expression(expr* node) {expressions.push_back(node);} void add_expression(expr* node) {expressions.push_back(node);}
std::vector<expr*>& get_expressions() {return expressions;} std::vector<expr*>& get_expressions() {return expressions;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class parameter:public expr { class parameter:public expr {
@ -244,7 +244,7 @@ public:
void set_parameter_type(param_type pt) {type = pt;} void set_parameter_type(param_type pt) {type = pt;}
void set_parameter_name(identifier* node) {name = node;} void set_parameter_name(identifier* node) {name = node;}
void set_default_value(expr* node) {default_value = 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 { class ternary_operator:public expr {
@ -258,7 +258,10 @@ public:
expr(location, expr_type::ast_ternary), expr(location, expr_type::ast_ternary),
condition(nullptr), left(nullptr), right(nullptr) {} condition(nullptr), left(nullptr), right(nullptr) {}
~ternary_operator(); ~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 { class binary_operator:public expr {
@ -277,7 +280,9 @@ public:
geq, geq,
bitwise_or, bitwise_or,
bitwise_xor, bitwise_xor,
bitwise_and bitwise_and,
condition_and,
condition_or
}; };
private: private:
@ -293,7 +298,7 @@ public:
void set_type(binary_type operator_type) {type = operator_type;} void set_type(binary_type operator_type) {type = operator_type;}
void set_left(expr* node) {left = node;} void set_left(expr* node) {left = node;}
void set_right(expr* node) {right = node;} void set_right(expr* node) {right = node;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class unary_operator:public expr { class unary_operator:public expr {
@ -315,7 +320,7 @@ public:
~unary_operator(); ~unary_operator();
void set_type(unary_type operator_type) {type = operator_type;} void set_type(unary_type operator_type) {type = operator_type;}
void set_value(expr* node) {value = node;} void set_value(expr* node) {value = node;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class call_expr:public expr { class call_expr:public expr {
@ -330,7 +335,7 @@ public:
~call_expr(); ~call_expr();
void set_first(expr* node) {first = node;} void set_first(expr* node) {first = node;}
void add_call(expr* node) {calls.push_back(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 { class call_hash:public expr {
@ -342,7 +347,7 @@ public:
expr(location, expr_type::ast_callh), expr(location, expr_type::ast_callh),
field(name) {} field(name) {}
~call_hash(); ~call_hash();
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class call_vector:public expr { class call_vector:public expr {
@ -355,7 +360,7 @@ public:
~call_vector(); ~call_vector();
void add_slice(slice_vector* node) {calls.push_back(node);} void add_slice(slice_vector* node) {calls.push_back(node);}
std::vector<slice_vector*>& get_slices() {return calls;} std::vector<slice_vector*>& get_slices() {return calls;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class call_function:public expr { class call_function:public expr {
@ -368,7 +373,7 @@ public:
~call_function(); ~call_function();
void add_argument(expr* node) {args.push_back(node);} void add_argument(expr* node) {args.push_back(node);}
std::vector<expr*>& get_argument() {return args;} std::vector<expr*>& get_argument() {return args;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class slice_vector:public expr { class slice_vector:public expr {
@ -383,33 +388,39 @@ public:
~slice_vector(); ~slice_vector();
void set_begin(expr* node) {begin = node;} void set_begin(expr* node) {begin = node;}
void set_end(expr* node) {end = node;} void set_end(expr* node) {end = node;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class definition_expr:public expr { class definition_expr:public expr {
private: private:
identifier* variable; identifier* variable_name;
multi_define* variables; multi_define* variables;
expr* value; expr* value;
public: public:
definition_expr(const span& location): definition_expr(const span& location):
expr(location, expr_type::ast_def), expr(location, expr_type::ast_def),
variable(nullptr), variables(nullptr), value(nullptr) {} variable_name(nullptr), variables(nullptr), value(nullptr) {}
~definition_expr(); ~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 { class multi_define:public expr {
private: private:
std::vector<expr*> variables; std::vector<expr*> variables;
expr* value;
public: public:
multi_define(const span& location): multi_define(const span& location):
expr(location, expr_type::ast_multi_id) {} expr(location, expr_type::ast_multi_id),
value(nullptr) {}
~multi_define(); ~multi_define();
void add_var(expr* node) {variables.push_back(node);} 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 { class tuple_expr:public expr {
@ -421,7 +432,7 @@ public:
expr(location, expr_type::ast_tuple) {} expr(location, expr_type::ast_tuple) {}
~tuple_expr(); ~tuple_expr();
void add_element(expr* node) {elements.push_back(node);} 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 { class multi_assign:public expr {
@ -435,7 +446,7 @@ public:
~multi_assign(); ~multi_assign();
void set_left(tuple_expr* node) {left = node;} void set_left(tuple_expr* node) {left = node;}
void set_right(expr* node) {right = node;} void set_right(expr* node) {right = node;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class while_expr:public expr { class while_expr:public expr {
@ -452,7 +463,7 @@ public:
void set_code_block (code_block* node) {block = node;} void set_code_block (code_block* node) {block = node;}
expr* get_condition() {return condition;} expr* get_condition() {return condition;}
code_block* get_code_block() {return block;} code_block* get_code_block() {return block;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class for_expr:public expr { class for_expr:public expr {
@ -476,7 +487,7 @@ public:
expr* get_condition() {return condition;} expr* get_condition() {return condition;}
expr* get_step() {return step;} expr* get_step() {return step;}
code_block* get_code_block() {return block;} code_block* get_code_block() {return block;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class iter_expr:public expr { class iter_expr:public expr {
@ -493,7 +504,7 @@ public:
void set_call(expr* node) {call = node;} void set_call(expr* node) {call = node;}
identifier* get_name() {return name;} identifier* get_name() {return name;}
expr* get_call() {return call;} expr* get_call() {return call;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class forei_expr:public expr { class forei_expr:public expr {
@ -522,7 +533,7 @@ public:
iter_expr* get_iterator() {return iterator;} iter_expr* get_iterator() {return iterator;}
expr* get_value() {return vector_node;} expr* get_value() {return vector_node;}
code_block* get_code_block() {return block;} code_block* get_code_block() {return block;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class condition_expr:public expr { class condition_expr:public expr {
@ -542,7 +553,7 @@ public:
if_expr* get_if_statement() {return if_stmt;} if_expr* get_if_statement() {return if_stmt;}
std::vector<if_expr*>& get_elsif_stataments() {return elsif_stmt;} std::vector<if_expr*>& get_elsif_stataments() {return elsif_stmt;}
if_expr* get_else_statement() {return else_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 { class if_expr:public expr {
@ -559,7 +570,7 @@ public:
void set_code_block(code_block* node) {block = node;} void set_code_block(code_block* node) {block = node;}
expr* get_condition() {return condition;} expr* get_condition() {return condition;}
code_block* get_code_block() {return block;} code_block* get_code_block() {return block;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class continue_expr:public expr { class continue_expr:public expr {
@ -567,7 +578,7 @@ public:
continue_expr(const span& location): continue_expr(const span& location):
expr(location, expr_type::ast_continue) {} expr(location, expr_type::ast_continue) {}
~continue_expr() = default; ~continue_expr() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class break_expr:public expr { class break_expr:public expr {
@ -575,7 +586,7 @@ public:
break_expr(const span& location): break_expr(const span& location):
expr(location, expr_type::ast_break) {} expr(location, expr_type::ast_break) {}
~break_expr() = default; ~break_expr() = default;
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };
class return_expr:public expr { class return_expr:public expr {
@ -589,5 +600,5 @@ public:
~return_expr(); ~return_expr();
void set_value(expr* node) {value = node;} void set_value(expr* node) {value = node;}
expr* get_value() {return value;} expr* get_value() {return value;}
virtual void accept(ast_visitor*) override; void accept(ast_visitor*) override;
}; };

View File

@ -408,131 +408,145 @@ expr* parse::calc() {
auto node = bitwise_or(); auto node = bitwise_or();
if (lookahead(tok::quesmark)) { if (lookahead(tok::quesmark)) {
// trinocular calculation // trinocular calculation
ast tmp(toks[ptr].loc, ast_trino); auto tmp = new ternary_operator(toks[ptr].loc);
match(tok::quesmark); match(tok::quesmark);
tmp.add(std::move(node)); tmp->set_condition(node);
tmp.add(calc()); tmp->set_left(calc());
match(tok::colon); match(tok::colon);
tmp.add(calc()); tmp->set_right(calc());
node=std::move(tmp); node = tmp;
} else if (tok::eq<=toks[ptr].type && toks[ptr].type<=tok::lnkeq) { } 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 // 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); ast tmp(toks[ptr].loc, (u32)toks[ptr].type-(u32)tok::eq+ast_equal);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(toks[ptr].type); match(toks[ptr].type);
tmp.add(calc()); 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) { } 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); ast tmp(toks[ptr].loc, (u32)toks[ptr].type-(u32)tok::btandeq+ast_btandeq);
tmp.add(std::move(node)); tmp.add(std::move(node));
match(toks[ptr].type); match(toks[ptr].type);
tmp.add(calc()); tmp.add(calc());
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::bitwise_or() { expr* parse::bitwise_or() {
ast node=bitwise_xor(); auto node = bitwise_xor();
while(lookahead(tok::btor)) { while(lookahead(tok::btor)) {
ast tmp(toks[ptr].loc, ast_bitor); auto tmp = new binary_operator(toks[ptr].loc);
tmp.add(std::move(node)); tmp->set_type(binary_operator::binary_type::bitwise_or);
tmp->set_left(node);
match(tok::btor); match(tok::btor);
tmp.add(bitwise_xor()); tmp->set_right(bitwise_xor());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::bitwise_xor() { expr* parse::bitwise_xor() {
ast node=bitwise_and(); auto node = bitwise_and();
while(lookahead(tok::btxor)) { while(lookahead(tok::btxor)) {
ast tmp(toks[ptr].loc, ast_bitxor); auto tmp = new binary_operator(toks[ptr].loc);
tmp.add(std::move(node)); tmp->set_type(binary_operator::binary_type::bitwise_xor);
tmp->set_left(node);
match(tok::btxor); match(tok::btxor);
tmp.add(bitwise_and()); tmp->set_right(bitwise_and());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::bitwise_and() { expr* parse::bitwise_and() {
ast node=or_expr(); auto node = or_expr();
while(lookahead(tok::btand)) { while(lookahead(tok::btand)) {
ast tmp(toks[ptr].loc, ast_bitand); auto tmp = new binary_operator(toks[ptr].loc);
tmp.add(std::move(node)); tmp->set_type(binary_operator::binary_type::bitwise_and);
tmp->set_left(node);
match(tok::btand); match(tok::btand);
tmp.add(or_expr()); tmp->set_right(or_expr());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::or_expr() { expr* parse::or_expr() {
ast node=and_expr(); auto node = and_expr();
while(lookahead(tok::opor)) { while(lookahead(tok::opor)) {
ast tmp(toks[ptr].loc, ast_or); auto tmp = new binary_operator(toks[ptr].loc);
tmp.add(std::move(node)); tmp->set_type(binary_operator::binary_type::condition_or);
tmp->set_left(node);
match(tok::opor); match(tok::opor);
tmp.add(and_expr()); tmp->set_right(and_expr());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::and_expr() { expr* parse::and_expr() {
ast node=cmp_expr(); auto node = cmp_expr();
while(lookahead(tok::opand)) { while(lookahead(tok::opand)) {
ast tmp(toks[ptr].loc, ast_and); auto tmp = new binary_operator(toks[ptr].loc);
tmp.add(std::move(node)); tmp->set_type(binary_operator::binary_type::condition_and);
tmp->set_left(node);
match(tok::opand); match(tok::opand);
tmp.add(cmp_expr()); tmp->set_right(cmp_expr());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::cmp_expr() { expr* parse::cmp_expr() {
ast node=additive_expr(); auto node = additive_expr();
while(tok::cmpeq<=toks[ptr].type && toks[ptr].type<=tok::geq) { 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 // 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); auto tmp = new binary_operator(toks[ptr].loc);
tmp.add(std::move(node)); 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); match(toks[ptr].type);
tmp.add(additive_expr()); tmp->set_right(additive_expr());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
} }
binary_operator* parse::additive_expr() { expr* parse::additive_expr() {
ast node=multive_expr(); auto node = multive_expr();
while(lookahead(tok::add) || lookahead(tok::sub) || lookahead(tok::floater)) { 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) { switch(toks[ptr].type) {
case tok::add: tmp.set_type(ast_add); break; case tok::add: tmp->set_type(binary_operator::binary_type::add); break;
case tok::sub: tmp.set_type(ast_sub); break; case tok::sub: tmp->set_type(binary_operator::binary_type::sub); break;
case tok::floater: tmp.set_type(ast_link); break; case tok::floater: tmp->set_type(binary_operator::binary_type::concat); break;
default: break; default: break;
} }
tmp.add(std::move(node)); tmp->set_left(node);
match(toks[ptr].type); match(toks[ptr].type);
tmp.add(multive_expr()); tmp->set_right(multive_expr());
tmp.update_span(); update_location(tmp);
node=std::move(tmp); node = tmp;
} }
update_location(node); update_location(node);
return node; return node;
@ -582,47 +596,48 @@ unary_operator* parse::unary() {
expr* parse::scalar() { expr* parse::scalar() {
expr* node = nullptr; expr* node = nullptr;
if (lookahead(tok::tknil)) { if (lookahead(tok::tknil)) {
node=nil(); node = nil();
match(tok::tknil); match(tok::tknil);
} else if (lookahead(tok::num)) { } else if (lookahead(tok::num)) {
node=num(); node = num();
} else if (lookahead(tok::str)) { } else if (lookahead(tok::str)) {
node=str(); node = str();
} else if (lookahead(tok::id)) { } else if (lookahead(tok::id)) {
node=id(); node = id();
} else if (lookahead(tok::tktrue) || lookahead(tok::tkfalse)) { } else if (lookahead(tok::tktrue) || lookahead(tok::tkfalse)) {
node=bools(); node = bools();
} else if (lookahead(tok::func)) { } else if (lookahead(tok::func)) {
node=func(); node = func();
} else if (lookahead(tok::lbracket)) { } else if (lookahead(tok::lbracket)) {
node=vec(); node = vec();
} else if (lookahead(tok::lbrace)) { } else if (lookahead(tok::lbrace)) {
node=hash(); node = hash();
} else if (lookahead(tok::lcurve)) { } else if (lookahead(tok::lcurve)) {
const auto& loc=toks[ptr].loc; const auto& loc=toks[ptr].loc;
match(tok::lcurve); match(tok::lcurve);
node=calc(); node = calc();
node->set_begin(loc.begin_line, loc.begin_column); node->set_begin(loc.begin_line, loc.begin_column);
update_location(node); update_location(node);
match(tok::rcurve); match(tok::rcurve);
} else if (lookahead(tok::var)) { } else if (lookahead(tok::var)) {
match(tok::var); match(tok::var);
node.set_type(ast_def); auto def_node = new definition_expr(toks[ptr].loc);
node.add(id()); def_node->set_identifier(id());
match(tok::eq); match(tok::eq);
node.add(calc()); def_node->set_value(calc());
node = def_node;
} else { } else {
die(thisspan, "expected scalar"); die(thisspan, "expected scalar");
return node; return node;
} }
// check call and avoid ambiguous syntax // check call and avoid ambiguous syntax
if (is_call(toks[ptr].type) && !(lookahead(tok::lcurve) && toks[ptr+1].type==tok::var)) { if (is_call(toks[ptr].type) && !(lookahead(tok::lcurve) && toks[ptr+1].type==tok::var)) {
ast tmp=std::move(node); auto call_node = new call_expr(toks[ptr].loc);
node={toks[ptr].loc, ast_call}; call_node->set_first(node);
node.add(std::move(tmp));
while(is_call(toks[ptr].type)) { while(is_call(toks[ptr].type)) {
node.add(call_scalar()); call_node->add_call(call_scalar());
} }
node = call_node;
} }
update_location(node); update_location(node);
return node; return node;
@ -721,18 +736,18 @@ expr* parse::definition() {
if (lookahead(tok::var)) { if (lookahead(tok::var)) {
match(tok::var); match(tok::var);
switch(toks[ptr].type) { switch(toks[ptr].type) {
case tok::id: node.add(id());break; case tok::id: node->set_identifier(id());break;
case tok::lcurve: node.add(outcurve_def());break; case tok::lcurve: node->set_multi_define(outcurve_def());break;
default: die(thisspan, "expected identifier");break; default: die(thisspan, "expected identifier");break;
} }
} else if (lookahead(tok::lcurve)) { } else if (lookahead(tok::lcurve)) {
node.add(incurve_def()); node->set_multi_define(incurve_def());
} }
match(tok::eq); match(tok::eq);
if (lookahead(tok::lcurve)) { if (lookahead(tok::lcurve)) {
node.add(check_tuple()?multi_scalar():calc()); node->set_value(check_tuple()?multi_scalar():calc());
} else { } else {
node.add(calc()); node->set_value(calc());
} }
update_location(node); update_location(node);
return node; return node;
@ -759,7 +774,7 @@ multi_define* parse::outcurve_def() {
return node; return node;
} }
expr* parse::multi_id() { multi_define* parse::multi_id() {
auto node = new multi_define(toks[ptr].loc); auto node = new multi_define(toks[ptr].loc);
while(!lookahead(tok::eof)) { while(!lookahead(tok::eof)) {
// only identifier is allowed here // only identifier is allowed here

View File

@ -105,13 +105,13 @@ private:
expr* expression(); expr* expression();
code_block* expression_block(); code_block* expression_block();
expr* calc(); expr* calc();
binary_operator* bitwise_or(); expr* bitwise_or();
binary_operator* bitwise_xor(); expr* bitwise_xor();
binary_operator* bitwise_and(); expr* bitwise_and();
binary_operator* or_expr(); expr* or_expr();
binary_operator* and_expr(); expr* and_expr();
binary_operator* cmp_expr(); expr* cmp_expr();
binary_operator* additive_expr(); expr* additive_expr();
expr* multive_expr(); expr* multive_expr();
unary_operator* unary(); unary_operator* unary();
expr* scalar(); expr* scalar();