annotate ide/compiler/parser.py @ 1:92df07bc2081

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