Changeset 118

Show
Ignore:
Timestamp:
10/12/07 11:09:34 (1 year ago)
Author:
Don Clugston
Message:

SyntaxTree? now calculates (tensor) rank of all symbols.

Files:

Legend:

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

    r117 r118  
    4545 * AbstractSyntaxTree, containing a syntax tree and a symbol table. 
    4646 * 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. 
    4748 * 
    4849 * Syntax trees are represented as placeholder expressions, with associativity 
     
    5960        if (i>0) result ~=","; 
    6061        result ~= `Symbol(` ~ mixin_typeOf(symbols[i]) 
    61                       ~ `,` ~ mixin_valueOf(symbols[i]) ~ `)`; 
     62                      ~ `,` ~ mixin_valueOf(symbols[i]) 
     63                      ~ `,` ~ mixin_rankOf(symbols[i]) ~ `)`; 
    6264    }     
    6365    result ~="])"; 
    6466    return "mixin(" ~ wrapInQuotes(result) ~ ")"; 
    6567} 
     68 
    6669 
    6770/** The result of semantic analysis of the original expression 
     
    8487    char [] type;  /// the name of the type, as text 
    8588    char [] value; /// the value, as text. 
    86     char [] element; /// mangleof the type of the elements, if an array. 
    8789    int rank;      /// the tensor rank (0=scalar, 1=vector, 2 = matrix, 3=tensor) 
    8890} 
     
    331333} 
    332334 
     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. 
     339char [] 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 
    333346//  ==== TESTS ==== 
    334347 
  • trunk/blade/SyntaxTreeDemo.d

    r117 r118  
    3434                output ~="\n"; 
    3535                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; 
    3739            } else { 
    3840              if (prevWasParen) { 
     
    4951char [] displayParseResult(AbstractSyntaxTree tree) 
    5052{ 
    51     char [] sym="  Rank\tType\tValue\n"; 
     53    char [] sym="  Rank\tConst?\tType\tValue\n"; 
    5254    for (int i=0; i<tree.symbolTable.length; ++i) { 
    5355    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; 
    5557        char c = q.value[0]; 
    5658        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; 
    5961    } 
    6062    return `Expression is: ` ~ tree.expression ~ \n ~ prettyTree(tree.expression) ~ \n ~ sym; 
     
    8890void main() 
    8991{ 
    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]; 
    9193    double fróg = 32; 
    92     alias fróg fgf; 
     94    alias fróg fgf;     
    9395 
    9496    bool b; 
     
    98100    mixin(vec("b = ags.af[fróg*2..56] in s")); 
    99101    mixin(vec("func(d*35,d+  4)")); 
    100     mixin(vec("QUAL")); 
     102    mixin(vec("QUAL^s")); 
    101103    mixin(vec("sin(3.45)")); 
    102104}