From eaf0de74ccbbbee1a02b2c79c4ff5043ff1686e3 Mon Sep 17 00:00:00 2001 From: ranchuan Date: Tue, 3 Dec 2024 17:23:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AD=98=E5=82=A8=E5=92=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 发现要处理结构体相关数据的存储, 还是要存储原始byte数据要好处理一些 是否需要把所有变量的存储都改为存储原始byte数据 浮点数好像不好处理 --- .gitignore | 4 +- .vscode/settings.json | 6 ++ main.c | 15 +-- node_declear.py | 2 +- node_run.py | 223 ++++++++++++++++++++++++++++++++++++++---- 5 files changed, 222 insertions(+), 28 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index ba0430d..0cdecec 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -__pycache__/ \ No newline at end of file +__pycache__/ +*.exe +*.csv \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a0ec4fd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "Kconfig": "kconfig", + "limits.h": "c" + } +} \ No newline at end of file diff --git a/main.c b/main.c index fde8bd5..725b2c7 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,8 @@ +// #include "limits.h" + // 行注释 struct _struct_a; @@ -13,8 +15,6 @@ struct _struct_a /* 块注释 */ { }; - - enum _enum_a; enum _enum_a { @@ -22,13 +22,14 @@ enum _enum_a { Enum1, Enum2, }; +int a = sizeof(enum _enum_a); // 暂不支持匿名枚举类型 -// enum { -// Enumb0=0, -// Enumb1, -// Enumb2, -// }; +enum _enum_b{ + Enumb0=0, + Enumb1, + Enumb2, +}; union _union_a { diff --git a/node_declear.py b/node_declear.py index a178e28..5a55cb2 100644 --- a/node_declear.py +++ b/node_declear.py @@ -115,7 +115,7 @@ def dist_node_type_typedef(token_list:list[lex_token]): token_list=token_list[1:] else: # c语言预设类型 - while(token_list[0].token in + while(token_list[0].token in [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) diff --git a/node_run.py b/node_run.py index a937d28..8eca5cf 100644 --- a/node_run.py +++ b/node_run.py @@ -1,37 +1,222 @@ -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 copy import deepcopy + + + +class variable_type(object): + def __init__(self,attr:list[str],size:int,limit:tuple[int,int]=None): + # 对于函数 attr就是返回值类型 attr中包含 '()' 则表示函数 + self.attr=attr + self.size=size + self.pack=4 # 默认4字节对齐 + self.member:list[(variable_type,str)]=[] + if not (limit is None): + self.min=limit[0] + self.max=limit[1] + if(self.min>self.max): + raise Exception(f"{self.attr} 最小值大于最大值 {limit}") + self.enum_types={} + # 对于函数 member就是形式参数 + def add_member(self,member,name:str): + if(isinstance(member,(variable_type))): + self.member.append((member,name)) + else: + raise Exception(f"参数类型不对 {type(member)}") + def add_enum_item(self,key:str,index:int): + if (not key in self.enum_types.keys()) and (not index in self.enum_types.values()): + self.enum_types[key]=index + return + print(self.enum_types.keys(),self.enum_types.values()) + raise Exception(f"枚举类型键或值已存在 {key} {index}") + def get_enum_value(self,key:str): + return self.enum_types.get(key,None) + def eq(self,attr:list[str]): + attr.sort() + self.attr.sort() + if(len(self.attr) != len(attr)): + return False + for item1,item2 in zip(self.attr,attr): + if(item1 != item2): + return False + return True + def get_size(self): + if("struct" in self.attr): + size=0 + for item in self.member: + item_size=((item.get_size()+(self.pack-1))//self.pack)*self.pack + size+=item_size + return size + elif("union" in self.attr): + size=0 + for item in self.member: + item_size=item.get_size() + if(size str: + print("变量类型") + for item in self.variable_type_list: + print(f"\t{item.attr}") + print("变量别名") + for item in self.variable_type_alias: + print(f"\t{item.attr.attr} eq {item.alias}") + print("变量栈") + for item in self.stack: + print(f"\t{item.attr.attr} {item.name}={item.value()}") + return "" +if __name__ == "__main__": + f=file() + f.add_enum_type("enum1",[("ENUM0",0),("ENUM1",1)]) + f.add_struct_type(4,"struct1",[(f.get_variable_type(['int']),'a')]) + f.add_type_alias(['unsigned','char'],'uint8_t') + f.add_variable(['unsigned','char'],'char_a',120) + f.add_variable(['uint8_t'],'char_b',100) + print(f) \ No newline at end of file