root/trunk/blade/SyntaxTreeDemo.d

Revision 139, 3.1 kB (checked in by Don Clugston, 1 year ago)

Some CTFE speedups. Bugfix for scalar multiply folding.

  • Property svn:eol-style set to native
Line 
1 // Create an abstract syntax tree
2 /**
3 BUGS: Doesn't support:
4 unary operators:  & !
5 && and || operators
6 NCEG floating point operators
7 new, delete, anonymous classes
8 mixin, assert, and import expressions
9 */
10
11 module blade.SyntaxTreeDemo;
12 import blade.SyntaxTree : syntaxtreeof, AbstractSyntaxTree, Symbol, AST;
13
14 // Display the syntax tree
15 char [] prettyTree(char [] syntaxtree)
16 {
17     char [] output="";
18     int depth=0;
19     bool prevWasParen=false;
20     for(int i=0; i<syntaxtree.length; ++i) {
21         if (syntaxtree[i]=='(') {
22             if (i>0 && syntaxtree[i-1]==')') { // it's an opCall
23                 output ~="\n";
24                 for (int k=0; k<depth; ++k) output~="  ";
25                 output~="()\n";             
26             }
27             depth++; prevWasParen=true;
28         } else if (syntaxtree[i]==')') {
29             depth--;
30             prevWasParen=true;
31         }  else {
32             if (syntaxtree[i]>='A' && syntaxtree[i]<='Z') {
33                 output ~="\n";
34                 for (int k=0; k<depth; ++k) output~="  ";
35                 output~= "  " ~ syntaxtree[i];
36                 if (i<syntaxtree.length-1 && syntaxtree[i+1]=='(') output~='(';
37                 prevWasParen=true;
38             } else {
39               if (prevWasParen) {
40                 output ~="\n";
41                 for (int k=0; k<depth; ++k) output~="  ";
42               }
43               output ~= syntaxtree[i]; prevWasParen=false;
44             }
45         }
46     }
47     return output;
48 }
49
50 char [] displayParseResult(AbstractSyntaxTree tree)
51 {
52     char [] sym="  Rank\tCategory\tElement\tType\tValue\n";
53     for (int i=0; i<tree.symbolTable.length; ++i) {
54     Symbol q = tree.symbolTable[i];
55         sym~=cast(char)('A'+i) ~ ": " ~ q.rank ~ \t;
56         char c = q.value[0];
57         if (tree.symbolTable[i].type=="") sym ~= "ERROR";
58         else if ((c>='0' && c<='9')||c=='-') sym ~= "Numeric";
59         else if (c=='"' || c=='\'') sym ~= "String";
60         else if (tree.symbolTable[i].type[$-1]==')') sym ~= "Function";
61         sym ~=\t ~ q.element;
62         sym ~=\t ~ q.type ~ \t ~ q.value ~ \n;
63     }
64     return `Expression is: ` ~ tree.expression ~ \n ~ prettyTree(tree.expression) ~ \n ~ sym;   
65 }
66
67
68 char [] vec(char [] expression)
69 {
70     return "pragma(msg, displayParseResult(" ~ syntaxtreeof(expression)~ "));";
71 }
72
73 int tr() { return 3; }
74
75 struct ags {
76     int af[100];
77 }
78
79 const int butNotThis=6;
80
81 int func(int x, int youCanSeeThis = butNotThis ) { return 2; }
82
83 struct Blah(int Y) {
84     char [] s;
85     int k;
86 }
87
88 const QUAL = Blah!(7)("abc", 4);
89 import std.math;
90
91 void main()
92 {
93     const double [9] s = [0.0, 3,4, 56,6585, 56,2.1, 465, 534];
94     double fróg = 32;
95     alias fróg fgf;   
96
97     bool b;
98     const int d = -2;
99     const char ww='q';
100     mixin(vec(`++fgf+ tr + s[5+d..s.length]*1.23e78/(45+fgf)~ww+x"30003132"c~"abc+fg*we+4"`));
101     mixin(vec("b = ags.af[fróg*2..56] in s"));
102     mixin(vec("func(d*35,d+  4)"));
103     mixin(vec("QUAL^st"));
104     mixin(vec("sin(3.45)"));
105    
106     static z = [3.4, 565, 31.3, 0];
107     double [] a = new double[4];
108     double [] dun = [17.0, 28.25, 1, 56.2];
109
110     mixin(vec(" a   += dun*(25.5*3.01)-z"));
111 }
Note: See TracBrowser for help on using the browser.