This commit is contained in:
Valk Richard Li
2020-06-15 03:48:28 -07:00
committed by GitHub
parent 7f73a2f5ae
commit f4715f47c3
4 changed files with 61 additions and 22 deletions

View File

@@ -23,7 +23,7 @@ function::=
func argument_list expressions
;
argument_list::=
'(' [{id ','} ([id '...']|{id '=' calculation ','})] ')'
'(' [{id ','} ([id '...']|{id '=' scalar ','})] ')'
;
expr::=
definition

View File

@@ -14,6 +14,7 @@ enum token_type
tok_left_brace,tok_right_brace,
tok_semi,tok_and,tok_or,tok_comma,tok_dot,tok_ellipsis,tok_quesmark,
tok_colon,tok_add,tok_sub,tok_mult,tok_div,tok_link,tok_not,
tok_equal,
tok_add_equal,tok_sub_equal,tok_mult_equal,tok_div_equal,tok_link_equal,
tok_cmp_equal,tok_cmp_not_equal,tok_less_than,tok_greater_than,tok_less_equal,tok_greater_equal
};
@@ -23,7 +24,7 @@ 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_args,
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,
@@ -48,9 +49,10 @@ enum parse_error
lack_comma,
lack_colon,
lack_scalar,
lack_identifier,
};
void error_info(int line,int error_type)
void error_info(int line,int error_type,std::string error_str="")
{
std::string info=">> [parse] error: line ";
std::string detail;
@@ -58,17 +60,18 @@ void error_info(int line,int error_type)
switch(error_type)
{
case unknown: detail="unknown error."; break;
case error_token: detail="this token should not exist here."; 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 left bracket."; break;
case lack_left_brace: detail="lack left brace."; break;
case lack_right_brace: detail="lack right brace."; break;
case lack_semi: detail="lack \';\' here."; break;
case lack_comma: detail="lack comma."; break;
case lack_colon: detail="lack colon."; break;
case lack_scalar: detail="lack scalar"; 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;
}
std::cout<<detail<<std::endl;
return;

View File

@@ -15,7 +15,7 @@
#define IS_NOTE_HEAD(c) (c=='#')
#ifndef TOKEN_TABLE_SIZE
#define TOKEN_TABLE_SIZE 44
#define TOKEN_TABLE_SIZE 45
struct token_table
{
std::string str;
@@ -55,6 +55,7 @@ struct token_table
{"/" ,tok_div },
{"~" ,tok_link },
{"!" ,tok_not },
{"=" ,tok_equal },
{"+=" ,tok_add_equal },
{"-=" ,tok_sub_equal },
{"*=" ,tok_mult_equal },

View File

@@ -227,7 +227,6 @@ nasal_ast nasal_parse::func_gen()
if(tok_list[ptr].type==tok_left_curve)
{
node.add_child(args_list_gen());
++error;
++ptr;
}
if(ptr>=tok_list_size)
@@ -241,13 +240,52 @@ nasal_ast nasal_parse::func_gen()
}
nasal_ast nasal_parse::args_list_gen()
{
int last_id_line;
nasal_ast node;
node.set_line(tok_list[ptr].line);
node.set_type(ast_args);
++ptr;
while(ptr<tok_list_size && tok_list[ptr].type!=tok_right_curve)
{
;
nasal_ast tmp;
++ptr;
if(tok_list[ptr].type!=tok_identifier)
{
++error;
error_info(tok_list[ptr].line,lack_identifier);
return node;
}
tmp=id_gen();
last_id_line=tmp.get_line();
++ptr;
if(ptr<tok_list_size && (tok_list[ptr].type==tok_equal || tok_list[ptr].type==tok_ellipsis))
{
nasal_ast special_arg;
special_arg.set_line(tok_list[ptr].line);
if(tok_list[ptr].type==tok_equal)
{
special_arg.add_child(tmp);
++ptr;
special_arg.add_child(scalar());
}
else
{
special_arg=tmp;
special_arg.set_type(ast_dynamic_id);
}
node.add_child(special_arg);
}
else
{
--ptr;
node.add_child(tmp);
}
++ptr;
if(ptr>=tok_list_size || (tok_list[ptr].type!=tok_comma && tok_list[ptr].type!=tok_right_curve))
{
++error;
error_info(last_id_line,lack_comma);
--ptr;
}
}
return node;
}
@@ -276,7 +314,7 @@ nasal_ast nasal_parse::expr()
case tok_break: node=break_expr(); break;
case tok_return: node=return_expr(); break;
case tok_semi: --ptr; break;
default: error_info(tok_list[ptr].line,error_token);++error;break;
default: error_info(tok_list[ptr].line,error_token,tok_list[ptr].str);++error;break;
}
return node;
}
@@ -427,8 +465,7 @@ nasal_ast nasal_parse::additive_expr()
nasal_ast nasal_parse::multive_expr()
{
nasal_ast node;
if(tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)
node=unary();
node=(tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar();
++ptr;
while(ptr<tok_list_size && (tok_list[ptr].type==tok_mult || tok_list[ptr].type==tok_div))
{
@@ -441,10 +478,8 @@ nasal_ast nasal_parse::multive_expr()
}
tmp.add_child(node);
++ptr;
if(ptr<tok_list_size && (tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not))
tmp.add_child(unary());
else if(ptr<tok_list_size && tok_list[ptr].type!=tok_sub && tok_list[ptr].type!=tok_not)
tmp.add_child(scalar());
if(ptr<tok_list_size)
tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar());
node=tmp;
++ptr;
}