Skip to content
Snippets Groups Projects
Commit 56d59f91 authored by Nicholas Robinson's avatar Nicholas Robinson
Browse files

ast weed & cfg rule

- can now successfully check that class has at least one constructor
- added rule fixing `this.x = 123;` in `J1_publicfields.java`
- cleaned up Test.py a bit more
parent 4c07b0a6
No related branches found
No related tags found
No related merge requests found
......@@ -29,17 +29,24 @@ class ClassDcl(Node):
self.classBody = node.children[5]
def weed(self):
# Every class must contain at least one explicit constructor
hasConstructor = False
classBodyDcls = self.classBody.children[1]
for d in classBodyDcls.children:
if d.children:
if d.children[0].name == 'constructorDcl':
hasConstructor = True
if not hasConstructor:
if not checkConstructor(self.classBody.children[1]):
return 'Class ' + self.ID + ' does not contain a constructor.'
return ''
# Every class must contain at least one explicit constructor
# classBodyDcls: Node
# cfgrules:
# classBodyDcls classBodyDcl classBodyDcls
# classBodyDcls
def checkConstructor(classBodyDcls):
if classBodyDcls.children:
classBodyDcl = classBodyDcls.children[0]
if classBodyDcl.children[0].name == 'constructorDcl':
return True
return checkConstructor(classBodyDcls.children[1])
return False
##################### Helpers ##########################################
......
......@@ -55,13 +55,13 @@ def main():
(tree, error) = parse(tokens)
except:
print("Exception in Parsing")
# print(tree)
# Error in Parsing
if tree is None:
print("ERROR in Parsing:")
print(error)
for i in error.args:
print(i)
print("ERROR in Parsing: " + error.args[0])
# for n in error.args[1]: # the parse tree
# print(n)
print("**********************************************************")
continue
......@@ -70,6 +70,7 @@ def main():
(ast, error) = AstBuilding.astbuild(tree)
except:
print("Exception in AstBuilding")
# print(ast)
# Error in AstBuilding
if ast is None:
......
......@@ -155,7 +155,7 @@ postfixExpr
primaryNoArrayAccess
arrayID
start
198
199
start BOF packageDcl importDcls topDcls EOF
packageDcl PACKAGE name SEMICO
packageDcl PACKAGE ID SEMICO
......@@ -242,6 +242,7 @@ arrayType name LSQRBRACK RSQRBRACK
arrayType ID LSQRBRACK RSQRBRACK
variableDcl ID
variableDcl ID ASSIGN variableInit
variableDcl name ASSIGN variableInit
variableInit expr
args exprs
args
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment