Skip to content

Commit

Permalink
Bug fix: get_tree when using no parameter functions
Browse files Browse the repository at this point in the history
当调用无参数的函数是,其parameters会成为None,导致调用get_tree函数报错,添加一个判断即可解决问题
  • Loading branch information
iewnfod committed Sep 17, 2023
1 parent d7e423d commit 633f1e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/AST/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def __init__(self, id, parameters, statements, returns=None, *args, **kwargs):
super().__init__(*args, **kwargs)

def get_tree(self, level=0):
return LEVEL_STR * level + self.type + ' ' + str(self.id) + '\n' + self.parameters.get_tree(level+1) + '\n' + self.statements.get_tree(level+1)
if self.parameters:
return LEVEL_STR * level + self.type + ' ' + str(self.id) + '\n' + self.parameters.get_tree(level+1) + '\n' + self.statements.get_tree(level+1)
else:
return LEVEL_STR * level + self.type + ' ' + str(self.id) + '\n' + self.statements.get_tree(level+1)

def exe(self):
stack.add_function(self)
Expand All @@ -26,7 +29,10 @@ def __init__(self, id, parameters=None, *args, **kwargs):
super().__init__(*args, **kwargs)

def get_tree(self, level=0):
return LEVEL_STR * level + self.type + ' ' + str(self.id) + '\n' + self.parameters.get_tree(level+1)
if self.parameters:
return LEVEL_STR * level + self.type + ' ' + str(self.id) + '\n' + self.parameters.get_tree(level+1)
else:
return LEVEL_STR * level + self.type + ' ' + str(self.id)

def exe(self):
new_dict = {} # {变量名: (值, 类型, 是否是常量)}
Expand Down
8 changes: 5 additions & 3 deletions test/test.cpc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DECLARE a : INTEGER
a <- 1
OUTPUT a
FUNCTION a RETURNS INTEGER
RETURN 1
ENDFUNCTION

OUTPUT 1

0 comments on commit 633f1e7

Please sign in to comment.