添加存储和修改变量
发现要处理结构体相关数据的存储, 还是要存储原始byte数据要好处理一些 是否需要把所有变量的存储都改为存储原始byte数据 浮点数好像不好处理
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
__pycache__/
|
||||
__pycache__/
|
||||
*.exe
|
||||
*.csv
|
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"Kconfig": "kconfig",
|
||||
"limits.h": "c"
|
||||
}
|
||||
}
|
15
main.c
15
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 {
|
||||
|
@@ -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)
|
||||
|
223
node_run.py
223
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<item_size):
|
||||
size=item_size
|
||||
return size
|
||||
elif("enum" in self.attr):
|
||||
return 4
|
||||
else:
|
||||
return self.size
|
||||
|
||||
class variable(object):
|
||||
def __init__(self,name:str,value=None,attr:list[str]=[]):
|
||||
def __init__(self,name:str,attr:variable_type,value=None):
|
||||
self.name=name
|
||||
self.value_=value
|
||||
self.attr=attr
|
||||
def set_value(self,value):
|
||||
if("const" in self.attr):
|
||||
raise Exception(f"变量 {self.name} 不可写")
|
||||
self.value_=ValueError
|
||||
self.value_=value
|
||||
def value(self):
|
||||
return self.value_
|
||||
def member_value(self,member_stack:list[str]):
|
||||
return self.value_
|
||||
|
||||
|
||||
class variable_alias(object):
|
||||
def __init__(self,alias:str,attr:variable_type):
|
||||
self.attr=attr
|
||||
self.alias=alias
|
||||
|
||||
class file(object):
|
||||
def __init__(self):
|
||||
self.variable_list:list[variable]=[]
|
||||
self.function_list:list=[]
|
||||
self.variable_type_list:list=[]
|
||||
self.stack:list[variable]=[]
|
||||
self.variable_type_list:list[variable_type]=[
|
||||
variable_type(["char"],1,(-128,127)),
|
||||
variable_type(["short"],2,(-32768,32767)),
|
||||
variable_type(["int"],4,(-2147483648,2147483647)),
|
||||
variable_type(["long"],4,(-2147483648,2147483647)),
|
||||
variable_type(["long","long"],8,(-9223372036854775808,9223372036854775807)),
|
||||
variable_type(["float"],4),
|
||||
variable_type(["double"],8),
|
||||
variable_type(["unsigned","char"],1,(0,0xff)),
|
||||
variable_type(["unsigned","short"],2,(0,0xffff)),
|
||||
variable_type(["unsigned","int"],4,(0,0xffffffff)),
|
||||
variable_type(["unsigned","long"],4,(0,0xffffffff)),
|
||||
variable_type(["unsigned","long","long"],8,(0,0xffffffff_ffffffff)),
|
||||
]
|
||||
variable_list_len=len(self.variable_type_list)
|
||||
for i in range(variable_list_len):
|
||||
item=deepcopy(self.variable_type_list[i])
|
||||
item2=deepcopy(self.variable_type_list[i])
|
||||
item3=deepcopy(self.variable_type_list[i])
|
||||
item.attr.insert(0,"static")
|
||||
self.variable_type_list.append(item)
|
||||
item2.attr.insert(0,"const")
|
||||
self.variable_type_list.append(item2)
|
||||
item3.attr.insert(0,"static")
|
||||
item3.attr.insert(1,"const")
|
||||
self.variable_type_list.append(item3)
|
||||
self.variable_type_alias:list[variable_alias]=[]
|
||||
self.unname_struct_index=0
|
||||
self.unname_union_index=0
|
||||
self.unname_enum_index=0
|
||||
def variable_type_check(self,attr:list[str]):
|
||||
for item in self.variable_type_list:
|
||||
if(item.eq(attr)):
|
||||
return True
|
||||
return False
|
||||
def enum_type_check(self,key):
|
||||
for item in self.variable_type_list:
|
||||
if("enum" in item.attr):
|
||||
if item.get_enum_value(key):
|
||||
return True
|
||||
return False
|
||||
def get_variable_type(self,attr:list[str]):
|
||||
if(len(attr)==1):
|
||||
for item in self.variable_type_alias:
|
||||
if(item.alias==attr[0]):
|
||||
return item.attr
|
||||
for item in self.variable_type_list:
|
||||
if(item.eq(attr)):
|
||||
return item
|
||||
raise Exception(f"变量类型不存在 {attr}")
|
||||
def add_struct_type(self,pack:int,type_name:str,member:list[(variable_type,str,)]):
|
||||
if(type_name is None):
|
||||
type_name=f"unname_struct{self.unname_struct_index}"
|
||||
self.unname_struct_index+=1
|
||||
attr=["struct",type_name]
|
||||
if(self.variable_type_check(attr)):
|
||||
raise Exception(f"类型已存在 {attr}")
|
||||
vt=variable_type(attr,0,None)
|
||||
vt.pack=pack
|
||||
for item in member:
|
||||
if("static" in item[0].attr):
|
||||
raise Exception(f"结构体成员不能包含 static 属性 {item[0].attr}")
|
||||
vt.add_member(item[0],item[1])
|
||||
self.variable_type_list.append(vt)
|
||||
def add_union_type(self,type_name:str,member:list[(variable_type,str)]):
|
||||
if(type_name is None):
|
||||
type_name=f"unname_union{self.unname_union_index}"
|
||||
self.unname_union_index+=1
|
||||
attr=["union",type_name]
|
||||
if(self.variable_type_check(attr)):
|
||||
raise Exception(f"类型已存在 {attr}")
|
||||
vt=variable_type(attr,0,None)
|
||||
for item in member:
|
||||
if("static" in item[0].attr):
|
||||
raise Exception(f"结构体成员不能包含 static 属性 {item[0].attr}")
|
||||
vt.add_member(item[0],item[1])
|
||||
self.variable_type_list.append(vt)
|
||||
def add_enum_type(self,type_name:str,member:list[(str,int,)]):
|
||||
if(type_name is None):
|
||||
type_name=f"unname_enum{self.unname_union_index}"
|
||||
self.unname_enum_index+=1
|
||||
attr=["enum",type_name]
|
||||
if(self.variable_type_check(attr)):
|
||||
raise Exception(f"类型已存在 {attr}")
|
||||
vt=variable_type(attr,4,None)
|
||||
for item in member:
|
||||
if(self.enum_type_check(item[0])):
|
||||
raise Exception(f"枚举键已存在 {member}")
|
||||
vt.add_enum_item(item[0],item[1])
|
||||
self.variable_type_list.append(vt)
|
||||
def add_type_alias(self,attr:list[str],new_name:str):
|
||||
if(len(attr)==1):
|
||||
for item in self.variable_type_alias:
|
||||
if(item.alias==attr[0]):
|
||||
self.variable_type_alias.append(variable_alias(new_name,item.attr))
|
||||
return
|
||||
for item in self.variable_type_list:
|
||||
if(item.eq(attr)):
|
||||
self.variable_type_alias.append(variable_alias(new_name,item))
|
||||
def add_variable(self,attr:list[str],name:str,value=None):
|
||||
for item in self.stack:
|
||||
if(item.name==name):
|
||||
raise Exception(f"符号名称已存在 {name}")
|
||||
if(len(attr)==1):
|
||||
for item in self.variable_type_alias:
|
||||
if(item.alias==attr[0]):
|
||||
self.stack.append(variable(name,item.attr,value))
|
||||
return
|
||||
for item in self.variable_type_list:
|
||||
if(item.eq(attr)):
|
||||
self.stack.append(variable(name,item,value))
|
||||
def modify_variable(self,name:str,value):
|
||||
for item in self.stack:
|
||||
if(item.name==name):
|
||||
item.set_value(value)
|
||||
raise Exception(f"符号名称不存在 {name}")
|
||||
def __str__(self) -> 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)
|
Reference in New Issue
Block a user