2024-12-03 08:53:09 +08:00
|
|
|
from lex_c import lex_token
|
|
|
|
import lex_c
|
|
|
|
from parser_c import node
|
|
|
|
from parser_c import node_file
|
|
|
|
from parser_c import node_variable_def
|
|
|
|
from parser_c import node_struct_decl
|
|
|
|
from parser_c import node_struct_def
|
|
|
|
from parser_c import node_union_decl
|
|
|
|
from parser_c import node_union_def
|
|
|
|
from parser_c import node_enum_decl
|
|
|
|
from parser_c import node_enum_def
|
|
|
|
from parser_c import node_func_decl
|
|
|
|
from parser_c import node_typedef
|
|
|
|
from parser_c import node_func_def
|
|
|
|
|
|
|
|
from parser_c import find_sentence
|
|
|
|
from parser_c import dist_node_type
|
|
|
|
from parser_c import find_close
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dist_node_type_struct(token_list:list[lex_token]):
|
|
|
|
if(token_list[0].token==lex_c.TOKEN_STRUCT):
|
|
|
|
if(token_list[1].token==lex_c.TOKEN_SYMBOL):
|
|
|
|
if(len(token_list)==2):
|
|
|
|
return node_struct_decl(name=token_list[1].buff.decode("utf-8"),token_list=token_list)
|
|
|
|
elif(token_list[2].token==lex_c.TOKEN("{")):
|
|
|
|
if not token_list[-1].token==lex_c.TOKEN("}"):
|
|
|
|
raise Exception("没有出现预期的符号 '}'")
|
|
|
|
v_list:list[node_variable_def]=[]
|
|
|
|
token_list_local=token_list[3:-1]
|
|
|
|
while len(token_list_local)>0:
|
|
|
|
sentence=find_sentence(token_list_local)
|
|
|
|
v_list.append(dist_node_type(token_list=sentence))
|
|
|
|
token_list_local=token_list_local[len(sentence):]
|
|
|
|
return node_struct_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,body=v_list)
|
|
|
|
raise Exception(f"语法错误 {token_list[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dist_node_type_union(token_list:list[lex_token]):
|
|
|
|
if(token_list[0].token==lex_c.TOKEN_UNION):
|
|
|
|
if(token_list[1].token==lex_c.TOKEN_SYMBOL):
|
|
|
|
if(len(token_list)==2):
|
|
|
|
return node_union_decl(name=token_list[1].buff.decode("utf-8"),token_list=token_list)
|
|
|
|
elif(token_list[2].token==lex_c.TOKEN("{")):
|
|
|
|
if not token_list[-1].token==lex_c.TOKEN("}"):
|
|
|
|
raise Exception("没有出现预期的符号 '}'")
|
|
|
|
v_list:list[node_variable_def]=[]
|
|
|
|
token_list_local=token_list[3:-1]
|
|
|
|
while len(token_list_local)>0:
|
|
|
|
sentence=find_sentence(token_list_local)
|
|
|
|
v_list.append(dist_node_type(token_list=sentence))
|
|
|
|
token_list_local=token_list_local[len(sentence):]
|
|
|
|
return node_union_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,body=v_list)
|
|
|
|
raise Exception(f"语法错误 {token_list[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dist_node_type_enum(token_list:list[lex_token]):
|
|
|
|
if(token_list[0].token==lex_c.TOKEN_ENUM):
|
|
|
|
if(token_list[1].token==lex_c.TOKEN_SYMBOL):
|
|
|
|
if(len(token_list)==2):
|
|
|
|
return node_enum_decl(name=token_list[1].buff.decode("utf-8"),token_list=token_list)
|
|
|
|
elif(token_list[2].token==lex_c.TOKEN("{")):
|
|
|
|
if not token_list[-1].token==lex_c.TOKEN("}"):
|
|
|
|
raise Exception("没有出现预期的符号 '}'")
|
|
|
|
token_list_local=token_list[3:-1]
|
|
|
|
index=0
|
|
|
|
v_list:list[dict]=[]
|
|
|
|
while len(token_list_local)>0:
|
|
|
|
if(token_list_local[0].token==lex_c.TOKEN_SYMBOL):
|
|
|
|
key=token_list_local[0].buff.decode("utf-8")
|
|
|
|
if(token_list_local[1].token==lex_c.TOKEN("=") and token_list_local[2].token==lex_c.TOKEN_NUM):
|
|
|
|
index=int(token_list_local[2].buff.decode("utf-8"))
|
|
|
|
token_list_local=token_list_local[3:]
|
|
|
|
else:
|
|
|
|
index+=1
|
|
|
|
token_list_local=token_list_local[1:]
|
|
|
|
v_list.append({key:index})
|
|
|
|
if(len(token_list_local)>0):
|
|
|
|
if(token_list_local[0].token!=lex_c.TOKEN(",")):
|
|
|
|
raise Exception(f"枚举类型应该使用 ',' 分隔符")
|
|
|
|
token_list_local=token_list_local[1:]
|
|
|
|
return node_enum_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,body=v_list)
|
|
|
|
raise Exception(f"语法错误 {token_list[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
def dist_node_type_typedef(token_list:list[lex_token]):
|
|
|
|
if(token_list[0].token==lex_c.TOKEN_TYPEDEF):
|
|
|
|
attr=[]
|
|
|
|
token_list_local=token_list
|
|
|
|
if(token_list[-1].token!=lex_c.TOKEN_SYMBOL):
|
|
|
|
raise Exception(f"没有定义新类型 {token_list[-1]}")
|
|
|
|
name=token_list[-1].buff.decode("utf-8")
|
|
|
|
token_list=token_list[1:]
|
|
|
|
while token_list[0].token in [lex_c.TOKEN_UNSIGNED,lex_c.TOKEN_CONST]:
|
|
|
|
attr.append(token_list[0].name)
|
|
|
|
token_list=token_list[1:]
|
|
|
|
if(token_list[0].token==lex_c.TOKEN_STRUCT or token_list[0].token==lex_c.TOKEN_UNION):
|
|
|
|
attr.append(token_list[0].name)
|
|
|
|
if(token_list[1].token==lex_c.TOKEN_SYMBOL):
|
|
|
|
node_r=None
|
|
|
|
attr.append(token_list[1].buff.decode("utf-8"))
|
|
|
|
if(token_list[2].token==lex_c.TOKEN("{")):
|
|
|
|
node_r=dist_node_type(token_list=token_list[1:-1])
|
|
|
|
elif(token_list[2].token==lex_c.TOKEN("*")):
|
|
|
|
attr.append(token_list[2].name)
|
|
|
|
return node_typedef(name=name,token_list=token_list_local,attr=attr,body=node_r)
|
|
|
|
if(token_list[0].token==lex_c.TOKEN_SYMBOL):
|
|
|
|
# 使用typedef 定义过的自定义类型
|
|
|
|
attr.append(token_list[0].buff.decode("utf-8"))
|
|
|
|
token_list=token_list[1:]
|
|
|
|
else:
|
|
|
|
# c语言预设类型
|
2024-12-03 17:23:22 +08:00
|
|
|
while(token_list[0].token in
|
2024-12-03 08:53:09 +08:00
|
|
|
[lex_c.TOKEN_INT,lex_c.TOKEN_CHAR,lex_c.TOKEN_SHORT,lex_c.TOKEN_LONG,lex_c.TOKEN_FLOAT,
|
|
|
|
lex_c.TOKEN_DOUBLE,lex_c.TOKEN_VOID,lex_c.TOKEN("*")]):
|
|
|
|
attr.append(token_list[0].name)
|
|
|
|
token_list=token_list[1:]
|
|
|
|
if(len(token_list)>1):
|
|
|
|
raise Exception(f"意外的token {token_list[0]}")
|
|
|
|
return node_typedef(name=name,token_list=token_list_local,attr=attr,body=None)
|
|
|
|
raise Exception(f"语法错误 {token_list[0]}")
|
|
|
|
|
|
|
|
|