1
|
1 import hashlib
|
|
2 # Import compiler components:
|
100
|
3 from ..frontends.ks import KsParser
|
|
4 #from .codegenerator import CodeGenerator
|
|
5 #from .nodes import ExportedSymbol
|
101
|
6 from ..core.errors import CompilerException
|
|
7 from ..core import version
|
100
|
8 #from .. import version
|
1
|
9
|
99
|
10 class KsCompiler:
|
1
|
11 def __repr__(self):
|
99
|
12 return 'LCFOS compiler {0}'.format(version)
|
1
|
13
|
|
14 def generateSignature(self, src):
|
|
15 return hashlib.md5(bytes(src,encoding='ascii')).hexdigest()
|
|
16
|
|
17 def compilesource(self, src):
|
|
18 """ Front end that handles the stages: """
|
6
|
19 self.errorlist = []
|
5
|
20 # Pass 1: parsing and type checking
|
101
|
21 p = KsParser(src)
|
7
|
22 try:
|
101
|
23 ast = p.parseModule() # Parse a module into an AST
|
7
|
24 except CompilerException as e:
|
101
|
25 print(e)
|
7
|
26 p.errorlist.append( (e.row, e.col, e.msg) )
|
5
|
27 if len(p.errorlist) > 0:
|
|
28 self.errorlist = p.errorlist
|
|
29 return
|
101
|
30 # pass 2: Optimization
|
|
31 # TODO: implement optimization
|
|
32
|
|
33 # Pass 3: code generation
|
1
|
34 CodeGenerator().generatecode(ast)
|
|
35 # Attach a signature:
|
|
36 ast.signature = self.generateSignature(src)
|
|
37 # Generate exported symbols:
|
|
38 ast.exports = []
|
|
39 for proc in ast.procs:
|
|
40 if proc.public:
|
|
41 sym = ExportedSymbol(proc.name, proc.typ)
|
|
42 sym.imageoffset = proc.entrypoint
|
|
43 ast.exports.append(sym)
|
|
44 return ast
|
|
45
|
15
|
46 def compileProject(self, project):
|
|
47 mods = []
|
|
48 for fname in project.files:
|
|
49 print('Compiling {0}...'.format(fname))
|
|
50 source = project.loadProjectFile(fname)
|
|
51 mod = self.compilesource(source)
|
|
52 mods.append(mod)
|
|
53 return mods
|
|
54
|