添加一些函数注释
This commit is contained in:
6
ReadMe.txt
Normal file
6
ReadMe.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
|
||||
2024.12.6
|
||||
函数变量需要先编译为字节数据才能确定地址
|
||||
|
15
compiler.py
Normal file
15
compiler.py
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
from cpu import instruction as ins
|
||||
from cpu import cpu
|
||||
|
||||
|
||||
|
||||
class compiler(object):
|
||||
def __init__(self,reg_num:int) -> None:
|
||||
self.symbol_stack_list:list[dict]=[]
|
||||
self.symbol_reg_list:list[dict]=[]
|
||||
self.cpu=cpu(0x1000_0000,1*1024*1024,0x2000_0000,1*1024*1024)
|
||||
|
||||
|
||||
|
||||
|
2
main.c
2
main.c
@@ -69,5 +69,5 @@ const char* get_type(int s) {
|
||||
|
||||
|
||||
int main(){
|
||||
return 0;
|
||||
return sizeof(&get_type);
|
||||
}
|
||||
|
65
node_run.py
65
node_run.py
@@ -102,7 +102,7 @@ class variable_alias(object):
|
||||
self.attr=attr
|
||||
self.alias=alias
|
||||
|
||||
class file(object):
|
||||
class run(object):
|
||||
def __init__(self):
|
||||
self.variable_type_list:list[variable_type]=[
|
||||
variable_type(["char"],1,(-128,127)),
|
||||
@@ -137,17 +137,31 @@ class file(object):
|
||||
self.cpu=cpu(0x1000_0000,1*1024*1024,0x2000_0000,1*1024*1024)
|
||||
self.stack:list[variable]=[]
|
||||
def variable_type_check(self,attr:list[str]):
|
||||
'''
|
||||
校验变量类型
|
||||
attr:变量属性
|
||||
如果该变量类型已存在 则返回True 否则返回False
|
||||
'''
|
||||
for item in self.variable_type_list:
|
||||
if(item.eq(attr)):
|
||||
return True
|
||||
return False
|
||||
def enum_type_check(self,key):
|
||||
'''
|
||||
校验枚举类型
|
||||
key:枚举的名称
|
||||
如果枚举名称已存在 则返回True 否则返回False
|
||||
'''
|
||||
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]):
|
||||
'''
|
||||
根据变量属性列表返回变量类
|
||||
attr:属性列表
|
||||
'''
|
||||
if(len(attr)==1):
|
||||
for item in self.variable_type_alias:
|
||||
if(item.alias==attr[0]):
|
||||
@@ -156,7 +170,13 @@ class file(object):
|
||||
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,)]):
|
||||
def add_struct_type(self,pack:int,type_name:str,member:list[tuple[variable_type,str]]):
|
||||
'''
|
||||
添加一个结构体类型
|
||||
pack:按pack字节对齐
|
||||
type_name:类型名,如果为None则生成匿名结构体类型
|
||||
member:成员类型和成员名称
|
||||
'''
|
||||
if(type_name is None):
|
||||
type_name=f"unname_struct{self.unname_struct_index}"
|
||||
self.unname_struct_index+=1
|
||||
@@ -170,7 +190,13 @@ class file(object):
|
||||
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)]):
|
||||
def add_union_type(self,type_name:str,member:list[tuple[variable_type,str]]):
|
||||
'''
|
||||
添加一个联合体类型
|
||||
pack:按pack字节对齐
|
||||
type_name:类型名,如果为None则生成匿名联合体类型
|
||||
member:成员类型和成员名称
|
||||
'''
|
||||
if(type_name is None):
|
||||
type_name=f"unname_union{self.unname_union_index}"
|
||||
self.unname_union_index+=1
|
||||
@@ -183,7 +209,12 @@ class file(object):
|
||||
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,)]):
|
||||
def add_enum_type(self,type_name:str,member:list[tuple[str,int]]):
|
||||
'''
|
||||
添加一个枚举类型
|
||||
type_name:类型名,如果为None则生成匿名结构体类型
|
||||
member:枚举名称和枚举值
|
||||
'''
|
||||
if(type_name is None):
|
||||
type_name=f"unname_enum{self.unname_union_index}"
|
||||
self.unname_enum_index+=1
|
||||
@@ -197,6 +228,11 @@ class file(object):
|
||||
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):
|
||||
'''
|
||||
添加一个类型别名
|
||||
attr:类型的属性列表,
|
||||
member:成员类型和成员名称
|
||||
'''
|
||||
if(len(attr)==1):
|
||||
for item in self.variable_type_alias:
|
||||
if(item.alias==attr[0]):
|
||||
@@ -206,6 +242,12 @@ class file(object):
|
||||
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):
|
||||
'''
|
||||
添加一个变量
|
||||
attr:变量类型的属性列表
|
||||
name:变量名称
|
||||
value:变量值 为None时不设置初值 这个参数暂时没有使用
|
||||
'''
|
||||
for item in self.stack:
|
||||
if(item.name==name):
|
||||
raise Exception(f"符号名称已存在 {name}")
|
||||
@@ -220,6 +262,12 @@ class file(object):
|
||||
addr=self.cpu.create_var(item.get_size())
|
||||
self.stack.append(variable(name,item,addr))
|
||||
def set_variable(self,name:str,value,member_stack:list[str]=[]):
|
||||
'''
|
||||
设置变量值
|
||||
name:要设置的变量名称
|
||||
value:变量值
|
||||
member_stack:变量的成员
|
||||
'''
|
||||
for item in self.stack:
|
||||
if(item.name==name):
|
||||
addr,size=item.member_value(member_stack)
|
||||
@@ -227,6 +275,11 @@ class file(object):
|
||||
return
|
||||
raise Exception(f"符号名称不存在 {name}")
|
||||
def get_variable(self,name:str,member_stack:list[str]=[]):
|
||||
'''
|
||||
获取变量的值
|
||||
name:变量名
|
||||
member_stack:变量成员,成员的成员
|
||||
'''
|
||||
for item in self.stack:
|
||||
if(item.name==name):
|
||||
addr,size=item.member_value(member_stack)
|
||||
@@ -250,13 +303,15 @@ class file(object):
|
||||
return ret_str
|
||||
|
||||
if __name__ == "__main__":
|
||||
f=file()
|
||||
f=run()
|
||||
f.add_enum_type("enum1",[("ENUM0",0),("ENUM1",1)])
|
||||
f.add_struct_type(4,"struct1",[
|
||||
(f.get_variable_type(['int']),'a'),
|
||||
(f.get_variable_type(['int']),'b'),
|
||||
])
|
||||
f.add_type_alias(['unsigned','char'],'uint8_t')
|
||||
f.add_type_alias(['struct','struct1'],'struct1_def')
|
||||
f.add_type_alias(['struct1_def'],'struct1_def_def')
|
||||
f.add_variable(['unsigned','char'],'char_a',120)
|
||||
f.add_variable(['uint8_t'],'char_b',100)
|
||||
f.add_variable(['struct',"struct1"],'struct_a')
|
||||
|
Reference in New Issue
Block a user