view python/ppci/compilers/kscompiler.py @ 102:63937c8d1478

Fixes and start with ir generator
author windel
date Tue, 25 Dec 2012 11:11:24 +0100
parents af0d7913677a
children ed230e947dc6
line wrap: on
line source

import hashlib
# Import compiler components:
from ..frontends.ks import KsParser
from ..frontends.ks import KsIrGenerator
#from .codegenerator import CodeGenerator
#from .nodes import ExportedSymbol
from ..core.errors import CompilerException
from ..core import version
#from .. import version

class KsCompiler:
   def __repr__(self):
      return 'LCFOS compiler {0}'.format(version)

   def generateSignature(self, src):
      return hashlib.md5(bytes(src,encoding='ascii')).hexdigest()

   def compilesource(self, src):
      """ Front end that handles the stages: """
      self.errorlist = []
      # Pass 1: parsing and type checking
      p = KsParser(src)
      try:
         ast = p.parseModule() # Parse a module into an AST
      except CompilerException as e:
         print(e)
         p.errorlist.append( (e.row, e.col, e.msg) )
      if len(p.errorlist) > 0:
         self.errorlist = p.errorlist
         return
      # pass 2: Optimization
      # TODO: implement optimization

      # Pass 3: code generation
      ir = KsIrGenerator().generateIr(ast)
      # Attach a signature:
      ast.signature = self.generateSignature(src)
      # Generate exported symbols:
      return ast

   def compileProject(self, project):
      mods = []
      for fname in project.files:
         print('Compiling {0}...'.format(fname))
         source = project.loadProjectFile(fname)
         mod = self.compilesource(source)
         mods.append(mod)
      return mods