Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active March 3, 2023 22:18
Show Gist options
  • Save back2dos/5078439 to your computer and use it in GitHub Desktop.
Save back2dos/5078439 to your computer and use it in GitHub Desktop.
import haxe.macro.Expr;
import haxe.macro.Context;
class Build {
static public function types() {
var pos = Context.currentPos();
function mkPath(name:String):TypePath {
var parts = name.split('.');
return {
sub: null,
params: [],
name: parts.pop(),
pack: parts
}
}
function mkType(s:String):ComplexType
return TPath(mkPath(s));
function mkField(name:String, type:ComplexType):Field
return {
pos: pos,
name: name,
meta: [],
kind: FVar(type),
doc: null,
access: []
}
function declare(name:String, fields:Array<Field>, ?superType:TypePath)
Context.defineType({
pos: pos, //the position the type is associated with - should probably point to your XML file
params: [], //we have no type parameters in this example
pack: [], //no package
name: name,
fields: [], //we use a different mechanism to add fields, so we pass none here
isExtern: false, //not extern
meta: [], //no metadata
kind: TDAlias( //here we "alias" (which is what a typedef does) to a fitting ComplexType
if (superType == null)
TAnonymous(fields)
else
TExtend(superType, fields)
)
});
declare('Base', [mkField('val', mkType('String'))]);
declare('C', [], mkPath('Base'));
declare('B', [mkField('c', mkType('C'))], mkPath('Base'));
declare('A', [mkField('b', mkType('B'))], mkPath('Base'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment