diff python/c3/astnodes.py @ 220:3f6c30a5d234

Major change in expression parsing to enable pointers and structs
author Windel Bouwman
date Sat, 06 Jul 2013 21:32:20 +0200
parents 8b2e5f3cd579
children 848c4b15fd0b
line wrap: on
line diff
--- a/python/c3/astnodes.py	Sat Jul 06 12:38:09 2013 +0200
+++ b/python/c3/astnodes.py	Sat Jul 06 21:32:20 2013 +0200
@@ -5,6 +5,8 @@
 Finally code is generated from it.
 """
 
+from ppci import SourceLocation
+
 class Node:
    pass 
 
@@ -58,6 +60,8 @@
 class PointerType(Type):
     def __init__(self, ptype):
         self.ptype = ptype
+    def __repr__(self):
+        return '({}*)'.format(self.ptype)
 
 class StructureType(Type):
     def __init__(self, mems):
@@ -112,23 +116,33 @@
         return 'Func {}'.format(self.name)
 
 # Operations / Expressions:
-class Unop(Node):
-   def __init__(self, a, op):
-      self.a = a
-      self.op = op 
-   def __repr__(self):
+class Expression(Node):
+    pass
+
+class Unop(Expression):
+    def __init__(self, op, a, loc):
+        assert isinstance(a, Expression)
+        assert isinstance(op, str)
+        self.a = a
+        self.op = op 
+        self.loc = loc
+    def __repr__(self):
       return 'UNOP {}'.format(self.op)
 
-class Binop(Node):
-   def __init__(self, a, op, b, loc):
-      self.a = a
-      self.b = b
-      self.op = op # Operation: '+', '-', '*', '/', 'mod'
-      self.loc = loc
-   def __repr__(self):
-      return 'BINOP {}'.format(self.op)
+class Binop(Expression):
+    def __init__(self, a, op, b, loc):
+        assert isinstance(a, Expression), type(a)
+        assert isinstance(b, Expression)
+        assert isinstance(op, str)
+        self.a = a
+        self.b = b
+        self.op = op # Operation: '+', '-', '*', '/', 'mod'
+        self.loc = loc
 
-class VariableUse(Node):
+    def __repr__(self):
+        return 'BINOP {}'.format(self.op)
+
+class VariableUse(Expression):
    def __init__(self, target, loc):
       self.target = target
       self.loc = loc
@@ -136,40 +150,14 @@
       nm = self.target.name if hasattr(self.target, 'name') else ''
       return 'VAR USE {}'.format(nm)
 
-class Literal(Node):
+class Literal(Expression):
    def __init__(self, val, loc):
       self.val = val
       self.loc = loc
    def __repr__(self):
       return 'LITERAL {}'.format(self.val)
 
-
-# Statements
-class CompoundStatement(Node):
-   def __init__(self, statements):
-      self.statements = statements
-   def __repr__(self):
-      return 'COMPOUND STATEMENT'
-
-class EmptyStatement(Node):
-   def __repr__(self):
-      return 'NOP'
-
-class ReturnStatement(Node):
-   def __init__(self, expr):
-      self.expr = expr
-   def __repr__(self):
-      return 'RETURN STATEMENT'
-
-class Assignment(Node):
-   def __init__(self, lval, rval, loc):
-      self.lval = lval
-      self.rval = rval
-      self.loc = loc
-   def __repr__(self):
-      return 'ASSIGNMENT'
-
-class FunctionCall(Node):
+class FunctionCall(Expression):
   def __init__(self, proc, args, loc):
     self.proc = proc
     self.args = args
@@ -177,7 +165,41 @@
   def __repr__(self):
     return 'CALL {0} '.format(self.proc)
 
-class IfStatement(Node):
+# Statements
+class Statement(Node):
+    pass
+
+class CompoundStatement(Statement):
+    def __init__(self, statements):
+        self.statements = statements
+
+    def __repr__(self):
+        return 'COMPOUND STATEMENT'
+
+class EmptyStatement(Statement):
+   def __repr__(self):
+      return 'NOP'
+
+class ReturnStatement(Statement):
+   def __init__(self, expr, loc):
+      self.expr = expr
+      self.loc = loc
+   def __repr__(self):
+      return 'RETURN STATEMENT'
+
+class Assignment(Node):
+    def __init__(self, lval, rval, loc):
+        assert isinstance(lval, Node)
+        assert isinstance(rval, Node)
+        assert isinstance(loc, SourceLocation)
+        self.lval = lval
+        self.rval = rval
+        self.loc = loc
+
+    def __repr__(self):
+        return 'ASSIGNMENT'
+
+class IfStatement(Statement):
    def __init__(self, condition, truestatement, falsestatement, loc):
       self.condition = condition
       self.truestatement = truestatement
@@ -186,7 +208,7 @@
    def __repr__(self):
       return 'IF-statement'
 
-class WhileStatement(Node):
+class WhileStatement(Statement):
    def __init__(self, condition, statement, loc):
       self.condition = condition
       self.statement = statement