Changeset 148
- Timestamp:
- 11/28/07 03:17:22 (9 months ago)
- Files:
-
- trunk/blade/BladeDemo.d (modified) (1 diff)
- trunk/blade/BladeRank.d (modified) (8 diffs)
- trunk/blade/BladeSimplify.d (modified) (1 diff)
- trunk/blade/SyntaxTree.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/BladeDemo.d
r146 r148 34 34 //mixin(vectorize("dot(q,q*dot(q,q))")); 35 35 // should simplify to: dot(q.q) * dot(q,q) 36 36 /* 37 37 mixin(vectorize(` a += (d[2..$-1]*2.01*a[2]-another[][1])["abc".length-3..$]`)); 38 38 39 39 mixin(vectorize(" a-= 2.01*( 3.04+k)*r")); 40 40 mixin(vectorize("q+= q*2.01")); 41 */ 42 // mixin(vectorize("a+=d[2,[1,$]]")); 41 43 writefln("a=", a); 42 44 } trunk/blade/BladeRank.d
r147 r148 50 50 * The sub-expression must be 51 51 * - a single character (eg "X"), OR 52 * - an expression in parenthesis. 52 * - an expression in parenthesis, OR 53 * - an array literal 53 54 */ 54 55 int exprLength(char [] s) … … 61 62 } 62 63 int numParens = 0; 64 int numBrack = 0; 63 65 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) { 71 71 return i; 72 72 } … … 83 83 if (expr.length==1) { 84 84 if (expr=="$") return 0; 85 assert(expr[0]>='A' && expr[0]<='Z', expr); 85 86 return rank[expr[0]-'A']-'0'; 86 87 } … … 124 125 */ 125 126 int exprRank(char [] expr, char [] rank) 126 { 127 { 127 128 // BUG: also need to deal with comma, ?:, &&, ||, is, !is, in, 128 129 // unary &, unary ! … … 159 160 while (expr[y+1]=='<' || expr[y+1]=='>' || expr[y+1]=='=') ++y; 160 161 161 char [] op = expr[x+1..y+1]; 162 char [] op = expr[x+1..y+1]; 162 163 char [] left = expr[0..x+1]; 163 164 char [] right = expr[y+1..$]; 165 if (expr[0]=='[') { // array literal 166 assert(0, expr); 167 } 164 168 if (expr[x+1]=='[') right = expr[y+1..$-1]; // drop off the ']'. 165 169 int lrank = subexprRank(left, rank); … … 178 182 } else if (right[z+1]==',') { 179 183 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). 181 187 int rrank = subexprRank(right[0..z+1], rank); 182 188 if (rrank<0) return rrank; 183 189 if (rrank>1) return RankError.NonScalarIndex; 184 190 if (rrank==0) --totrank; 191 if (rrank==1) assert(0, right); 185 192 186 193 if (z==right.length-1) return totrank; 187 194 right = right[z+2..$]; 188 189 195 z = exprLength(right); 190 //assert(0, right[0..z+1] ~ "#" ~right[z+2..$]);191 } 196 //assert(0, right[0..z+1]); 197 }while (true); 192 198 } else { 193 199 // slicing -- does not change the rank … … 233 239 234 240 unittest { 241 // assert(exprRank("A[B,[B,C],B]", "600")==4); 242 // const y = exprRank("A+=(B[C,$])", "1100"); 235 243 assert(exprRank("A+((((++B)+D)--)*C)", "1010")==1); 236 244 assert(exprRank("A+(B*C)", "000")==0); … … 248 256 assert(exprRank("d(A,B)", "10")==RankError.RankMismatchDotProduct); 249 257 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); 251 260 } 252 261 trunk/blade/BladeSimplify.d
r147 r148 61 61 else { 62 62 char [] expr2 = removeDuplicates(tree); 63 assert(0, ranks); 63 64 // Check for rank errors 64 65 int wholerank = exprRank(expr2, ranks); trunk/blade/SyntaxTree.d
r147 r148 29 29 * 30 30 * 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. 32 32 * 33 33 * NOTES: … … 276 276 preOp!("++") opAddAssign(T:int=int)(int x){ return null; } 277 277 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 */284 278 AST!("(" ~ text ~ "[" ~ T.text ~ ".." ~ U.text ~ "])") opSlice(T, U)(T x, U y){ return null; } 285 279 AST!("(" ~ text ~ "[" ~ AllText!(T) ~ "])") opIndex(T...)(T x){ return null; } 286 // }287 280 AST!("((" ~ text ~ "[" ~ AllText!(T) ~ "])=" ~ U.text ~ ")") opIndexAssign(U, T...)(U,T){ return null; } 288 281 AST!("(" ~ text ~ "(" ~ AllText!(T) ~ "))") opCall(T...)(T){ return null; } … … 316 309 * 317 310 * Bugs: 318 * Doesn't support: 311 * Doesn't support: 319 312 * unary operators & ! 320 313 * && and || operators 321 314 * ?: 322 * $ not really supported: a[$-1] should be replaced by a[a.length-1]323 315 * Comparison and NCEG floating point operators 324 316 * cast … … 327 319 */ 328 320 char [] mixin_getPrecedence(char [] expr) 329 { 321 { 330 322 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. 331 325 bool lastWasSymbol=false; // hack for array literals 332 326 char [] code = "typeof(";
