Changeset 176

Show
Ignore:
Timestamp:
01/16/08 05:01:53 (6 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/Blade.d

    r173 r176  
    5858module blade.Blade; 
    5959 
    60 public import blade.SyntaxTree : AbstractSyntaxTree, syntaxtreeof, AST, Symbol; 
     60public import blade.SyntaxTree : AbstractSyntaxTree, mixin_tupleAndSyntaxtreeof, AST, Symbol; 
    6161private import blade.BladeUtil : enquote, itoa; 
    6262private import blade.BladeRank : isStrided, exprRank; 
     
    7575{     
    7676    debug (BladeFrontEnd) { 
    77     return `pragma(msg, \n ~ "// " __FILE__ ~ "(" ~__LINE__.stringof[0..$-1] ~ ") ` ~ enquote(expr) ~ `" ~ \n ~ makeVectorCode(` ~ syntaxtreeof(expr) ~ ")~\\n);"     
    78     "mixin(makeVectorCode(" ~ syntaxtreeof(expr) ~ "));"; 
     77    return `pragma(msg, \n ~ "// " __FILE__ ~ "(" ~__LINE__.stringof[0..$-1] ~ ") ` ~ enquote(expr) ~ `" ~ \n ~ ` ~ mixin_tupleAndSyntaxtreeof("makeVectorCode", expr) ~ "~\\n);" 
     78    "mixin(" ~ mixin_tupleAndSyntaxtreeof("makeVectorCode", expr)~ ");"; 
    7979    } else { 
    80        return "mixin(makeVectorCode(" ~ syntaxtreeof(expr) ~ "));"; 
    81     } 
    82 }     
     80       return "mixin(" ~ mixin_tupleAndSyntaxtreeof("makeVectorCode", expr)~ ");"; 
     81    } 
     82} 
    8383 
    8484// Simplify the expression, categorise it,  
    8585// and dispatch to the appropriate code generator. 
    86 char [] makeVectorCode(AbstractSyntaxTree tree) 
     86char [] makeVectorCode(Types...)(AbstractSyntaxTree tree) 
    8787{ 
    8888    RevisedExpression revised = simplifySyntaxTree(tree); 
     
    363363    int firstVector = findVectorForLength(tree); 
    364364    result ~= getValueForSymbol(tree.mapping[firstVector], tree).invoker ~ ".length"; 
    365 //    result ~= tree.symbolTable[firstVector].value ~ ".length"; 
    366365    result ~= vals; 
    367366 
  • trunk/blade/BladeVisitor.d

    r175 r176  
    2020V.ReturnType beginVisit(V)(V visitor, char [] expr) 
    2121{ 
    22     if (exprLength(expr)==expr.length) { 
    23         return doVisit(visitor, expr); //  
     22    if (isSimpleExpr(expr)) { 
     23        return doVisit(visitor, expr); 
    2424    } else return doVisit(visitor, "(" ~ expr ~ ")"); 
    2525} 
     
    127127} 
    128128 
     129// Return true if the function is a single variable, or an intrinsic. 
     130bool isSimpleExpr(char [] s) 
     131{ 
     132    if (s.length==1) return true; 
     133    if (s[0]<'a' || s[0]>'z') return false; 
     134    return exprLength(s)==s.length; 
     135} 
     136 
    129137unittest { 
    130138    assert(exprLength("A*B")==1); 
     
    142150bool expressionContainsAssignment(char [] expr) 
    143151{ 
     152    if (isSimpleExpr(expr)) return false; 
    144153    // BUG: also need to deal with comma, ?:, &&, ||, is, !is, in,  
    145154    // unary &, unary ! 
    146155    if (expr.length>1 && expr[0]=='(') expr = expr[1..$-1]; 
    147     else if (expr.length==1 && ((expr[0]>='A' && expr[0]<='Z') || expr[0]=='$')){ 
    148         return false; 
    149     } 
    150     if (expr.length>3 && (expr[0]>='a' && expr[1]<='z')) { // function 
    151         return false; 
    152     } 
    153156    if (expr.length>2 && (expr[0..2]=="++" || expr[0..2]=="--")) { 
    154157        return false; 
     
    158161    } 
    159162    if (expr.length>2 && (expr[$-2..$]=="++" || expr[$-2..$]=="--")) { 
    160         return false; // BUG: actually this is an assignment 
     163        return false; // BUG: actually this _is_ an assignment 
    161164    } 
    162165    int y = exprLength(expr); 
  • 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])]");