Created
March 23, 2014 11:11
-
-
Save Simn/9721730 to your computer and use it in GitHub Desktop.
Haxe macro custom generator example
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
-cp src | |
-main Main | |
--macro YourGenerator.use() |
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
class Main { | |
static function main() { | |
trace("Hello world"); | |
} | |
} |
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.Type; | |
class YourGenerator { | |
static public function use() { | |
Context.onGenerate(generate); | |
} | |
static function generate(types:Array<Type>) { | |
// types contains all the compiled types, see http://api.haxe.org/haxe/macro/Type.html | |
// Typically you now do something like this: | |
for (type in types ){ | |
switch(type) { | |
case TInst(c, _): | |
trace('Generating class $c'); | |
case TEnum(en, _): | |
trace('Generating enum $en'); | |
case TAbstract(a, _): | |
trace('Generating abstract $a'); | |
case TType(t, _): | |
// Ignore, I guess. | |
case t: | |
trace("This never happens: " + t); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment