forked from eth-sri/bayonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.d
300 lines (256 loc) · 7.66 KB
/
expression.d
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
// Written in the D programming language.
import std.array, std.algorithm, std.range, std.conv, std.string;
import lexer, parser, scope_, declaration, util;
enum SemState{
initial,
started,
completed,
error,
}
abstract class Node{
// debug auto cccc=0;
Location loc;
abstract @property string kind();
// semantic information
SemState sstate;
}
abstract class Expression: Node{
int brackets=0;
override string toString(){return _brk("{}()");}
protected string _brk(string s){return std.array.replicate("(",brackets)~s~std.array.replicate(")",brackets); return s;}
override @property string kind(){return "expression";}
bool isCompound(){ return false; }
}
class TypeAnnotationExp: Expression{
Expression e,t;
this(Expression e, Expression t){
this.e=e; this.t=t;
}
override @property string kind(){ return e.kind; }
override string toString(){ return _brk(e.toString()~": "~t.toString()); }
}
// workaround for the bug:
UnaryExp!(Tok!"&") isAddressExp(Expression self){return cast(UnaryExp!(Tok!"&"))self;}
class ErrorExp: Expression{
this(){}//{sstate = SemState.error;}
override string toString(){return _brk("__error");}
}
class LiteralExp: Expression{
Token lit;
this(Token lit){ // TODO: suitable contract
this.lit=lit;
}
override string toString(){
return lit.toString();
}
}
class Identifier: Expression{
string name;
// alias name this; // stupid compiler bug prevents this from being useful
@property auto ptr(){return name.ptr;}
@property auto length(){return name.length;}
this(string name){ // TODO: make more efficient, this is a bottleneck!
static string[string] uniq;
auto n=uniq.get(name,null);
if(n !is null) this.name = n;
else this.name = uniq[name] = name;
static int x = 0;
}
override string toString(){return _brk(name);}
override @property string kind(){return "identifier";}
// semantic information:
Declaration meaning;
Scope scope_;
}
class PlaceholderExp: Expression{
Identifier ident;
this(Identifier ident){ this.ident = ident; }
override string toString(){ return _brk("?"); }
override @property string kind(){ return "placeholder"; }
}
class UnaryExp(TokenType op): Expression{
Expression e;
this(Expression next){e = next;}
override string toString(){
import std.uni;
return _brk(TokChars!op~(TokChars!op[$-1].isAlpha()?" ":"")~e.toString());
}
static if(op==Tok!"&"){
override @property string kind(){
return "address";
}
//override UnaryExp!(Tok!"&") isAddressExp(){return this;}
}
}
class PostfixExp(TokenType op): Expression{
Expression e;
this(Expression next){e = next;}
override string toString(){return _brk(e.toString()~TokChars!op);}
}
class IndexExp: Expression{ //e[a...]
Expression e;
Expression[] a;
this(Expression exp, Expression[] args){e=exp; a=args;}
override string toString(){
return _brk(e.toString()~'['~join(map!(to!string)(a),",")~']');
}
}
class CallExp: Expression{
Expression e;
Expression[] args;
this(Expression exp, Expression[] args){e=exp; this.args=args;}
override string toString(){
return _brk(e.toString()~'('~join(map!(to!string)(args),",")~')');
}
}
abstract class ABinaryExp: Expression{
Expression e1,e2;
this(Expression left, Expression right){e1=left; e2=right;}
abstract @property string operator();
}
class BinaryExp(TokenType op): ABinaryExp{
this(Expression left, Expression right){super(left,right);}
static if(op==Tok!"@") static string delegate(Expression,Expression) toStringImpl;
override string toString(){
static if(op==Tok!"@") if(toStringImpl) return toStringImpl(e1,e2);
return _brk(e1.toString() ~ " "~TokChars!op~" "~e2.toString());
}
//override string toString(){return e1.toString() ~ " "~ e2.toString~TokChars!op;} // RPN
override @property string operator(){
return TokChars!op;
}
}
class IDBinaryExp: ABinaryExp{
Identifier op;
this(Expression left, Identifier op,Expression right){super(left,right); this.op=op;}
override string toString(){
return _brk(e1.toString() ~ " "~op.toString()~" "~e2.toString());
}
//override string toString(){return e1.toString() ~ " "~ e2.toString~TokChars!op;} // RPN
override @property string operator(){
return op.toString();
}
}
class FieldExp: Expression{
Expression e;
Identifier f;
this(Expression e,Identifier f){ this.e=e; this.f=f; }
override string toString(){
return _brk(e.toString()~"."~f.toString());
}
}
class IteExp: Expression{
Expression cond;
CompoundExp then, othw;
this(Expression cond, CompoundExp then, CompoundExp othw){
this.cond=cond; this.then=then; this.othw=othw;
}
override string toString(){return _brk("if "~cond.toString() ~ " " ~ then.toString() ~ (othw?" else " ~ (othw.s.length==1&&cast(IteExp)othw.s[0]?othw.s[0].toString():othw.toString()):""));}
override bool isCompound(){ return true; }
}
class RepeatExp: Expression{
Expression num;
CompoundExp bdy;
this(Expression num, CompoundExp bdy){
this.num=num; this.bdy=bdy;
}
override string toString(){ return _brk("repeat "~num.toString()~" "~bdy.toString()); }
override bool isCompound(){ return true; }
}
class CompoundExp: Expression{
Expression[] s;
this(Expression[] ss){s=ss;}
override string toString(){return "{\n"~indent(join(map!(a=>a.toString()~(a.isCompound()?"":";"))(s),"\n"))~"\n}";}
override bool isCompound(){ return true; }
// semantic information
BlockScope blscope_;
}
class TupleExp: Expression{
Expression[] e;
this(Expression[] e){
this.e=e;
}
override string toString(){ return "("~e.map!(to!string).join(",")~")"; }
final @property size_t length(){ return e.length; }
}
class LambdaExp: Expression{
FunctionDef fd;
this(FunctionDef fd){
this.fd=fd;
}
override string toString(){
return "("~join(map!(to!string)(fd.params),",")~")"~fd.body_.toString();
}
}
class ArrayExp: Expression{
Expression[] e;
this(Expression[] e){
this.e=e;
}
override string toString(){ return "["~e.map!(to!string).join(",")~"]";}
}
class ReturnExp: Expression{
Expression e;
this(Expression e){
this.e=e;
}
override string toString(){ return "return"~(e?" "~e.toString():""); }
string expected;
}
class ForExp: Expression{
Identifier var;
bool leftExclusive;
Expression left;
bool rightExclusive;
Expression right;
CompoundExp bdy;
this(Identifier var,bool leftExclusive,Expression left,bool rightExclusive,Expression right,CompoundExp bdy){
this.var=var;
this.leftExclusive=leftExclusive; this.left=left;
this.rightExclusive=rightExclusive; this.right=right;
this.bdy=bdy;
}
override string toString(){ return _brk("for "~var.toString()~" in "~
(leftExclusive?"(":"[")~left.toString()~".."~right.toString()~
(rightExclusive?")":"]")~bdy.toString()); }
override @property string kind(){ return "for loop"; }
override bool isCompound(){ return true; }
}
class AssertExp: Expression{
Expression e;
this(Expression e){
this.e=e;
}
override string toString(){ return "assert("~e.toString()~")"; }
}
class ObserveExp: Expression{
Expression e;
this(Expression e){
this.e=e;
}
override string toString(){ return "observe("~e.toString()~")"; }
}
class CObserveExp: Expression{
Expression var;
Expression val;
this(Expression var,Expression val){
this.var=var; this.val=val;
}
override string toString(){ return "cobserve("~var.toString()~","~val.toString()~")"; }
}
class BuiltInExp: Expression{
TokenType which;
this(TokenType which)in{assert(util.among(which,Tok!"new",Tok!"fwd",Tok!"dup",Tok!"drop",Tok!"FwdQ",Tok!"RunSw"));}body{
this.which=which;
}
override string toString(){ return TokenTypeToString(which); }
}
class AtExp: Expression{
Identifier name;
Expression node;
this(Identifier name, Expression node){
this.name=name;
this.node=node;
}
override string toString(){ return _brk(name.toString()~"@"~node.toString()); }
}