$ haxe -cp zomg --interp -main Wat
-
-
Save JoshCheek/2fdaba7abdd22aac8c61 to your computer and use it in GitHub Desktop.
Haxe macro to define functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: zomg/A.hx | |
package zomg; | |
class A { | |
public static function x() { | |
trace("this is a"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: zomg/B.hx | |
class B { | |
public static function x() { | |
trace("this is b"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
class MyMacro { | |
macro static function loadEm():Array<Field> { | |
var fields = Context.getBuildFields(); | |
var calls = { | |
pos: Context.currentPos(), | |
expr: EBlock([ | |
{ pos: Context.currentPos(), | |
expr: ECall({ | |
pos: Context.currentPos(), | |
expr: EField({ | |
pos: Context.currentPos(), | |
expr: EField({ | |
expr: EConst(CIdent("zomg")), | |
pos: Context.currentPos(), | |
}, "A"), | |
}, "x"), | |
}, | |
[]), | |
}, | |
{ pos: Context.currentPos(), | |
expr: ECall({ | |
pos: Context.currentPos(), | |
expr: EField({ | |
pos: Context.currentPos(), | |
expr: EConst(CIdent("B")), | |
}, "x"), | |
}, | |
[]), | |
} | |
]), | |
}; | |
var c = macro class X { | |
public static function main() { | |
${calls}; | |
} | |
}; | |
fields.push(c.fields[0]); | |
return fields; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@:build(MyMacro.loadEm()) | |
class Wat { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment