-
Notifications
You must be signed in to change notification settings - Fork 4
/
absyn.c
605 lines (536 loc) · 13 KB
/
absyn.c
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* absyn.c - Abstract Syntax Functions. Most functions create an instance of an
* abstract syntax rule.
*/
#include <stdio.h>
#include "util.h"
#include "symbol.h" /* symbol table data structures */
#include "absyn.h" /* abstract syntax data structures */
#include "type.h"
A_var A_SimpleVar(A_pos pos, S_symbol sym)
{A_var p = checked_malloc(sizeof(*p));
p->kind=A_simpleVar;
p->pos=pos;
p->u.simple=sym;
return p;
}
A_var A_FieldVar(A_pos pos, A_var var, S_symbol sym)
{A_var p = checked_malloc(sizeof(*p));
p->kind=A_fieldVar;
p->pos=pos;
p->u.field.var=var;
p->u.field.sym=sym;
return p;
}
A_var A_SubscriptVar(A_pos pos, A_var var, A_exp exp)
{A_var p = checked_malloc(sizeof(*p));
p->kind=A_subscriptVar;
p->pos=pos;
p->u.subscript.var=var;
p->u.subscript.exp=exp;
return p;
}
A_exp A_VarExp(A_pos pos, A_var var)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_varExp;
p->pos=pos;
p->u.var=var;
return p;
}
A_exp A_NilExp(A_pos pos)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_nilExp;
p->pos=pos;
return p;
}
A_exp A_NumberExp(A_pos pos, string s)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_numberExp;
p->pos=pos;
p->dec_type=isInt(s)?Ty_int:Ty_float;
p->u.number=s;
return p;
}
A_exp A_StringExp(A_pos pos, S_table table, string s)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_stringExp;
p->pos=pos;
A_ty t=S_look(table,S_Symbol(s));
if(t==NULL) p->dec_type=S_Symbol(s);
else p->dec_type=t->dec_type;
p->u.stringg=s;
return p;
}
A_exp A_CallExp(A_pos pos, S_symbol func, A_expList args)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_callExp;
p->pos=pos;
p->u.call.func=func;
p->u.call.args=args;
return p;
}
A_exp A_OpExp(A_pos pos, A_oper oper, A_exp left, A_exp right)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_opExp;
p->pos=pos;
p->u.op.oper=oper;
p->u.op.left=left;
p->u.op.right=right;
return p;
}
A_exp A_UnaryOpExp(A_pos pos, A_unaryOper oper, A_exp exp)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_unaryOpExp;
p->pos=pos;
p->u.unaryOp.oper = oper;
p->u.unaryOp.exp=exp;
return p;
}
A_exp A_RecordExp(A_pos pos, S_symbol typ, A_efieldList fields)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_recordExp;
p->pos=pos;
p->u.record.typ=typ;
p->u.record.fields=fields;
return p;
}
A_exp A_SeqExp(A_pos pos, A_expList seq)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_seqExp;
p->pos=pos;
p->u.seq=seq;
return p;
}
A_exp A_AssignExp(A_pos pos, A_exp name, A_exp exp)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_assignExp;
p->pos=pos;
p->u.assign.name=name;
p->u.assign.exp=exp;
return p;
}
A_exp A_IfExp(A_pos pos, A_expList cond_clauses, A_expList elsee)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_ifExp;
p->pos=pos;
p->u.iff.cond_clauses=cond_clauses;
p->u.iff.elsee=elsee;
return p;
}
A_exp A_WhileExp(A_pos pos, A_exp test)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_whileExp;
p->pos=pos;
p->u.whilee.test=test;
return p;
}
A_exp A_ForExp(A_pos pos, A_exp var, A_exp reverse, A_exp range)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_forExp;
p->pos=pos;
p->u.forr.var=var;
p->u.forr.reverse=reverse;
p->u.forr.range=range;
return p;
}
A_exp A_LoopExp(A_pos pos, A_exp labelopt, A_exp iteration, A_expList basicloop, A_exp idopt)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_loopExp;
p->pos=pos;
p->u.loop.labelopt=labelopt;
p->u.loop.iteration=iteration;
p->u.loop.basicloop=basicloop;
p->u.loop.idopt=idopt;
return p;
}
A_exp A_CondExp(A_pos pos, A_exp test, A_expList stmts)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_condExp;
p->pos=pos;
p->u.cond.test=test;
p->u.cond.stmts=stmts;
return p;
}
A_exp A_BreakExp(A_pos pos)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_breakExp;
p->pos=pos;
return p;
}
A_exp A_ExitExp(A_pos pos, A_exp exitname, A_exp exitcondition)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_exitExp;
p->pos=pos;
p->u.exit.exitname=exitname;
p->u.exit.exitcondition=exitcondition;
return p;
}
A_exp A_LetExp(A_pos pos, A_decList decs, A_exp body)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_letExp;
p->pos=pos;
p->u.let.decs=decs;
p->u.let.body=body;
return p;
}
A_exp A_ArrayExp(A_pos pos, S_symbol typ, A_exp size, A_exp init)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_arrayExp;
p->pos=pos;
p->u.array.typ=typ;
p->u.array.size=size;
p->u.array.init=init;
return p;
}
A_exp A_ReturnExp(A_pos pos, A_exp retval)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_returnExp;
p->pos=pos;
p->u.retval=retval;
return p;
}
A_exp A_GotoExp(A_pos pos, A_exp gotolabel)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_gotoExp;
p->pos=pos;
p->u.gotolabel=gotolabel;
return p;
}
A_exp A_EnumExp(A_pos pos, A_expList enumids)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_enumExp;
p->pos=pos;
p->u.enumids=enumids;
return p;
}
A_exp A_IntdefExp(A_pos pos, A_exp intdef)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_intdefExp;
p->pos=pos;
p->u.intdef=intdef;
return p;
}
A_exp A_FloatdefExp(A_pos pos, A_exp numdigits, A_exp rangeopt)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_floatdefExp;
p->pos=pos;
p->u.floatt.numdigits=numdigits;
p->u.floatt.rangeopt=rangeopt;
return p;
}
A_exp A_FixeddefExp(A_pos pos, A_exp delta, A_exp range)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_fixeddefExp;
p->pos=pos;
p->u.fixed.delta=delta;
p->u.fixed.range=range;
return p;
}
A_exp A_FixeddefdigitExp(A_pos pos, A_exp delta, A_exp numdigits, A_exp rangeopt)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_fixeddefdigitExp;
p->pos=pos;
p->u.fixeddig.delta=delta;
p->u.fixeddig.numdigits=numdigits;
p->u.fixeddig.rangeopt=rangeopt;
return p;
}
A_exp A_UnconarraydefExp(A_pos pos, A_expList indexs, A_exp subtypeind)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_unconarraydefExp;
p->pos=pos;
p->u.unconarray.indexs=indexs;
p->u.unconarray.subtypeind=subtypeind;
return p;
}
A_exp A_ConarraydefExp(A_pos pos, A_expList iterindex, A_exp subtypeind)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_conarraydefExp;
p->pos=pos;
p->u.conarray.iterindex=iterindex;
p->u.conarray.subtypeind=subtypeind;
return p;
}
A_exp A_NullrecorddefExp(A_pos pos)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_nullrecorddefExp;
p->pos=pos;
return p;
}
A_exp A_Pragma(A_pos pos, string pragmaname)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_pragma;
p->pos=pos;
p->u.pragmaname=pragmaname;
return p;
}
A_exp A_Pragmalist(A_pos pos, A_exp name, A_expList pragmaargs)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_pragmalist;
p->pos=pos;
p->u.pragmalist.name=name;
p->u.pragmalist.pragmaargs=pragmaargs;
return p;
}
A_exp A_RecorddefExp(A_pos pos, A_expList pragmas, A_exp complist)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_recorddefExp;
p->pos=pos;
p->u.recorddef.pragmas=pragmas;
p->u.recorddef.complist=complist;
return p;
}
A_exp A_Alternative(A_pos pos, A_expList choices, A_expList stmts)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_alternative;
p->pos=pos;
p->u.alternative.choices=choices;
p->u.alternative.stmts=stmts;
return p;
}
A_exp A_Case(A_pos pos, A_exp header, A_expList pragmas, A_expList alternatives)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_caseExp;
p->pos=pos;
p->u.caseexp.header=header;
p->u.caseexp.pragmas=pragmas;
p->u.caseexp.alternatives=alternatives;
return p;
}
A_exp A_RaiseExp(A_pos pos, A_exp nameopt)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_raiseExp;
p->pos=pos;
p->u.nameopt=nameopt;
return p;
}
A_exp A_Procedure(A_pos pos, A_exp procedurename)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_procedure;
p->pos=pos;
p->u.procedurename=procedurename;
return p;
}
A_exp A_FunctionUse(A_pos pos, A_exp name, A_expList value)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_functionUse;
p->pos=pos;
p->u.functionuse.name=name;
p->u.functionuse.value=value;
return p;
}
A_exp A_CompAssoc(A_pos pos, A_expList choices, A_exp expression)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_compAssoc;
p->pos=pos;
p->u.compassoc.choices=choices;
p->u.compassoc.expression=expression;
return p;
}
A_exp A_CompUnit(A_pos pos, A_expList contextspec, A_exp privatee, A_exp unit, A_expList pragmas)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_compUnit;
p->pos=pos;
p->u.compunit.contextspec=contextspec;
p->u.compunit.privatee=privatee;
p->u.compunit.unit=unit;
p->u.compunit.pragmas=pragmas;
return p;
}
A_exp A_Useclause(A_pos pos, A_exp type, A_expList names)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_useclause;
p->pos=pos;
p->u.useclause.type=type;
p->u.useclause.names=names;
return p;
}
A_exp A_ContextSpecwith(A_pos pos, A_expList withclause, A_expList useclauseopt)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_contextSpecwith;
p->pos=pos;
p->u.contextspecwith.withclause=withclause;
p->u.contextspecwith.useclauseopt=useclauseopt;
return p;
}
A_exp A_SubprogSpec(A_pos pos, A_exp name, A_expList formalpart)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_subprogSpec;
p->pos=pos;
p->u.subprogSpec.name=name;
p->u.subprogSpec.formalpart=formalpart;
return p;
}
A_exp A_NameConstr(A_pos pos, A_exp name, A_exp constraint)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_nameConstr;
p->pos=pos;
p->dec_type=name->dec_type;
p->u.nameconstr.name=name;
p->u.nameconstr.constraint=constraint;
return p;
}
A_exp A_DecimalConstr(A_pos pos, A_exp expression, A_exp rangeopt)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_decimalConstr;
p->pos=pos;
p->u.decimalconstr.expression=expression;
p->u.decimalconstr.rangeopt=rangeopt;
return p;
}
A_exp A_NotImplemented(A_pos pos, string msg)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_notImplemented;
p->pos=pos;
p->u.msg=msg;
return p;
}
A_exp A_IdentExp(A_pos pos, S_symbol ident)
{A_exp p = checked_malloc(sizeof(*p));
p->kind=A_identExp;
p->pos=pos;
p->u.ident=ident;
return p;
}
A_dec A_FunctionDec(A_pos pos, A_fundecList function)
{A_dec p = checked_malloc(sizeof(*p));
p->kind=A_functionDec;
p->pos=pos;
p->u.function=function;
return p;
}
A_dec A_VarDec(A_pos pos, S_symbol var, S_symbol typ, A_exp init)
{A_dec p = checked_malloc(sizeof(*p));
p->kind=A_varDec;
p->pos=pos;
p->u.var.var=var;
p->u.var.typ=typ;
p->u.var.init=init;
p->u.var.escape=TRUE;
return p;
}
A_dec A_TypeDec(A_pos pos, A_nametyList type)
{A_dec p = checked_malloc(sizeof(*p));
p->kind=A_typeDec;
p->pos=pos;
p->u.type=type;
return p;
}
A_dec A_GlobalDec(A_pos pos)
{A_dec p = checked_malloc(sizeof(*p));
p->kind=A_globalDec;
p->pos=pos;
return p;
}
A_ty A_NameTy(A_pos pos, S_symbol name)
{A_ty p = checked_malloc(sizeof(*p));
p->kind=A_nameTy;
p->pos=pos;
p->u.name=name;
return p;
}
A_ty A_RecordTy(A_pos pos, A_fieldList record)
{A_ty p = checked_malloc(sizeof(*p));
p->kind=A_recordTy;
p->pos=pos;
p->u.record=record;
return p;
}
A_ty A_ArrayTy(A_pos pos, S_symbol array)
{A_ty p = checked_malloc(sizeof(*p));
p->kind=A_arrayTy;
p->pos=pos;
p->u.array=array;
return p;
}
A_ty A_ObjectTy(A_pos pos, string qualifier, A_exp subtype, A_exp init)
{A_ty p = checked_malloc(sizeof(*p));
p->kind=A_objectTy;
p->pos=pos;
p->dec_type=subtype->dec_type;
p->u.obj.qualifier=qualifier;
p->u.obj.subtype = subtype;
p->u.obj.init= init;
return p;
}
A_ty A_NumTy(A_pos pos, A_exp assignexp)
{A_ty p = checked_malloc(sizeof(*p));
p->kind=A_numTy;
p->pos=pos;
p->u.assignexp=assignexp;
return p;
}
A_ty A_TypeDecTy(A_pos pos,A_expList discrimopt, A_exp typecomp)
{A_ty p = checked_malloc(sizeof(*p));
p->kind=A_typeDecTy;
p->pos=pos;
p->u.typedec.discrimopt=discrimopt;
p->u.typedec.typecomp=typecomp;
return p;
}
A_field A_Field(A_pos pos, S_symbol name, S_symbol typ)
{A_field p = checked_malloc(sizeof(*p));
p->pos=pos;
p->name=name;
p->typ=typ;
p->escape=TRUE;
return p;
}
A_fieldList A_FieldList(A_field head, A_fieldList tail)
{A_fieldList p = checked_malloc(sizeof(*p));
p->head=head;
p->tail=tail;
return p;
}
A_expList A_ExpList(A_exp head, A_expList tail)
{A_expList p = checked_malloc(sizeof(*p));
p->head=head;
p->tail=tail;
return p;
}
A_fundec A_Fundec(A_pos pos, S_symbol name, A_fieldList params, S_symbol result,
A_exp body)
{A_fundec p = checked_malloc(sizeof(*p));
p->pos=pos;
p->name=name;
p->params=params;
p->result=result;
p->body=body;
return p;
}
A_fundecList A_FundecList(A_fundec head, A_fundecList tail)
{A_fundecList p = checked_malloc(sizeof(*p));
p->head=head;
p->tail=tail;
return p;
}
A_decList A_DecList(A_dec head, A_decList tail)
{A_decList p = checked_malloc(sizeof(*p));
p->head=head;
p->tail=tail;
return p;
}
A_namety A_Namety(S_symbol name, A_ty ty)
{A_namety p = checked_malloc(sizeof(*p));
p->name=name;
p->ty=ty;
return p;
}
A_nametyList A_NametyList(A_namety head, A_nametyList tail)
{A_nametyList p = checked_malloc(sizeof(*p));
p->head=head;
p->tail=tail;
return p;
}
A_efield A_Efield(S_symbol name, A_exp exp)
{A_efield p = checked_malloc(sizeof(*p));
p->name=name;
p->exp=exp;
return p;
}
A_efieldList A_EfieldList(A_efield head, A_efieldList tail)
{A_efieldList p = checked_malloc(sizeof(*p));
p->head=head;
p->tail=tail;
return p;
}