-
Notifications
You must be signed in to change notification settings - Fork 6
/
Macro.hx
68 lines (61 loc) · 1.55 KB
/
Macro.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
import haxe.macro.*;
import haxe.macro.Expr;
using haxe.macro.Tools;
using Lambda;
class Macro
{
public static function build()
{
var methods = [];
var fields = Context.getBuildFields();
for (field in fields)
{
if (field.meta.exists(function(m){return m.name=="live";}))
{
switch (field.kind)
{
case FFun(f):
var name = field.name;
var args = f.args.map(function(a){ return a.name; }).join(",");
var expr = f.expr.map(processExpr);
var body = expr.toString();
methods.push('$name:function($args)$body');
f.expr = macro live.call(this, "update", [_]);
case _:
}
}
}
// static init causes issues with nme
// var liveExpr = macro new Live();
// var live = {name:"live", pos:Context.currentPos(), meta:[], doc:null, access:[AStatic], kind:FVar(null, liveExpr)};
// fields.push(live);
var script = "{"+methods.join(",")+"}";
sys.io.File.saveContent("bin/script.hs", script);
return fields;
}
static function processExpr(expr:Expr):Expr
{
return switch (expr.expr)
{
case EBinop(OpAssign, e1, e2):
getSetter(e1, processExpr(e2));
case EField(e, field):
e = processExpr(e);
var prop = Context.makeExpr(field, expr.pos);
macro getProperty($e, $prop);
case _: expr.map(processExpr);
}
}
static function getSetter(expr:Expr, value:Expr):Expr
{
return switch (expr.expr)
{
case EField(e, field):
e = processExpr(e);
var prop = Context.makeExpr(field, expr.pos);
macro setProperty($e, $prop, $value);
case _:
processExpr(expr);
}
}
}