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
|
|
6 #from .errors import CompilerException
|
|
7 #from .. import version
|
|
8 version='0.0.1'
|
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
|
1
|
21 tokens = lexer.tokenize(src) # Lexical stage
|
5
|
22 p = Parser(tokens)
|
7
|
23 try:
|
|
24 ast = p.parseModule() # Parse a module
|
|
25 except CompilerException as e:
|
|
26 p.errorlist.append( (e.row, e.col, e.msg) )
|
5
|
27 if len(p.errorlist) > 0:
|
|
28 self.errorlist = p.errorlist
|
|
29 return
|
|
30 # Pass 2: code generation
|
1
|
31 CodeGenerator().generatecode(ast)
|
|
32 # Attach a signature:
|
|
33 ast.signature = self.generateSignature(src)
|
|
34 # Generate exported symbols:
|
|
35 ast.exports = []
|
|
36 for proc in ast.procs:
|
|
37 if proc.public:
|
|
38 sym = ExportedSymbol(proc.name, proc.typ)
|
|
39 sym.imageoffset = proc.entrypoint
|
|
40 ast.exports.append(sym)
|
|
41 return ast
|
|
42
|
15
|
43 def compileProject(self, project):
|
|
44 mods = []
|
|
45 for fname in project.files:
|
|
46 print('Compiling {0}...'.format(fname))
|
|
47 source = project.loadProjectFile(fname)
|
|
48 mod = self.compilesource(source)
|
|
49 mods.append(mod)
|
|
50 return mods
|
|
51
|