comparison src/TypeRulesGenerator.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/TypeRulesGenerator.d@f51305056196
children 451ede0105e0
comparison
equal deleted inserted replaced
805:a3fab8b74a7d 806:bcb74c9b895c
1 #! /usr/bin/rdmd
2 /++
3 Author: Aziz Köksal
4 License: GPL3
5 +/
6 module TypeRulesGenerator;
7
8 import tango.io.File;
9 import tango.io.FilePath;
10
11 alias char[] string;
12
13 void main(char[][] args)
14 {
15 char[] text = "// Generated by TypeRulesGenerator.d\n"
16 "module TypeRules.d;\n\n";
17 text ~= "char[][][] unaryExpsResults = [\n";
18 foreach (results; unaryExpsResults)
19 {
20 text ~= " [";
21 foreach (result; results)
22 text ~= '"' ~ result ~ '"' ~ ", ";
23 text[$-2] = ']';
24 text[$-1] = ',';
25 text ~= \n;
26 }
27 text[$-2] = '\n';
28 text[$-1] = ']';
29 text ~= ";\n\n";
30
31 text ~= "char[][][][] binaryExpsResults = [\n";
32 foreach (expResults; binaryExpsResults)
33 {
34 text ~= " [\n";
35 foreach (results; expResults)
36 {
37 text ~= " [";
38 foreach (result; results)
39 text ~= '"' ~ result ~ '"' ~ ", ";
40 text[$-2] = ']';
41 text[$-1] = ',';
42 text ~= \n;
43 }
44 text[$-2] = '\n';
45 text[$-1] = ' ';
46 text ~= " ],\n";
47 }
48 text[$-2] = '\n';
49 text[$-1] = ']';
50 text ~= ";\n";
51
52 // Write the text to a D module.
53 auto file = new File("TypeRulesData.d");
54 file.write(text);
55 }
56
57 template ExpressionType(char[] T1, char[] T2, char[] expression)
58 {
59 mixin("alias "~T1~" X;");
60 mixin("alias "~T2~" Y;");
61 X x;
62 Y y;
63 static if(is(typeof(mixin(expression)) ResultType))
64 const char[] result = ResultType.stringof;
65 else
66 const char[] result = "Error";
67 }
68 alias ExpressionType EType;
69
70 // pragma(msg, EType!("char", "int", "&x").result);
71
72 static const string[] basicTypes = [
73 "char"[], "wchar", "dchar", "bool",
74 "byte", "ubyte", "short", "ushort",
75 "int", "uint", "long", "ulong",
76 /+"cent", "ucent",+/
77 "float", "double", "real",
78 "ifloat", "idouble", "ireal",
79 "cfloat", "cdouble", "creal"/+, "void"+/
80 ];
81
82 static const string[] unaryExpressions = [
83 "!x",
84 "&x",
85 "~x",
86 "+x",
87 "-x",
88 "++x",
89 "--x",
90 "x++",
91 "x--",
92 ];
93
94 static const string[] binaryExpressions = [
95 "x!<>=y",
96 "x!<>y",
97 "x!<=y",
98 "x!<y",
99 "x!>=y",
100 "x!>y",
101 "x<>=y",
102 "x<>y",
103
104 "x=y", "x==y", "x!=y",
105 "x<=y", "x<y",
106 "x>=y", "x>y",
107 "x<<=y", "x<<y",
108 "x>>=y","x>>y",
109 "x>>>=y", "x>>>y",
110 "x|=y", "x||y", "x|y",
111 "x&=y", "x&&y", "x&y",
112 "x+=y", "x+y",
113 "x-=y", "x-y",
114 "x/=y", "x/y",
115 "x*=y", "x*y",
116 "x%=y", "x%y",
117 "x^=y", "x^y",
118 "x~=y",
119 "x~y",
120 "x,y"
121 ];
122
123 char[] genBinaryExpArray(char[] expression)
124 {
125 char[] result = "[\n";
126 foreach (t1; basicTypes)
127 {
128 result ~= "[\n";
129 foreach (t2; basicTypes)
130 result ~= `EType!("`~t1~`", "`~t2~`", "`~expression~`").result,`\n;
131 result[result.length-2] = ']'; // Overwrite last comma.
132 result[result.length-1] = ','; // Overwrite last \n.
133 }
134 result[result.length-1] = ']'; // Overwrite last comma.
135 return result;
136 }
137 // pragma(msg, mixin(genBinaryExpArray("x%y")).stringof);
138
139 char[] genBinaryExpsArray()
140 {
141 char[] result = "[\n";
142 foreach (expression; binaryExpressions)
143 {
144 result ~= genBinaryExpArray(expression);
145 result ~= ",\n";
146 }
147 result[result.length-2] = ']';
148 return result;
149 }
150
151 // pragma(msg, mixin(genBinaryExpsArray()).stringof);
152
153 char[] genUnaryExpArray(char[] expression)
154 {
155 char[] result = "[\n";
156 foreach (t1; basicTypes)
157 result ~= `EType!("`~t1~`", "int", "`~expression~`").result,`\n;
158 result[result.length-2] = ']'; // Overwrite last comma.
159 return result;
160 }
161
162 char[] genUnaryExpsArray()
163 {
164 char[] result = "[\n";
165 foreach (expression; unaryExpressions)
166 result ~= genUnaryExpArray(expression) ~ ",\n";
167 result[result.length-2] = ']';
168 return result;
169 }
170
171 // pragma(msg, mixin(genUnaryExpsArray()).stringof);
172
173 auto unaryExpsResults = mixin(genUnaryExpsArray());
174 auto binaryExpsResults = mixin(genBinaryExpsArray());