-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler_errors_source.py
400 lines (325 loc) · 7.25 KB
/
compiler_errors_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from compile import compileString
import os
def testLevelCheck():
""" level! error with nested procedures """
error = compileString("""
program p;
procedure q(a, b: integer);
procedure r(c, d: integer);
begin
a := 4
end;
begin
a := 7
end;
begin q(5, 6)
end
""", os.devnull, suppress_errors = True)
return error
def testMaxValueCheck():
"""produces error 'value too large'"""
error = compileString("""
program p;
const c = 4294967296;
var x: integer;
begin
x := c
end
""", os.devnull, suppress_errors = True)
return error
def testBooleanParamMode():
""" cant pass cond as value """
error = compileString("""
program p;
type a = array [1..10] of integer;
var myarr: a;
var x: integer;
procedure q(f: boolean);
begin
f := true
end;
begin
q(5<4)
end;
""", os.devnull, suppress_errors = True)
return error
def testProcedureDeclarationsCheck():
error = compileString("""
program p;
x := 5; {declaration expected}
const x : integer; { = expected, expression not constant, }
const 5 = 5 {constant name expected}
var y: integer;
begin
y := 5
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program p;
const w = 1 {; expected}
type i : integer { = expected, ; expected}
type array = array [1..5] of integer; { type name expected}
var y: integer;
begin
y := 5
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program p;
var a : integer {; expected}
var y: integer;
procedure (5: integer); {procedure name expected, formal params expected}
begin
end;
begin
y := 5
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program p;
var y: integer;
procedure myproc (var a: integer { ) expected}
begin
a := 5
end;
procedure anotherproc (var a: integer) { ; expected}
begin
a := 5
end {; expected}
begin
y := 5
end;
""", os.devnull, suppress_errors = True)
return error
# error = compileString("""
# program p;
# procedure (var a: integer);
# begin
# end;
# begin
# end;
# """, os.devnull, suppress_errors = True)
# error += compileString("""
# program p;
# procedure q(5: integer);
# begin
# end;
# begin
# end;
# """, os.devnull, suppress_errors = True)
# print(error)
def testProgramDeclCheck():
error = compileString("""
p;
begin
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program;
begin
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program p
begin
end;
""", os.devnull, suppress_errors = True)
return error
def testGetChar():
error = compileString("""
program p;
var x: integer;
begin
write(|)
end;
""")
return error
# arrays must be passed by reference
def testPassByValueCheck():
error = compileString("""
program p;
type arr = array [1..10] of boolean;
var a1: arr;
var p: integer;
var boo: boolean;
procedure f(a: arr; z: boolean);
begin
a[1] := z
end;
begin
p := 1;
f(a1, 5>4)
end;
""", os.devnull, suppress_errors = True)
return error
def testSelectorCheck():
#['not a field', 'not a record', 'identifier expected', 'index out of bounds', 'index not integer', 'not an array', '] expected']
error = compileString("""
program p;
type arrtype = array [1 .. 7] of integer;
type rectype = record f: integer; g: arrtype end;
var p, q: rectype;
var z: arrtype;
var x, y: integer;
begin
p.g[8] := 1; {oob}
p.g[q] := 1; {index not integer}
p.h := 12; {not a field}
z[1 := 2; {] expected}
q[1] := 1; {not an array}
x.f := 9; {not a record}
x.5 := 4 {ident expected}
end;
""", os.devnull, suppress_errors = True)
return error
def testFactorCheck():
error = compileString("""
program p;
type arrtype = array [1 .. 7] of integer;
var z: arrtype;
var x, y: integer;
begin
y := not x; {not boolean}
y := (5 ; { ) expected}
x := arrtype; {var or const expected}
x := .^^ {factor expected x2}
end;
""", os.devnull, suppress_errors = True)
return error
def testTermCheck():
error = compileString("""
program p;
type arrtype = array [1 .. 7] of integer;
var z: arrtype;
var x, y: integer;
begin
x := 3 * z; {bad type}
end;
""", os.devnull, suppress_errors = True)
return error
# expressions and simpleExpression
def testExpressionCheck():
error = compileString("""
program p;
type arrtype = array [1 .. 7] of integer;
var z: arrtype;
var x, y: integer;
begin
x := -z -z; {bad type, bad type}
x := 1 >= z {bad type}
end;
""", os.devnull, suppress_errors = True)
return error
def testCompoundStatementCheck():
error = compileString("""
program p;
var x: integer;
{begin expected}
x := 1
x := 2
{end expected}
""", os.devnull, suppress_errors = True)
return error
def testAssignmentStatementCheck():
error = compileString("""
program p;
var x: integer;
var y: boolean;
begin
5; {statement expected, invalid statement}
x := y; {incompatible assignment }
y = x; {:= expected}
y and y {:= expected}
end;
""", os.devnull, suppress_errors = True)
return error
def testProcCallCheck():
error = compileString("""
program p;
type aa = integer;
var x, y: integer;
procedure q(var a, b: integer);
begin
a := 7
end;
procedure q2;
var a: integer;
begin
a := 7
end;
begin
x := 1;
q(5, 5); {illegal param mode x2}
q(x, x, x); {extra param}
q(x); {too few param}
q(x,x ;{) expected}
q2(x) {extra param}
aa {variable or procedure expected}
end
""", os.devnull, suppress_errors = True)
return error
def testConditionalStatementCheck():
error = compileString("""
program p;
var x, y: integer;
var p, q: boolean;
begin
if p {then expected}
x := 5;
while p {do expected}
x := 5;
while x then{bool expected}
x := 1
end
""", os.devnull, suppress_errors = True)
error += compileString("""
program p;
var x, y: integer;
begin
if x then{bool expected}
x := 5
end
""", os.devnull, suppress_errors = True)
return error
def testTypeCheck():
""" type expected"""
error = compileString("""
program p;
type a: 5;
type b = c;
type d = array 1..4 integer;
var x: a;
begin
x := 5
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program p;
type d = array [1:4] of integer; {. expected}
type e = array [1.4] integer; {. expected, of expected}
type f = array [-1..-4] of integer; {bad upper, lower bound}
type g = record h: integer {end expected}
var x: integer;
begin
x := 5
end;
""", os.devnull, suppress_errors = True)
return error
def testTypedIdsCheck():
error = compileString("""
program p;
type s = record g, h = integer end; {: expected}
var x: integer;
begin
x := 5
end;
""", os.devnull, suppress_errors = True)
error += compileString("""
program p;
type s = record g, 1 : integer end; {identifier expected}
var x: integer;
begin
x := 5
end;
""", os.devnull, suppress_errors = True)
return error