Mercurial > lcfOS
comparison python/ppci/compilers/kscompiler.py @ 101:af0d7913677a
Fixes and splitting into 3 stage
author | windel |
---|---|
date | Mon, 24 Dec 2012 17:55:08 +0100 |
parents | fe145e42259d |
children | 63937c8d1478 |
comparison
equal
deleted
inserted
replaced
100:fe145e42259d | 101:af0d7913677a |
---|---|
1 import hashlib | 1 import hashlib |
2 # Import compiler components: | 2 # Import compiler components: |
3 from ..frontends.ks import KsParser | 3 from ..frontends.ks import KsParser |
4 #from .codegenerator import CodeGenerator | 4 #from .codegenerator import CodeGenerator |
5 #from .nodes import ExportedSymbol | 5 #from .nodes import ExportedSymbol |
6 #from .errors import CompilerException | 6 from ..core.errors import CompilerException |
7 from ..core import version | |
7 #from .. import version | 8 #from .. import version |
8 version='0.0.1' | |
9 | 9 |
10 class KsCompiler: | 10 class KsCompiler: |
11 def __repr__(self): | 11 def __repr__(self): |
12 return 'LCFOS compiler {0}'.format(version) | 12 return 'LCFOS compiler {0}'.format(version) |
13 | 13 |
16 | 16 |
17 def compilesource(self, src): | 17 def compilesource(self, src): |
18 """ Front end that handles the stages: """ | 18 """ Front end that handles the stages: """ |
19 self.errorlist = [] | 19 self.errorlist = [] |
20 # Pass 1: parsing and type checking | 20 # Pass 1: parsing and type checking |
21 tokens = lexer.tokenize(src) # Lexical stage | 21 p = KsParser(src) |
22 p = Parser(tokens) | |
23 try: | 22 try: |
24 ast = p.parseModule() # Parse a module | 23 ast = p.parseModule() # Parse a module into an AST |
25 except CompilerException as e: | 24 except CompilerException as e: |
25 print(e) | |
26 p.errorlist.append( (e.row, e.col, e.msg) ) | 26 p.errorlist.append( (e.row, e.col, e.msg) ) |
27 if len(p.errorlist) > 0: | 27 if len(p.errorlist) > 0: |
28 self.errorlist = p.errorlist | 28 self.errorlist = p.errorlist |
29 return | 29 return |
30 # Pass 2: code generation | 30 # pass 2: Optimization |
31 # TODO: implement optimization | |
32 | |
33 # Pass 3: code generation | |
31 CodeGenerator().generatecode(ast) | 34 CodeGenerator().generatecode(ast) |
32 # Attach a signature: | 35 # Attach a signature: |
33 ast.signature = self.generateSignature(src) | 36 ast.signature = self.generateSignature(src) |
34 # Generate exported symbols: | 37 # Generate exported symbols: |
35 ast.exports = [] | 38 ast.exports = [] |