annotate ide/compiler/compiler.py @ 7:2db4d2b362e6

Added xml project
author windel
date Sat, 15 Oct 2011 10:03:21 +0200
parents 1784af239df4
children de004f808e56
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
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 return ast
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44