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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
/*****
* exp.h
* Andy Hammerlindl 2002/8/19
*
* Represents the abstract syntax tree for the expressions in the
* language. this is translated into virtual machine code using trans()
* and with the aid of the environment class.
*****/
#ifndef EXP_H
#define EXP_H
#include "types.h"
#include "symbol.h"
#include "absyn.h"
#include "varinit.h"
#include "name.h"
#include "guideflags.h"
namespace trans {
class coenv;
class coder;
struct label_t;
typedef label_t *label;
class application;
}
namespace absyntax {
using trans::coenv;
using trans::label;
using trans::application;
using trans::access;
using sym::symbol;
using types::record;
using types::array;
class exp : public varinit {
protected:
// The cached type (from a call to cgetType).
types::ty *ct;
public:
exp(position pos)
: varinit(pos), ct(0) {}
void prettyprint(ostream &out, Int indent) = 0;
// When reporting errors with function calls, it is nice to say "no
// function f(int)" instead of "no function matching signature
// (int)." Hence, this method returns the name of the expression if
// there is one.
virtual symbol getName()
{
return symbol::nullsym;
}
// Checks if the expression can be used as the right side of a scale
// expression. ie. 3sin(x)
// If a "non-scalable" expression is scaled a warning is issued.
virtual bool scalable() { return true; }
// Specifies if the value of the expression should be written to interactive
// prompt if typed as a stand-alone expression. For example:
// > 2+3;
// should write 5, but
// > x=2+3;
// shouldn't. (These choices are largely aesthetic)
virtual bool writtenToPrompt() { return true; }
// Translates the expression to the given target type. This should only be
// called with a type returned by getType(). It does not perform implicit
// casting.
virtual void transAsType(coenv &e, types::ty *target);
// Translates the expression to the given target type, possibly using an
// implicit cast.
void transToType(coenv &e, types::ty *target);
// Translates the expression and returns the resultant type.
// For some expressions, this will be ambiguous and return an error.
// Trans may only return ty_error, if it (or one of its recursively
// called children in the syntax tree) reported an error to em.
virtual types::ty *trans(coenv &) = 0;
// getType() figures out the type of the expression without translating
// the code into the virtual machine language or reporting errors to em.
// This must follow a few rules to ensure proper translation:
// 1. If this returns a valid type, t, trans(e) must return t or
// report an error, and transToType(e, t) must run either reporting
// an error or reporting no error and yielding the same result as
// trans(e).
// 2. If this returns a superposition of types (ie. for overloaded
// functions), trans must not return a singular type, and every
// type in the superposition must run without error properly
// if fed to transAsType(e, t).
// 3. If this returns ty_error, then so must a call to trans(e) and any
// call to trans, transAsType, or transToType must report an error
// to em.
// 4. Any call to transAsType(e, t) with a type that is not returned by
// getType() (or one of the subtypes in case of a superposition)
// must report an error.
// Any call to transToType(e, t) with a type that is not returned by
// getType() (or one of the subtypes in case of a superposition)
// or any type not implicitly castable from the above must report an
// error.
virtual types::ty *getType(coenv &) = 0;
// This is an optimization which works in some cases to by-pass the slow
// overloaded function resolution provided by the application class.
//
// If an expression is called with arguments given by sig, getCallee must
// either return 0 (the default), or if it returns a varEntry, the varEntry
// must correspond to the function which would be called after normal
// function resolution.
//
// The callee must produce no side effects as there are no guarantees when
// the varEntry will be translated.
virtual trans::varEntry *getCallee(coenv &e, types::signature *sig) {
//#define DEBUG_GETAPP
#if DEBUG_GETAPP
cout << "exp fail" << endl;
cout << "exp fail at " << getPos() << endl;
prettyprint(cout, 2);
#endif
return 0;
}
// Same result as getType, but caches the result so that subsequent
// calls are faster. For this to work correctly, the expression should
// only be used in one place, so the environment doesn't change between
// calls.
virtual types::ty *cgetType(coenv &e) {
#ifdef DEBUG_CACHE
testCachedType(e);
#endif
return ct ? ct : ct = getType(e);
}
void testCachedType(coenv &e);
// The expression is being written. Translate code such that the value
// (represented by the exp value) is stored into the address represented by
// this expression.
// In terms of side-effects, this expression must be evaluated (once) before
// value is evaluated (once).
virtual void transWrite(coenv &e, types::ty *t, exp *value) {
em.error(getPos());
em << "expression cannot be used as an address";
// Translate the value for errors.
value->transToType(e, t);
}
// Translates code for calling a function. The arguments, in the order they
// appear in the function's signature, must all be on the stack.
virtual void transCall(coenv &e, types::ty *target);
// transConditionalJump must produce code equivalent to the following:
// Evaluate the expression as a boolean. If the result equals cond, jump to
// the label dest, otherwise do not jump. In either case, no value is left
// on the stack.
virtual void transConditionalJump(coenv &e, bool cond, label dest);
// This is used to ensure the proper order and number of evaluations. When
// called, it immediately translates code to perform the side-effects
// consistent with a corresponding call to transAsType(e, target).
//
// The return value, called an evaluation for lack of a better name, is
// another expression that responds to the trans methods exactly as would the
// original expression, but without producing side-effects. It is also no
// longer overloaded, due to the resolution effected by giving a target type
// to evaluate().
//
// The methods transAsType, transWrite, and transCall of the evaluation must
// be called with the same target type as the original call to evaluate.
// When evaluate() is called during the translation of a function, that
// function must still be in translation when the evaluation is translated.
//
// The base implementation uses a tempExp (see below). This is
// sufficient for most expressions.
virtual exp *evaluate(coenv &e, types::ty *target);
// NOTE: could add a "side-effects" method which says if the expression has
// side-effects. This might allow some small optimizations in translating.
};
class tempExp : public exp {
access *a;
types::ty *t;
public:
tempExp(coenv &e, varinit *v, types::ty *t);
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) {
return t;
}
};
// Wrap a varEntry so that it can be used as an expression.
// Translating the varEntry must cause no side-effects.
class varEntryExp : public exp {
trans::varEntry *v;
public:
varEntryExp(position pos, trans::varEntry *v)
: exp(pos), v(v) {}
varEntryExp(position pos, types::ty *t, access *a);
varEntryExp(position pos, types::ty *t, vm::bltin f);
void prettyprint(ostream &out, Int indent);
types::ty *getType(coenv &);
types::ty *trans(coenv &e);
trans::varEntry *getCallee(coenv &e, types::signature *sig);
void transAct(action act, coenv &e, types::ty *target);
void transAsType(coenv &e, types::ty *target);
void transWrite(coenv &e, types::ty *t, exp *value);
void transCall(coenv &e, types::ty *target);
};
class nameExp : public exp {
name *value;
public:
nameExp(position pos, name *value)
: exp(pos), value(value) {}
nameExp(position pos, symbol id)
: exp(pos), value(new simpleName(pos, id)) {}
nameExp(position pos, string s)
: exp(pos), value(new simpleName(pos, symbol::trans(s))) {}
void prettyprint(ostream &out, Int indent);
symbol getName()
{
return value->getName();
}
void transAsType(coenv &e, types::ty *target) {
value->varTrans(trans::READ, e, target);
// After translation, the cached type is no longer needed and should be
// garbage collected. This could presumably be done in every class derived
// from exp, but here it is most important as nameExp can have heavily
// overloaded types cached.
ct=0;
}
types::ty *trans(coenv &e) {
types::ty *t=cgetType(e);
if (t->kind == types::ty_error) {
em.error(getPos());
em << "no matching variable \'" << *value << "\'";
return types::primError();
}
if (t->kind == types::ty_overloaded) {
em.error(getPos());
em << "use of variable \'" << *value << "\' is ambiguous";
return types::primError();
}
else {
transAsType(e, t);
return t;
}
}
types::ty *getType(coenv &e) {
types::ty *t=value->varGetType(e);
return t ? t : types::primError();
}
trans::varEntry *getCallee(coenv &e, types::signature *sig) {
#ifdef DEBUG_GETAPP
cout << "nameExp" << endl;
#endif
return value->getCallee(e, sig);
}
void transWrite(coenv &e, types::ty *target, exp *newValue) {
newValue->transToType(e, target);
this->value->varTrans(trans::WRITE, e, target);
ct=0; // See note in transAsType.
}
void transCall(coenv &e, types::ty *target) {
value->varTrans(trans::CALL, e, target);
ct=0; // See note in transAsType.
}
exp *evaluate(coenv &, types::ty *) {
// Names have no side-effects.
return this;
}
};
// Most fields accessed are handled as parts of qualified names, but in cases
// like f().x or (new t).x, a separate expression is needed.
class fieldExp : public nameExp {
exp *object;
symbol field;
// fieldExp has a lot of common functionality with qualifiedName, so we
// essentially hack qualifiedName, by making our object expression look
// like a name.
class pseudoName : public name {
exp *object;
public:
pseudoName(exp *object)
: name(object->getPos()), object(object) {}
// As a variable:
void varTrans(trans::action act, coenv &e, types::ty *target) {
assert(act == trans::READ);
object->transToType(e, target);
}
types::ty *varGetType(coenv &e) {
return object->getType(e);
}
trans::varEntry *getCallee(coenv &, types::signature *) {
#ifdef DEBUG_GETAPP
cout << "pseudoName" << endl;
#endif
return 0;
}
// As a type:
types::ty *typeTrans(coenv &, bool tacit = false) {
if (!tacit) {
em.error(getPos());
em << "expression is not a type";
}
return types::primError();
}
trans::varEntry *getVarEntry(coenv &) {
em.compiler(getPos());
em << "expression cannot be used as part of a type";
return 0;
}
trans::tyEntry *tyEntryTrans(coenv &) {
em.compiler(getPos());
em << "expression cannot be used as part of a type";
return 0;
}
trans::frame *tyFrameTrans(coenv &) {
return 0;
}
void prettyprint(ostream &out, Int indent);
void print(ostream& out) const {
out << "<exp>";
}
symbol getName() {
return object->getName();
}
};
// Try to get this into qualifiedName somehow.
types::ty *getObject(coenv &e);
public:
fieldExp(position pos, exp *object, symbol field)
: nameExp(pos, new qualifiedName(pos,
new pseudoName(object),
field)),
object(object), field(field) {}
void prettyprint(ostream &out, Int indent);
symbol getName()
{
return field;
}
exp *evaluate(coenv &e, types::ty *) {
// Evaluate the object.
return new fieldExp(getPos(),
new tempExp(e, object, getObject(e)),
field);
}
};
class arrayExp : public exp {
protected:
exp *set;
array *getArrayType(coenv &e);
array *transArray(coenv &e);
public:
arrayExp(position pos, exp *set)
: exp(pos), set(set) {}
};
class subscriptExp : public arrayExp {
exp *index;
public:
subscriptExp(position pos, exp *set, exp *index)
: arrayExp(pos, set), index(index) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
void transWrite(coenv &e, types::ty *t, exp *value);
exp *evaluate(coenv &e, types::ty *) {
return new subscriptExp(getPos(),
new tempExp(e, set, getArrayType(e)),
new tempExp(e, index, types::primInt()));
}
};
class slice : public absyn {
exp *left;
exp *right;
public:
slice(position pos, exp *left, exp *right)
: absyn(pos), left(left), right(right) {}
void prettyprint(ostream &out, Int indent);
exp *getLeft() { return left; }
exp *getRight() { return right; }
// Translates code to put the left and right expressions on the stack (in that
// order). If left is omitted, zero is pushed on the stack in it's place. If
// right is omitted, nothing is pushed in its place.
void trans(coenv &e);
slice *evaluate(coenv &e) {
return new slice(getPos(),
left ? new tempExp(e, left, types::primInt()) : 0,
right ? new tempExp(e, right, types::primInt()) : 0);
}
};
class sliceExp : public arrayExp {
slice *index;
public:
sliceExp(position pos, exp *set, slice *index)
: arrayExp(pos, set), index(index) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
void transWrite(coenv &e, types::ty *t, exp *value);
exp *evaluate(coenv &e, types::ty *) {
return new sliceExp(getPos(),
new tempExp(e, set, getArrayType(e)),
index->evaluate(e));
}
};
// The expression "this," that evaluates to the lexically enclosing record.
class thisExp : public exp {
public:
thisExp(position pos)
: exp(pos) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
exp *evaluate(coenv &, types::ty *) {
// this has no side-effects
return this;
}
};
class literalExp : public exp {
public:
literalExp(position pos)
: exp(pos) {}
bool scalable() { return false; }
exp *evaluate(coenv &, types::ty *) {
// Literals are constant, they have no side-effects.
return this;
}
};
class intExp : public literalExp {
Int value;
public:
intExp(position pos, Int value)
: literalExp(pos), value(value) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primInt(); }
};
class realExp : public literalExp {
protected:
double value;
public:
realExp(position pos, double value)
: literalExp(pos), value(value) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primReal(); }
};
class stringExp : public literalExp {
string str;
public:
stringExp(position pos, string str)
: literalExp(pos), str(str) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primString(); }
const string& getString() { return str; }
};
class booleanExp : public literalExp {
bool value;
public:
booleanExp(position pos, bool value)
: literalExp(pos), value(value) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primBoolean(); }
};
class cycleExp : public literalExp {
public:
cycleExp(position pos)
: literalExp(pos) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primCycleToken(); }
};
class newPictureExp : public literalExp {
public:
newPictureExp(position pos)
: literalExp(pos) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primPicture(); }
};
class nullPathExp : public literalExp {
public:
nullPathExp(position pos)
: literalExp(pos) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primPath(); }
};
class nullExp : public literalExp {
public:
nullExp(position pos)
: literalExp(pos) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primNull(); }
};
class quoteExp : public exp {
runnable *value;
public:
quoteExp(position pos, runnable *value)
: exp(pos), value(value) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primCode(); }
};
// A list of expressions used in a function call.
class explist : public absyn {
typedef mem::vector<exp *> expvector;
expvector exps;
public:
explist(position pos)
: absyn(pos) {}
virtual ~explist() {}
virtual void add(exp *e) {
exps.push_back(e);
}
virtual void prettyprint(ostream &out, Int indent);
virtual size_t size() {
return exps.size();
}
virtual exp * operator[] (size_t index) {
return exps[index];
}
};
struct argument {
exp *val;
symbol name;
// No constructor due to the union in camp.y
#if 0
argument(exp *val=0, symbol name=0)
: val(val), name(name) {}
#endif
void prettyprint(ostream &out, Int indent);
};
class arglist : public gc {
public:
typedef mem::vector<argument> argvector;
argvector args;
argument rest;
// As the language allows named arguments after rest arguments, store the
// index of the rest argument in order to ensure proper left-to-right
// execution.
static const size_t DUMMY_REST_POSITION = 9999;
size_t restPosition;
arglist()
: args(), rest(), restPosition(DUMMY_REST_POSITION) {}
virtual ~arglist() {}
virtual void addFront(argument a) {
args.insert(args.begin(), a);
}
virtual void addFront(exp *val, symbol name=symbol::nullsym) {
argument a; a.val=val; a.name=name;
addFront(a);
}
virtual void add(argument a) {
if (rest.val && !a.name) {
em.error(a.val->getPos());
em << "unnamed argument after rest argument";
return;
}
args.push_back(a);
}
virtual void add(exp *val, symbol name=symbol::nullsym) {
argument a; a.val=val; a.name=name;
add(a);
}
virtual void addRest(argument a) {
if (rest.val) {
em.error(a.val->getPos());
em << "additional rest argument";
return;
}
rest = a;
assert(restPosition == DUMMY_REST_POSITION);
restPosition = size();
}
virtual void prettyprint(ostream &out, Int indent);
virtual size_t size() {
return args.size();
}
virtual argument& operator[] (size_t index) {
return args[index];
}
virtual argument& getRest() {
return rest;
}
};
// callExp has a global cache of resolved overloaded functions. This clears
// this cache so the associated data can be garbage collected.
void clearCachedCalls();
class callExp : public exp {
protected:
exp *callee;
arglist *args;
private:
// Per object caching - Cache the application when it's determined.
application *cachedApp;
// In special cases, no application object is needed and we can store the
// varEntry used in advance.
trans::varEntry *cachedVarEntry;
types::signature *argTypes(coenv& e, bool *searchable);
void reportArgErrors(coenv &e);
application *resolve(coenv &e,
types::overloaded *o,
types::signature *source,
bool tacit);
application *resolveWithCache(coenv &e,
types::overloaded *o,
types::signature *source,
bool tacit);
void reportMismatch(types::function *ft,
types::signature *source);
void reportNonFunction();
// Caches either the application object used to apply the function to the
// arguments, or in cases where the arguments match the function perfectly,
// the varEntry of the callee (or neither in case of an error). Returns
// what getType should return.
types::ty *cacheAppOrVarEntry(coenv &e, bool tacit);
types::ty *transPerfectMatch(coenv &e);
public:
callExp(position pos, exp *callee, arglist *args)
: exp(pos), callee(callee), args(args),
cachedApp(0), cachedVarEntry(0) { assert(args); }
callExp(position pos, exp *callee)
: exp(pos), callee(callee), args(new arglist()),
cachedApp(0), cachedVarEntry(0) {}
callExp(position pos, exp *callee, exp *arg1)
: exp(pos), callee(callee), args(new arglist()),
cachedApp(0), cachedVarEntry(0) {
args->add(arg1);
}
callExp(position pos, exp *callee, exp *arg1, exp *arg2)
: exp(pos), callee(callee), args(new arglist()),
cachedApp(0), cachedVarEntry(0) {
args->add(arg1);
args->add(arg2);
}
callExp(position pos, exp *callee, exp *arg1, exp *arg2, exp *arg3)
: exp(pos), callee(callee), args(new arglist()),
cachedApp(0), cachedVarEntry(0) {
args->add(arg1);
args->add(arg2);
args->add(arg3);
}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
// Returns true if the function call resolves uniquely without error. Used
// in implementing the special == and != operators for functions.
virtual bool resolved(coenv &e);
};
class pairExp : public exp {
exp *x;
exp *y;
public:
pairExp(position pos, exp *x, exp *y)
: exp(pos), x(x), y(y) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primPair(); }
};
class tripleExp : public exp {
exp *x;
exp *y;
exp *z;
public:
tripleExp(position pos, exp *x, exp *y, exp *z)
: exp(pos), x(x), y(y), z(z) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primTriple(); }
};
class transformExp : public exp {
exp *x;
exp *y;
exp *xx,*xy,*yx,*yy;
public:
transformExp(position pos, exp *x, exp *y, exp *xx, exp *xy, exp *yx,
exp *yy)
: exp(pos), x(x), y(y), xx(xx), xy(xy), yx(yx), yy(yy) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primTransform(); }
};
class castExp : public exp {
ty *target;
exp *castee;
types::ty *tryCast(coenv &e, types::ty *t, types::ty *s,
symbol csym);
public:
castExp(position pos, ty *target, exp *castee)
: exp(pos), target(target), castee(castee) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
};
class nullaryExp : public callExp {
public:
nullaryExp(position pos, symbol op)
: callExp(pos, new nameExp(pos, op)) {}
};
class unaryExp : public callExp {
public:
unaryExp(position pos, exp *base, symbol op)
: callExp(pos, new nameExp(pos, op), base) {}
};
class binaryExp : public callExp {
public:
binaryExp(position pos, exp *left, symbol op, exp *right)
: callExp(pos, new nameExp(pos, op), left, right) {}
};
class equalityExp : public callExp {
public:
equalityExp(position pos, exp *left, symbol op, exp *right)
: callExp(pos, new nameExp(pos, op), left, right) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
};
// Scaling expressions such as 3sin(x).
class scaleExp : public binaryExp {
exp *getLeft() {
return (*this->args)[0].val;
}
exp *getRight() {
return (*this->args)[1].val;
}
public:
scaleExp(position pos, exp *left, exp *right)
: binaryExp(pos, left, symbol::trans("*"), right) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
//types::ty *getType(coenv &e);
bool scalable() { return false; }
};
// Used for tension, which takes two real values, and a boolean to denote if it
// is a tension atleast case.
class ternaryExp : public callExp {
public:
ternaryExp(position pos, exp *left, symbol op, exp *right, exp *last)
: callExp(pos, new nameExp(pos, op), left, right, last) {}
};
// The a ? b : c ternary operator.
class conditionalExp : public exp {
exp *test;
exp *onTrue;
exp *onFalse;
public:
conditionalExp(position pos, exp *test, exp *onTrue, exp *onFalse)
: exp(pos), test(test), onTrue(onTrue), onFalse(onFalse) {}
void prettyprint(ostream &out, Int indent);
void baseTransToType(coenv &e, types::ty *target);
void transToType(coenv &e, types::ty *target);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
};
class andOrExp : public exp {
protected:
exp *left;
symbol op;
exp *right;
public:
andOrExp(position pos, exp *left, symbol op, exp *right)
: exp(pos), left(left), op(op), right(right) {}
virtual types::ty *trans(coenv &e) = 0;
virtual types::ty *getType(coenv &) {
return types::primBoolean();
}
};
class orExp : public andOrExp {
public:
orExp(position pos, exp *left, symbol op, exp *right)
: andOrExp(pos, left, op, right) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
void transConditionalJump(coenv &e, bool cond, label dest);
};
class andExp : public andOrExp {
public:
andExp(position pos, exp *left, symbol op, exp *right)
: andOrExp(pos, left, op, right) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
void transConditionalJump(coenv &e, bool cond, label dest);
};
class joinExp : public callExp {
public:
joinExp(position pos, symbol op)
: callExp(pos, new nameExp(pos, op)) {}
void pushFront(exp *e) {
args->addFront(e);
}
void pushBack(exp *e) {
args->add(e);
}
void prettyprint(ostream &out, Int indent);
};
class specExp : public exp {
symbol op;
exp *arg;
camp::side s;
public:
specExp(position pos, symbol op, exp *arg, camp::side s=camp::OUT)
: exp(pos), op(op), arg(arg), s(s) {}
void setSide(camp::side ss) {
s=ss;
}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
};
class assignExp : public exp {
protected:
exp *dest;
exp *value;
// This is basically a hook to facilitate selfExp. dest is given as an
// argument since it will be a temporary in translation in order to avoid
// multiple evaluation.
virtual exp *ultimateValue(exp *) {
return value;
}
public:
assignExp(position pos, exp *dest, exp *value)
: exp(pos), dest(dest), value(value) {}
void prettyprint(ostream &out, Int indent);
// Don't write the result of an assignment to the prompt.
bool writtenToPrompt() { return false; }
void transAsType(coenv &e, types::ty *target);
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
};
class selfExp : public assignExp {
symbol op;
exp *ultimateValue(exp *dest) {
return new binaryExp(getPos(), dest, op, value);
}
public:
selfExp(position pos, exp *dest, symbol op, exp *value)
: assignExp(pos, dest, value), op(op) {}
void prettyprint(ostream &out, Int indent);
void transAsType(coenv &e, types::ty *target);
};
class prefixExp : public exp {
exp *dest;
symbol op;
public:
prefixExp(position pos, exp *dest, symbol op)
: exp(pos), dest(dest), op(op) {}
void prettyprint(ostream &out, Int indent);
bool scalable() { return false; }
// Don't write the result to the prompt.
bool writtenToPrompt() { return false; }
types::ty *trans(coenv &e);
types::ty *getType(coenv &e);
};
// Postfix expresions are illegal. This is caught here as we can give a
// more meaningful error message to the user, rather than a "parse
// error."
class postfixExp : public exp {
exp *dest;
symbol op;
public:
postfixExp(position pos, exp *dest, symbol op)
: exp(pos), dest(dest), op(op) {}
void prettyprint(ostream &out, Int indent);
types::ty *trans(coenv &e);
types::ty *getType(coenv &) { return types::primError(); }
};
} // namespace absyntax
#endif
|