Created
March 27, 2014 14:43
-
-
Save fujidig/9809077 to your computer and use it in GitHub Desktop.
usage: "haxe Main.hxml". -D dumpの効果により、dump/にマクロ展開後のASTが出力されます
This file contains 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.build()) class Main { | |
public static function main() { | |
trace(hello()); | |
} | |
} |
This file contains 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
-main Main | |
-D dump | |
-js Main.js | |
-cmd node Main.js |
This file contains 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.Expr; | |
class MyMacro { | |
macro public static function build() : Array<Field> { | |
var pos = haxe.macro.Context.currentPos(); | |
var fields = haxe.macro.Context.getBuildFields(); | |
/* | |
static function hello(): String { return "Hello, world!"; } | |
というフィールドを生成する | |
*/ | |
var tstring = TPath({ pack : [], name : "String", params : [], sub : null }); | |
var e1 = {expr: EConst(CString("Hello, world!")), pos: pos}; | |
var e2 = {expr: EReturn(e1), pos: pos}; | |
var ftype = FFun({args:[], expr: e2, ret: tstring}); | |
fields.push({ name : "hello", pos: pos, kind: ftype, access: [AStatic]}); | |
return fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment