123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package python;
- #if macro
- import haxe.macro.Expr;
- import haxe.macro.Context;
- import haxe.macro.ExprTools;
- #end
- #if !macro
- import python.lib.Types.PyIterator;
- #end
- class Macros {
- @:noUsing macro public static function importModule (module:String):haxe.macro.Expr {
- return macro untyped __python__($v{"import " + module});
- }
- @:noUsing macro public static function importAs (module:String, className : String):haxe.macro.Expr
- {
- var n = className.split(".").join("_");
- var e = "import " + module + " as " + n;
- var e1 = "_hx_c."+n+" = "+n;
- return macro{
- untyped __python__($v{e});
- untyped __python__($v{e1});
- }
- }
- @:noUsing macro public static function pyFor <T>(v:Expr, it:Expr, b:Expr):haxe.macro.Expr
- {
- var id = switch (v.expr) {
- case EConst(CIdent(x)):x;
- case _ : Context.error("unexpected " + ExprTools.toString(v) + ": const ident expected", v.pos);
- }
- var res = macro @:pos(it.pos) {
- var $id = $it.getNativeIterator().__next__();
- $it;
- $b;
- }
- return macro (untyped __python_for__)($res);
- }
- @:noUsing macro public static function importFromAs (from:String, module:String, className : String):haxe.macro.Expr {
- var n = className.split(".").join("_");
- var e = "from " + from + " import " + module + " as " + n;
- var e1 = "_hx_c."+n+" = " + n;
- return macro {
- untyped __python__($v{e});
- untyped __python__($v{e1});
- }
- }
- @:noUsing macro public static function callField (o:Expr, field:ExprOf<String>, params:Array<Expr>):haxe.macro.Expr {
- var field = macro untyped __field__($o, $field);
- return macro untyped __call__($a{[field].concat(params)});
- }
- #if !macro macro #end public static function callNamed (e:Expr, args:Expr):haxe.macro.Expr {
- var fArgs = switch (Context.typeof(e)) {
- case TFun(args, ret): args;
- case _ : haxe.macro.Context.error("e must be of type function", e.pos);
- }
- switch (args.expr) {
- case EObjectDecl(fields):
- for (f in fields) {
- var found = false;
- for (a in fArgs) {
- found = a.name == f.field;
- if (found) break;
- }
- if (!found) {
- haxe.macro.Context.error("field " + f.field + " is not a valid argument (valid names " + [for (a in fArgs) a.name].join(",") + ")", args.pos);
- }
- }
- // TODO check at least if fields are valid (maybe if types match);
- case _ : haxe.macro.Context.error("args must be an ObjectDeclaration like { name : 1 }", args.pos);
- }
- return macro @:pos(e.pos) untyped __named__($e, $args);
- }
- }
|