Changeset 332

Show
Ignore:
Timestamp:
07/22/08 11:18:20 (2 months ago)
Author:
JarrettBillingsley
Message:

Finished semantic analysis.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/v2new/compile.bat

    r325 r332  
    11@echo off 
    22build test.d -clean -debug -g -unittest -w -version=MDExtendedCoro 
    3 rem build test.d -clean -release -w 
     3rem build test.d -clean -release -w -version=MDExtendedCoro 
  • branches/v2new/minid/alloc.d

    r329 r332  
    208208                { 
    209209                    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!"); 
    211211     
    212212                    auto ret = reallocImpl(p, oldSize, newSize); 
  • branches/v2new/minid/ast.d

    r331 r332  
    8383    "DecStmt", 
    8484    "FuncEnvStmt", 
     85    "AppendStmt", 
    8586 
    8687    "CondExp", 
     
    143144    "SuperCallExp", 
    144145    "RawNamespaceExp", 
    145     "InternalArrayComp", 
    146     "InternalTableComp", 
    147146 
    148147    "ForeachComprehension", 
     
    17771776 
    17781777/** 
     1778This node is an internal node used when rewriting array comprehensions.  It is used to append the 
     1779values of the comprehension onto the array being built, and differs from a CatAssignStmt in that 
     1780arrays will be appended as single elements, rather than having their elements concatenated. 
     1781*/ 
     1782class 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/** 
    17791803The base class for all expressions. 
    17801804*/ 
     
    31453169        this.parent = parent; 
    31463170        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 : PrimaryExp 
    3154 { 
    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 : PrimaryExp 
    3170 { 
    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; 
    31793171    } 
    31803172} 
  • branches/v2new/minid/astvisitor.d

    r330 r332  
    432432        return s; 
    433433    } 
    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  
    175175        scope sem = new Semantic(this); 
    176176        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); 
    180180    } 
    181181 
  • branches/v2new/minid/interpreter.d

    r331 r332  
    27602760private void checkNumParams(MDThread* t, uword n) 
    27612761{ 
    2762     assert(t.stackIndex > t.stackBase, (printStack(t), printCallStack(t), "fail.")); 
     2762    debug assert(t.stackIndex > t.stackBase, (printStack(t), printCallStack(t), "fail.")); 
    27632763 
    27642764    // Don'_t count 'this' 
  • branches/v2new/minid/parser.d

    r331 r332  
    26802680        return new(c) Identifier(c, loc, str); 
    26812681    } 
    2682      
     2682 
    26832683    private Identifier dummyFuncLiteralName(CompileLoc loc) 
    26842684    { 
  • branches/v2new/minid/semantic.d

    r331 r332  
    10651065            case AstTag.InExp:       auto old = e.as!(InExp);       return new(c) NotInExp(c, e.location, e.endLocation, old.op1, old.op2); 
    10661066            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" 
    10681069 
    10691070            default: 
     
    13171318        e.value = visit(e.value); 
    13181319        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) 
    13231380    { 
    13241381        e.exp = visit(e.exp); 
    13251382        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 
    13291489    public ForComprehension visitForComp(ForComprehension e) 
    13301490    { 
     
    13751535        return e; 
    13761536    } 
     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    } 
    13771545} 
  • branches/v2new/samples/simple.md

    r331 r332  
    11module simple 
     2 
     3local x = {[v] = k for k, v in y if v < 10} 
    24 
    35/+ 
  • branches/v2new/test.d

    r331 r332  
    3333    auto vm = new MDVM; 
    3434    auto t = openVM(vm); 
    35      
     35 
    3636    uword memSize; 
    3737 
     
    5353//  Timer.init(t); 
    5454// 
    55 //      auto funcReg = loadFunc(t, `samples/simple.md`); 
     55//      auto funcReg = loadFunc(t, `samples/speed.md`); 
    5656//      pushNull(t); 
    5757//      rawCall(t, funcReg, 0);