annotate python/ppci/compilers/kscompiler.py @ 100:fe145e42259d

Fixes after movage
author windel
date Mon, 24 Dec 2012 16:35:22 +0100
parents 6efbeb903777
children af0d7913677a
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 import hashlib
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 # Import compiler components:
100
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
3 from ..frontends.ks import KsParser
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
4 #from .codegenerator import CodeGenerator
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
5 #from .nodes import ExportedSymbol
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
6 #from .errors import CompilerException
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
7 #from .. import version
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
8 version='0.0.1'
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9
99
windel
parents: 96
diff changeset
10 class KsCompiler:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 def __repr__(self):
99
windel
parents: 96
diff changeset
12 return 'LCFOS compiler {0}'.format(version)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 def generateSignature(self, src):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 return hashlib.md5(bytes(src,encoding='ascii')).hexdigest()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 def compilesource(self, src):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 """ Front end that handles the stages: """
6
1784af239df4 Added error list
windel
parents: 5
diff changeset
19 self.errorlist = []
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
20 # Pass 1: parsing and type checking
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 tokens = lexer.tokenize(src) # Lexical stage
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
22 p = Parser(tokens)
7
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
23 try:
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
24 ast = p.parseModule() # Parse a module
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
25 except CompilerException as e:
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
26 p.errorlist.append( (e.row, e.col, e.msg) )
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
27 if len(p.errorlist) > 0:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
28 self.errorlist = p.errorlist
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
29 return
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
30 # Pass 2: code generation
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 CodeGenerator().generatecode(ast)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 # Attach a signature:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 ast.signature = self.generateSignature(src)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 # Generate exported symbols:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 ast.exports = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 for proc in ast.procs:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 if proc.public:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 sym = ExportedSymbol(proc.name, proc.typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 sym.imageoffset = proc.entrypoint
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 ast.exports.append(sym)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 return ast
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
43 def compileProject(self, project):
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
44 mods = []
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
45 for fname in project.files:
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
46 print('Compiling {0}...'.format(fname))
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
47 source = project.loadProjectFile(fname)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
48 mod = self.compilesource(source)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
49 mods.append(mod)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
50 return mods
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
51