forked from HeapsIO/heaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacroParser.hx
307 lines (299 loc) · 9.67 KB
/
MacroParser.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package hxsl;
import haxe.macro.Expr;
using haxe.macro.Tools;
class MacroParser {
public function new() {
}
function error( msg : String, pos : Position ) : Dynamic {
return Ast.Error.t(msg,pos);
}
function applyMeta( m : MetadataEntry, v : Ast.VarDecl ) {
switch( m.params ) {
case []:
// fallthrough
case [ { expr : EConst(CString(n)), pos : pos } ] if( m.name == "var" || m.name == "global" || m.name == "input" ):
v.qualifiers.push(Name(n));
case [ { expr : EConst(CInt(n)), pos : pos } ] if( m.name == "const" ):
v.qualifiers.push(Const(Std.parseInt(n)));
return;
case [ { expr : EConst(CInt(a) | CFloat(a)) }, { expr : EConst(CInt(b) | CFloat(b)) } ] if( m.name == "range" ):
v.qualifiers.push(Range(Std.parseFloat(a),Std.parseFloat(b)));
return;
case [ { expr : EConst(CInt(a)) } ] if( m.name == "perInstance" ):
v.qualifiers.push(PerInstance(Std.parseInt(a)));
return;
case [ { expr: EConst(CString(s)), pos: pos } ] if (m.name == "doc"):
v.qualifiers.push(Doc(s));
return;
case [ { expr: EConst(CString(s)) } ] if (m.name == "sampler"):
v.qualifiers.push(Sampler(s));
return;
case [ e ] if (m.name == "borrow"):
var path = [];
function loop( e : Expr ) {
switch( e.expr ) {
case EConst(CIdent(s)): path.push(s);
case EConst(CString(s)): path.push(s);
case EField(e, f): loop(e); path.push(f);
default:
error("Should be a shader type path", e.pos);
}
}
loop(e);
v.qualifiers.push(Borrow(path.join(".")));
return;
default:
error("Invalid meta parameter for "+m.name, m.pos);
}
switch( m.name ) {
case "var":
if( v.kind == null ) v.kind = Var else error("Duplicate type qualifier", m.pos);
case "global":
if( v.kind == null ) v.kind = Global else error("Duplicate type qualifier", m.pos);
case "param":
if( v.kind == null ) v.kind = Param else error("Duplicate type qualifier", m.pos);
case "input":
if( v.kind == null ) v.kind = Input else error("Duplicate type qualifier", m.pos);
case "const":
v.qualifiers.push(Const());
case "private":
v.qualifiers.push(Private);
case "nullable":
v.qualifiers.push(Nullable);
case "perObject":
v.qualifiers.push(PerObject);
case "shared":
v.qualifiers.push(Shared);
case "lowp":
v.qualifiers.push(Precision(Low));
case "mediump":
v.qualifiers.push(Precision(Medium));
case "highp":
v.qualifiers.push(Precision(High));
case "ignore":
v.qualifiers.push(Ignore);
case "perInstance":
v.qualifiers.push(PerInstance(1));
default:
error("Unsupported qualifier " + m.name, m.pos);
}
}
function getTexDim( n : String, f : Ast.TexDimension -> Bool -> Ast.Type ) {
var arr = false;
if( StringTools.endsWith(n,"Array") ) {
arr = true;
n = n.substr(0,-5);
}
return switch( n ) {
case "1D": f(T1D,arr);
case "2D": f(T2D,arr);
case "3D": f(T3D,arr);
case "Cube": f(TCube,arr);
default: null;
}
}
public function parseType( t : ComplexType, pos : Position ) : Ast.Type {
switch( t ) {
case TPath( { pack : [], name : name, sub : null, params : [] } ):
switch( name ) {
case "Int": return TInt;
case "Bool": return TBool;
case "Float": return TFloat;
case "Vec2": return TVec(2,VFloat);
case "Vec3": return TVec(3,VFloat);
case "Vec4": return TVec(4,VFloat);
case "IVec2": return TVec(2,VInt);
case "IVec3": return TVec(3,VInt);
case "IVec4": return TVec(4,VInt);
case "BVec2": return TVec(2,VBool);
case "BVec3": return TVec(3,VBool);
case "BVec4": return TVec(4,VBool);
case "Mat4": return TMat4;
case "Mat3": return TMat3;
case "Mat3x4": return TMat3x4;
case "Mat2": return TMat2;
case "String": return TString;
case "Bytes2": return TBytes(2);
case "Bytes3": return TBytes(3);
case "Bytes4": return TBytes(4);
case "Channel": return TChannel(1);
case "Channel2": return TChannel(2);
case "Channel3": return TChannel(3);
case "Channel4": return TChannel(4);
case _ if( StringTools.startsWith(name,"Sampler") ):
var t = getTexDim(name.substr(7), (d,arr) -> TSampler(d,arr));
if( t != null ) return t;
}
case TPath( { pack : [], name : name, sub : null, params : pl } ) if( StringTools.startsWith(name,"RWTexture") ):
var chans = switch( pl[0] ) {
case TPType(TPath({ pack : [], name : n, sub : null, params : [] })):
switch( n ) {
case "Float": 1;
case "Vec2": 2;
case "Vec3": 3;
case "Vec4": 4;
default: 0;
}
case null, _: 0;
}
if( chans == 0 )
error("Unsupported RWTexture parameter, should be Float|Vec2|Vec3|Vec4", pos);
var t = getTexDim(name.substr(9), (dim,arr) -> TRWTexture(dim,arr,chans));
if( t != null )
return t;
case TPath( { pack : [], name : name = ("Array"|"Buffer"|"RWBuffer"|"PartialBuffer"|"RWPartialBuffer"), sub : null, params : [t, size] } ):
var t = switch( t ) {
case TPType(t): parseType(t, pos);
default: null;
}
var size : Ast.SizeDecl = switch( size ) {
case TPExpr({ expr : EConst(CInt(v)) }): SConst(Std.parseInt(v));
case TPType(TPath( { pack : pack, name : name, sub : null, params : [] } )):
var pack = pack.copy();
pack.push(name);
SVar( { id : 0, type : null, name : pack.join("."), kind : null } );
default: null;
}
if( t != null && size != null )
return switch( name ) {
case "Array": TArray(t, size);
case "Buffer": TBuffer(t,size,Uniform);
case "RWBuffer": TBuffer(t,size,RW);
case "PartialBuffer": TBuffer(t,size,Partial);
case "RWPartialBuffer": TBuffer(t,size,RWPartial);
default: throw "assert";
}
case TAnonymous(fl):
return TStruct([for( f in fl ) {
switch( f.kind ) {
case FVar(t,e):
if( e != null ) error("No expression allowed in structure", e.pos);
var v : Ast.VarDecl = {
name : f.name,
type : t == null ? null : parseType(t, f.pos),
qualifiers : [],
kind : null,
expr : null,
};
for( m in f.meta )
applyMeta(m,v);
{ id : 0, name : v.name, type : v.type, kind : v.kind, qualifiers : v.qualifiers };
default:
error("Only variables are allowed in structures", f.pos);
}
}]);
default:
}
error("Unsupported type " + t.toString(), pos);
return null;
}
public function parseExpr( e : Expr ) : Ast.Expr {
var ed : Ast.ExprDef = switch( e.expr ) {
case EBlock(el):
EBlock([for( e in el ) parseExpr(e)]);
case EMeta(m, e):
var e2 = parseExpr(e);
switch( e2.expr ) {
case EVars(vl):
for( v in vl )
applyMeta(m, v);
e2.expr;
default:
switch( m.name ) {
case ":extends":
ECall({ expr : EIdent("extends"), pos : m.pos }, [e2]);
case ":import":
ECall({ expr : EIdent("import"), pos : m.pos }, [e2]);
default:
EMeta(m.name, [for( e in m.params ) parseExpr(e)], e2);
}
}
case EVars(vl):
EVars([for( v in vl ) {
{
name : v.name,
expr : v.expr == null ? null : parseExpr(v.expr),
type : v.type == null ? null : parseType(v.type, e.pos),
kind : null,
qualifiers : v.isFinal ? [Final] : [],
}
}]);
case EFunction(FNamed(name,_),f) if( f.expr != null ):
EFunction({
name : name,
ret : f.ret == null ? null : (switch( f.ret ) {
case TPath( { pack:[], name:"Void", sub:null } ): TVoid;
default: parseType(f.ret, e.pos);
}),
args : [for( a in f.args ) {
{
name : a.name,
type : a.type == null ? null : parseType(a.type, e.pos),
kind : Local,
qualifiers : [],
expr : a.value == null ? (a.opt ? { expr : EConst(CNull), pos : e.pos } : null) : parseExpr(a.value),
}
}],
expr : parseExpr(f.expr),
});
case EBinop(op, e1, e2):
EBinop(op, parseExpr(e1), parseExpr(e2));
case EUnop(op, false, e1):
EUnop(op, parseExpr(e1));
case EUnop(op, true, e1):
EUnop(op, parseExpr(e1)); // TODO : ++/-- postfix with lvalue
case EConst(c):
switch( c ) {
case CString(s):
EConst(CString(s));
case CInt(v):
EConst(CInt(Std.parseInt(v)));
case CIdent("null"):
EConst(CNull);
case CIdent("true"):
EConst(CBool(true));
case CIdent("false"):
EConst(CBool(false));
case CIdent("discard"):
EDiscard;
case CIdent(s):
EIdent(s);
case CFloat(f):
EConst(CFloat(Std.parseFloat(f)));
case CRegexp(_):
null;
}
case EField(e, f):
EField(parseExpr(e), f);
case ECall(e, args):
ECall(parseExpr(e), [for( a in args ) parseExpr(a)]);
case EParenthesis(e):
EParenthesis(parseExpr(e));
case EIf(cond, eif, eelse), ETernary(cond, eif, eelse):
EIf(parseExpr(cond), parseExpr(eif), eelse == null ? null : parseExpr(eelse));
case EFor({ expr : EBinop(OpIn,{ expr : EConst(CIdent(n)) }, eloop) },eblock):
EFor(n, parseExpr(eloop), parseExpr(eblock));
case EReturn(e):
EReturn(e == null ? null : parseExpr(e));
case EBreak:
EBreak;
case EContinue:
EContinue;
case EArray(e1, e2):
EArray(parseExpr(e1), parseExpr(e2));
case EArrayDecl(el):
EArrayDecl([for( e in el ) parseExpr(e)]);
case ESwitch(e, cases, def):
ESwitch(parseExpr(e), [for( c in cases ) { expr : c.expr == null ? { expr : EBlock([]), pos : c.values[0].pos } : parseExpr(c.expr), values : [for( v in c.values ) parseExpr(v)] }], def == null ? null : parseExpr(def));
case EWhile(cond, e, normalWhile):
EWhile(parseExpr(cond), parseExpr(e), normalWhile);
case EObjectDecl([]):
EBlock([]);
default:
null;
};
if( ed == null )
error("Unsupported expression", e.pos);
return { expr : ed, pos : e.pos };
}
}