changeset 164:e023d3ce1d63

Fix to loc of assignment
author Windel Bouwman
date Mon, 18 Mar 2013 22:15:57 +0100
parents 8104fc8b5e90
children 598d3888a11c
files python/astviewer.py python/c3/__init__.py python/c3/parser.py python/c3/scope.py python/c3/semantics.py python/c3/typecheck.py python/ide.py
diffstat 7 files changed, 34 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/python/astviewer.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/astviewer.py	Mon Mar 18 22:15:57 2013 +0100
@@ -1,5 +1,6 @@
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
+from c3 import Visitor
 
 def astToNamedElement(astNode, parentNode):
    """ Helper to convert and AST tree to NamedElement tree: """
@@ -20,9 +21,10 @@
    def setAst(self, ast):
       """ Create a new model and add all ast elements to it """
       model = QStandardItemModel()
+      print(ast)
       if ast:
          astToNamedElement(ast, model.invisibleRootItem())
-      self.setModel( model )
+      self.setModel(model)
       self.expandAll()
 
    def selectHandler(self, index):
--- a/python/c3/__init__.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/c3/__init__.py	Mon Mar 18 22:15:57 2013 +0100
@@ -6,4 +6,5 @@
 from .analyse import Analyzer
 from .codegenerator import CodeGenerator
 from .astprinter import AstPrinter
+from .visitor import Visitor
 
--- a/python/c3/parser.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/c3/parser.py	Mon Mar 18 22:15:57 2013 +0100
@@ -127,10 +127,10 @@
    # Statements:
    def parseAssignment(self, lval):
       lval = self.sema.actOnVariableUse(lval)
-      self.Consume('=')
+      loc = self.Consume('=').loc
       rval = self.parseExpression()
       self.Consume(';')
-      return astnodes.Assignment(lval, rval)
+      return self.sema.actOnAssignment(lval, rval, loc)
 
    def parseProcedureCall(self, procedure):
       self.Consume('(')
--- a/python/c3/scope.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/c3/scope.py	Mon Mar 18 22:15:57 2013 +0100
@@ -6,7 +6,21 @@
       self.symbols = {}
       self.parent = parent
    def __iter__(self):
-      return iter(self.symbols.values())
+      # Iterate in a deterministic manner:
+      return iter(self.Constants + self.Variables + self.Functions)
+   @property
+   def Syms(self):
+      syms = self.symbols.values()
+      return sorted(syms, key=lambda v: v.name)
+   @property
+   def Constants(self):
+      return [s for s in self.Syms if type(s) is astnodes.Constant]
+   @property
+   def Variables(self):
+      return [s for s in self.Syms if type(s) is astnodes.Variable]
+   @property
+   def Functions(self):
+      return [s for s in self.Syms if type(s) is astnodes.Function]
    def getSymbol(self, name):
       if name in self.symbols:
          return self.symbols[name]
@@ -27,12 +41,14 @@
 intType = astnodes.BaseType('int')
 doubleType = astnodes.BaseType('double')
 voidType = astnodes.BaseType('void')
-boolType = astnodes.BaseType('void')
+boolType = astnodes.BaseType('bool')
+stringType = astnodes.BaseType('string')
+byteType = astnodes.BaseType('byte')
 
 def createBuiltins(scope):
    for tn in ['u64', 'u32', 'u16', 'u8']:
       scope.addSymbol(astnodes.BaseType(tn))
-   for t in [intType, doubleType, voidType, boolType]:
+   for t in [intType, doubleType, voidType, boolType, stringType, byteType]:
       scope.addSymbol(t)
 
 topScope = Scope()
--- a/python/c3/semantics.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/c3/semantics.py	Mon Mar 18 22:15:57 2013 +0100
@@ -56,5 +56,10 @@
       n.loc = loc
       return n
    def actOnVariableUse(self, d):
-      return astnodes.VariableUse(d)
+      vu = astnodes.VariableUse(d)
+      return vu
+   def actOnAssignment(self, lval, rval, loc):
+      a = astnodes.Assignment(lval, rval)
+      a.loc = loc
+      return a
 
--- a/python/c3/typecheck.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/c3/typecheck.py	Mon Mar 18 22:15:57 2013 +0100
@@ -25,7 +25,8 @@
          if not equalTypes(sym.condition.typ, boolType):
             self.diag.error('Condition must be a boolean expression', sym.condition.loc)
       elif type(sym) is Assignment:
-         pass
+         if not equalTypes(sym.lval.typ, sym.rval.typ):
+            self.diag.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
       elif type(sym) is ReturnStatement:
          pass
       elif type(sym) is ProcedureCall:
--- a/python/ide.py	Mon Mar 18 20:13:57 2013 +0100
+++ b/python/ide.py	Mon Mar 18 22:15:57 2013 +0100
@@ -343,6 +343,7 @@
         self.buildOutput.append(str(self.compiler))
         ast = testc3.c3compile(source, self.diag)
         #ast = self.compiler.compilesource(source)
+        print('setting ast', ast)
         self.astViewer.setAst(ast)
         self.buildOutput.append("Done!")
   def buildProject(self):