annotate applications/ide/compiler/compiler.py @ 52:67056de5da0f

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