added macro to export perph

This commit is contained in:
2023-07-08 12:51:08 +02:00
parent 4084659a4a
commit 4f881117cf
2 changed files with 80 additions and 10 deletions

77
src/macros/Exporter.hx Normal file
View File

@@ -0,0 +1,77 @@
package macros;
import haxe.macro.Context;
import haxe.macro.Expr;
using Lambda;
class Exporter {
macro public static function buildExport():Array<haxe.macro.Expr.Field> {
var fields = Context.getBuildFields();
var getExp = [];
for (field in fields){
if (field.meta == null) continue;
var s = "";
for (meta in field.meta){
if (meta.name == "export"){
switch (meta.params[0].expr){
case EConst(CString(s1)):
s = s1;
default:
Context.error("Invalid export name", meta.pos);
}
}
}
if (s == "") continue;
switch (field.kind){
case FFun(f):
var funName = field.name;
switch (f.ret){
case TPath(p):
switch (p.name){
case "Int":
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.Int(this.$funName()) );
case "Float":
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.Float(this.$funName()) );
case "Bool":
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.Bool(this.$funName()) );
case "String":
getExp.push( macro $v{s} => (i: Int) -> lib.exporter.Response.ValueType.String(this.$funName()) );
default:
Context.error("Only Int, Float, Bool and String can be exported", field.pos);
}
default:
Context.error("Only functions returning a type can be exported", field.pos);
}
default:
Context.error("Only functions can be exported", field.pos);
}
}
var exportField: Field = {
name: "export",
pos: Context.currentPos(),
kind: FFun({
args: [],
ret: TPath({ name: "ExportConfig", pack: []}),
expr: macro {
return {
getDelegates: $a{getExp},
};
}
}),
access: [APublic],
doc: null,
meta: [],
};
fields.push(exportField);
return fields;
}
}