diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index 08a68716af6a5db5c60d99575083155abfebf2dd..f8cb15e94b83d3bb810d943bd2883f39eef4ca94 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -158,7 +158,7 @@ class ArrayCreateNode(ASTNode): self.children.append(self.arrayType) self.children.append(self.arraySize) - + def disambigName(self): helperDisambigName(self.arraySize) @@ -183,7 +183,7 @@ class AssignNode(ASTNode): self.children.append(self.right) self.children.append(self.left) - + def disambigName(self): helperDisambigName(self.right) helperDisambigName(self.left) @@ -208,10 +208,10 @@ class CastNode(ASTNode): self.children.append(self.left) self.children.append(self.right) - + def disambigName(self): helperDisambigName(self.left) - helperDisambigName(self.right) + helperDisambigName(self.right) ################################################################################### # classInstanceCreate unqualCreate @@ -271,7 +271,7 @@ class ExprNode(ASTNode): # make a TypeStruct node and populate myType field for self super().checkType() # check children's type first to populate their myType field - print(self.left) + # Unary operations: if not self.left: if self.op == '-' and self.right.myType.isNum(): @@ -336,7 +336,7 @@ class FieldAccessNode(ASTNode): self.ID = NameNode(parseTree.children[2], False, typeName) self.children.append(self.primary) - + def disambigName(self): if not self.primary: # this implies that the ID has nothing that comes before it helperDisambigName(self.ID) diff --git a/MemberNodes.py b/MemberNodes.py index b44ff9c996b1261fca2b829cec759a470ddfdb04..e3cacccf34d73b5957b09572ae583c3441a4a533 100644 --- a/MemberNodes.py +++ b/MemberNodes.py @@ -9,7 +9,7 @@ from collections import OrderedDict # field class FieldNode(ASTNode): # always list all fields in the init method to show the class structure - def __init__(self, parseTree, typeName): + def __init__(self, parseTree, typeName, order): self.parseTree = parseTree self.name = '' self.variableDcl = None @@ -17,6 +17,7 @@ class FieldNode(ASTNode): self.env = None self.children = [] self.typeName = typeName + self.order = order # get field name nameNodes = getParseTreeNodes(['ID'], parseTree, ['params', 'type', 'methodBody']) @@ -41,7 +42,7 @@ class FieldNode(ASTNode): # method class MethodNode(ASTNode): # always list all fields in the init method to show the class structure - def __init__(self, parseTree, typeName): + def __init__(self, parseTree, typeName, order): self.parseTree = parseTree self.name = '' self.methodType = '' @@ -52,6 +53,7 @@ class MethodNode(ASTNode): self.env = None self.children = [] self.typeName = typeName + self.order = order # get method name nameNodes = getParseTreeNodes(['ID'], parseTree, ['params', 'type', 'methodBody']) diff --git a/Test.py b/Test.py index 9c968556c137bd46c15b99b3037b349ce7248578..c1f8929da44c0b003bb205b88eeee3a4fc5b6b15 100644 --- a/Test.py +++ b/Test.py @@ -47,7 +47,7 @@ def a2Multiple(): for c in testCases: # get all files from stdlib folder testFiles = [join(dp, f) for dp, dn, filenames in walk('stdlib/2.0/java/') for f in filenames] - + if '.java' in c: # add this one file testFiles.append(c) @@ -82,7 +82,7 @@ def run(testFiles): parseTrees = [] for f in testFiles: - print(f) + # print(f) if f.split("/")[-1] == ".DS_Store": continue content = open(f, "r").read() @@ -142,7 +142,7 @@ def run(testFiles): # except Exception as e: # print("ERROR at AST building for file {}".format(f)) # traceback.print_stack() - # return "building AST: " + e.args[0] + # return "building AST: " + e.args[0] ASTs = astBuild(parseTrees) # for (n, t) in ASTs: @@ -150,7 +150,7 @@ def run(testFiles): # print("--------------------") # t.printTree() # print("\n \n\n \n") - + try: buildEnvAndLink(ASTs) except Exception as e: @@ -160,11 +160,11 @@ def run(testFiles): return "buildEnvAndLink: " + e.args[0] # print("<<<<------- after buildEnvAndLink -------->>>>>>") - for (n, t) in ASTs: - print(n) - print("--------------------") - t.printTree() - print("\n \n\n \n") + # for (n, t) in ASTs: + # print(n) + # print("--------------------") + # t.printTree() + # print("\n \n\n \n") # try: # disamiguateAndTypeChecking(ASTs) diff --git a/TypeNodes.py b/TypeNodes.py index 920449d447b60fd57a0840b92e9effea79db2147..e443a85018fbec8b5aa75265d4d5fd104e35657b 100644 --- a/TypeNodes.py +++ b/TypeNodes.py @@ -72,7 +72,7 @@ class ClassInterNode(ASTNode): # parent interface methods for inter in self.superInter: superInterContains.extend(inter.getContains(hierarchy + [self.canonName])) - + # parent class methods if hasattr(self, 'superClass') and self.superClass: superClassContains.extend(self.superClass.getContains(hierarchy + [self.canonName])) @@ -89,7 +89,7 @@ class ClassInterNode(ASTNode): break if not sicOverwritten: superContains.append(sic) - + superContains.extend(superClassContains) elif not self.superInter and self.canonName != 'java.lang.Object': @@ -176,17 +176,17 @@ class ClassNode(ClassInterNode): self.superInter.append(n.lex) elif node.name == 'classBody': - fieldNodes = getParseTreeNodes(['fieldDcl'], node, ['constructorDcl', 'methodDcl']) - for f in fieldNodes: - self.fields.append(FieldNode(f, self.name)) - - constructorDcl = getParseTreeNodes(['constructorDcl'], node, ['fieldDcl', 'methodDcl']) - for c in constructorDcl: - self.constructors.append(MethodNode(c, self.name)) - - methodNodes = getParseTreeNodes(['methodDcl'], node, ['constructorDcl', 'fieldDcl']) - for m in methodNodes: - self.methods.append(MethodNode(m, self.name)) + order = 0 + memberDcls = getParseTreeNodes(['classBodyDcl'], node, ['constructorDcl', 'methodDcl', 'fieldDcl']) + + for m in memberDcls: + if m.children[0].name == 'fieldDcl': + self.fields.append(FieldNode(m.children[0], self.name, order)) + elif m.children[0].name == 'methodDcl': + self.methods.append(MethodNode(m.children[0], self.name, order)) + elif m.children[0].name == 'constructorDcl': + self.constructors.append(MethodNode(m.children[0], self.name, order)) + order += 1 self.canonName = self.packageName + '.' + self.name self.children += self.fields + self.methods + self.constructors @@ -280,7 +280,7 @@ class InterNode(ClassInterNode): elif node.name == 'interfaceBody': nodes = getParseTreeNodes(['interfaceMethodDcl'], node) for n in nodes: - self.methods.append(MethodNode(n, self.name)) + self.methods.append(MethodNode(n, self.name, 0)) # order = 0 since no method body in interface needs to be checked self.canonName = self.packageName + '.' + self.name self.children.extend(self.methods) @@ -337,7 +337,7 @@ def safeReplace(cur, new, className): # 13. A protected method must not replace a public method if 'public' in cur.mods and 'protected' in new.mods: raise Exception("ERROR: Protected {0} '{1}' in class '{2}' replaces public {0}".format(methodOrField, new.name, className)) - + # 14. A method must not replace a final method # quick fix for final method getClass from java.lang.Object if 'final' in cur.mods and cur.name != 'getClass':