diff --git a/AST.py b/AST.py index fbb2702a388b4f85e59647e4c5ccf691f38ca527..0793590411a12db617ffbabe9608abc63401c19c 100644 --- a/AST.py +++ b/AST.py @@ -136,6 +136,7 @@ def getParseTreeNodes(names, tree, terminateList = []): result.extend(getParseTreeNodes(names, n, terminateList)) return result +# Getting a list of nodes with the provided list of names of the nodes def getASTNode(names, AST): result = [] if not AST: diff --git a/LineNodes.py b/LineNodes.py index ca2938496d64d4a8cb1271a7b51a6b82f161bbd1..86464f9e9dc8e10f6d597f1663c93917aded5af5 100644 --- a/LineNodes.py +++ b/LineNodes.py @@ -1,4 +1,4 @@ -from AST import ASTNode, getParseTreeNodes +from AST import ASTNode, getParseTreeNodes, getASTNode from Environment import Env from ExprPrimaryNodes import makeNodeFromExpr, makeNodeFromAllPrimary, MethodInvNode, ClassCreateNode from TheTypeNode import TypeNode, TypeStruct @@ -113,7 +113,12 @@ class VarDclNode(ASTNode): # Checking for definite assignment if checkAssign: if not self.variableInit: - raise Exception("ERROR: local variable declaration not assigned") + raise Exception("ERROR: local variable declaration {} is not assigned".format(self.name)) + # Checking if the local variable appears in it's own intializer + nameNodes = getASTNode(["NameNode"], self.variableInit) + for node in nameNodes: + if self.name in node.IDs: + raise Exception("ERROR: local variable {} appears in it's own intialization".format(self.name)) self.myType = self.dclType.myType self.children.append(self.dclType) diff --git a/Test.py b/Test.py index e0dcfd6c2655a0b4a8ca3a8ecb6cb457c0b55649..34940af9bed273aba80ab7769e4bb4eaa6d115a8 100644 --- a/Test.py +++ b/Test.py @@ -134,7 +134,7 @@ def run(testFiles): parseTrees.append((f, tree)) # for (n, t) in parseTrees: - # if n == "Tests/A3/J1_accessstaticfield/Main.java": + # if n == "./Tests/A4/Je_8_DefiniteAssignment_InitToItself.java": # print(n) # print(t) try: @@ -142,11 +142,12 @@ def run(testFiles): except Exception as e: return "AST buidling: " + e.args[0] - # for (n, t) in ASTs: - # print(n) - # print("--------------------") - # t.printTree() - # print("\n \n\n \n") + for (n, t) in ASTs: + if n == "./Tests/A4/Je_8_DefiniteAssignment_ComplexInitializer.java": + print(n) + print("--------------------") + t.printTree() + print("\n \n\n \n") try: buildEnvAndLink(ASTs)