diff --git a/parser_c.py b/parser_c.py index 46e9435..f5d4dfd 100644 --- a/parser_c.py +++ b/parser_c.py @@ -117,6 +117,12 @@ class node_symbol(node): class node_string(node): type:str="string" +# int节点 +@dataclasses.dataclass +class node_int(node): + type:str="int" + + # 找到闭合的括号 def find_close(token_list:list[lex_token],token:tuple[int,int]): @@ -288,6 +294,8 @@ def dist_node_type_funcdecl(token_list:list[lex_token]): # 第一个token是symbol的处理 def dist_node_type_symbol(token_list:list[lex_token]): # 变量赋值或函数调用 + if(len(token_list)==1): + return node_symbol(name=token_list[0].buff.decode("utf-8"),token_list=token_list) if(token_list[1].token == lex_c.TOKEN("(")): child=find_child(token_list=token_list[2:-1]) return node_call("call",token_list=token_list,child=child) @@ -332,17 +340,26 @@ def dist_node_type(token_list:list[lex_token]): return dist_node_type_typedef(token_list=token_list) if(token_list[0].token==lex_c.TOKEN_SWITCH): child=find_child(token_list) - return node_switch(name="switch",token_list=token_list,child=child) + return node_switch(name="",token_list=token_list,child=child) if(token_list[0].token==lex_c.TOKEN_CASE): - return node_case(name="case",token_list=token_list,child=[]) + name=token_list[1].buff.decode("utf-8") + return node_case(name=name,token_list=token_list,child=[]) if(token_list[0].token==lex_c.TOKEN_DEFAULT): - return node_default(name="default",token_list=token_list,child=[]) + return node_default(name="",token_list=token_list,child=[]) if(token_list[0].token==lex_c.TOKEN_BREAK): - return node_break(name="break",token_list=token_list,child=[]) + return node_break(name="",token_list=token_list,child=[]) if(token_list[0].token==lex_c.TOKEN_RETURN): - return node_return(name="return",token_list=token_list,child=[]) + if(len(token_list)>1): + child=[dist_node_type(token_list[1:])] + else: + child=[] + return node_return(name="",token_list=token_list,child=child) if(token_list[0].token==lex_c.TOKEN_STRING): - return node_string(name="string",token_list=token_list,child=[]) + name=token_list[0].buff.decode("utf-8") + return node_string(name=name,token_list=token_list,child=[]) + if(token_list[0].token==lex_c.TOKEN_NUM): + name=token_list[0].buff.decode("utf-8") + return node_int(name=name,token_list=token_list,child=[]) if(token_list[-1].token==lex_c.TOKEN(")")): # 函数声明