change name of enum:ast_hashmember->ast_pair, ast_new_iter->ast_iter

change ast_hashmember to ast_pair is because this type in fact is the same as std::pair<std::string,nasal_ref> in C++
This commit is contained in:
ValKmjolnir 2022-01-31 17:22:44 +08:00
parent baa4f5a258
commit eaa54035ff
6 changed files with 29 additions and 31 deletions

View File

@ -14,7 +14,7 @@ enum ast_node
ast_func, // func keyword
ast_hash, // hash, basic value type
ast_vec, // vector, basic value type
ast_hashmember, // elements in hashmap
ast_pair, // pair of key and value in hashmap
ast_call, // mark a sub-tree of calling an identifier
ast_callh, // id.name
ast_callv, // id[index]
@ -49,7 +49,7 @@ enum ast_node
ast_forindex, // forindex keyword
ast_foreach, // foreach keyword
ast_while, // while
ast_new_iter, // iterator, used in forindex/foreach
ast_iter, // iterator, used in forindex/foreach
ast_conditional, // mark a sub-tree of conditional expression
ast_if, // if keyword
ast_elsif, // elsif keyword

View File

@ -294,7 +294,7 @@ void nasal_codegen::find_symbol(const nasal_ast& node)
find_symbol(node[1]);
}
// find iterator(foreach, forindex), check
else if(node.type()==ast_new_iter)
else if(node.type()==ast_iter)
add_sym(node[0].str());
// check children
else
@ -528,7 +528,7 @@ void nasal_codegen::call_func(const nasal_ast& ast)
{
if(!ast.size())
gen(op_callfv,0,ast.line());
else if(ast[0].type()==ast_hashmember)
else if(ast[0].type()==ast_pair)
{
hash_gen(ast);
gen(op_callfh,0,ast.line());
@ -847,7 +847,7 @@ void nasal_codegen::forindex_gen(const nasal_ast& ast)
gen(op_cnt,0,ast[1].line());
int ptr=code.size();
gen(op_findex,0,ast.line());
if(ast[0].type()==ast_new_iter)
if(ast[0].type()==ast_iter)
{
const std::string& str=ast[0][0].str();
local.empty()?
@ -875,7 +875,7 @@ void nasal_codegen::foreach_gen(const nasal_ast& ast)
gen(op_cnt,0,ast.line());
int ptr=code.size();
gen(op_feach,0,ast.line());
if(ast[0].type()==ast_new_iter)
if(ast[0].type()==ast_iter)
{
const std::string& str=ast[0][0].str();
local.empty()?

View File

@ -67,11 +67,9 @@ public:
{
++error;
if(!line)
{
std::cerr<<"["<<stage<<"] "<<file<<": "<<info<<'\n';
return;
}
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
else
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
}
void chkerr(){if(error)std::exit(1);}
};

View File

@ -4,11 +4,11 @@
class nasal_import
{
private:
nasal_err& nerr;
nasal_err& nerr;
std::vector<std::string> files;
bool check_import(const nasal_ast&);
bool check_exist(const std::string&);
void linker(nasal_ast&,nasal_ast&&);
bool check_import(const nasal_ast&);
bool check_exist(const std::string&);
void linker(nasal_ast&,nasal_ast&&);
nasal_ast file_import(nasal_ast&);
nasal_ast load(nasal_ast&,uint16_t);
public:
@ -22,9 +22,9 @@ bool nasal_import::check_import(const nasal_ast& node)
/*
only this kind of node can be recognized as 'import':
call
id:import
call_func
string:'filename'
|_id:import
|_call_func
|_string:'filename'
*/
return (
node.type()==ast_call &&

View File

@ -20,14 +20,14 @@ void calc_const_num(nasal_ast& root)
double res;
switch(root.type())
{
case ast_add:res=vec[0].num()+vec[1].num();break;
case ast_sub:res=vec[0].num()-vec[1].num();break;
case ast_mult:res=vec[0].num()*vec[1].num();break;
case ast_div:res=vec[0].num()/vec[1].num();break;
case ast_less:res=vec[0].num()<vec[1].num();break;
case ast_leq:res=vec[0].num()<=vec[1].num();break;
case ast_grt:res=vec[0].num()>vec[1].num();break;
case ast_geq:res=vec[0].num()>=vec[1].num();break;
case ast_add: res=vec[0].num()+vec[1].num(); break;
case ast_sub: res=vec[0].num()-vec[1].num(); break;
case ast_mult:res=vec[0].num()*vec[1].num(); break;
case ast_div: res=vec[0].num()/vec[1].num(); break;
case ast_less:res=vec[0].num()<vec[1].num(); break;
case ast_leq: res=vec[0].num()<=vec[1].num();break;
case ast_grt: res=vec[0].num()>vec[1].num(); break;
case ast_geq: res=vec[0].num()>=vec[1].num();break;
}
if(std::isinf(res) || std::isnan(res)) // inf and nan will cause number hashmap error in codegen
return;

View File

@ -63,7 +63,7 @@ private:
nasal_ast id();
nasal_ast vec();
nasal_ast hash();
nasal_ast hmem();
nasal_ast pair();
nasal_ast func();
nasal_ast args();
nasal_ast lcurve_expr();
@ -305,7 +305,7 @@ nasal_ast nasal_parse::hash()
match(tok_lbrace);
while(tokens[ptr].type!=tok_rbrace)
{
node.add(hmem());
node.add(pair());
if(tokens[ptr].type==tok_comma)
match(tok_comma);
else if(tokens[ptr].type==tok_id || tokens[ptr].type==tok_str)// first set of hashmember
@ -316,9 +316,9 @@ nasal_ast nasal_parse::hash()
match(tok_rbrace,"expected \'}\' when generating hash");
return node;
}
nasal_ast nasal_parse::hmem()
nasal_ast nasal_parse::pair()
{
nasal_ast node(tokens[ptr].line,ast_hashmember);
nasal_ast node(tokens[ptr].line,ast_pair);
if(tokens[ptr].type==tok_id)
{
node.add(id());
@ -715,7 +715,7 @@ nasal_ast nasal_parse::callf()
match(tok_lcurve);
while(tokens[ptr].type!=tok_rcurve)
{
node.add(special_call?hmem():calc());
node.add(special_call?pair():calc());
if(tokens[ptr].type==tok_comma)
match(tok_comma);
else if(tokens[ptr].type==tok_eof)
@ -935,7 +935,7 @@ nasal_ast nasal_parse::iter_gen()
if(tokens[ptr].type==tok_var)
{
match(tok_var);
node.set_type(ast_new_iter);
node.set_type(ast_iter);
node.add(id());
match(tok_id);
}