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

constructor

parent eb5d7d87
No related branches found
No related tags found
1 merge request!14Insta field
......@@ -373,10 +373,9 @@ class ClassCreateNode(ASTNode):
self.code += p(instruction="push", arg1="eax", comment="pushing object as first argument")
# Evaluate arguments and pushing parameters
if self.args and hasattr(self.args, "codeGen"):
if not hasattr(self.args, "code"):
self.args.codeGen()
self.args.codeGen()
self.code += self.args.code
label = "M_"+self.cons.typeName+"_"+self.cons.name+"_"+self.cons.paramTypes
label = "M_" + self.cons.typeName + "_" + self.cons.paramTypes
self.code += importHelper(classDef.name, self.typeName, label)
self.code += p(instruction="call", arg1=label, comment="Calling constructor")
......
......@@ -82,7 +82,7 @@ class FieldNode(ASTNode):
p(instruction="mov", arg1="[ebx]", arg2="eax", comment="eax is a pointer to field value in heap")
self.code += ";End of declaration of static field\n"
###########################################################
......@@ -245,36 +245,49 @@ class MethodNode(ASTNode):
if hasattr(self, "code") and self.code != "":
return
myClass = self.env.getNode(self.typeName, 'type')
self.label = "M_" + self.typeName + "_" + self.paramTypes
self.code = pLabel(self.typeName + "_" + self.paramTypes, "method") # label
thisLoc = len(self.params) * 4 + 8
bodyCode = ""
# init fields
# body code
if self.body:
bodyCode = ""
# push all local var to stack
vars = getVarDclNodes(self.body)
for i in range(len(vars)):
vars[i].offset = i * 4 + 16
bodyCode += p("push", 0)
# call parent constructor
if myClass.superClass:
suLabel = "M_" + myClass.superClass.name + "_"
bodyCode += importHelper(myClass.superClass.name, self.typeName, suLabel)
bodyCode += p("mov", "eax", "[ebp - " + thisLoc + "]")
bodyCode += p("push", "eax", None, "# Pass THIS as argument to superClass.")
bodyCode += p("call", suLabel)
# init fields
fields = sorted(myClass.fields, key=lambda x: x.order)
for f in fields:
if not 'static' in field.mods and f.variableDcl.variableInit:
f.variableDcl.variableInit.right.codeGen()
bodyCode += f.variableDcl.variableInit.right.code
bodyCode += p("mov", "ebx", "[ebp - " + thisLoc + "]") # THIS
bodyCode += p("mov", "[ebx + " + f.offset + " ]", "eax")
# body code
if self.body:
self.body.codeGen()
bodyCode += self.body.code
bodyCode += self.label + "_end: ; end of method for " + self.name + "\n"
# pop off all the local var
for i in range(len(vars)):
bodyCode += p("pop", "edx")
self.code += genProcedure(bodyCode, "method definition for " + self.name)
else:
self.code += p("ret", "")
self.code += genProcedure(bodyCode, "Constructor definition for " + self.name + " " + self.paramTypes)
############# helper for forward ref checking ########
# Input: AST Node
......
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