#ifndef __NASAL_PARSE_H__ #define __NASAL_PARSE_H__ /* _,,,_ .' `'. / ____ \ Fucking Nasal Parser | .-'_ _\/ / \_/ a a| / (,` \ | .----. | -' | /| '--. \ '= / || ]| `-. /`-.__.' || ]| ::| .-'`-.__ \__ || ]| ::| / `` `. || ]| ::| _ | \ \ \ \| ]| .-' / \| \ | \ L.__ .--'( | |\ `. | \ ,---|_ \---------, | | '. './\ \/ .--._|=- |_ /| | \ '. `'.'. /`\/ .-' '. / | | | `'. `;-:-;`)| |-./ | | /_ `'--./_ ` )/'-------------')/) | \ | `""""----"`\//`""`/,===..'`````````/ ( | | | / `---` `===' / ) | / \ / / ( | | '------. |'--------------------'| ) | \ `-| | / | `--...,______| | ( | | | | | ) ,| | | | | ( /|| | | | | )/ `" / \ | | (/ .' /I\ '.| | /) .-'_.'/ \'. | | / ``` `"""` `| .-------------------.|| `"` `"` */ class nasal_parse { #define error_line (tok_list[ptr].line) #define is_call(type) ((type)==tok_lcurve || (type)==tok_lbracket || (type)==tok_dot) private: int ptr; int error; nasal_ast root; std::vector tok_list; int in_function; // count when generating function block,used to check return-expression int in_loop; // count when generating loop block,used to check break/continue-expression void reset(); void die(int,std::string); void match(int); bool check_multi_def(); bool check_multi_scalar(); bool check_function_end(nasal_ast&); bool check_special_call(); bool need_semi_check(nasal_ast&); void check_memory_reachable(nasal_ast&); nasal_ast null_node_gen(); nasal_ast nil_gen(); nasal_ast num_gen(); nasal_ast str_gen(); nasal_ast id_gen(); nasal_ast vec_gen(); nasal_ast hash_gen(); nasal_ast hmem_gen(); // hash member nasal_ast func_gen(); nasal_ast args_gen(); nasal_ast expr(); nasal_ast exprs_gen(); nasal_ast calc(); nasal_ast or_expr(); nasal_ast and_expr(); nasal_ast cmp_expr(); nasal_ast additive_expr(); nasal_ast multive_expr(); nasal_ast unary(); nasal_ast scalar(); nasal_ast call_scalar(); nasal_ast call_hash(); nasal_ast call_vec(); nasal_ast call_func(); nasal_ast subvec(); nasal_ast definition(); nasal_ast var_incurve_def(); nasal_ast var_outcurve_def(); nasal_ast multi_id(); nasal_ast multi_scalar(bool); nasal_ast multi_assgin(); nasal_ast loop(); nasal_ast while_loop(); nasal_ast for_loop(); nasal_ast forei_loop(); nasal_ast new_iter_gen(); nasal_ast conditional(); nasal_ast continue_expr(); nasal_ast break_expr(); nasal_ast return_expr(); public: int get_error(); void set_toklist(std::vector&); void main_process(); nasal_ast& get_root(); }; int nasal_parse::get_error() { return error; } void nasal_parse::set_toklist(std::vector& toks) { tok_list=toks; return; } void nasal_parse::main_process() { reset(); root.set_line(1); root.set_type(ast_root); while(tok_list[ptr].type!=tok_eof) { root.add_child(expr()); if(tok_list[ptr].type==tok_semi) match(tok_semi); else if(need_semi_check(root.get_children().back())) { // the last expression can be recognized without semi if(tok_list[ptr].type!=tok_eof) die(error_line,"expected \";\""); } } return; } nasal_ast& nasal_parse::get_root() { return root; } void nasal_parse::reset() { ptr=in_function=in_loop=error=0; root.clear(); return; } void nasal_parse::die(int line,std::string info) { ++error; std::cout<<">> [parse] line "<1 || tmp.get_children()[0].get_type()==ast_subvec)) die(tmp.get_line(),"cannot get the memory in temporary sliced vector"); } } else if(node.get_type()!=ast_id) die(node.get_line(),"cannot use calculation as the memory of scalar"); return; } nasal_ast nasal_parse::null_node_gen() { nasal_ast node(tok_list[ptr].line,ast_null); return node; } nasal_ast nasal_parse::nil_gen() { nasal_ast node(tok_list[ptr].line,ast_nil); return node; } nasal_ast nasal_parse::num_gen() { nasal_ast node(tok_list[ptr].line,ast_num); node.set_num(trans_string_to_number(tok_list[ptr].str)); return node; } nasal_ast nasal_parse::str_gen() { nasal_ast node(tok_list[ptr].line,ast_str); node.set_str(tok_list[ptr].str); return node; } nasal_ast nasal_parse::id_gen() { nasal_ast node(tok_list[ptr].line,ast_id); node.set_str(tok_list[ptr].str); return node; } nasal_ast nasal_parse::vec_gen() { nasal_ast node(tok_list[ptr].line,ast_vec); match(tok_lbracket); while(tok_list[ptr].type!=tok_rbracket) { node.add_child(calc()); if(tok_list[ptr].type==tok_comma) match(tok_comma); else if(tok_list[ptr].type!=tok_rbracket) break; } match(tok_rbracket); return node; } nasal_ast nasal_parse::hash_gen() { nasal_ast node(tok_list[ptr].line,ast_hash); match(tok_lbrace); while (tok_list[ptr].type!=tok_rbrace) { node.add_child(hmem_gen()); if(tok_list[ptr].type==tok_comma) match(tok_comma); else if(tok_list[ptr].type!=tok_rbrace) break; } match(tok_rbrace); return node; } nasal_ast nasal_parse::hmem_gen() { nasal_ast node(tok_list[ptr].line,ast_hashmember); if(tok_list[ptr].type==tok_id) { node.add_child(id_gen()); match(tok_id); } else if(tok_list[ptr].type==tok_str) { node.add_child(str_gen()); match(tok_str); } else match(tok_id); match(tok_colon); node.add_child(calc()); return node; } nasal_ast nasal_parse::func_gen() { nasal_ast node(tok_list[ptr].line,ast_func); match(tok_func); if(tok_list[ptr].type==tok_lcurve) node.add_child(args_gen()); else node.add_child(null_node_gen()); node.add_child(exprs_gen()); return node; } nasal_ast nasal_parse::args_gen() { nasal_ast node(tok_list[ptr].line,ast_args); match(tok_lcurve); while(tok_list[ptr].type!=tok_rcurve) { nasal_ast tmp; tmp=id_gen(); match(tok_id); if(tok_list[ptr].type==tok_eq || tok_list[ptr].type==tok_ellipsis) { nasal_ast special_arg(tok_list[ptr].line); if(tok_list[ptr].type==tok_eq) { special_arg.add_child(tmp); match(tok_eq); special_arg.add_child(calc()); special_arg.set_type(ast_default_arg); } else { match(tok_ellipsis); special_arg=tmp; special_arg.set_type(ast_dynamic_id); } node.add_child(special_arg); } else node.add_child(tmp); if(tok_list[ptr].type==tok_comma) match(tok_comma); else if(tok_list[ptr].type!=tok_rcurve) break; } match(tok_rcurve); std::string args_format="func("; int node_child_size=node.get_children().size(); for(int i=0;i argname_table; for(int i=0;i