annotate python/libs/compiler/parsergen.py @ 69:60cc36ef5a50

Rework in compiler
author windel
date Sat, 27 Oct 2012 14:31:58 +0200
parents python/libs/compiler/parser.py@32078200cdd6
children
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 This module parses source code into an abstract syntax tree (AST)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5 from .symboltable import SymbolTable
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 from .nodes import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
7 from .errors import CompilerException, Error
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 from .modules import loadModule
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9 from .display import printNode
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 from .builtin import *
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 from . import assembler
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13 class Parser:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 def __init__(self, tokens):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 """ provide the parser with the tokens iterator from the lexer. """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16 self.tokens = tokens
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 self.NextToken()
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
18 self.errorlist = []
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20 def Error(self, msg):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 raise CompilerException(msg, self.token.row, self.token.col)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 # Lexer helpers:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24 def Consume(self, typ=''):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 if self.token.typ == typ or typ == '':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 v = self.token.val
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 self.NextToken()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 return v
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29 else:
7
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
30 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.token.val))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 def hasConsumed(self, typ):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 if self.token.typ == typ:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 self.Consume(typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 return True
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 return False
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 def NextToken(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 self.token = self.tokens.__next__()
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
40 # TODO: store filename in location?
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
41 self.location = (self.token.row, self.token.col)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
43 # Helpers to find location of the error in the code:
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
44 def setLocation(self, obj, location):
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
45 obj.location = location
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
46 return obj
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
47 def getLocation(self):
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
48 return self.location
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
49
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
51 Recursive descent parser functions:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 A set of mutual recursive functions.
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 Starting symbol is the Module.
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
55 def parseModule(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
56 self.imports = []
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
57 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58 self.Consume('module')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59 modname = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
60 self.Consume(';')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
61 mod = Module(modname)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
62
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
63 # Construct a symbol table for this program
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
64 mod.symtable = SymbolTable()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
65 # Add built in types and functions:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
66 for x in [real, integer, boolean, char, chr_func]:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
67 mod.symtable.addSymbol(x)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
68
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
69 self.cst = mod.symtable
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
70 self.parseImportList()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
71
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
72 self.parseDeclarationSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
73 # Procedures only allowed in this scope
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
74 self.parseProcedureDeclarations()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
75
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
76 if self.hasConsumed('begin'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
77 mod.initcode = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
78 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
79 mod.initcode = EmptyStatement()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
80
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
81 self.Consume('end')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
82 endname = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
83 if endname != modname:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
84 self.Error('end denoter must be module name')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
85 self.Consume('.')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
86
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
87 mod.imports = self.imports
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
88 return self.setLocation(mod, loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
89
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
90 # Import part
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
91 def parseImportList(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
92 if self.hasConsumed('import'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
93 self.parseImport()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
94 while self.hasConsumed(','):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
95 self.parseImport()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
96 self.Consume(';')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
97
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
98 def parseImport(self):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
99 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
100 modname = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
101 mod = loadModule(modname)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
102 self.setLocation(mod, loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
103 self.cst.addSymbol(mod)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
104
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
105 # Helper to parse an identifier defenitions
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
106 def parseIdentDef(self):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
107 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
108 name = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
109 ispublic = self.hasConsumed('*')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
110 # Make a node of this thing:
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
111 i = Id(name)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
112 i.ispublic = ispublic
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
113 return self.setLocation(i, loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
114
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
115 def parseIdentList(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
116 ids = [ self.parseIdentDef() ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
117 while self.hasConsumed(','):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
118 ids.append( self.parseIdentDef() )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
119 return ids
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
120
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
121 def parseQualIdent(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
122 """ Parse a qualified identifier """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
123 name = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
124 if self.cst.has(Module, name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
125 modname = name
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
126 mod = self.cst.get(Module, modname)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
127 self.Consume('.')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
128 name = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
129 # Try to find existing imported symbol:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
130 for imp in self.imports:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
131 if imp.modname == modname and imp.name == name:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
132 return imp
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
133 # Try to find the symbol in the modules exports:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
134 for sym in mod.exports:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
135 if sym.name == name:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
136 impsym = ImportedSymbol(modname, name)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
137 impsym.typ = sym.typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
138 impsym.signature = mod.signature
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
139 self.imports.append(impsym)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
140 return impsym
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
141 self.Error("Cannot find symbol {0}".format(name))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
142 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
143 return self.cst.getSymbol(name)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
144
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
145 # Helper to parse a designator
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
146 def parseDesignator(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
147 """ A designator designates an object.
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
148 The base location in memory is denoted by the qualified identifier
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
149 The actual address depends on the selector.
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
150 """
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
151 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
152 obj = self.parseQualIdent()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
153 typ = obj.typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
154 selectors = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
155 while self.token.typ in ['.', '[', '^']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
156 if self.hasConsumed('.'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
157 field = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
158 if typ is PointerType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
159 selectors.append(Deref())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
160 typ = typ.pointedType
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
161 if not type(typ) is RecordType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
162 self.Error("field reference, type not record but {0}".format(typ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
163 typ = typ.fields[field]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
164 selectors.append(Field(field))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
165 elif self.hasConsumed('['):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
166 indexes = self.parseExpressionList()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
167 self.Consume(']')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
168 for idx in indexes:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
169 if not type(typ) is ArrayType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
170 self.Error('Cannot index non array type')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
171 if not isType(idx.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
172 self.Error('Only integer expressions can be used as an index')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
173 selectors.append(Index(idx, typ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
174 typ = typ.elementType
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
175 elif self.hasConsumed('^'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
176 selectors.append(Deref())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
177 typ = typ.pointedType
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
178 return self.setLocation(Designator(obj, selectors, typ), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
179
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
180 # Declaration sequence
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
181 def parseDeclarationSequence(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
182 """ 1. constants, 2. types, 3. variables """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
183 self.parseConstantDeclarations()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
184 self.parseTypeDeclarations()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
185 self.parseVariableDeclarations()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
186
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
187 # Constants
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
188 def evalExpression(self, expr):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
189 if type(expr) is Binop:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
190 a = self.evalExpression(expr.a)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
191 b = self.evalExpression(expr.b)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
192 if expr.op == '+':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
193 return a + b
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
194 elif expr.op == '-':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
195 return a - b
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
196 elif expr.op == '*':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
197 return a * b
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
198 elif expr.op == '/':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
199 return float(a) / float(b)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
200 elif expr.op == 'mod':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
201 return int(a % b)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
202 elif expr.op == 'div':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
203 return int(a / b)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
204 elif expr.op == 'or':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
205 return a or b
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
206 elif expr.op == 'and':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
207 return a and b
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
208 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
209 self.Error('Cannot evaluate expression with {0}'.format(expr.op))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
210 elif type(expr) is Constant:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
211 return expr.value
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
212 elif type(expr) is Designator:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
213 if type(expr.obj) is Constant:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
214 return self.evalExpression(expr.obj)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
215 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
216 self.Error('Cannot evaluate designated object {0}'.format(expr.obj))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
217 elif type(expr) is Unop:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
218 a = self.evalExpression(expr.a)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
219 if expr.op == 'not':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
220 return not a
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
221 elif expr.op == '-':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
222 return -a
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
223 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
224 self.Error('Unimplemented unary operation {0}'.format(expr.op))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
225 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
226 self.Error('Cannot evaluate expression {0}'.format(expr))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
227
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
228 def parseConstExpression(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
229 e = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
230 return self.evalExpression(e), e.typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
231
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
232 def parseConstantDeclarations(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
233 """ Parse const part of a module """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
234 if self.hasConsumed('const'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
235 while self.token.typ == 'ID':
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
236 i = self.parseIdentDef()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
237 self.Consume('=')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
238 constvalue, typ = self.parseConstExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
239 self.Consume(';')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
240 c = Constant(constvalue, typ, name=i.name, public=i.ispublic)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
241 self.setLocation(c, i.location)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
242 self.cst.addSymbol(c)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
243
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
244 # Type system
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
245 def parseTypeDeclarations(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
246 if self.hasConsumed('type'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
247 while self.token.typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
248 typename, export = self.parseIdentDef()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
249 self.Consume('=')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
250 typ = self.parseStructuredType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
251 self.Consume(';')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
252 t = DefinedType(typename, typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
253 self.cst.addSymbol(t)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
254
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
255 def parseType(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
256 if self.token.typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
257 typename = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
258 if self.cst.has(Type, typename):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
259 typ = self.cst.get(Type, typename)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
260 while type(typ) is DefinedType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
261 typ = typ.typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
262 return typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
263 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
264 self.Error('Cannot find type {0}'.format(typename))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
265 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
266 return self.parseStructuredType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
267
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
268 def parseStructuredType(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
269 if self.hasConsumed('array'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
270 dimensions = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
271 dimensions.append( self.parseConstExpression() )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
272 while self.hasConsumed(','):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
273 dimensions.append( self.parseConstExpression() )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
274 self.Consume('of')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
275 arr = self.parseType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
276 for dimension, consttyp in reversed(dimensions):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
277 if not isType(consttyp, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
278 self.Error('array dimension must be an integer type (not {0})'.format(consttyp))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
279 if dimension < 2:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
280 self.Error('array dimension must be bigger than 1 (not {0})'.format(dimension))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
281 arr = ArrayType(dimension, arr)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
282 return arr
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
283 elif self.hasConsumed('record'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
284 fields = {}
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
285 while self.token.typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
286 # parse a fieldlist:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
287 identifiers = self.parseIdentList()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
288 self.Consume(':')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
289 typ = self.parseType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
290 self.Consume(';')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
291 for i in identifiers:
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
292 if i.name in fields.keys():
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
293 self.Error('record field "{0}" multiple defined.'.format(i.name))
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
294 fields[i.name] = typ
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
295 # TODO store this in another way, symbol table?
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
296 self.Consume('end')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
297 return RecordType(fields)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
298 elif self.hasConsumed('pointer'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
299 self.Consume('to')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
300 typ = self.parseType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
301 return PointerType(typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
302 elif self.hasConsumed('procedure'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
303 parameters, returntype = self.parseFormalParameters()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
304 return ProcedureType(parameters, returntype)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
305 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
306 self.Error('Unknown structured type "{0}"'.format(self.token.val))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
307
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
308 # Variable declarations:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
309 def parseVariableDeclarations(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
310 if self.hasConsumed('var'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
311 if self.token.typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
312 while self.token.typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
313 ids = self.parseIdentList()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
314 self.Consume(':')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
315 typename = self.parseType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
316 self.Consume(';')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
317 for i in ids:
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
318 v = Variable(i.name, typename, public=i.ispublic)
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
319 self.setLocation(v, i.location)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
320 self.cst.addSymbol(v)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
321 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
322 self.Error('Expected ID, got'+str(self.token))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
323
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
324 # Procedures
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
325 def parseFPsection(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
326 if self.hasConsumed('const'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
327 kind = 'const'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
328 elif self.hasConsumed('var'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
329 kind = 'var'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
330 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
331 kind = 'value'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
332 names = [ self.Consume('ID') ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
333 while self.hasConsumed(','):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
334 names.append( self.Consume('ID') )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
335 self.Consume(':')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
336 typ = self.parseType()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
337 parameters = [Parameter(kind, name, typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
338 for name in names]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
339 return parameters
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
340
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
341 def parseFormalParameters(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
342 parameters = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
343 self.Consume('(')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
344 if not self.hasConsumed(')'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
345 parameters += self.parseFPsection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
346 while self.hasConsumed(';'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
347 parameters += self.parseFPsection()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
348 self.Consume(')')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
349 if self.hasConsumed(':'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
350 returntype = self.parseQualIdent()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
351 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
352 returntype = void
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
353 return ProcedureType(parameters, returntype)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
354
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
355 def parseProcedureDeclarations(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
356 procedures = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
357 while self.token.typ == 'procedure':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
358 p = self.parseProcedureDeclaration()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
359 procedures.append(p)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
360 self.Consume(';')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
361 return procedures
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
362
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
363 def parseProcedureDeclaration(self):
6
1784af239df4 Added error list
windel
parents: 5
diff changeset
364 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
365 self.Consume('procedure')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
366 i = self.parseIdentDef()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
367 procname = i.name
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
368 proctyp = self.parseFormalParameters()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
369 procsymtable = SymbolTable(parent = self.cst)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
370 self.cst = procsymtable # Switch symbol table:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
371 # Add parameters as variables to symbol table:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
372 for parameter in proctyp.parameters:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
373 vname = parameter.name
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
374 vtyp = parameter.typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
375 if parameter.kind == 'var':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
376 vtyp = PointerType(vtyp)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
377 variable = Variable(vname, vtyp, False)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
378 if parameter.kind == 'const':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
379 variable.isReadOnly = True
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
380 variable.isParameter = True
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
381 self.cst.addSymbol(variable)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
382 self.Consume(';')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
383 self.parseDeclarationSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
384 # Mark all variables as local:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
385 for variable in self.cst.getAllLocal(Variable):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
386 variable.isLocal = True
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
387
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
388 if self.hasConsumed('begin'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
389 block = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
390 if self.hasConsumed('return'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
391 returnexpression = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
392 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
393 returnexpression = None
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
394
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
395 if proctyp.returntype.isType(void):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
396 if not returnexpression is None:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
397 self.Error('Void procedure cannot return a value')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
398 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
399 if returnexpression is None:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
400 self.Error('Procedure must return a value')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
401 if not isType(returnexpression.typ, proctyp.returntype):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
402 self.Error('Returned type {0} does not match function return type {1}'.format(returnexpression.typ, proctyp.returntype))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
403
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
404 self.Consume('end')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
405 endname = self.Consume('ID')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
406 if endname != procname:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
407 self.Error('endname should match {0}'.format(name))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
408 self.cst = procsymtable.parent # Switch back to parent symbol table
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
409 proc = Procedure(procname, proctyp, block, procsymtable, returnexpression)
6
1784af239df4 Added error list
windel
parents: 5
diff changeset
410 self.setLocation(proc, loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
411 self.cst.addSymbol(proc)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
412 proc.public = i.ispublic
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
413 return proc
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
414
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
415 # Statements:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
416 def parseAssignment(self, lval):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
417 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
418 self.Consume(':=')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
419 rval = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
420 if isType(lval.typ, real) and isType(rval.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
421 rval = Unop(rval, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
422 if type(rval.typ) is NilType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
423 if not type(lval.typ) is ProcedureType and not type(lval.typ) is PointerType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
424 self.Error('Can assign nil only to pointers or procedure types, not {0}'.format(lval))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
425 elif not isType(lval.typ, rval.typ):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
426 self.Error('Type mismatch {0} != {1}'.format(lval.typ, rval.typ))
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
427 return self.setLocation(Assignment(lval, rval), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
428
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
429 def parseExpressionList(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
430 expressions = [ self.parseExpression() ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
431 while self.hasConsumed(','):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
432 expressions.append( self.parseExpression() )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
433 return expressions
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
434
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
435 def parseProcedureCall(self, procedure):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
436 self.Consume('(')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
437 if self.token.typ != ')':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
438 args = self.parseExpressionList()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
439 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
440 args = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
441 self.Consume(')')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
442 parameters = procedure.typ.parameters
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
443 if len(args) != len(parameters):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
444 self.Error("Procedure requires {0} arguments, {1} given".format(len(parameters), len(args)))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
445 for arg, param in zip(args, parameters):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
446 if not arg.typ.isType(param.typ):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
447 print(arg.typ, param.typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
448 self.Error('Mismatch in parameter')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
449 return ProcedureCall(procedure, args)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
450
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
451 def parseIfStatement(self):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
452 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
453 self.Consume('if')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
454 ifs = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
455 condition = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
456 if not isType(condition.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
457 self.Error('condition of if statement must be boolean')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
458 self.Consume('then')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
459 truestatement = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
460 ifs.append( (condition, truestatement) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
461 while self.hasConsumed('elsif'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
462 condition = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
463 if not isType(condition.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
464 self.Error('condition of if statement must be boolean')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
465 self.Consume('then')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
466 truestatement = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
467 ifs.append( (condition, truestatement) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
468 if self.hasConsumed('else'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
469 statement = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
470 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
471 statement = None
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
472 self.Consume('end')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
473 for condition, truestatement in reversed(ifs):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
474 statement = IfStatement(condition, truestatement, statement)
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
475 return self.setLocation(statement, loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
476
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
477 def parseCase(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
478 # TODO
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
479 pass
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
480
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
481 def parseCaseStatement(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
482 self.Consume('case')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
483 expr = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
484 self.Consume('of')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
485 self.parseCase()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
486 while self.hasConsumed('|'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
487 self.parseCase()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
488 self.Consume('end')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
489
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
490 def parseWhileStatement(self):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
491 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
492 self.Consume('while')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
493 condition = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
494 self.Consume('do')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
495 statements = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
496 if self.hasConsumed('elsif'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
497 self.Error('elsif in while not yet implemented')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
498 self.Consume('end')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
499 return self.setLocation(WhileStatement(condition, statements), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
500
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
501 def parseRepeatStatement(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
502 self.Consume('repeat')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
503 stmt = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
504 self.Consume('until')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
505 cond = self.parseBoolExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
506
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
507 def parseForStatement(self):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
508 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
509 self.Consume('for')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
510 variable = self.parseDesignator()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
511 if not variable.typ.isType(integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
512 self.Error('loop variable of for statement must have integer type')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
513 assert(variable.typ.isType(integer))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
514 self.Consume(':=')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
515 begin = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
516 if not begin.typ.isType(integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
517 self.Error('begin expression of a for statement must have integer type')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
518 self.Consume('to')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
519 end = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
520 if not end.typ.isType(integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
521 self.Error('end expression of a for statement must have integer type')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
522 if self.hasConsumed('by'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
523 increment, typ = self.parseConstExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
524 if not typ.isType(integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
525 self.Error('Increment must be integer')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
526 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
527 increment = 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
528 assert(type(increment) is int)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
529 self.Consume('do')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
530 statements = self.parseStatementSequence()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
531 self.Consume('end')
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
532 return self.setLocation(ForStatement(variable, begin, end, increment, statements), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
533
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
534 def parseAsmcode(self):
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
535 # TODO: move this to seperate file
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
536 def parseOpcode():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
537 return self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
538 def parseOperand():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
539 if self.hasConsumed('['):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
540 memref = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
541 memref.append(parseOperand())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
542 self.Consume(']')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
543 return memref
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
544 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
545 if self.token.typ == 'NUMBER':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
546 return self.Consume('NUMBER')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
547 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
548 ID = self.Consume('ID')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
549 if self.cst.has(Variable, ID):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
550 return self.cst.get(Variable, ID)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
551 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
552 return ID
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
553
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
554 def parseOperands(n):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
555 operands = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
556 if n > 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
557 operands.append( parseOperand() )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
558 n = n - 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
559 while n > 0:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
560 self.Consume(',')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
561 operands.append(parseOperand())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
562 n = n - 1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
563 return operands
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
564 self.Consume('asm')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
565 asmcode = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
566 while self.token.typ != 'end':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
567 opcode = parseOpcode()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
568 func, numargs = assembler.opcodes[opcode]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
569 operands = parseOperands(numargs)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
570 asmcode.append( (opcode, operands) )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
571 #print('opcode', opcode, operands)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
572 self.Consume('end')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
573 return AsmCode(asmcode)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
574
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
575 def parseStatement(self):
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
576 try:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
577 # Determine statement type based on the pending token:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
578 if self.token.typ == 'if':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
579 return self.parseIfStatement()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
580 elif self.token.typ == 'case':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
581 return self.parseCaseStatement()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
582 elif self.token.typ == 'while':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
583 return self.parseWhileStatement()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
584 elif self.token.typ == 'repeat':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
585 return self.parseRepeatStatement()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
586 elif self.token.typ == 'for':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
587 return self.parseForStatement()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
588 elif self.token.typ == 'asm':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
589 return self.parseAsmcode()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
590 elif self.token.typ == 'ID':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
591 # Assignment or procedure call
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
592 designator = self.parseDesignator()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
593 if self.token.typ == '(' and type(designator.typ) is ProcedureType:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
594 return self.parseProcedureCall(designator)
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
595 elif self.token.typ == ':=':
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
596 return self.parseAssignment(designator)
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
597 else:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
598 self.Error('Unknown statement following designator: {0}'.format(self.token))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
599 else:
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
600 # TODO: return empty statement??:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
601 return EmptyStatement()
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
602 self.Error('Unknown statement {0}'.format(self.token))
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
603 except CompilerException as e:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
604 print(e)
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
605 self.errorlist.append( (e.row, e.col, e.msg))
7
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
606 # Do error recovery by skipping all tokens until next ; or end
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
607 while not (self.token.typ == ';' or self.token.typ == 'end'):
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
608 self.Consume(self.token.typ)
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 4
diff changeset
609 return EmptyStatement()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
610
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
611 def parseStatementSequence(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
612 """ Sequence of statements seperated by ';' """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
613 statements = [ self.parseStatement() ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
614 while self.hasConsumed(';'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
615 statements.append( self.parseStatement() )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
616 return StatementSequence( statements )
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
617
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
618 # Parsing expressions:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
619 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
620 grammar of expressions:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
621 expression = SimpleExpression [ reloperator SimpleExpression ]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
622 reloperator = '=' | '<=' | '>=' | '<>'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
623 Simpleexpression = [ '+' | '-' ] term { addoperator term }
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
624 addoperator = '+' | '-' | 'or'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
625 term = factor { muloperator factor }
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
626 muloperator = '*' | '/' | 'div' | 'mod' | 'and'
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
627 factor = number | nil | true | false | "(" expression ")" |
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
628 designator [ actualparameters ] | 'not' factor
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
629 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
630 def parseExpression(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
631 """ The connector between the boolean and expression domain """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
632 expr = self.parseSimpleExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
633 if self.token.typ in ['>=','<=','<','>','<>','=']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
634 relop = self.Consume()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
635 expr2 = self.parseSimpleExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
636 # Automatic type convert to reals:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
637 if isType(expr.typ, real) and isType(expr2.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
638 expr2 = Unop(expr2, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
639 if isType(expr2.typ, real) and isType(expr.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
640 expr = Unop(expr, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
641 # Type check:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
642 if not isType(expr.typ, expr2.typ):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
643 self.Error('Type mismatch in relop')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
644 if isType(expr.typ, real) and relop in ['<>', '=']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
645 self.Error('Cannot check real values for equality')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
646
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
647 expr = Relop(expr, relop, expr2, boolean)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
648 return expr
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
649
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
650 # Parsing arithmatic expressions:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
651 def parseTerm(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
652 a = self.parseFactor()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
653 while self.token.typ in ['*', '/', 'mod', 'div', 'and']:
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
654 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
655 op = self.Consume()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
656 b = self.parseTerm()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
657 # Type determination and checking:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
658 if op in ['mod', 'div']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
659 if not isType(a.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
660 self.Error('First operand should be integer, not {0}'.format(a.typ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
661 if not isType(b.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
662 self.Error('Second operand should be integer, not {0}'.format(b.typ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
663 typ = integer
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
664 elif op == '*':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
665 if isType(a.typ, integer) and isType(b.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
666 typ = integer
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
667 elif isType(a.typ, real) or isType(b.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
668 if isType(a.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
669 # Automatic type cast
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
670 a = Unop(a, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
671 if isType(b.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
672 b = Unop(b, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
673 if not isType(a.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
674 self.Error('first operand must be a real!')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
675 if not isType(b.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
676 self.Error('second operand must be a real!')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
677 typ = real
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
678 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
679 self.Error('Unknown operands for multiply: {0}, {1}'.format(a, b))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
680 elif op == '/':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
681 # Division always yields a real result, for integer division use div
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
682 if isType(a.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
683 # Automatic type cast
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
684 a = Unop(a, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
685 if isType(b.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
686 b = Unop(b, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
687 if not isType(a.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
688 self.Error('first operand must be a real!')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
689 if not isType(b.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
690 self.Error('second operand must be a real!')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
691 typ = real
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
692 elif op == 'and':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
693 if not isType(a.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
694 self.Error('First operand of and must be boolean')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
695 if not isType(b.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
696 self.Error('Second operand of and must be boolean')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
697 typ = boolean
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
698 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
699 self.Error('Unknown operand {0}'.format(op))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
700
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
701 a = self.setLocation(Binop(a, op, b, typ), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
702 return a
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
703
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
704 def parseFactor(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
705 if self.hasConsumed('('):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
706 e = self.parseExpression()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
707 self.Consume(')')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
708 return e
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
709 elif self.token.typ == 'NUMBER':
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
710 loc = self.getLocation()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
711 val = self.Consume('NUMBER')
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
712 return self.setLocation(Constant(val, integer), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
713 elif self.token.typ == 'REAL':
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
714 loc = self.getLocation()
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
715 val = self.Consume('REAL')
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
716 return self.setLocation(Constant(val, real), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
717 elif self.token.typ == 'CHAR':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
718 val = self.Consume('CHAR')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
719 return Constant(val, char)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
720 elif self.token.typ == 'STRING':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
721 txt = self.Consume('STRING')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
722 return StringConstant(txt)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
723 elif self.token.typ in ['true', 'false']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
724 val = self.Consume()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
725 val = True if val == 'true' else False
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
726 return Constant(val, boolean)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
727 elif self.hasConsumed('nil'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
728 return Constant(0, NilType())
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
729 elif self.hasConsumed('not'):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
730 f = self.parseFactor()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
731 if not isType(f.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
732 self.Error('argument of boolean negation must be boolean type')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
733 return Unop(f, 'not', boolean)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
734 elif self.token.typ == 'ID':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
735 designator = self.parseDesignator()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
736 # TODO: handle functions different here?
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
737 if self.token.typ == '(' and type(designator.typ) is ProcedureType:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
738 return self.parseProcedureCall(designator)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
739 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
740 return designator
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
741 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
742 self.Error('Expected NUMBER, ID or ( expr ), got'+str(self.token))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
743
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
744 def parseSimpleExpression(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
745 """ Arithmatic expression """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
746 if self.token.typ in ['+', '-']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
747 # Handle the unary minus
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
748 op = self.Consume()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
749 a = self.parseTerm()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
750 typ = a.typ
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
751 if not isType(typ,real) and not isType(typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
752 self.Error('Unary minus or plus can be only applied to real or integers')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
753 if op == '-':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
754 a = Unop(a, op, typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
755 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
756 a = self.parseTerm()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
757 while self.token.typ in ['+', '-', 'or']:
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
758 loc = self.getLocation()
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
759 op = self.Consume()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
760 b = self.parseTerm()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
761 if op in ['+', '-']:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
762 if isType(a.typ, real) or isType(b.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
763 typ = real
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
764 if isType(a.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
765 # Automatic type cast
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
766 a = Unop(a, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
767 if not isType(a.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
768 self.Error('first operand must be a real!')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
769 if isType(b.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
770 b = Unop(b, 'INTTOREAL', real)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
771 if not isType(b.typ, real):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
772 self.Error('second operand must be a real!')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
773 elif isType(a.typ, integer) and isType(b.typ, integer):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
774 typ = integer
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
775 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
776 self.Error('Invalid types {0} and {1}'.format(a.typ, b.typ))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
777 elif op == 'or':
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
778 if not isType(a.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
779 self.Error('first operand must be boolean for or operation')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
780 if not isType(b.typ, boolean):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
781 self.Error('second operand must be boolean for or operation')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
782 typ = boolean
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
783 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
784 self.Error('Unknown operand {0}'.format(op))
4
0d5ef85b8698 Improved link between ast viewer and code edit
windel-eee
parents: 1
diff changeset
785 a = self.setLocation(Binop(a, op, b, typ), loc)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
786 return a
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
787