changeset 152:b73bc14a3aa3

Light coupling ide and c3 frontend
author Windel Bouwman
date Sat, 02 Mar 2013 09:56:12 +0100
parents afc8c0207984
children e05b2b216bfc
files python/c3/lexer.py python/c3/parser.py python/c3/semantics.py python/c3/typecheck.py python/error.png python/ide.py python/ppci/__init__.py python/ppci/errors.py python/testc3.py
diffstat 9 files changed, 51 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/python/c3/lexer.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/c3/lexer.py	Sat Mar 02 09:56:12 2013 +0100
@@ -1,5 +1,6 @@
 import collections, re
-from ppci.errors import CompilerException, SourceLocation
+
+from ppci import CompilerError, SourceLocation
 
 """
  Lexical analyzer part. Splits the input character stream into tokens.
@@ -68,6 +69,6 @@
      if pos != len(s):
        col = pos - line_start
        pos = line
-       raise CompilerException('Unexpected character {0}'.format(s[pos]), pos)
+       raise CompilerError('Unexpected character {0}'.format(s[pos]), pos)
      yield Token('END', '', line)
 
--- a/python/c3/parser.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/c3/parser.py	Sat Mar 02 09:56:12 2013 +0100
@@ -1,5 +1,5 @@
 from . import astnodes, lexer, semantics
-from ppci.errors import CompilerException, SourceLocation
+from ppci import CompilerError
 
 # binop precedence for expressions:
 binopPrecs = {'or': 5, 'and': 10, \
@@ -15,10 +15,10 @@
       self.initLex(source)
       try:
          self.parsePackage()
-      except CompilerException as e:
-         self.diag.diag(e)
+      except CompilerError as e:
+         self.diag.addDiag(e)
    def Error(self, msg):
-      raise CompilerException(msg, self.token.loc)
+      raise CompilerError(msg, self.token.loc)
    # Lexer helpers:
    def Consume(self, typ):
       if self.Peak == typ:
--- a/python/c3/semantics.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/c3/semantics.py	Sat Mar 02 09:56:12 2013 +0100
@@ -1,6 +1,5 @@
 from . import astnodes
 from .scope import Scope, topScope
-from ppci.errors import CompilerException
 
 class Semantics:
    """ This class constructs the AST from parser input """
@@ -9,7 +8,7 @@
    def addSymbol(self, s):
       if self.curScope.hasSymbol(s.name):
          msg = 'Redefinition of {0}'.format(s.name)
-         self.diag.diag(CompilerException(msg, s.loc))
+         self.diag.error(msg, s.loc)
       else:
          self.curScope.addSymbol(s)
    def handlePackage(self, name, loc):
--- a/python/c3/typecheck.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/c3/typecheck.py	Sat Mar 02 09:56:12 2013 +0100
@@ -4,14 +4,11 @@
 from .astnodes import IfStatement, WhileStatement, ReturnStatement
 from .astnodes import FunctionType, BaseType
 from . import astnodes
-from ppci.errors import CompilerException
 from .scope import topScope
 
 class TypeChecker:
    def __init__(self, diag):
       self.diag = diag
-   def err(self, msg, loc):
-      self.diag.diag(CompilerException(msg, loc))
    def checkPackage(self, pkg):
       for s in pkg.scope:
          self.check(s)
@@ -20,7 +17,7 @@
          return d.scope.getSymbol(d.tname)
       else:
          msg = 'Cannot resolve name {0}'.format(d.tname)
-         self.err(msg, d.loc)
+         self.diag.error(msg, d.loc)
    def check(self, sym):
       if type(sym) is Variable:
          if type(sym.typ) is Designator:
Binary file python/error.png has changed
--- a/python/ide.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/ide.py	Sat Mar 02 09:56:12 2013 +0100
@@ -12,6 +12,7 @@
 from astviewer import AstViewer
 from codeeditor import CodeEdit
 stutil = __import__('st-util')
+import testc3
 
 lcfospng = base64.decodestring(b'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A\n/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sJEhMKBk7B678AAAA/SURBVFjD\n7dbBCQAgDATBi9h/y7EFA4Kf2QLCwH1S6XQu6sqoujublc8BAAAAAAAAAAB8B+zXT6YJAAAAAKYd\nWSgFQNUyijIAAAAASUVORK5CYII=\n')
 
@@ -33,8 +34,7 @@
    def setErrorList(self, errorlist):
       model = QStandardItemModel()
       for e in errorlist:
-         row, col, msg = e
-         item = QStandardItem(str(msg))
+         item = QStandardItem(str(e))
          item.setData(e)
          model.appendRow(item)
       self.setModel(model)
@@ -193,6 +193,11 @@
     self.settings = QSettings('windelsoft', 'lcfoside')
     self.loadSettings()
 
+    ce = self.newFile()
+    ce.setSource(testc3.testsrc)
+
+    self.diag = ppci.DiagnosticsManager()
+
   # File handling:
   def newProject(self):
      filename = QFileDialog.getSaveFileName(self, \
@@ -203,7 +208,10 @@
         self.project.save()
 
   def newFile(self):
-     self.loadFile('main.ks')
+     ce = CodeEdit()
+     self.mdiArea.addSubWindow(ce)
+     ce.show()
+     return ce
   def openFile(self):
      filename = QFileDialog.getOpenFileName(self, "Open K# file...", "*.ks", "K# source files (*.ks)")
      if filename:
@@ -281,9 +289,6 @@
         self.restoreState(self.settings.value('mainwindowstate'))
      if self.settings.contains('mainwindowgeometry'):
         self.restoreGeometry(self.settings.value('mainwindowgeometry'))
-     if self.settings.contains('openedproject'):
-        projectfile = self.settings.value('openedproject')
-        #self.loadProject(projectfile)
      if self.settings.contains('lastfile'):
         self.loadFile(self.settings.value('lastfile'))
   
@@ -297,6 +302,10 @@
      if ac:
         if ac.filename:
            self.settings.setValue('lastfile', ac.filename)
+        else:
+           self.settings.remove('lastfile')
+     else:
+        self.settings.remove('lastfile')
      ev.accept()
 
   # Error handling:
@@ -311,34 +320,38 @@
          ce.clearErrors()
 
   def errorSelected(self, err):
-     row, col, msg = err
      ce = self.activeMdiChild()
      if not ce:
         return
-     ce.highlightErrorLocation(row, col)
+     ce.highlightErrorLocation(err.loc.row, err.loc.col)
 
   # Project loading:
 
   # Build recepy:
   def buildFile(self):
      ce = self.activeMdiChild()
+     print('BUILD file')
      if ce:
         source = ce.source
         self.buildOutput.clear()
         self.buildOutput.append(str(self.compiler))
-        ast = self.compiler.compilesource(source)
+        ast = testc3.c3compile(source, self.diag)
+        #ast = self.compiler.compilesource(source)
         self.astViewer.setAst(ast)
         self.buildOutput.append("Done!")
   def buildProject(self):
      """ Build project """
+     print('BUILD project')
      self.buildOutput.clear()
+     self.diag.diags.clear()
      self.buildOutput.append(str(self.compiler))
-     mods = self.compiler.compileProject(self.project)
+     self.buildFile()
+     #mods = self.compiler.compileProject(self.project)
 
-     #self.builderrors.setErrorList(self.compiler.errorlist)
-     self.astViewer.setAst(mods[0])
-     #for err in self.compiler.errorlist:
-     #   self.buildOutput.append(str(err))
+     self.builderrors.setErrorList(self.diag.diags)
+     #self.astViewer.setAst(mods[0])
+     for err in self.diag.diags:
+        self.buildOutput.append(str(err))
      self.buildOutput.append("Done!")
 
 if __name__ == '__main__':
--- a/python/ppci/__init__.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/ppci/__init__.py	Sat Mar 02 09:56:12 2013 +0100
@@ -9,3 +9,7 @@
    print("Needs to be run in python version 3.x")
    sys.exit(1)
 
+from .common import SourceLocation, SourceRange
+from .errors import CompilerError, DiagnosticsManager
+from .errors import printError
+
--- a/python/ppci/errors.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/ppci/errors.py	Sat Mar 02 09:56:12 2013 +0100
@@ -3,30 +3,12 @@
    Diagnostic utils
 """
 
-from collections import namedtuple
-
-SourceLocation = namedtuple('SourceLocation', ['row', 'col'])
-SourceRange = namedtuple('SourceRange', ['p1', 'p2'])
-
-class CompilerException(Exception):
+class CompilerError(Exception):
   def __init__(self, msg, loc):
     self.msg = msg
     self.loc = loc
   def __repr__(self):
-     return 'error {0} at {1}'.format(self.msg, self.loc)
-  def __str__(self):
-     return 'error {0} at {1}'.format(self.msg, self.loc)
-
-class ErrorNode:
-   def __init__(self, row, col, msg):
-      self.row, self.col = row,col
-      self.msg = msg
-
-def Error(msg, node=None):
-   if node is None:
-      raise CompilerException(msg)
-   else:
-      raise CompilerException(msg, node.row, node.col)
+     return 'Compilererror {0} at {1}'.format(self.msg, self.loc)
 
 def printError(source, e):
      def printLine(row, txt):
@@ -53,9 +35,11 @@
         for r in range(ro+1, afterrow+1):
           printLine(r, lines[r-1])
 
-class Diagnostics:
+class DiagnosticsManager:
    def __init__(self):
       self.diags = []
-   def diag(self, d):
+   def addDiag(self, d):
       self.diags.append(d)
+   def error(self, msg, loc):
+      self.addDiag(CompilerError(msg, loc))
 
--- a/python/testc3.py	Fri Mar 01 17:13:56 2013 +0100
+++ b/python/testc3.py	Sat Mar 02 09:56:12 2013 +0100
@@ -1,6 +1,4 @@
-import c3
-from ppci.errors import printError, Diagnostics
-import time
+import c3, time, ppci
 
 testsrc = """
 package test;
@@ -50,11 +48,10 @@
    for c in ast.getChildren():
       printAst(c, indent + '  ')
 
-def c3compile(src):
+def c3compile(src, diag):
    print('[0] source:')
    print(src)
    print('[1] parsing')
-   diag = Diagnostics()
    sema = c3.Semantics(diag)
    p = c3.Parser(sema, diag)
    tc = c3.TypeChecker(diag)
@@ -72,7 +69,7 @@
 
    for d in diag.diags:
       print('ERROR:')
-      printError(testsrc, d)
+      ppci.printError(testsrc, d)
    print('[2] ast:')
    printAst(sema.mod)
 
@@ -83,7 +80,8 @@
       print('Not generating code')
    
 def do():
-   c3compile(testsrc)
+   diag = ppci.DiagnosticsManager()
+   c3compile(testsrc, diag)
 
 do()