mirror of
https://github.com/ValKmjolnir/Nasal-Interpreter.git
synced 2026-07-26 21:08:45 +08:00
update
This commit is contained in:
@@ -23,7 +23,7 @@ function::=
|
|||||||
func argument_list expressions
|
func argument_list expressions
|
||||||
;
|
;
|
||||||
argument_list::=
|
argument_list::=
|
||||||
'(' [{id ','} ([id '...']|{id '=' calculation ','})] ')'
|
'(' [{id ','} ([id '...']|{id '=' scalar ','})] ')'
|
||||||
;
|
;
|
||||||
expr::=
|
expr::=
|
||||||
definition
|
definition
|
||||||
|
|||||||
+13
-10
@@ -14,6 +14,7 @@ enum token_type
|
|||||||
tok_left_brace,tok_right_brace,
|
tok_left_brace,tok_right_brace,
|
||||||
tok_semi,tok_and,tok_or,tok_comma,tok_dot,tok_ellipsis,tok_quesmark,
|
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_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_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
|
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_null=0,ast_root,ast_block,
|
||||||
ast_nil,ast_number,ast_string,ast_identifier,ast_function,ast_hash,ast_vector,
|
ast_nil,ast_number,ast_string,ast_identifier,ast_function,ast_hash,ast_vector,
|
||||||
ast_hashmember,
|
ast_hashmember,
|
||||||
ast_args,
|
ast_args,ast_default_arg,ast_dynamic_id,
|
||||||
ast_and,ast_or,
|
ast_and,ast_or,
|
||||||
ast_equal,ast_add_equal,ast_sub_equal,ast_mult_equal,ast_div_equal,ast_link_equal,
|
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_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_comma,
|
||||||
lack_colon,
|
lack_colon,
|
||||||
lack_scalar,
|
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 info=">> [parse] error: line ";
|
||||||
std::string detail;
|
std::string detail;
|
||||||
@@ -58,17 +60,18 @@ void error_info(int line,int error_type)
|
|||||||
switch(error_type)
|
switch(error_type)
|
||||||
{
|
{
|
||||||
case unknown: detail="unknown error."; break;
|
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_id: detail="lack identifier."; break;
|
||||||
case lack_left_curve: detail="lack \'(\'."; break;
|
case lack_left_curve: detail="lack \'(\'."; break;
|
||||||
case lack_right_curve: detail="lack \')\'."; break;
|
case lack_right_curve: detail="lack \')\'."; break;
|
||||||
case lack_left_bracket: detail="lack left bracket."; break;
|
case lack_left_bracket: detail="lack \'[\'."; break;
|
||||||
case lack_left_brace: detail="lack left brace."; break;
|
case lack_left_brace: detail="lack \'{\'."; break;
|
||||||
case lack_right_brace: detail="lack right brace."; break;
|
case lack_right_brace: detail="lack \'}\'."; break;
|
||||||
case lack_semi: detail="lack \';\' here."; break;
|
case lack_semi: detail="lack \';\'."; break;
|
||||||
case lack_comma: detail="lack comma."; break;
|
case lack_comma: detail="lack \',\'."; break;
|
||||||
case lack_colon: detail="lack colon."; break;
|
case lack_colon: detail="lack \':\'."; break;
|
||||||
case lack_scalar: detail="lack scalar"; break;
|
case lack_scalar: detail="lack scalar."; break;
|
||||||
|
case lack_identifier: detail="lack identifier."; break;
|
||||||
}
|
}
|
||||||
std::cout<<detail<<std::endl;
|
std::cout<<detail<<std::endl;
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
#define IS_NOTE_HEAD(c) (c=='#')
|
#define IS_NOTE_HEAD(c) (c=='#')
|
||||||
|
|
||||||
#ifndef TOKEN_TABLE_SIZE
|
#ifndef TOKEN_TABLE_SIZE
|
||||||
#define TOKEN_TABLE_SIZE 44
|
#define TOKEN_TABLE_SIZE 45
|
||||||
struct token_table
|
struct token_table
|
||||||
{
|
{
|
||||||
std::string str;
|
std::string str;
|
||||||
@@ -55,6 +55,7 @@ struct token_table
|
|||||||
{"/" ,tok_div },
|
{"/" ,tok_div },
|
||||||
{"~" ,tok_link },
|
{"~" ,tok_link },
|
||||||
{"!" ,tok_not },
|
{"!" ,tok_not },
|
||||||
|
{"=" ,tok_equal },
|
||||||
{"+=" ,tok_add_equal },
|
{"+=" ,tok_add_equal },
|
||||||
{"-=" ,tok_sub_equal },
|
{"-=" ,tok_sub_equal },
|
||||||
{"*=" ,tok_mult_equal },
|
{"*=" ,tok_mult_equal },
|
||||||
|
|||||||
+45
-10
@@ -227,7 +227,6 @@ nasal_ast nasal_parse::func_gen()
|
|||||||
if(tok_list[ptr].type==tok_left_curve)
|
if(tok_list[ptr].type==tok_left_curve)
|
||||||
{
|
{
|
||||||
node.add_child(args_list_gen());
|
node.add_child(args_list_gen());
|
||||||
++error;
|
|
||||||
++ptr;
|
++ptr;
|
||||||
}
|
}
|
||||||
if(ptr>=tok_list_size)
|
if(ptr>=tok_list_size)
|
||||||
@@ -241,13 +240,52 @@ nasal_ast nasal_parse::func_gen()
|
|||||||
}
|
}
|
||||||
nasal_ast nasal_parse::args_list_gen()
|
nasal_ast nasal_parse::args_list_gen()
|
||||||
{
|
{
|
||||||
|
int last_id_line;
|
||||||
nasal_ast node;
|
nasal_ast node;
|
||||||
node.set_line(tok_list[ptr].line);
|
node.set_line(tok_list[ptr].line);
|
||||||
node.set_type(ast_args);
|
node.set_type(ast_args);
|
||||||
++ptr;
|
|
||||||
while(ptr<tok_list_size && tok_list[ptr].type!=tok_right_curve)
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -276,7 +314,7 @@ nasal_ast nasal_parse::expr()
|
|||||||
case tok_break: node=break_expr(); break;
|
case tok_break: node=break_expr(); break;
|
||||||
case tok_return: node=return_expr(); break;
|
case tok_return: node=return_expr(); break;
|
||||||
case tok_semi: --ptr; 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;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -427,8 +465,7 @@ nasal_ast nasal_parse::additive_expr()
|
|||||||
nasal_ast nasal_parse::multive_expr()
|
nasal_ast nasal_parse::multive_expr()
|
||||||
{
|
{
|
||||||
nasal_ast node;
|
nasal_ast node;
|
||||||
if(tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)
|
node=(tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar();
|
||||||
node=unary();
|
|
||||||
++ptr;
|
++ptr;
|
||||||
while(ptr<tok_list_size && (tok_list[ptr].type==tok_mult || tok_list[ptr].type==tok_div))
|
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);
|
tmp.add_child(node);
|
||||||
++ptr;
|
++ptr;
|
||||||
if(ptr<tok_list_size && (tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not))
|
if(ptr<tok_list_size)
|
||||||
tmp.add_child(unary());
|
tmp.add_child((tok_list[ptr].type==tok_sub || tok_list[ptr].type==tok_not)?unary():scalar());
|
||||||
else if(ptr<tok_list_size && tok_list[ptr].type!=tok_sub && tok_list[ptr].type!=tok_not)
|
|
||||||
tmp.add_child(scalar());
|
|
||||||
node=tmp;
|
node=tmp;
|
||||||
++ptr;
|
++ptr;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user