annotate python/ks/parser.py @ 198:33d50727a23c

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