Skip to content
Snippets Groups Projects
Commit 9ee21a5c authored by Xun Yang's avatar Xun Yang
Browse files

change class create to use type

parent 2aee38ff
No related branches found
No related tags found
No related merge requests found
......@@ -177,10 +177,16 @@ class ClassCreateNode(ASTNode):
# always list all fields in the init method to show the class structure
def __init__(self, parseTree):
self.parseTree = parseTree.children[0] # input is classInstanceCreate unqualCreate
self.className = getParseTreeNodes(['ID', 'COMPID'], self.parseTree.children[1])[0].lex
self.className = TypeNode(parseTree.children[0].children[1])
self.args = ArgsNode(self.parseTree.children[3])
self.env = None
self.children = [self.args]
self.children = [self.args, self.className]
def checkType(self):
# check class is not abstract
if 'abstract' in self.className.myType.typePointer.mods:
raise Exception('ERROR: Cannot create an instance of abstract class {}.'.format(self.className.myType.name))
# TODO: more type checking
......
......@@ -27,9 +27,6 @@ class FieldNode(ASTNode):
for m in node.children:
self.mods.append(m.lex)
elif node.name == 'type':
self.fieldType = TypeNode(node)
elif node.name == 'variableDcl':
self.variableDcl = VarDclNode(node)
......@@ -81,6 +78,7 @@ class MethodNode(ASTNode):
self.body = BlockNode(n)
if self.body: self.children.append(self.body)
self.children.append(self.methodType)
def __eq__(self, other):
if self.name == other.name and len(self.params) == len(other.params):
......
......@@ -3,6 +3,9 @@ from AST import ASTNode, getParseTreeNodes
# TypeNode: an AST node represents a type
# TypeStruct: a struct holding type information for type checking
# TypeNode represents a parse tree unit that contains a type,
# TypeStruct is not a unit on parseTree, it is just a struct living in different AST nodes to keep track of their type
class TypeNode(ASTNode):
# always list all fields in the init method to show the class structure
def __init__(self, parseTree):
......@@ -11,7 +14,7 @@ class TypeNode(ASTNode):
self.env = None
self.children = []
self.myType = "" # empty string or typeStruct
if parseTree == 'VOID':
self.myType = TypeStruct('void')
else:
......
from AST import ASTNode, getParseTreeNodes
from Environment import Env
from TheTypeNode import TypeNode
from TheTypeNode import TypeNode, TypeStruct
# LiteralNode
# ParamNode
......@@ -22,6 +22,7 @@ class LiteralNode(ASTNode):
self.parseTree = parseTree
self.name = LiteralNode.toLiType.get(parseTree.children[0].name) # type of the literal
self.value = parseTree.children[0].lex # the value
self.myType = TypeStruct(self.name)
self.env = None
self.children = []
......
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