添加int节点

This commit is contained in:
ranchuan
2024-12-05 19:02:48 +08:00
parent 82288471d7
commit 9712264213

View File

@@ -117,6 +117,12 @@ class node_symbol(node):
class node_string(node): class node_string(node):
type:str="string" 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]): 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的处理 # 第一个token是symbol的处理
def dist_node_type_symbol(token_list:list[lex_token]): 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("(")): if(token_list[1].token == lex_c.TOKEN("(")):
child=find_child(token_list=token_list[2:-1]) child=find_child(token_list=token_list[2:-1])
return node_call("call",token_list=token_list,child=child) 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) return dist_node_type_typedef(token_list=token_list)
if(token_list[0].token==lex_c.TOKEN_SWITCH): if(token_list[0].token==lex_c.TOKEN_SWITCH):
child=find_child(token_list) 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): 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): 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): 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): 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): 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(")")): if(token_list[-1].token==lex_c.TOKEN(")")):
# 函数声明 # 函数声明