Mercurial > lcfOS
diff applications/ide/compiler/compiler.py @ 39:600f48b74799
Move ide
author | windel |
---|---|
date | Fri, 03 Feb 2012 18:40:43 +0100 |
parents | ide/compiler/compiler.py@de004f808e56 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/applications/ide/compiler/compiler.py Fri Feb 03 18:40:43 2012 +0100 @@ -0,0 +1,53 @@ +import hashlib +# Import compiler components: +from . import lexer +from .parser import Parser +from .codegenerator import CodeGenerator +from .nodes import ExportedSymbol +from .errors import CompilerException + +class Compiler: + versie = '0.9.3' + + def __repr__(self): + return 'LCFOS compiler {0}'.format(self.versie) + + 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 + tokens = lexer.tokenize(src) # Lexical stage + p = Parser(tokens) + try: + ast = p.parseModule() # Parse a module + except CompilerException as e: + p.errorlist.append( (e.row, e.col, e.msg) ) + if len(p.errorlist) > 0: + self.errorlist = p.errorlist + return + # Pass 2: code generation + CodeGenerator().generatecode(ast) + # Attach a signature: + ast.signature = self.generateSignature(src) + # Generate exported symbols: + ast.exports = [] + for proc in ast.procs: + if proc.public: + sym = ExportedSymbol(proc.name, proc.typ) + sym.imageoffset = proc.entrypoint + ast.exports.append(sym) + 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 + +