This commit is contained in:
Valk Richard Li 2020-06-17 01:18:04 -07:00 committed by GitHub
parent f4715f47c3
commit d9049c5ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 155 additions and 87 deletions

View File

@ -39,14 +39,10 @@ expressions::=
'{' {expr} '}'
;
calculation::=
|scalar
|trinocular
calculation '?' calculation ':' calculation
|or_expr
|calculation ('=' | '+=' | '-=' | '*=' | '/=' | '~=') calculation
;
trinocular::=
calculation '?' calculation ':' calculation
;
or_expr::=
and_expr or and_expr
;
@ -66,30 +62,27 @@ unary::=
('-'|'!') scalar
;
scalar::=
function_call
|identifier_call
|vector_call
|hash_call
function {call_scalar}
|identifier {call_scalar}
|vector {call_scalar}
|hash {call_scalar}
|number
|string
|'(' calculation ')'
;
function_call::=
function {call_scalar}
;
identifier_call::=
id {call_scalar}
;
vector_call::=
vector {call_scalar}
;
hash_call::=
hash {call_scalar}
|'(' calculation ')' {call_scalar}
;
call_scalar::=
call_hash
|call_vector
|call_func
;
call_hash::=
'.' id
|'[' {(calculation | subvec) ','} ']'
|'(' {calculation ','} ')'
;
call_vector::=
'[' {(calculation | subvec) ','} ']'
;
call_func::=
'(' {calculation ','} ')'
|'(' {hashmember ','} ')'
;
subvec::=

View File

@ -23,13 +23,14 @@ enum ast_node
{
ast_null=0,ast_root,ast_block,
ast_nil,ast_number,ast_string,ast_identifier,ast_function,ast_hash,ast_vector,
ast_hashmember,
ast_hashmember,ast_call,ast_call_hash,ast_call_vec,ast_call_func,
ast_args,ast_default_arg,ast_dynamic_id,
ast_and,ast_or,
ast_equal,ast_add_equal,ast_sub_equal,ast_mult_equal,ast_div_equal,ast_link_equal,
ast_cmp_equal,ast_cmp_not_equal,ast_less_than,ast_less_equal,ast_greater_than,ast_greater_equal,
ast_add,ast_sub,ast_mult,ast_div,ast_link,
ast_unary_sub,ast_unary_not,
ast_trinocular,
ast_for,ast_forindex,ast_foreach,ast_while,
ast_definition,ast_multi_assign,ast_calculation,
ast_continue,ast_break,ast_return,
@ -39,7 +40,6 @@ enum parse_error
{
unknown,
error_token,
lack_id,
lack_left_curve,
lack_right_curve,
lack_left_bracket,
@ -50,6 +50,7 @@ enum parse_error
lack_colon,
lack_scalar,
lack_identifier,
lack_calculation,
};
void error_info(int line,int error_type,std::string error_str="")
@ -59,19 +60,19 @@ void error_info(int line,int error_type,std::string error_str="")
std::cout<<info<<line<<": ";
switch(error_type)
{
case unknown: detail="unknown error."; break;
case error_token: detail="error token \'"+error_str+"\'"; break;
case lack_id: detail="lack identifier."; break;
case lack_left_curve: detail="lack \'(\'."; break;
case lack_right_curve: detail="lack \')\'."; break;
case lack_left_bracket: detail="lack \'[\'."; break;
case lack_left_brace: detail="lack \'{\'."; break;
case lack_right_brace: detail="lack \'}\'."; break;
case lack_semi: detail="lack \';\'."; break;
case lack_comma: detail="lack \',\'."; break;
case lack_colon: detail="lack \':\'."; break;
case lack_scalar: detail="lack scalar."; break;
case lack_identifier: detail="lack identifier."; break;
case unknown: detail="unknown error."; break;
case error_token: detail="error token \'"+error_str+"\'"; break;
case lack_left_curve: detail="expected \'(\'."; break;
case lack_right_curve: detail="expected \')\'."; break;
case lack_left_bracket: detail="expected \'[\'."; break;
case lack_left_brace: detail="expected \'{\'."; break;
case lack_right_brace: detail="expected \'}\'."; break;
case lack_semi: detail="expected \';\'."; break;
case lack_comma: detail="expected \',\'."; break;
case lack_colon: detail="expected \':\'."; break;
case lack_scalar: detail="expected scalar here."; break;
case lack_identifier: detail="expected identifier here."; break;
case lack_calculation: detail="expected arithmetic-expression here."; break;
}
std::cout<<detail<<std::endl;
return;

View File

@ -23,7 +23,6 @@ private:
nasal_ast expr();
nasal_ast exprs_gen();
nasal_ast calculation();
nasal_ast trinocular();
nasal_ast or_expr();
nasal_ast and_expr();
nasal_ast cmp_expr();
@ -31,11 +30,10 @@ private:
nasal_ast multive_expr();
nasal_ast unary();
nasal_ast scalar();
nasal_ast func_call();
nasal_ast id_call();
nasal_ast vector_call();
nasal_ast hash_call();
nasal_ast call_scalar();
nasal_ast call_hash();
nasal_ast call_vector();
nasal_ast call_func();
nasal_ast subvec();
nasal_ast definition();
nasal_ast multi_id();
@ -190,7 +188,7 @@ nasal_ast nasal_parse::hash_member_gen()
nasal_ast node;
if(tok_list[ptr].type!=tok_identifier)
{
error_info(tok_list[ptr].line,lack_id);
error_info(tok_list[ptr].line,lack_identifier);
return node;
}
node.set_line(tok_list[ptr].line);
@ -354,13 +352,59 @@ nasal_ast nasal_parse::exprs_gen()
nasal_ast nasal_parse::calculation()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
int tok_type=tok_list[ptr].type;
return node;
}
nasal_ast nasal_parse::trinocular()
{
nasal_ast node;
node=or_expr();
++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_quesmark)
{
nasal_ast tmp;
tmp.set_line(tok_list[ptr].line);
tmp.set_type(ast_trinocular);
tmp.add_child(node);
++ptr;
if(ptr<tok_list_size) tmp.add_child(calculation());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_colon)
{
++error;
error_info(tmp.get_line(),lack_colon);
return node;
}
++ptr;
if(ptr<tok_list_size) tmp.add_child(calculation());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
}
else if(
ptr<tok_list_size &&
(
tok_list[ptr].type==tok_equal ||
tok_list[ptr].type==tok_add_equal ||
tok_list[ptr].type==tok_sub_equal ||
tok_list[ptr].type==tok_mult_equal ||
tok_list[ptr].type==tok_div_equal ||
tok_list[ptr].type==tok_link_equal
)
)
{
nasal_ast tmp;
tmp.set_line(tok_list[ptr].line);
switch(tok_list[ptr].type)
{
case tok_equal: tmp.set_type(ast_equal); break;
case tok_add_equal: tmp.set_type(ast_add_equal); break;
case tok_sub_equal: tmp.set_type(ast_sub_equal); break;
case tok_mult_equal: tmp.set_type(ast_mult_equal); break;
case tok_div_equal: tmp.set_type(ast_div_equal); break;
case tok_link_equal: tmp.set_type(ast_link_equal); break;
}
tmp.add_child(node);
++ptr;
if(ptr<tok_list_size) tmp.add_child(calculation());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
}
else --ptr;
return node;
}
nasal_ast nasal_parse::or_expr()
@ -375,7 +419,8 @@ nasal_ast nasal_parse::or_expr()
tmp.set_type(ast_or);
tmp.add_child(node);
++ptr;
tmp.add_child(and_expr());
if(ptr<tok_list_size) tmp.add_child(and_expr());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
++ptr;
}
@ -394,7 +439,8 @@ nasal_ast nasal_parse::and_expr()
tmp.set_type(ast_and);
tmp.add_child(node);
++ptr;
tmp.add_child(cmp_expr());
if(ptr<tok_list_size) tmp.add_child(cmp_expr());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
++ptr;
}
@ -431,7 +477,8 @@ nasal_ast nasal_parse::cmp_expr()
}
tmp.add_child(node);
++ptr;
tmp.add_child(additive_expr());
if(ptr<tok_list_size) tmp.add_child(additive_expr());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
++ptr;
}
@ -455,7 +502,8 @@ nasal_ast nasal_parse::additive_expr()
}
tmp.add_child(node);
++ptr;
tmp.add_child(multive_expr());
if(ptr<tok_list_size) tmp.add_child(multive_expr());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
++ptr;
}
@ -478,8 +526,8 @@ nasal_ast nasal_parse::multive_expr()
}
tmp.add_child(node);
++ptr;
if(ptr<tok_list_size)
tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar());
if(ptr<tok_list_size) tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar());
else{ ++error; error_info(tmp.get_line(),lack_calculation);}
node=tmp;
++ptr;
}
@ -495,7 +543,9 @@ nasal_ast nasal_parse::unary()
case tok_sub:node.set_type(ast_unary_sub);break;
case tok_not:node.set_type(ast_unary_not);break;
}
node.add_child(scalar());
++ptr;
if(ptr<tok_list_size) node.add_child(scalar());
else{ ++error; error_info(node.get_line(),lack_calculation);}
return node;
}
nasal_ast nasal_parse::scalar()
@ -506,18 +556,20 @@ nasal_ast nasal_parse::scalar()
node=number_gen();
else if(tok_list[ptr].type==tok_string)
node=string_gen();
else if(
tok_list[ptr].type==tok_func ||
tok_list[ptr].type==tok_identifier ||
tok_list[ptr].type==tok_left_bracket ||
tok_list[ptr].type==tok_left_brace
)
node=call_scalar();
else if(tok_list[ptr].type==tok_identifier)
node=id_gen();
else if(tok_list[ptr].type==tok_func)
node=func_gen();
else if(tok_list[ptr].type==tok_left_bracket)
node=vector_gen();
else if(tok_list[ptr].type==tok_left_brace)
node=hash_gen();
else if(tok_list[ptr].type==tok_left_curve)
{
int curve_line=tok_list[ptr].line;
++ptr;
node=calculation();
if(ptr<tok_list_size) node=calculation();
else{ ++error; error_info(node.get_line(),lack_calculation);}
++ptr;
if(ptr>=tok_list_size || tok_list[ptr].type!=tok_right_curve)
{
@ -525,31 +577,53 @@ nasal_ast nasal_parse::scalar()
error_info(curve_line,lack_right_curve);
}
}
return node;
}
nasal_ast nasal_parse::func_call()
{
nasal_ast node;
return node;
}
nasal_ast nasal_parse::id_call()
{
nasal_ast node;
return node;
}
nasal_ast nasal_parse::vector_call()
{
nasal_ast node;
return node;
}
nasal_ast nasal_parse::hash_call()
{
nasal_ast node;
++ptr;
if(ptr<tok_list_size && (tok_list[ptr].type==tok_left_curve || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_dot))
{
nasal_ast tmp=node;
node.clear();
node.set_line(tok_list[ptr].line);
node.set_type(ast_call);
node.add_child(tmp);
}
while(ptr<tok_list_size && (tok_list[ptr].type==tok_left_curve || tok_list[ptr].type==tok_left_bracket || tok_list[ptr].type==tok_dot))
node.add_child(call_scalar());
--ptr;
return node;
}
nasal_ast nasal_parse::call_scalar()
{
nasal_ast node;
switch(tok_list[ptr].type)
{
case tok_left_curve: node=call_func(); break;
case tok_left_bracket: node=call_vector(); break;
case tok_dot: node=call_hash(); break;
}
return node;
}
nasal_ast nasal_parse::call_hash()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_call_hash);
++ptr;
if(ptr<tok_list_size && tok_list[ptr].type==tok_identifier) node.set_str(tok_list[ptr].str);
else{ ++error; error_info(node.get_line(),lack_identifier);}
return node;
}
nasal_ast nasal_parse::call_vector()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_call_vec);
return node;
}
nasal_ast nasal_parse::call_func()
{
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_call_func);
return node;
}
nasal_ast nasal_parse::subvec()