Changeset 332
- Timestamp:
- 07/22/08 11:18:20 (2 months ago)
- Files:
-
- branches/v2new/compile.bat (modified) (1 diff)
- branches/v2new/minid/alloc.d (modified) (1 diff)
- branches/v2new/minid/ast.d (modified) (4 diffs)
- branches/v2new/minid/astvisitor.d (modified) (1 diff)
- branches/v2new/minid/compiler.d (modified) (1 diff)
- branches/v2new/minid/interpreter.d (modified) (1 diff)
- branches/v2new/minid/parser.d (modified) (1 diff)
- branches/v2new/minid/semantic.d (modified) (3 diffs)
- branches/v2new/samples/simple.md (modified) (1 diff)
- branches/v2new/test.d (modified) (2 diffs)
- branches/v2new/test.opt (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/v2new/compile.bat
r325 r332 1 1 @echo off 2 2 build test.d -clean -debug -g -unittest -w -version=MDExtendedCoro 3 rem build test.d -clean -release -w 3 rem build test.d -clean -release -w -version=MDExtendedCoro branches/v2new/minid/alloc.d
r329 r332 208 208 { 209 209 if(oldSize > 0 && _memBlocks.lookup(p) is null) 210 throw new Exception("AWFUL: You're trying to free something that wasn't allocated on the MiniD Heap !");210 throw new Exception("AWFUL: You're trying to free something that wasn't allocated on the MiniD Heap, or are performing a double free!"); 211 211 212 212 auto ret = reallocImpl(p, oldSize, newSize); branches/v2new/minid/ast.d
r331 r332 83 83 "DecStmt", 84 84 "FuncEnvStmt", 85 "AppendStmt", 85 86 86 87 "CondExp", … … 143 144 "SuperCallExp", 144 145 "RawNamespaceExp", 145 "InternalArrayComp",146 "InternalTableComp",147 146 148 147 "ForeachComprehension", … … 1777 1776 1778 1777 /** 1778 This node is an internal node used when rewriting array comprehensions. It is used to append the 1779 values of the comprehension onto the array being built, and differs from a CatAssignStmt in that 1780 arrays will be appended as single elements, rather than having their elements concatenated. 1781 */ 1782 class AppendStmt : Statement 1783 { 1784 /** 1785 */ 1786 public Identifier arrayName; 1787 1788 /** 1789 */ 1790 public Expression value; 1791 1792 /** 1793 */ 1794 public this(ICompiler c, CompileLoc location, Identifier arrayName, Expression value) 1795 { 1796 super(c, location, location, AstTag.AppendStmt); 1797 this.arrayName = arrayName; 1798 this.value = value; 1799 } 1800 } 1801 1802 /** 1779 1803 The base class for all expressions. 1780 1804 */ … … 3145 3169 this.parent = parent; 3146 3170 this.attrs = attrs; 3147 }3148 }3149 3150 /**3151 An internal AST node that is what TableComprehensions get transformed into during the semantic pass.3152 */3153 class InternalTableComp : PrimaryExp3154 {3155 /**3156 */3157 public Statement loop;3158 3159 public this(ICompiler c, CompileLoc location, CompileLoc endLocation, Statement loop)3160 {3161 super(c, location, endLocation, AstTag.InternalTableComp);3162 this.loop = loop;3163 }3164 }3165 3166 /**3167 An internal AST node that is what ArrayComprehensions get transformed into during the semantic pass.3168 */3169 class InternalArrayComp : PrimaryExp3170 {3171 /**3172 */3173 public Statement loop;3174 3175 public this(ICompiler c, CompileLoc location, CompileLoc endLocation, Statement loop)3176 {3177 super(c, location, endLocation, AstTag.InternalArrayComp);3178 this.loop = loop;3179 3171 } 3180 3172 } branches/v2new/minid/astvisitor.d
r330 r332 432 432 return s; 433 433 } 434 } 434 435 public override Expression visit(ArrayCtorExp e) 436 { 437 Stdout("["); 438 439 foreach(f; e.values) 440 visit(f); 441 442 Stdout("]"); 443 444 return e; 445 } 446 447 public override Statement visit(ForeachStmt s) 448 { 449 Stdout("foreach("); 450 visit(s.indices[0]); 451 452 foreach(idx; s.indices[1 .. $]) 453 { 454 Stdout(", "); 455 visit(idx); 456 } 457 458 Stdout("; "); 459 visit(s.container[0]); 460 461 foreach(con; s.container[1 .. $]) 462 { 463 Stdout(", "); 464 visit(con); 465 } 466 467 Stdout(")"); 468 visit(s.code); 469 return s; 470 } 471 472 public override Statement visit(IfStmt s) 473 { 474 Stdout("if("); 475 476 if(s.condVar) 477 { 478 Stdout("local "); 479 visit(s.condVar); 480 Stdout(" = "); 481 } 482 483 visit(s.condition); 484 Stdout(")"); 485 visit(s.ifBody); 486 487 if(s.elseBody) 488 { 489 Stdout("else"); 490 visit(s.elseBody); 491 } 492 493 return s; 494 } 495 496 public override Expression visit(LTExp e) 497 { 498 visit(e.op1); 499 Stdout(" < "); 500 visit(e.op2); 501 return e; 502 } 503 504 public override Statement visit(AppendStmt s) 505 { 506 Stdout("append "); 507 visit(s.arrayName); 508 Stdout(", "); 509 visit(s.value); 510 return s; 511 } 512 513 public override Expression visit(TableCtorExp e) 514 { 515 Stdout("{"); 516 517 foreach(ref f; e.fields) 518 { 519 Stdout("["); 520 visit(f.key); 521 Stdout("] = "); 522 visit(f.value); 523 Stdout(","); 524 } 525 526 Stdout("}"); 527 return e; 528 } 529 530 public override Expression visit(IndexExp e) 531 { 532 visit(e.op); 533 Stdout("["); 534 visit(e.index); 535 Stdout("]"); 536 return e; 537 } 538 } branches/v2new/minid/compiler.d
r331 r332 175 175 scope sem = new Semantic(this); 176 176 mod = sem.visit(mod); 177 178 scope test = new TestVisitor(this);179 test.visit(mod);177 178 //scope test = new TestVisitor(this); 179 //test.visit(mod); 180 180 } 181 181 branches/v2new/minid/interpreter.d
r331 r332 2760 2760 private void checkNumParams(MDThread* t, uword n) 2761 2761 { 2762 assert(t.stackIndex > t.stackBase, (printStack(t), printCallStack(t), "fail."));2762 debug assert(t.stackIndex > t.stackBase, (printStack(t), printCallStack(t), "fail.")); 2763 2763 2764 2764 // Don'_t count 'this' branches/v2new/minid/parser.d
r331 r332 2680 2680 return new(c) Identifier(c, loc, str); 2681 2681 } 2682 2682 2683 2683 private Identifier dummyFuncLiteralName(CompileLoc loc) 2684 2684 { branches/v2new/minid/semantic.d
r331 r332 1065 1065 case AstTag.InExp: auto old = e.as!(InExp); return new(c) NotInExp(c, e.location, e.endLocation, old.op1, old.op2); 1066 1066 case AstTag.NotInExp: auto old = e.as!(NotInExp); return new(c) InExp(c, e.location, e.endLocation, old.op1, old.op2); 1067 case AstTag.NotExp: return e.op; 1067 1068 // TODO: what about multiple 'not's? "!!x" 1068 1069 1069 1070 default: … … 1317 1318 e.value = visit(e.value); 1318 1319 e.forComp = visitForComp(e.forComp); 1319 return e; 1320 } 1321 1322 public override ArrayComprehension visit(ArrayComprehension e) 1320 1321 /* 1322 Rewrite: 1323 1324 x = {[v] = k for k, v in y if v < 10} 1325 1326 as: 1327 1328 x = (function() 1329 { 1330 local __temp = {} 1331 1332 foreach(k, v; y) 1333 if(v < 10) 1334 __temp[v] = k 1335 1336 return __temp 1337 })() 1338 */ 1339 1340 // (function() 1341 scope funcBody = new List!(Statement)(c.alloc); 1342 1343 { 1344 auto __temp = new(c) Identifier(c, e.location, c.newString("__temp")); 1345 1346 // local __temp = {} 1347 { 1348 scope dummy = new List!(Identifier)(c.alloc); 1349 dummy ~= __temp; 1350 auto init = new(c) TableCtorExp(c, e.location, e.endLocation, null); 1351 funcBody ~= new(c) VarDecl(c, e.location, e.endLocation, Protection.Local, dummy.toArray(), init); 1352 } 1353 1354 // foreach(i; y) 1355 // if(i < 10) 1356 // __temp[v] = k 1357 { 1358 scope dummy = new List!(Expression)(c.alloc); 1359 dummy ~= new(c) IndexExp(c, e.endLocation, new(c) IdentExp(c, __temp), e.key); 1360 auto idxa = new(c) AssignStmt(c, e.location, e.endLocation, dummy.toArray(), e.value); 1361 funcBody ~= rewriteForComp(e.forComp, idxa); 1362 } 1363 1364 1365 // return __temp 1366 { 1367 scope dummy = new List!(Expression)(c.alloc); 1368 dummy ~= new(c) IdentExp(c, __temp); 1369 funcBody ~= new(c) ReturnStmt(c, e.location, e.location, dummy.toArray()); 1370 } 1371 } 1372 1373 auto _body = new(c) BlockStmt(c, e.location, e.endLocation, funcBody.toArray()); 1374 auto funcDef = new(c) FuncDef(c, e.location, dummyComprehensionName!("table")(e.location), null, false, _body); 1375 auto funcExp = new(c) FuncLiteralExp(c, e.location, funcDef); 1376 return new(c) CallExp(c, e.endLocation, funcExp, null, null); 1377 } 1378 1379 public override Expression visit(ArrayComprehension e) 1323 1380 { 1324 1381 e.exp = visit(e.exp); 1325 1382 e.forComp = visitForComp(e.forComp); 1326 return e; 1327 } 1328 1383 1384 /* 1385 Rewrite: 1386 1387 x = [i for i in y if i < 10] 1388 1389 as: 1390 1391 x = (function() 1392 { 1393 local __temp = [] 1394 1395 foreach(i; y) 1396 if(i < 10) 1397 append __temp, i 1398 1399 return __temp 1400 })() 1401 */ 1402 1403 // (function() 1404 scope funcBody = new List!(Statement)(c.alloc); 1405 1406 { 1407 auto __temp = new(c) Identifier(c, e.location, c.newString("__temp")); 1408 1409 // local __temp = [] 1410 { 1411 scope dummy = new List!(Identifier)(c.alloc); 1412 dummy ~= __temp; 1413 auto init = new(c) ArrayCtorExp(c, e.location, e.endLocation, null); 1414 funcBody ~= new(c) VarDecl(c, e.location, e.endLocation, Protection.Local, dummy.toArray(), init); 1415 } 1416 1417 // append __temp, i 1418 auto append = new(c) AppendStmt(c, e.location, __temp, e.exp); 1419 1420 // foreach(i; y) 1421 // if(i < 10) 1422 funcBody ~= rewriteForComp(e.forComp, append); 1423 1424 // return __temp 1425 { 1426 scope dummy = new List!(Expression)(c.alloc); 1427 dummy ~= new(c) IdentExp(c, __temp); 1428 funcBody ~= new(c) ReturnStmt(c, e.location, e.location, dummy.toArray()); 1429 } 1430 } 1431 1432 auto _body = new(c) BlockStmt(c, e.location, e.endLocation, funcBody.toArray()); 1433 auto funcDef = new(c) FuncDef(c, e.location, dummyComprehensionName!("array")(e.location), null, false, _body); 1434 auto funcExp = new(c) FuncLiteralExp(c, e.location, funcDef); 1435 return new(c) CallExp(c, e.endLocation, funcExp, null, null); 1436 } 1437 1438 private Statement rewriteForComp(ForComprehension e, Statement inner) 1439 { 1440 if(auto x = e.as!(ForeachComprehension)) 1441 return rewrite(x, inner); 1442 else 1443 { 1444 auto x = e.as!(ForNumComprehension); 1445 assert(x !is null); 1446 return rewrite(x, inner); 1447 } 1448 } 1449 1450 private Statement rewrite(ForeachComprehension e, Statement inner) 1451 { 1452 if(e.ifComp) 1453 { 1454 if(e.forComp) 1455 inner = rewrite(e.ifComp, rewriteForComp(e.forComp, inner)); 1456 else 1457 inner = rewrite(e.ifComp, inner); 1458 } 1459 else if(e.forComp) 1460 inner = rewriteForComp(e.forComp, inner); 1461 1462 auto indices = e.indices; 1463 auto cont = e.container; 1464 e.indices = null; 1465 e.container = null; 1466 return new(c) ForeachStmt(c, e.location, indices, cont, inner); 1467 } 1468 1469 private Statement rewrite(ForNumComprehension e, Statement inner) 1470 { 1471 if(e.ifComp) 1472 { 1473 if(e.forComp) 1474 inner = rewrite(e.ifComp, rewriteForComp(e.forComp, inner)); 1475 else 1476 inner = rewrite(e.ifComp, inner); 1477 } 1478 else if(e.forComp) 1479 inner = rewriteForComp(e.forComp, inner); 1480 1481 return new(c) ForNumStmt(c, e.location, e.index, e.lo, e.hi, e.step, inner); 1482 } 1483 1484 private Statement rewrite(IfComprehension e, Statement inner) 1485 { 1486 return new(c) IfStmt(c, e.location, e.endLocation, null, e.condition, inner, null); 1487 } 1488 1329 1489 public ForComprehension visitForComp(ForComprehension e) 1330 1490 { … … 1375 1535 return e; 1376 1536 } 1537 1538 package Identifier dummyComprehensionName(dchar[] type)(CompileLoc loc) 1539 { 1540 pushFormat(c.thread, "<" ~ type ~ " comprehension at {}({}:{})>", loc.file, loc.line, loc.col); 1541 auto str = c.newString(getString(c.thread, -1)); 1542 pop(c.thread); 1543 return new(c) Identifier(c, loc, str); 1544 } 1377 1545 } branches/v2new/samples/simple.md
r331 r332 1 1 module simple 2 3 local x = {[v] = k for k, v in y if v < 10} 2 4 3 5 /+ branches/v2new/test.d
r331 r332 33 33 auto vm = new MDVM; 34 34 auto t = openVM(vm); 35 35 36 36 uword memSize; 37 37 … … 53 53 // Timer.init(t); 54 54 // 55 // auto funcReg = loadFunc(t, `samples/s imple.md`);55 // auto funcReg = loadFunc(t, `samples/speed.md`); 56 56 // pushNull(t); 57 57 // rawCall(t, funcReg, 0);
