diff python/c3/parser.py @ 280:02385f62f250

Rework from str interface to Instruction interface
author Windel Bouwman
date Sat, 02 Nov 2013 10:03:26 +0100
parents e64bae57cda8
children 05184b95fa16
line wrap: on
line diff
--- a/python/c3/parser.py	Sat Oct 12 09:56:23 2013 +0200
+++ b/python/c3/parser.py	Sat Nov 02 10:03:26 2013 +0100
@@ -139,31 +139,31 @@
         self.Consume(';')
 
     def parseConstDef(self):
-      self.Consume('const')
-      t = self.parseTypeSpec()
-      def parseConst():
-         name = self.Consume('ID')
-         self.Consume('=')
-         val = self.Expression()
-         c = astnodes.Constant(name.val, t, val)
-         c.loc = name.loc
-      parseConst()
-      while self.hasConsumed(','):
-         parseConst()
-      self.Consume(';')
+        self.Consume('const')
+        t = self.parseTypeSpec()
+        def parseConst():
+            name = self.Consume('ID')
+            self.Consume('=')
+            val = self.Expression()
+            c = astnodes.Constant(name.val, t, val)
+            c.loc = name.loc
+        parseConst()
+        while self.hasConsumed(','):
+            parseConst()
+        self.Consume(';')
       
     # Procedures
     def parseFunctionDef(self):
-      loc = self.Consume('function').loc
-      returntype = self.parseTypeSpec()
-      fname = self.Consume('ID').val
-      f = astnodes.Function(fname, loc)
-      self.addDeclaration(f)
-      savePart = self.currentPart
-      self.currentPart = f
-      self.Consume('(')
-      parameters = []
-      if not self.hasConsumed(')'):
+        loc = self.Consume('function').loc
+        returntype = self.parseTypeSpec()
+        fname = self.Consume('ID').val
+        f = astnodes.Function(fname, loc)
+        self.addDeclaration(f)
+        savePart = self.currentPart
+        self.currentPart = f
+        self.Consume('(')
+        parameters = []
+        if not self.hasConsumed(')'):
             def parseParameter():
                 typ = self.parseTypeSpec()
                 name = self.Consume('ID')
@@ -175,10 +175,10 @@
             while self.hasConsumed(','):
                 parseParameter()
             self.Consume(')')
-      paramtypes = [p.typ for p in parameters]
-      f.typ = astnodes.FunctionType(paramtypes, returntype)
-      f.body = self.parseCompoundStatement()
-      self.currentPart = savePart
+        paramtypes = [p.typ for p in parameters]
+        f.typ = astnodes.FunctionType(paramtypes, returntype)
+        f.body = self.parseCompoundStatement()
+        self.currentPart = savePart
 
     # Statements:
 
@@ -204,7 +204,10 @@
 
     def parseReturnStatement(self):
         loc = self.Consume('return').loc
-        expr = self.Expression()
+        if self.Peak == ';':
+            expr = astnodes.Literal(0, loc)
+        else:
+            expr = self.Expression()
         self.Consume(';')
         return astnodes.ReturnStatement(expr, loc)