summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBenedikt Böhm <bb@xnull.de>2009-07-03 09:51:56 +0200
committerBenedikt Böhm <bb@xnull.de>2009-07-03 09:51:56 +0200
commit615a4495af2d46ac1408cebff48a89deccb72a08 (patch)
tree5d3420bb6077e111425da2f150b0cba8b700a91c /src
parent1c67357ad255533b03cb22d31b7c6cf31028fc99 (diff)
downloadswppy-615a4495af2d46ac1408cebff48a89deccb72a08.tar.gz
swppy-615a4495af2d46ac1408cebff48a89deccb72a08.tar.xz
swppy-615a4495af2d46ac1408cebff48a89deccb72a08.zip
cleanup ast
Diffstat (limited to 'src')
-rw-r--r--src/front/ast.py87
1 files changed, 30 insertions, 57 deletions
diff --git a/src/front/ast.py b/src/front/ast.py
index 99ccbee..b05f7fb 100644
--- a/src/front/ast.py
+++ b/src/front/ast.py
@@ -30,7 +30,7 @@ class Program(Node):
self.lineno = lineno
def generate(self):
- self.debug("Program.generate(): functions = %s" % self.functions)
+ self.debug("Program.generate(): %s" % repr(self))
for function in self.functions:
function.generate()
@@ -45,7 +45,7 @@ class Function(Node):
self.lineno = lineno
def generate(self):
- self.debug("Function.generate(): name = %s, params = %s, statement = %s" % (self.name, self.params, self.statement))
+ self.debug("Function.generate(): %s" % repr(self))
self.emitfunction(self.name)
begin = self.newlabel()
after = self.newlabel()
@@ -60,6 +60,9 @@ class Function(Node):
class Statement(Node):
pass
+class Expression(Node):
+ pass
+
class Sequence(Statement):
def __init__(self, s1, s2, lineno = -1):
self.s1 = s1
@@ -67,7 +70,7 @@ class Sequence(Statement):
self.lineno = lineno
def generate(self, before, after):
- self.debug("Sequence.generate(before = %d, after = %d): s1 = %s, s2 = %s" % (before, after, self.s1, self.s2))
+ self.debug("Sequence.generate(before = %d, after = %d): %s" % (before, after, repr(self)))
if not self.s1:
self.s2.generate(before, after)
elif not self.s2:
@@ -89,14 +92,14 @@ class IfStatement(Statement):
self.lineno = lineno
def generate(self, before, after):
- self.debug("IfStatement.generate(before = %d, after = %d): expression = %s, true = %s, false = %s" % (before, after, self.expression, self.true_statement, self.false_statement))
+ self.debug("IfStatement.generate(before = %d, after = %d): %s" % (before, after, repr(self)))
x = self.expression.reduce()
if self.false_statement:
true_label = self.newlabel()
false_label = self.newlabel()
- self.emit(Op.BEQ, x, "L%d" % false_label)
+ self.emit(Op.BEZ, x, "L%d" % false_label)
self.emitlabel(true_label)
self.true_statement.generate(true_label, after)
self.emit(Op.JMP, "L%d" % after)
@@ -104,7 +107,7 @@ class IfStatement(Statement):
self.false_statement.generate(false_label, after)
else:
true_label = self.newlabel()
- self.emit(Op.BEQ, x, "L%d" % after)
+ self.emit(Op.BEZ, x, "L%d" % after)
self.emitlabel(true_label)
self.true_statement.generate(true_label, after)
@@ -118,8 +121,8 @@ class WhileStatement(Statement):
self.lineno = lineno
def generate(self, before, after):
- self.debug("WhileStatement.generate(before = %d, after = %d): expression = %s, statement = %s" % (before, after, self.expression, self.statement))
- self.emit(Op.BEQ, self.expression.reduce(), "L%d" % after)
+ self.debug("WhileStatement.generate(before = %d, after = %d): %s" % (before, after, repr(self)))
+ self.emit(Op.BEZ, self.expression.reduce(), "L%d" % after)
label = self.newlabel()
self.emitlabel(label)
self.statement.generate(label, before)
@@ -134,7 +137,7 @@ class ReturnStatement(Statement):
self.lineno = lineno
def generate(self, before, after):
- self.debug("ReturnStatement.generate(before = %d, after = %d): expression = %s" % (before, after, self.expression))
+ self.debug("ReturnStatement.generate(before = %d, after = %d): %s" % (before, after, repr(self)))
self.emit(Op.RETURN, self.expression.reduce())
def __repr__(self):
@@ -147,26 +150,26 @@ class AssignStatement(Statement):
self.lineno = lineno
def generate(self, before, after):
- self.debug("AssignStatement.generate(before = %d, after = %d): ident = %s, expression = %s" % (before, after, self.ident, self.expression))
+ self.debug("AssignStatement.generate(before = %d, after = %d): %s" % (before, after, repr(self)))
self.emit(Op.STORE, self.ident, self.expression.reduce())
def __repr__(self):
return "<AssignStatement for %s at line %d>" % (self.ident, self.lineno)
-class FunctionCall(Statement):
+class FunctionCall(Statement, Expression):
def __init__(self, ident, arguments = [], lineno = -1):
self.ident = ident
self.arguments = arguments
self.lineno = lineno
def generate(self):
- self.debug("FunctionCall.generate(): ident = %s, arguments = %s" % (self.ident, self.arguments))
+ self.debug("FunctionCall.generate(): %s" % (repr(self)))
for argument in self.arguments:
self.emit(Op.PARAM, argument.reduce())
- self.emit(Op.CALL, self.ident, len(self.arguments))
+ self.emit(Op.CALL, self.ident, 0)
def reduce(self):
- self.debug("FunctionCall.reduce(): %s" % self)
+ self.debug("FunctionCall.reduce(): %s" % repr(self))
for argument in self.arguments:
self.emit(Op.PARAM, argument.reduce())
r = Register().new()
@@ -176,21 +179,11 @@ class FunctionCall(Statement):
def __repr__(self):
return "<FunctionCall for %s at line %d>" % (self.ident, self.lineno)
-class Expression(Node):
+class Operation(Expression):
def __init__(self, op, lineno = -1):
self.op = op
self.lineno = -1
- def __str__(self):
- return str(self.op)
-
- def __repr__(self):
- return "<Expression at line %d>" % self.lineno
-
-class Operation(Expression):
- def __init__(self, op, lineno = -1):
- super(Operation, self).__init__(op, lineno)
-
def __repr__(self):
return "<Operation at line %d>" % self.lineno
@@ -201,7 +194,7 @@ class UnaryExpression(Operation):
self.right = right
def reduce(self):
- self.debug("UnaryExpression.generate(): op = %s, right = %s" % (self.op, self.right))
+ self.debug("UnaryExpression.generate(): %s" % repr(self))
op = {
"!": Op.NOT,
"-": Op.MINUS,
@@ -210,9 +203,6 @@ class UnaryExpression(Operation):
self.emit(op, r)
return r
- def __str__(self):
- return "%s %s" % (str(self.op), str(self.right))
-
def __repr__(self):
return "<UnaryExpression: %s(%s) at line %d>" % (self.op, self.right, self.lineno)
@@ -224,7 +214,7 @@ class BinaryExpression(Operation):
self.right = right
def reduce(self):
- self.debug("BinaryExpression.reduce(): left = %s, op = %s, right = %s" % (self.left, self.op, self.right))
+ self.debug("BinaryExpression.reduce(): %s" % repr(self))
arith_op = {
"+": Op.ADD,
"-": Op.SUB,
@@ -254,50 +244,33 @@ class BinaryExpression(Operation):
return r
- def __str__(self):
- return "%s %s %s" % (str(self.left), str(self.op), str(self.right))
-
def __repr__(self):
return "<BinaryExpression: %s(%s, %s) at line %d>" % (self.op, self.left, self.right, self.lineno)
class Variable(Expression):
def __init__(self, name, lineno = -1):
- super(Variable, self).__init__(name, lineno)
+ self.name = name
+ self.lineno = lineno
def reduce(self):
- self.debug("Variable.reduce(): name = %s" % self.op)
+ self.debug("Variable.reduce(): %s" % repr(self))
r = Register().new()
- self.emit(Op.LOAD, self.op, r)
+ self.emit(Op.LOAD, self.name, r)
return r
def __repr__(self):
- return "<Variable: %s at line %d>" %(self.op, self.lineno)
+ return "<Variable: %s at line %d>" %(self.name, self.lineno)
class Constant(Expression):
def __init__(self, value, lineno = -1):
- super(Constant, self).__init__(value, lineno)
-
- def jumping(self, true_label, false_label):
- if self.op and true_label != 0:
- self.emit(Op.JMP, "L%d" % true_label)
- elif not self.op and false_label != 0:
- self.emit(Op.JMP, "L%d" % false_label)
+ self.value = value
+ self.lineno = lineno
def reduce(self):
- self.debug("Constant.reduce(): value = %s" % self.op)
+ self.debug("Constant.reduce(): %s" % repr(self))
r = Register().new()
- self.emit(Op.MOV, r, self.op)
+ self.emit(Op.MOV, r, self.value)
return r
def __repr__(self):
- return "<Constant: %d at line %d>" %(self.op, self.lineno)
-
-class Temporary(Expression):
- count = 0
-
- def __init__(self):
- self.number = Temporary.count
- Temporary.count += 1
-
- def __str__(self):
- return "r%d" % self.number
+ return "<Constant: %d at line %d>" %(self.value, self.lineno)