This commit is contained in:
Valk Richard Li 2021-01-02 23:57:21 +08:00
parent ea5116e963
commit bc64d530be
3 changed files with 73 additions and 162 deletions

View File

@ -24,68 +24,67 @@ enum ast_node
std::string ast_str(int type)
{
std::string str;
switch(type)
{
case ast_null: str="null";break;
case ast_root: str="root";break;
case ast_block: str="block";break;
case ast_nil: str="nil";break;
case ast_number: str="number";break;
case ast_string: str="string";break;
case ast_identifier: str="id";break;
case ast_function: str="function";break;
case ast_hash: str="hash";break;
case ast_vector: str="vector";break;
case ast_hashmember: str="hashmember";break;
case ast_call: str="call";break;
case ast_call_hash: str="call_hash";break;
case ast_call_vec: str="call_vector";break;
case ast_call_func: str="call_func";break;
case ast_subvec: str="subvec";break;
case ast_args: str="arguments";break;
case ast_default_arg: str="default_arg";break;
case ast_dynamic_id: str="dynamic_id";break;
case ast_and: str="and";break;
case ast_or: str="or";break;
case ast_equal: str="=";break;
case ast_add_equal: str="+=";break;
case ast_sub_equal: str="-=";break;
case ast_mult_equal: str="*=";break;
case ast_div_equal: str="/=";break;
case ast_link_equal: str="~=";break;
case ast_cmp_equal: str="==";break;
case ast_cmp_not_equal:str="!=";break;
case ast_less_than: str="<";break;
case ast_less_equal: str="<=";break;
case ast_greater_than: str=">";break;
case ast_greater_equal:str=">=";break;
case ast_add: str="+";break;
case ast_sub: str="-";break;
case ast_mult: str="*";break;
case ast_div: str="/";break;
case ast_link: str="~";break;
case ast_unary_sub: str="unary-";break;
case ast_unary_not: str="unary!";break;
case ast_trinocular: str="trinocular";break;
case ast_for: str="for";break;
case ast_forindex: str="forindex";break;
case ast_foreach: str="foreach";break;
case ast_while: str="while";break;
case ast_new_iter: str="new_iterator";break;
case ast_conditional: str="conditional";break;
case ast_if: str="if";break;
case ast_elsif: str="elsif";break;
case ast_else: str="else";break;
case ast_multi_id: str="multi_id";break;
case ast_multi_scalar: str="multi_scalar";break;
case ast_definition: str="definition";break;
case ast_multi_assign: str="multi_assignment";break;
case ast_continue: str="continue";break;
case ast_break: str="break";break;
case ast_return: str="return";break;
case ast_null: return "null";
case ast_root: return "root";
case ast_block: return "block";
case ast_nil: return "nil";
case ast_number: return "number";
case ast_string: return "string";
case ast_identifier: return "id";
case ast_function: return "function";
case ast_hash: return "hash";
case ast_vector: return "vector";
case ast_hashmember: return "hashmember";
case ast_call: return "call";
case ast_call_hash: return "callh";
case ast_call_vec: return "callv";
case ast_call_func: return "callf";
case ast_subvec: return "subvec";
case ast_args: return "args";
case ast_default_arg: return "deflt_arg";
case ast_dynamic_id: return "dyn_id";
case ast_and: return "and";
case ast_or: return "or";
case ast_equal: return "=";
case ast_add_equal: return "+=";
case ast_sub_equal: return "-=";
case ast_mult_equal: return "*=";
case ast_div_equal: return "/=";
case ast_link_equal: return "~=";
case ast_cmp_equal: return "==";
case ast_cmp_not_equal:return "!=";
case ast_less_than: return "<";
case ast_less_equal: return "<=";
case ast_greater_than: return ">";
case ast_greater_equal:return ">=";
case ast_add: return "+";
case ast_sub: return "-";
case ast_mult: return "*";
case ast_div: return "/";
case ast_link: return "~";
case ast_unary_sub: return "unary-";
case ast_unary_not: return "unary!";
case ast_trinocular: return "trino";
case ast_for: return "for";
case ast_forindex: return "forindex";
case ast_foreach: return "foreach";
case ast_while: return "while";
case ast_new_iter: return "iter";
case ast_conditional: return "conditional";
case ast_if: return "if";
case ast_elsif: return "elsif";
case ast_else: return "else";
case ast_multi_id: return "multi_id";
case ast_multi_scalar: return "multi_scalar";
case ast_definition: return "def";
case ast_multi_assign: return "multi_assign";
case ast_continue: return "continue";
case ast_break: return "break";
case ast_return: return "return";
}
return str;
return "null";
}
class nasal_ast
@ -223,7 +222,7 @@ void nasal_ast::print_ast(int depth)
std::cout<<":"<<this->str;
else if(this->type==ast_number)
std::cout<<":"<<this->num;
std::cout<<std::endl;
std::cout<<'\n';
int child_size=this->children.size();
for(int i=0;i<child_size;++i)
this->children[i].print_ast(depth+1);

View File

@ -806,87 +806,25 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
mem_call(ast.get_children()[0]);
gen(op_meq,0);
break;
case ast_add_equal:
// ast_add_equal(22)~ast_link_equal(26) op_addeq(23)~op_lnkeq(27)
case ast_add_equal:case ast_sub_equal:case ast_mult_equal:case ast_div_equal:case ast_link_equal:
calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]);
gen(op_addeq,0);
break;
case ast_sub_equal:
calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]);
gen(op_subeq,0);
break;
case ast_mult_equal:
calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]);
gen(op_muleq,0);
break;
case ast_div_equal:
calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]);
gen(op_diveq,0);
break;
case ast_link_equal:
calculation_gen(ast.get_children()[1]);
mem_call(ast.get_children()[0]);
gen(op_lnkeq,0);
gen(ast.get_type()-ast_add_equal+op_addeq,0);
break;
case ast_or:or_gen(ast);break;
case ast_and:and_gen(ast);break;
case ast_add:
// ast_add(33)~ast_link(37) op_add(18)~op_lnk(22)
case ast_add:case ast_sub:case ast_mult:case ast_div:case ast_link:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_add,0);
gen(ast.get_type()-ast_add+op_add,0);
break;
case ast_sub:
// ast_cmp_equal(27)~ast_greater_equal(32) op_eq(29)~op_geq(34)
case ast_cmp_equal:case ast_cmp_not_equal:case ast_less_than:case ast_less_equal:case ast_greater_than:case ast_greater_equal:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_sub,0);
break;
case ast_mult:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_mul,0);
break;
case ast_div:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_div,0);
break;
case ast_link:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_lnk,0);
break;
case ast_cmp_equal:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_eq,0);
break;
case ast_cmp_not_equal:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_neq,0);
break;
case ast_less_equal:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_leq,0);
break;
case ast_less_than:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_less,0);
break;
case ast_greater_equal:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_geq,0);
break;
case ast_greater_than:
calculation_gen(ast.get_children()[0]);
calculation_gen(ast.get_children()[1]);
gen(op_grt,0);
gen(ast.get_type()-ast_cmp_equal+op_eq,0);
break;
case ast_trinocular:trino_gen(ast);break;
case ast_unary_sub:
@ -909,11 +847,7 @@ void nasal_codegen::block_gen(nasal_ast& ast)
nasal_ast& tmp=ast.get_children()[i];
switch(tmp.get_type())
{
case ast_null:
case ast_nil:
case ast_number:
case ast_string:
case ast_function:break;
case ast_null:case ast_nil:case ast_number:case ast_string:case ast_function:break;
case ast_definition:definition_gen(tmp);break;
case ast_multi_assign:multi_assignment_gen(tmp);break;
case ast_conditional:conditional_gen(tmp);break;

View File

@ -10,28 +10,17 @@ private:
std::vector<std::string> filename_table;
int error;
void die(std::string,std::string);
void init();
bool check_import(nasal_ast&);
bool check_exist(std::string);
void linker(nasal_ast&,nasal_ast&);
nasal_ast file_import(nasal_ast&);
nasal_ast load(nasal_ast&);
public:
nasal_import();
int get_error();
void link(nasal_ast&);
nasal_ast& get_root();
};
nasal_import::nasal_import()
{
import_lex.clear();
import_par.clear();
import_ast.clear();
filename_table.clear();
return;
}
void nasal_import::die(std::string filename,std::string error_stage)
{
++error;
@ -39,13 +28,6 @@ void nasal_import::die(std::string filename,std::string error_stage)
return;
}
void nasal_import::init()
{
import_lex.clear();
import_par.clear();
return;
}
bool nasal_import::check_import(nasal_ast& node)
{
/*
@ -94,10 +76,9 @@ void nasal_import::linker(nasal_ast& root,nasal_ast& add_root)
nasal_ast nasal_import::file_import(nasal_ast& node)
{
// initializing
nasal_ast tmp;
tmp.set_line(0);
tmp.set_type(ast_root);
init();
nasal_ast tmp(0,ast_root);
import_lex.clear();
import_par.clear();
// get filename and set node to ast_null
std::string filename=node.get_children()[1].get_children()[0].get_str();
@ -131,9 +112,7 @@ nasal_ast nasal_import::file_import(nasal_ast& node)
nasal_ast nasal_import::load(nasal_ast& root)
{
nasal_ast new_root;
new_root.set_line(0);
new_root.set_type(ast_root);
nasal_ast new_root(0,ast_root);
std::vector<nasal_ast>& ref_vec=root.get_children();
int size=ref_vec.size();
@ -148,7 +127,6 @@ nasal_ast nasal_import::load(nasal_ast& root)
}
// add root to the back of new_root
linker(new_root,root);
// oops,i think it is not efficient if the root is too ... large?
return new_root;
}