This commit is contained in:
Valk Richard Li 2021-02-13 14:37:21 +08:00
parent 996ac59c79
commit e2ee9cff4c
3 changed files with 33 additions and 13 deletions

View File

@ -776,6 +776,10 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
calculation_gen(ast.get_children()[0]); calculation_gen(ast.get_children()[0]);
gen(op_unot,0); gen(op_unot,0);
break; break;
case ast_definition:
single_def(ast);
call_id(ast.get_children()[0]);
break;
} }
return; return;
} }

View File

@ -104,7 +104,7 @@ private:
std::vector<char> res; std::vector<char> res;
std::vector<token> token_list; std::vector<token> token_list;
int get_token_type(std::string); int get_token_type(std::string);
void die(std::string,int,int); void die(std::string,std::string,int,int);
std::string identifier_gen(); std::string identifier_gen();
std::string number_gen(); std::string number_gen();
std::string string_gen(); std::string string_gen();
@ -148,10 +148,13 @@ int nasal_lexer::get_token_type(std::string tk_str)
return tok_null; return tok_null;
} }
void nasal_lexer::die(std::string error_info,int line=-1,int column=-1) void nasal_lexer::die(std::string code,std::string error_info,int line=-1,int column=-1)
{ {
++error; ++error;
std::cout<<">> [lexer] line "<<line<<" column "<<column<<": "<<error_info<<"\n"; std::cout<<">> [lexer] line "<<line<<" column "<<column<<": \n"<<code<<"\n";
for(int i=0;i<column-1;++i)
std::cout<<(code[i]=='\t'?'\t':' ');
std::cout<<"^ "<<error_info<<'\n';
return; return;
} }
@ -179,7 +182,7 @@ std::string nasal_lexer::number_gen()
line_code+=token_str; line_code+=token_str;
if(token_str=="0x") if(token_str=="0x")
{ {
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
return token_str; return token_str;
@ -194,7 +197,7 @@ std::string nasal_lexer::number_gen()
line_code+=token_str; line_code+=token_str;
if(token_str=="0o") if(token_str=="0o")
{ {
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
return token_str; return token_str;
@ -210,7 +213,7 @@ std::string nasal_lexer::number_gen()
if(ptr>=res_size) if(ptr>=res_size)
{ {
line_code+=token_str; line_code+=token_str;
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
while(ptr<res_size && IS_DIGIT(res[ptr])) while(ptr<res_size && IS_DIGIT(res[ptr]))
@ -219,7 +222,7 @@ std::string nasal_lexer::number_gen()
if(token_str.back()=='.') if(token_str.back()=='.')
{ {
line_code+=token_str; line_code+=token_str;
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
} }
@ -230,7 +233,7 @@ std::string nasal_lexer::number_gen()
if(ptr>=res_size) if(ptr>=res_size)
{ {
line_code+=token_str; line_code+=token_str;
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
if(ptr<res_size && (res[ptr]=='-' || res[ptr]=='+')) if(ptr<res_size && (res[ptr]=='-' || res[ptr]=='+'))
@ -238,7 +241,7 @@ std::string nasal_lexer::number_gen()
if(ptr>=res_size) if(ptr>=res_size)
{ {
line_code+=token_str; line_code+=token_str;
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
while(ptr<res_size && IS_DIGIT(res[ptr])) while(ptr<res_size && IS_DIGIT(res[ptr]))
@ -247,7 +250,7 @@ std::string nasal_lexer::number_gen()
if(token_str.back()=='e' || token_str.back()=='E' || token_str.back()=='-' || token_str.back()=='+') if(token_str.back()=='e' || token_str.back()=='E' || token_str.back()=='-' || token_str.back()=='+')
{ {
line_code+=token_str; line_code+=token_str;
die("["+line_code+"_] incorrect number.",line,line_code.length()); die(line_code,"incorrect number.",line,line_code.length());
return "0"; return "0";
} }
} }
@ -293,7 +296,7 @@ std::string nasal_lexer::string_gen()
} }
// check if this string ends with a " or ' // check if this string ends with a " or '
if(ptr++>=res_size) if(ptr++>=res_size)
die("["+line_code+"_] get EOF when generating string.",line,line_code.length()); die(line_code,"get EOF when generating string.",line,line_code.length());
return token_str; return token_str;
} }
@ -345,7 +348,7 @@ void nasal_lexer::scanner()
line_code+=res[ptr]; line_code+=res[ptr];
token new_token(line,get_token_type(token_str),token_str); token new_token(line,get_token_type(token_str),token_str);
if(!new_token.type) if(!new_token.type)
die("["+line_code+"_] incorrect operator.",line,line_code.length()); die(line_code,"incorrect operator.",line,line_code.length());
token_list.push_back(new_token); token_list.push_back(new_token);
++ptr; ++ptr;
} }
@ -380,7 +383,7 @@ void nasal_lexer::scanner()
else else
{ {
line_code+=res[ptr++]; line_code+=res[ptr++];
die("["+line_code+"_] unknown character.",line,line_code.length()); die(line_code,"unknown character.",line,line_code.length());
} }
} }
token tk(line,tok_eof,""); token tk(line,tok_eof,"");

View File

@ -150,6 +150,8 @@ void nasal_parse::die(int line,std::string info)
{ {
++error; ++error;
std::cout<<">> [parse] line "<<line<<": "<<info<<".\n"; std::cout<<">> [parse] line "<<line<<": "<<info<<".\n";
if(tok_list[ptr].type==tok_eof)
return;
++ptr; ++ptr;
while(tok_list[ptr].type!=tok_eof)// panic while(tok_list[ptr].type!=tok_eof)// panic
{ {
@ -184,6 +186,8 @@ void nasal_parse::match(int type)
else else
die(error_line,"expect \'"+s+"\'"); die(error_line,"expect \'"+s+"\'");
} }
if(tok_list[ptr].type==tok_eof)
return;
++ptr; ++ptr;
return; return;
} }
@ -679,6 +683,15 @@ nasal_ast nasal_parse::scalar()
node=calc(); node=calc();
match(tok_rcurve); match(tok_rcurve);
} }
else if(tok_list[ptr].type==tok_var)
{
match(tok_var);
node.set_type(ast_definition);
node.add_child(id_gen());
match(tok_id);
match(tok_eq);
node.add_child(calc());
}
else else
{ {
die(error_line,"expected scalar"); die(error_line,"expected scalar");