Changeset 148

Show
Ignore:
Timestamp:
11/28/07 03:17:22 (9 months ago)
Author:
Don Clugston
Message:

More support for array literals; fixed most (but not all) of the rank determination issues for multi-index arrays.

Files:

Legend:

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

    r146 r148  
    3434    //mixin(vectorize("dot(q,q*dot(q,q))"));     
    3535    // should simplify to: dot(q.q) * dot(q,q) 
    36      
     36/*     
    3737    mixin(vectorize(` a += (d[2..$-1]*2.01*a[2]-another[][1])["abc".length-3..$]`)); 
    3838     
    3939    mixin(vectorize(" a-= 2.01*(        3.04+k)*r")); 
    4040    mixin(vectorize("q+= q*2.01")); 
     41*/     
     42//    mixin(vectorize("a+=d[2,[1,$]]")); 
    4143    writefln("a=", a); 
    4244} 
  • trunk/blade/BladeRank.d

    r147 r148  
    5050 * The sub-expression must be  
    5151 *  - a single character (eg "X"), OR 
    52  *  - an expression in parenthesis. 
     52 *  - an expression in parenthesis, OR 
     53 *  - an array literal  
    5354 */ 
    5455int exprLength(char [] s) 
     
    6162    } 
    6263    int numParens = 0; 
     64    int numBrack = 0; 
    6365    for (; i<s.length; ++i) { 
    64         if (s[i]=='(') { 
    65             numParens++; 
    66         } 
    67         if (s[i]==')') { 
    68             numParens--; 
    69         } 
    70         if (numParens == 0) { 
     66        if (s[i]=='(')  ++numParens; 
     67        if (s[i]==')') numParens--;        
     68        if (s[i]=='[') ++numBrack; 
     69        if (s[i]==']') --numBrack; 
     70        if (numParens == 0 && numBrack == 0) { 
    7171            return i; 
    7272        } 
     
    8383    if (expr.length==1) { 
    8484        if (expr=="$") return 0; 
     85        assert(expr[0]>='A' && expr[0]<='Z', expr); 
    8586        return rank[expr[0]-'A']-'0'; 
    8687    } 
     
    124125 */ 
    125126int exprRank(char [] expr, char [] rank) 
    126 {     
     127{ 
    127128    // BUG: also need to deal with comma, ?:, &&, ||, is, !is, in,  
    128129    // unary &, unary ! 
     
    159160    while (expr[y+1]=='<' || expr[y+1]=='>' || expr[y+1]=='=') ++y;     
    160161 
    161     char [] op = expr[x+1..y+1];     
     162    char [] op = expr[x+1..y+1]; 
    162163    char [] left = expr[0..x+1]; 
    163164    char [] right = expr[y+1..$]; 
     165    if (expr[0]=='[') { // array literal 
     166        assert(0, expr); 
     167    } 
    164168    if (expr[x+1]=='[') right = expr[y+1..$-1]; // drop off the ']'. 
    165169    int lrank = subexprRank(left, rank); 
     
    178182        } else if (right[z+1]==',') { 
    179183            int totrank = lrank; 
    180             for (;;) { 
     184            do { 
     185                // allow rank of 1 to be a slice operation 
     186                // (so A[1,[2,$-1], $] is possible). 
    181187                int rrank = subexprRank(right[0..z+1], rank); 
    182188                if (rrank<0) return rrank; 
    183189                if (rrank>1) return RankError.NonScalarIndex; 
    184190                if (rrank==0) --totrank; 
     191                if (rrank==1) assert(0, right); 
    185192                 
    186193                if (z==right.length-1) return totrank; 
    187194                right = right[z+2..$]; 
    188                  
    189195                z = exprLength(right); 
    190                 //assert(0, right[0..z+1]~ "#" ~right[z+2..$]); 
    191             } 
     196                //assert(0, right[0..z+1]); 
     197            }while (true); 
    192198        } else { 
    193199            // slicing -- does not change the rank 
     
    233239 
    234240unittest {     
     241//    assert(exprRank("A[B,[B,C],B]", "600")==4); 
     242//    const y = exprRank("A+=(B[C,$])", "1100"); 
    235243    assert(exprRank("A+((((++B)+D)--)*C)", "1010")==1); 
    236244    assert(exprRank("A+(B*C)", "000")==0); 
     
    248256    assert(exprRank("d(A,B)", "10")==RankError.RankMismatchDotProduct); 
    249257    assert(exprRank("d(B,(A*(d(B,B))))", "11")==0); 
    250 //    assert(exprRank("A[B,B,B]", "6034")==3); 
     258     
     259    assert(exprRank("A[B,B,B]", "6034")==3); 
    251260} 
    252261 
  • trunk/blade/BladeSimplify.d

    r147 r148  
    6161    else { 
    6262        char [] expr2 = removeDuplicates(tree); 
     63        assert(0, ranks); 
    6364        // Check for rank errors 
    6465        int wholerank = exprRank(expr2, ranks); 
  • trunk/blade/SyntaxTree.d

    r147 r148  
    2929* 
    3030* An unmatched ] in the expression generates a garbage error message after the 
    31 * sensible error message
     31* sensible error message; this is a compiler bug
    3232* 
    3333* NOTES: 
     
    276276    preOp!("++") opAddAssign(T:int=int)(int x){ return null; } 
    277277    preOp!("--") opSubAssign(T:int=int)(int x){ return null; }   
    278 /* 
    279   static if (text.length==0) { 
    280     AST!("[" ~ T.text ~ ".." ~ U.text ~ "]") opSlice(T, U)(T x, U y){ return null; }     
    281     AST!("[" ~ AllText!(T) ~ "]") opIndex(T...)(T x){ return null; } 
    282   } else {      
    283 */       
    284278    AST!("(" ~ text ~ "[" ~ T.text ~ ".." ~ U.text ~ "])") opSlice(T, U)(T x, U y){ return null; }     
    285279    AST!("(" ~ text ~ "[" ~ AllText!(T) ~ "])") opIndex(T...)(T x){ return null; } 
    286 //  } 
    287280    AST!("((" ~ text ~ "[" ~ AllText!(T) ~ "])=" ~ U.text ~ ")") opIndexAssign(U, T...)(U,T){ return null; } 
    288281    AST!("(" ~ text ~ "(" ~ AllText!(T) ~ "))") opCall(T...)(T){ return null; } 
     
    316309 *     
    317310 *   Bugs: 
    318  *    Doesn't support:  
     311 *    Doesn't support: 
    319312 *      unary operators & ! 
    320313 *      && and || operators 
    321314 *      ?: 
    322  *      $ not really supported: a[$-1] should be replaced by a[a.length-1] 
    323315 *      Comparison and NCEG floating point operators 
    324316 *      cast 
     
    327319 */ 
    328320char [] mixin_getPrecedence(char [] expr) 
    329 { 
     321{     
    330322    if (expr.length<2) return "`" ~ expr ~ "`"; 
     323    // The scheme doesn't work directly for array literals. Instead, change them 
     324    // into opIndex of a nameless `` symbol. 
    331325    bool lastWasSymbol=false; // hack for array literals 
    332326    char [] code = "typeof(";