Skip to content

Instantly share code, notes, and snippets.

@fujidig
Created March 27, 2014 14:43
Show Gist options
  • Save fujidig/9809077 to your computer and use it in GitHub Desktop.
Save fujidig/9809077 to your computer and use it in GitHub Desktop.
usage: "haxe Main.hxml". -D dumpの効果により、dump/にマクロ展開後のASTが出力されます
@:build(MyMacro.build()) class Main {
public static function main() {
trace(hello());
}
}
-main Main
-D dump
-js Main.js
-cmd node Main.js
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