comparison ast/Exp.d @ 139:a22e3663de89

Fixed up our simplify functions Removed some unused stuff and changed all function calls to have () attached. We should only omit parens when calling something that is supposed to be a property - not a function like simplify
author Anders Halager <halager@gmail.com>
date Fri, 18 Jul 2008 13:32:34 +0200
parents 2be29b296081
children a14ac9e5c858
comparison
equal deleted inserted replaced
138:b61de188cd0d 139:a22e3663de89
102 DFunction f = cast(DFunction)exp.type(); 102 DFunction f = cast(DFunction)exp.type();
103 assert(f !is null, "Can only call functions"); 103 assert(f !is null, "Can only call functions");
104 return f.returnType; 104 return f.returnType;
105 } 105 }
106 106
107 override CallExp simplify()
108 {
109 foreach (ref arg; args)
110 arg = arg.simplify();
111 exp = exp.simplify();
112 return this;
113 }
114
107 Exp exp; 115 Exp exp;
108 Exp[] args; 116 Exp[] args;
109 bool sret = false; 117 bool sret = false;
110 118
111 override SourceRange sourceRange() 119 override SourceRange sourceRange()
113 SourceRange res = exp.sourceRange; 121 SourceRange res = exp.sourceRange;
114 if (args.length > 0) 122 if (args.length > 0)
115 res = res + args[$ - 1].sourceRange; 123 res = res + args[$ - 1].sourceRange;
116 return res; 124 return res;
117 } 125 }
118
119 Exp simplify()
120 {
121 /*
122 if(auto t = type.asStruct)
123 {
124 DFunction func_t = cast(DFunction)exp.type();
125 assert(func_t !is null, "Calling on something that isn't a function");
126 if (cast(DStruct)func_t.returnType is null)
127 return this;
128
129 auto call = cast(Identifier)exp;
130 FuncDecl f = env.parentFunction;
131 auto i = new Identifier("temp.var");
132 i.env = f.env;
133 f.env.add(i);
134 f.env.find(i).setType(t);
135 auto ty = new Identifier(t.name);
136 auto var = new VarDecl(ty, i, null);
137 Exp[] args;
138 args ~= i;
139 args ~= this.args;
140 auto callExp = new CallExp(exp, args);
141 callExp.env = f.env;
142 var.env = f.env;
143 auto stmtVar = new DeclStmt(var);
144 auto stmtCall = new ExpStmt(callExp);
145 Stmt[] stmts;
146 foreach( index, s ; f.statements)
147 {
148 if(stmtIndex == index)
149 {
150 stmts ~= stmtVar;
151 stmts ~= stmtCall;
152 }
153 stmts ~= s;
154 }
155 f.statements = stmts;
156 callExp.sret = true;
157
158 return i;
159 }
160 */
161 return this;
162 }
163 } 126 }
164 127
165 class AssignExp : BinaryExp 128 class AssignExp : BinaryExp
166 { 129 {
167 this(SLoc op_loc, Operator op, Exp identifier, Exp exp) 130 this(SLoc op_loc, Operator op, Exp identifier, Exp exp)
169 super(ExpType.AssignExp, op_loc, op, identifier, exp); 132 super(ExpType.AssignExp, op_loc, op, identifier, exp);
170 this.identifier = identifier; 133 this.identifier = identifier;
171 this.exp = exp; 134 this.exp = exp;
172 } 135 }
173 136
174 Exp simplify() 137 override AssignExp simplify()
175 { 138 {
176 identifier = identifier.simplify; 139 identifier = identifier.simplify();
177 exp = exp.simplify; 140 exp = exp.simplify();
178 141
179 return this; 142 return this;
180 } 143 }
181 144
182 override SourceRange sourceRange() 145 override SourceRange sourceRange()
273 if (op >= Operator.Eq && op <= Operator.Ge) 236 if (op >= Operator.Eq && op <= Operator.Ge)
274 return "bool"; 237 return "bool";
275 return null; 238 return null;
276 } 239 }
277 240
278 Exp simplify() 241 override BinaryExp simplify()
279 { 242 {
280 left = left.simplify; 243 left = left.simplify();
281 right = right.simplify; 244 right = right.simplify();
282 return this; 245 return this;
283 } 246 }
284 247
285 Operator op; 248 Operator op;
286 Exp left, right; 249 Exp left, right;
293 { 256 {
294 super(ExpType.Negate, op); 257 super(ExpType.Negate, op);
295 this.exp = exp; 258 this.exp = exp;
296 } 259 }
297 260
298 Exp simplify() 261 override NegateExp simplify()
299 { 262 {
300 exp = exp.simplify; 263 exp = exp.simplify();
301 return this; 264 return this;
302 } 265 }
303 266
304 override DType type() { return exp.type(); } 267 override DType type() { return exp.type(); }
305 268
317 { 280 {
318 super(ExpType.Deref, op); 281 super(ExpType.Deref, op);
319 this.exp = exp; 282 this.exp = exp;
320 } 283 }
321 284
322 Exp simplify() 285 override DerefExp simplify()
323 { 286 {
324 exp = exp.simplify; 287 exp = exp.simplify();
325 return this; 288 return this;
326 } 289 }
327 290
328 override DType type() 291 override DType type()
329 { 292 {
350 char[] get() 313 char[] get()
351 { 314 {
352 return name; 315 return name;
353 } 316 }
354 317
355 Exp simplify() 318 override IntegerLit simplify()
356 { 319 {
357 return this; 320 return this;
358 } 321 }
359 322
360 override DType type() 323 override DType type()
410 if (s !is null) 373 if (s !is null)
411 return s.findMember(child.get); 374 return s.findMember(child.get);
412 return null; 375 return null;
413 } 376 }
414 377
415 Exp simplify() 378 override MemberReference simplify()
416 { 379 {
417 target = target.simplify; 380 target = target.simplify();
418 return this; 381 return this;
419 } 382 }
420 383
421 override DType type() 384 override DType type()
422 { 385 {
469 override SourceRange sourceRange() 432 override SourceRange sourceRange()
470 { 433 {
471 return target.sourceRange + SourceRange(right_bracket); 434 return target.sourceRange + SourceRange(right_bracket);
472 } 435 }
473 436
474 Exp simplify() 437 override IndexExp simplify()
475 { 438 {
476 target = target.simplify; 439 target = target.simplify();
477 index = index.simplify; 440 index = index.simplify();
478 return this; 441 return this;
479 } 442 }
480 443
481 Exp target; 444 Exp target;
482 Exp index; 445 Exp index;
495 override DType type() 458 override DType type()
496 { 459 {
497 return env.findType(this.castType); 460 return env.findType(this.castType);
498 } 461 }
499 462
500 Exp simplify() 463 override CastExp simplify()
501 { 464 {
502 castType.simplify; 465 castType = castType.simplify();
503 exp.simplify; 466 exp = exp.simplify();
504 return this; 467 return this;
505 } 468 }
506 469
507 override SourceRange sourceRange() 470 override SourceRange sourceRange()
508 { 471 {
654 if (auto id = cast(Identifier)o) 617 if (auto id = cast(Identifier)o)
655 return typeid(char[]).equals(&name, &id.name); 618 return typeid(char[]).equals(&name, &id.name);
656 return 0; 619 return 0;
657 } 620 }
658 621
659 Exp simplify() 622 override Identifier simplify()
660 { 623 {
661 return this; 624 return this;
662 } 625 }
663 626
664 void setType(DType myType) 627 void setType(DType myType)