annotate python/ppci/frontends/ks/parser.py @ 106:f2d980eef509

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