Changeset 118
- Timestamp:
- 10/12/07 11:09:34 (1 year ago)
- Files:
-
- trunk/blade/SyntaxTree.d (modified) (4 diffs)
- trunk/blade/SyntaxTreeDemo.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/SyntaxTree.d
r117 r118 45 45 * AbstractSyntaxTree, containing a syntax tree and a symbol table. 46 46 * The symbol table contains the type, and value for each symbol. 47 * If the symbol is an array, the number of dimensions is also returned. 47 48 * 48 49 * Syntax trees are represented as placeholder expressions, with associativity … … 59 60 if (i>0) result ~=","; 60 61 result ~= `Symbol(` ~ mixin_typeOf(symbols[i]) 61 ~ `,` ~ mixin_valueOf(symbols[i]) ~ `)`; 62 ~ `,` ~ mixin_valueOf(symbols[i]) 63 ~ `,` ~ mixin_rankOf(symbols[i]) ~ `)`; 62 64 } 63 65 result ~="])"; 64 66 return "mixin(" ~ wrapInQuotes(result) ~ ")"; 65 67 } 68 66 69 67 70 /** The result of semantic analysis of the original expression … … 84 87 char [] type; /// the name of the type, as text 85 88 char [] value; /// the value, as text. 86 char [] element; /// mangleof the type of the elements, if an array.87 89 int rank; /// the tensor rank (0=scalar, 1=vector, 2 = matrix, 3=tensor) 88 90 } … … 331 333 } 332 334 335 // BLADE-SPECIFIC SEMANTIC PASS 336 337 // When mixed in, creates a const integer describing the (tensor) rank of symbol sym. 338 // Possible results are 0 = scalar, 1 = vector, 2 = matrix, 3 = tensor of rank 3 or more. 339 char [] mixin_rankOf(char [] sym) 340 { 341 // Implementation: If sym[0][0] is a valid type, we know it's a matrix. 342 // else if sym[0] is a valid type, we know it's a vector. Etc. 343 return "is(typeof(" ~ sym ~ "[0]))?is(typeof(" ~ sym ~ "[0][0]))?is(typeof(" ~ sym ~ "[0][0][0]))?3:2:1:0"; 344 } 345 333 346 // ==== TESTS ==== 334 347 trunk/blade/SyntaxTreeDemo.d
r117 r118 34 34 output ~="\n"; 35 35 for (int k=0; k<depth; ++k) output~=" "; 36 output~= " " ~ syntaxtree[i]; prevWasParen=true; 36 output~= " " ~ syntaxtree[i]; 37 if (i<syntaxtree.length-1 && syntaxtree[i+1]=='(') output~='('; 38 prevWasParen=true; 37 39 } else { 38 40 if (prevWasParen) { … … 49 51 char [] displayParseResult(AbstractSyntaxTree tree) 50 52 { 51 char [] sym=" Rank\tType\tValue\n";53 char [] sym=" Rank\tConst?\tType\tValue\n"; 52 54 for (int i=0; i<tree.symbolTable.length; ++i) { 53 55 Symbol q = tree.symbolTable[i]; 54 sym~=cast(char)('A'+i) ~ ": " ~ q.type ~ \t ~ q.value~ \t;56 sym~=cast(char)('A'+i) ~ ": " ~ cast(char)('0' + q.rank) ~ \t; 55 57 char c = q.value[0]; 56 58 bool isConst = ((c>='0' && c<='9')||c=='-'|| c=='"' || c=='\''); 57 if (isConst) sym~="CONST"; 58 sym ~= \n;59 if (isConst) sym~="CONST"; 60 sym ~=\t ~ q.type ~ \t ~ q.value ~ \n; 59 61 } 60 62 return `Expression is: ` ~ tree.expression ~ \n ~ prettyTree(tree.expression) ~ \n ~ sym; … … 88 90 void main() 89 91 { 90 double [9] s = [0.0, 3,4, 56,6585, 56,2.1, 465, 534];92 const double [9] s = [0.0, 3,4, 56,6585, 56,2.1, 465, 534]; 91 93 double fróg = 32; 92 alias fróg fgf; 94 alias fróg fgf; 93 95 94 96 bool b; … … 98 100 mixin(vec("b = ags.af[fróg*2..56] in s")); 99 101 mixin(vec("func(d*35,d+ 4)")); 100 mixin(vec("QUAL "));102 mixin(vec("QUAL^s")); 101 103 mixin(vec("sin(3.45)")); 102 104 }
