# HG changeset patch # User Windel Bouwman # Date 1363641357 -3600 # Node ID e023d3ce1d6305a922bfa26ead2224b7d28d7239 # Parent 8104fc8b5e9068f4d8a970a0b8d6b96707fc0c03 Fix to loc of assignment diff -r 8104fc8b5e90 -r e023d3ce1d63 python/astviewer.py --- 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): diff -r 8104fc8b5e90 -r e023d3ce1d63 python/c3/__init__.py --- 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 diff -r 8104fc8b5e90 -r e023d3ce1d63 python/c3/parser.py --- 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('(') diff -r 8104fc8b5e90 -r e023d3ce1d63 python/c3/scope.py --- 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() diff -r 8104fc8b5e90 -r e023d3ce1d63 python/c3/semantics.py --- 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 diff -r 8104fc8b5e90 -r e023d3ce1d63 python/c3/typecheck.py --- 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: diff -r 8104fc8b5e90 -r e023d3ce1d63 python/ide.py --- 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):