Skip to content
Snippets Groups Projects
Commit a212c671 authored by Nicholas Robinson's avatar Nicholas Robinson Committed by Nicholas Wesley Robinson
Browse files

ExprNode LiteralNode codeGen

- adding the simplest form of their functions
- made it work
parent 21fdaf4e
No related branches found
No related tags found
1 merge request!9merge Expr literal
...@@ -85,6 +85,13 @@ class ASTNode(): ...@@ -85,6 +85,13 @@ class ASTNode():
c.codeGen() c.codeGen()
self.code += c.code self.code += c.code
def codeGen(self):
res = ""
for c in self.children:
if c and hasattr(c, 'codeGen'):
res += c.codeGen()
return res
def printNodePretty(self, prefix=0): def printNodePretty(self, prefix=0):
pp = pprint.PrettyPrinter(indent=prefix) pp = pprint.PrettyPrinter(indent=prefix)
pp.pprint(self.__class__.__name__) pp.pprint(self.__class__.__name__)
......
...@@ -421,6 +421,30 @@ class ExprNode(ASTNode): ...@@ -421,6 +421,30 @@ class ExprNode(ASTNode):
raise Exception("ERROR: Incompatible types. Left of {} type can't be used with right of {} type on operation {}".format(self.left.myType.name, self.right.myType.name, self.op)) raise Exception("ERROR: Incompatible types. Left of {} type can't be used with right of {} type on operation {}".format(self.left.myType.name, self.right.myType.name, self.op))
def codeGen(self):
res = ""
# Unary:
if not self.left:
return res
res += self.left.codeGen() # get left output
res += p("push", "eax", None, None)
res += self.right.codeGen() # get right output
res += p("pop", "ebx", None, None)
if self.op in ['+', '-', '*', '/', '%']:
# operation -> generated code
ops = {
'+': "add",
'-': "sub"
}
res += p(ops[self.op], "ebx", "eax", " " + ops[self.op] + " left + right")
res += p("mov", "eax", "ebx", " move result to eax")
return res
# returns True, False, Int or None (for non-constant expr) # returns True, False, Int or None (for non-constant expr)
# children of exprNode is either exprNode or literalNode # children of exprNode is either exprNode or literalNode
def getConstant(self): def getConstant(self):
......
from AST import ASTNode, getParseTreeNodes from AST import ASTNode, getParseTreeNodes
from Environment import Env from Environment import Env
from TheTypeNode import TypeNode, TypeStruct from TheTypeNode import TypeNode, TypeStruct
from CodeGenUtils import p
# LiteralNode # LiteralNode
# ParamNode # ParamNode
...@@ -40,6 +41,15 @@ class LiteralNode(ASTNode): ...@@ -40,6 +41,15 @@ class LiteralNode(ASTNode):
node = self.env.getNode(self.name, "type") node = self.env.getNode(self.name, "type")
self.myType = TypeStruct(self.name, node) self.myType = TypeStruct(self.name, node)
def codeGen(self):
res = ""
# int
if self.name == 'int':
res += p("mov", "eax", self.getConstant(), " set to literal")
return res
def getConstant(self): def getConstant(self):
if self.name == 'int': if self.name == 'int':
return int(self.value) return int(self.value)
......
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