Show
Ignore:
Timestamp:
01/16/08 05:01:53 (8 months ago)
Author:
Don Clugston
Message:

SyntaxTree? now creates a tuple of the types. This opens the way to significant simplification of some of the code (eg, I should be able to remove the rank stuff from SyntaxTree?).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/blade/SyntaxTree.d

    r173 r176  
    6262{ 
    6363    char [][] symbols = replaceSymbolsWithPlaceholders(expression); 
    64     char [] result = `AbstractSyntaxTree(` ~ mixin_getPrecedence(expression) ~ `,[`;     
     64    char [] result = `AbstractSyntaxTree(` ~ mixin_getPrecedence(expression) ~ `,[`; 
    6565         
    6666    for(int i=0; i<symbols.length; ++i) { 
    67         if (i>0) result ~=","; 
     67        if (i>0) result ~= ","; 
    6868        result ~= `Symbol(` ~ mixin_typeOf(symbols[i]) 
    6969                      ~ `,` ~ mixin_valueOf(symbols[i]) 
     
    7474    result ~="])"; 
    7575    return `mixin("` ~ enquote(result) ~ `")`; 
     76} 
     77 
     78/** 
     79*  As for syntaxtreeof(), except that it creates a tuple as well as the abstract syntax tree 
     80*/ 
     81char [] mixin_tupleAndSyntaxtreeof(char [] funcname, char [] expression) 
     82{ 
     83    char [][] symbols = replaceSymbolsWithPlaceholders(expression); 
     84    char [] result = `AbstractSyntaxTree(` ~ mixin_getPrecedence(expression) ~ `,[`; 
     85    char [] tuple; 
     86         
     87    for(int i=0; i<symbols.length; ++i) { 
     88        if (i>0) { 
     89            result ~= ","; 
     90            tuple ~= ","; 
     91        } 
     92        result ~= `Symbol(` ~ mixin_typeOf(symbols[i]) 
     93                      ~ `,` ~ mixin_valueOf(symbols[i]) 
     94                      ~ `,` ~ mixin_rankOf(symbols[i]) 
     95                      ~ `,` ~ mixin_elementOf(symbols[i]) ~ `)`; 
     96        tuple ~= mixin_typeOfOrVoid(symbols[i]); 
     97    }     
     98     
     99    result ~="])"; 
     100    return funcname ~ `!(mixin("` ~ enquote(tuple) ~ `"))(mixin("` ~ enquote(result) ~ `"))`; 
    76101} 
    77102 
     
    314339} 
    315340 
     341char [] mixin_typeOfOrVoid(char [] sym) 
     342{ 
     343    // If sym is a function, we take its address, since 
     344    //   typeof(x).stringof doesn't compile if x is a function. 
     345    // If sym doesn't compile at all, return "void". 
     346    return `mixin(!is(typeof(` ~ sym ~ `))?"\"void\"" : is(typeof(` ~ sym ~ `)==function)?"` ~ enquote("typeof(&" ~ sym ~ ").stringof")~`":"` ~ enquote("typeof(" ~ sym ~ ").stringof") ~ `")`; 
     347} 
     348 
    316349// Returns sym.stringof, with workarounds for compiler bugs 
    317350char [] mixin_valueOf(char [] sym) 
     
    347380    ~ s ~ `[0][0][0]":"` ~ s ~ `[0][0]":"` ~ s ~ `[0]":"` ~ s ~ `")`);    
    348381} 
    349  
    350382 
    351383//  ==== TESTS ==== 
     
    366398    assert(mixin(mixin_getPrecedence("(A+B)  in(C^D)"))=="(A+B)in(C^D)"); 
    367399    assert(mixin(mixin_getPrecedence("A"))=="A"); 
     400    assert(mixin(mixin_getPrecedence("--A"))=="--A"); 
     401//   assert(mixin(mixin_getPrecedence("A--"))=="A--"); // BUG: fails 
    368402     
    369403    assert(mixin(mixin_getPrecedence("A[B,[C,D]]"))=="A[B,([C,D])]");