changeset 169:ee0d30533dae

Added more tests and improved the diagnostic update
author Windel Bouwman
date Sat, 23 Mar 2013 18:34:41 +0100
parents 49f1ab80d040
children 4348da5ca307
files python/astviewer.py python/c3/codegenerator.py python/c3/parser.py python/c3/semantics.py python/c3/visitor.py python/codeedit.py python/ide.py python/testc3.py
diffstat 8 files changed, 83 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/python/astviewer.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/astviewer.py	Sat Mar 23 18:34:41 2013 +0100
@@ -8,9 +8,11 @@
       self.variableIco = QIcon(QPixmap('variableicon.png').scaled(32, 32))
       self.visitor = Visitor(self.p1, self.p2)
       self.model = QStandardItemModel()
+      self.model.setHorizontalHeaderLabels(['Object', 'Type'])
    def build(self, pkg):
-      self.model.clear()
-      self.model.setHorizontalHeaderLabels(['Object', 'Type'])
+      #self.model.clear()
+      c = self.model.rowCount()
+      self.model.removeRows(0, c)
       self.curItem = self.model.invisibleRootItem()
       self.visitor.visit(pkg)
    def p1(self, node):
--- a/python/c3/codegenerator.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/c3/codegenerator.py	Sat Mar 23 18:34:41 2013 +0100
@@ -73,7 +73,7 @@
          bb.Instructions.append(ins)
          return tmp
       else:
-         print('Unknown binop')
+         print('Unknown binop {0}'.format(code))
          bb.Instructions.append(ir.BinaryOperator('unk2', code.op, a, b))
          return 'unk2'
    elif type(code) is astnodes.Constant:
@@ -84,6 +84,10 @@
       tmp = unique()
       ins = ir.LoadInstruction(tmp, code.target.name)
       return tmp
+   elif type(code) is astnodes.Literal:
+      tmp = unique()
+      ins = ir.LoadInstruction(tmp, code.val)
+      return tmp
    else:
       print('Unknown expr:', code)
       return 'unk'
--- a/python/c3/parser.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/c3/parser.py	Sat Mar 23 18:34:41 2013 +0100
@@ -13,6 +13,7 @@
       self.diag = diag
    def parseSource(self, source):
       self.initLex(source)
+      self.sema.reinit()
       try:
          self.parsePackage()
       except CompilerError as e:
@@ -210,13 +211,6 @@
    # Parsing expressions:
    def parseExpression(self):
       return self.parseBinopRhs(self.parsePrimary(), 0)
-      # TODO: use this error handling:
-      try:
-         return self.parseBinopRhs(self.parsePrimary(), 0)
-      except CompilerError as e:
-         self.diag.addDiag(e)
-         self.skipToSemi(';')
-         return astnodes.Literal(0)
    def parsePrimary(self):
       if self.hasConsumed('('):
          e = self.parseExpression()
--- a/python/c3/semantics.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/c3/semantics.py	Sat Mar 23 18:34:41 2013 +0100
@@ -5,6 +5,10 @@
    """ This class constructs the AST from parser input """
    def __init__(self, diag):
       self.diag = diag
+   def reinit(self):
+      # Set mod to empty package:
+      self.mod = astnodes.Package('unnamed')
+      self.mod.scope = Scope(topScope)
    def addSymbol(self, s):
       if self.curScope.hasSymbol(s.name):
          msg = 'Redefinition of {0}'.format(s.name)
--- a/python/c3/visitor.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/c3/visitor.py	Sat Mar 23 18:34:41 2013 +0100
@@ -41,7 +41,7 @@
          # Those nodes do not have child nodes.
          pass
       else:
-         print('UNK visit', node)
+         print('UNK visit "{0}"'.format(node))
       self.f2(node)
 
       
--- a/python/codeedit.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/codeedit.py	Sat Mar 23 18:34:41 2013 +0100
@@ -17,7 +17,7 @@
    def __init__(self, scrollArea):
       super().__init__(scrollArea)
       self.scrollArea = scrollArea
-      self.setFont(QFont('Courier', 16))
+      self.setFont(QFont('Courier', 12))
       self.setFocusPolicy(Qt.StrongFocus)
       # TODO: only beam cursor in text area..
       self.setCursor(Qt.IBeamCursor)
--- a/python/ide.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/ide.py	Sat Mar 23 18:34:41 2013 +0100
@@ -34,9 +34,13 @@
       self.setModel(model)
       self.clicked.connect(self.itemSelected)
       self.errorIcon = QIcon('error.png')
+      self.model = QStandardItemModel()
+      self.model.setHorizontalHeaderLabels(['Message', 'Row', 'Column'])
+      self.setModel(self.model)
+
    def setErrorList(self, errorlist):
-      model = QStandardItemModel()
-      model.setHorizontalHeaderLabels(['Message', 'Row', 'Column'])
+      c = self.model.rowCount()
+      self.model.removeRows(0, c)
       for e in errorlist:
          item = QStandardItem(self.errorIcon, str(e.msg))
          item.setData(e)
@@ -44,13 +48,11 @@
          irow.setData(e)
          icol = QStandardItem(str(e.loc.col))
          icol.setData(e)
-         model.appendRow([item, irow, icol])
-      self.setModel(model)
+         self.model.appendRow([item, irow, icol])
    def itemSelected(self, index):
       if not index.isValid():
          return
-      model = self.model()
-      item = model.itemFromIndex(index)
+      item = self.model.itemFromIndex(index)
       err = item.data()
       self.sigErrorSelected.emit(err)
 
--- a/python/testc3.py	Fri Mar 22 19:09:38 2013 +0100
+++ b/python/testc3.py	Sat Mar 23 18:34:41 2013 +0100
@@ -81,6 +81,8 @@
    def setUp(self):
       self.diag = ppci.DiagnosticsManager()
       self.builder = c3.Builder(self.diag)
+   def testSrc(self):
+      self.builder.build(testsrc)
    def testFunctArgs(self):
       snippet = """
          package testargs;
@@ -117,6 +119,63 @@
       assert self.diag.diags[1].loc.row == 9
       assert self.diag.diags[2].loc.row == 10
       assert ir == None
+   def testEmpty(self):
+      snippet = """
+      package A
+      """
+      self.builder.build(snippet)
+   def testEmpty2(self):
+      snippet = ""
+      self.builder.build(snippet)
+   def testRedefine(self):
+      snippet = """
+      package test;
+      var int a;
+      var int b;
+      var int a;
+      """
+      self.diag.clear()
+      self.builder.build(snippet)
+      assert len(self.diag.diags) == 1
+      assert self.diag.diags[0].loc.row == 5
+   def testWhile(self):
+      snippet = """
+      package tstwhile;
+      var int a;
+      function void t()
+      {
+         var int i = 0;
+         while (i < 1054)
+         {
+            i = i + 3;
+            a = a + i
+         }
+      }
+      """
+      self.builder.build(snippet)
+   def testIf(self):
+      snippet = """
+      package tstIFF;
+      var int a;
+      function void t(int b)
+      {
+         a = 2;
+         if (a > b)
+         {
+            if (a > 1337)
+            {
+               b = 2;
+            }
+         }
+         else
+         {
+            b = 1;
+         }
+
+         return b;
+      }
+      """
+      self.builder.build(snippet)
 
 if __name__ == '__main__':
    do()