Skip to content

Instantly share code, notes, and snippets.

@postite
Last active August 29, 2015 14:01
Show Gist options
  • Save postite/120ecca5ab9af1d8f01b to your computer and use it in GitHub Desktop.
Save postite/120ecca5ab9af1d8f01b to your computer and use it in GitHub Desktop.
class Main
{
public function new()
{
trace("hello");
var g=new SubOB();
trace(g.pull);
g.pull="over";
trace(g.pull);
}
static public function main()
{
var app = new Main();
}
}
import haxe.macro.Context;
import haxe.macro.Expr;
class OBMacros{
public static function macroBuild() {
trace( "macrobuild");
var fields = Context.getBuildFields();
var hasManager = false;
for( f in fields ) {
for( m in f.meta )
switch( m.name ) {
case ":postite":
trace( "postite meta found");
switch( f.kind ) {
case FVar(t, _):
trace( "FVar"+t);
var pos = f.pos;
var ttype = t, tname;
trace(pos );
trace( ttype);
function e(expr) return { expr : expr, pos : pos };
// i substr( "_") because i don't know how to delete the field or replace it
//generate getter
var get = {
args : [],
params : [],
ret : t,
expr : Context.parse("return untyped "+f.name.substr(1),pos),
};
// tried @:noCompletion for fun
var meta = [{ name : ":noCompletion", params : [], pos : pos }];
fields.push({ name : "get_"+f.name.substr(1), pos : pos, meta :meta , access : [APrivate], doc : null, kind : FFun(get) });
//generate setter
var set = {
args : [{ name : "_v", opt : false, type : t, value : null }],
params : [],
ret : t,
expr : Context.parse("return this."+f.name.substr(1)+"=_v",pos),
};
//var meta = [{ name : ":hide", params : [], pos : pos }];
var meta = [{ name : ":hide", params : [], pos : pos }];
fields.push({ name : "set_"+f.name.substr(1), pos : pos, meta :meta , access : [APrivate], doc : null, kind : FFun(set) });
//generate (get,set) field
var meta = [{ name : ":isVar", params : [], pos : pos }];
var newField = {
name: f.name.substr(1),
doc: null,
meta: meta,
access: [APublic],
kind: FProp("get","set",macro "polo"), // cannot get the value from _pull eg :'marin'
pos: Context.currentPos()
};
fields.push(newField);
case _:
trace( "not a var");
}
default:
trace( "default");
}
switch( f.kind ) {
case FVar(t, _):
trace("kind="+ t);
// if( t != null )
// buildField(f,fields,t,t);
default:
}
}
return fields;
}
}
class SubOB extends OB{
@:postite
var _pull:String="marin";
public function new():Void
{
trace( "new sub");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment