Explorar o código

get rid of dirty __python__whatever__ syntax in some classes

frabbit %!s(int64=11) %!d(string=hai) anos
pai
achega
ad4dfe90f1
Modificáronse 4 ficheiros con 58 adicións e 40 borrados
  1. 1 1
      std/python/Boot.hx
  2. 25 8
      std/python/Syntax.hx
  3. 16 17
      std/python/internal/ArrayImpl.hx
  4. 16 14
      std/python/internal/StringImpl.hx

+ 1 - 1
std/python/Boot.hx

@@ -137,7 +137,7 @@ import math as _hx_math
 		if (isInstance(a, String) || isInstance(b, String)) {
 			return toString1(a,"") + toString1(b,"");
 		}
-		return Syntax.pyBinop(a, "+", b);
+		return Syntax.binop(a, "+", b);
     }
 
     @:keep static function toString (o:Dynamic) {

+ 25 - 8
std/python/Syntax.hx

@@ -37,12 +37,17 @@ class Syntax {
 
     @:noUsing macro public static function isIn <T>(a:Expr, b:Expr):haxe.macro.Expr
     {
-        return macro untyped __python_in__($a, $b);
+        return macro (untyped __python_in__)($a, $b);
     }
 
-    @:noUsing macro public static function pyBinop <T>(a:Expr, op:String, b:Expr):haxe.macro.Expr
+    @:noUsing macro public static function delete <T>(a:Expr):haxe.macro.Expr
     {
-        return macro untyped __python_binop__($a, $v{op}, $b);
+        return macro (untyped __python_del__)($a);
+    }
+
+    @:noUsing macro public static function binop <T>(a:Expr, op:String, b:Expr):haxe.macro.Expr
+    {
+        return macro (untyped __python_binop__)($a, $v{op}, $b);
     }
 
 
@@ -50,12 +55,17 @@ class Syntax {
     #if (!macro) macro #end
     public static function untypedPython <T>(b:String):haxe.macro.ExprOf<Dynamic>
     {
-        return macro untyped __python__($v{b});
+        return macro (untyped __python__)($v{b});
     }
 
     @:noUsing macro public static function arrayAccess <T>(x:Expr, rest:Array<Expr>):haxe.macro.ExprOf<Dynamic>
     {
-        return macro untyped __python_array_get__($a{[x].concat(rest)});
+        return macro (untyped __python_array_get__)($a{[x].concat(rest)});
+    }
+
+    @:noUsing macro public static function arrayAccessWithLeadingColon <T>(x:Expr, rest:Array<Expr>):haxe.macro.ExprOf<Dynamic>
+    {
+        return macro (untyped __python_array_access_leading_colon__)($a{[x].concat(rest)});
     }
 
     @:noUsing macro public static function pyFor <T>(v:Expr, it:Expr, b:Expr):haxe.macro.Expr
@@ -92,23 +102,25 @@ class Syntax {
         var field = python.Syntax.field(o, field);
         var params = [field].concat(params);
 
-        return macro untyped __call__($a{params});
+        return macro (untyped __call__)($a{params});
     }
 
     @:noUsing
     #if !macro macro #end
     public static function field (o:Expr, field:ExprOf<String>):haxe.macro.Expr
     {
-        return macro untyped __field__($o, $field);
+        return macro (untyped __field__)($o, $field);
     }
 
     @:noUsing
     #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) {
@@ -124,7 +136,12 @@ class Syntax {
                 // 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);
+        return macro @:pos(e.pos) (untyped __named__)($e, $args);
+    }
+
+    macro public static function callNamedUntyped (e:Expr, args:Expr):haxe.macro.Expr
+    {
+        return macro @:pos(e.pos) (untyped __named__)($e, $args);
     }
 
 

+ 16 - 17
std/python/internal/ArrayImpl.hx

@@ -31,12 +31,12 @@ private abstract Builtin(Dynamic) {}
 @:keep
 class ArrayImpl {
 
-	static inline function builtin():Builtin return untyped __python__("_hx_builtin");
+	static inline function builtin():Builtin return Syntax.untypedPython("_hx_builtin");
 
-	public static inline function get_length <T>(x:Array<T>):Int return Syntax.field(builtin(), "len")(x);
+	public static inline function get_length <T>(x:Array<T>):Int return Syntax.callField(builtin(), "len", x);
 
 	public static inline function concat<T>( a1:Array<T>, a2 : Array<T>) : Array<T> {
-		return untyped (untyped a1) + (untyped a2);
+		return Syntax.binop(a1, "+", a2);
 	}
 
 	public static inline function copy<T>(x:Array<T>) : Array<T> {
@@ -44,7 +44,7 @@ class ArrayImpl {
 	}
 
 	@:keep public static inline function iterator<T>(x:Array<T>) : Iterator<T> {
-		return new HaxeIterator(untyped x.__iter__());
+		return new HaxeIterator(Syntax.callField(x, "__iter__"));
 	}
 
 	public static function indexOf<T>(a:Array<T>, x : T, ?fromIndex:Int) : Int {
@@ -81,7 +81,7 @@ class ArrayImpl {
 	}
 
 	public static inline function pop<T>(x:Array<T>) : Null<T> {
-		return if (x.length == 0) null else untyped __field__(x, "pop")();
+		return if (x.length == 0) null else Syntax.callField(x, "pop");
 	}
 
 	public static inline function push<T>(x:Array<T>, e:T) : Int {
@@ -104,27 +104,27 @@ class ArrayImpl {
 
 	public static inline function shift<T>(x:Array<T>) : Null<T> {
 		if (x.length == 0) return null;
-		return untyped __field__(x, "pop")(0);
+		return Syntax.callField(x, "pop", 0);
 	}
 
 	public static inline function slice<T>(x:Array<T>, pos : Int, ?end : Int ) : Array<T> {
-		return untyped __python_array_get__(x, pos, end);
+		return Syntax.arrayAccess(x, pos, end);
 	}
 
 	public static inline function sort<T>(x:Array<T>, f:T->T->Int) : Void {
-		untyped __field__(x, "sort")( (untyped __named_arg__)("key", python.lib.FuncTools.cmp_to_key(f)));
+		Syntax.callNamedUntyped(Syntax.field(x, "sort"), { key : python.lib.FuncTools.cmp_to_key(f) });
 	}
 
 	public static inline function splice<T>(x:Array<T>, pos : Int, len : Int ) : Array<T> {
 		if (pos < 0) pos = x.length+pos;
 		if (pos < 0) pos = 0;
-		var res = untyped __python_array_get__(x, pos, pos+len);
-		untyped __python_del__(untyped __python_array_get__(x, pos, pos+len));
+		var res = Syntax.arrayAccess(x, pos, pos+len);
+		Syntax.delete((Syntax.arrayAccess(x, pos, pos+len)));
 		return res;
 	}
 
 	@:keep public static inline function map<S,T>(x:Array<T>, f : T -> S ) : Array<S> {
-		return Syntax.field(builtin(), "list")(Syntax.field(builtin(), "map")(f,cast x));
+		return Syntax.field(builtin(), "list")(Syntax.field(builtin(), "map")(f, x));
 	}
 
 	@:keep public static inline function filter<T>(x:Array<T>, f : T -> Bool ) : Array<T> {
@@ -140,26 +140,25 @@ class ArrayImpl {
 	}
 
 	@:keep private static inline function __get<T>(x:Array<T>, idx:Int):T {
-		var _hx_a = x;
-		if (idx >= _hx_a.length || idx < 0)
+
+		if (idx >= x.length || idx < 0)
 			return null;
 		else
 			return x[idx];
 	}
 
 	@:keep private static inline function __set<T>(x:Array<T>, idx:Int, v:T):T {
-		var _hx_a = x;
 
-		_hx_a[idx] = v;
+		x[idx] = v;
 		return v;
 	}
 
 	@:keep private static inline function __unsafe_get<T>(x:Array<T>,idx:Int):T {
-		return x[idx];
+		return Syntax.arrayAccess(x, idx);
 	}
 
 	@:keep private static inline function __unsafe_set<T>(x:Array<T>,idx:Int, val:T):T {
-		x[idx] = val;
+		Syntax.binop(Syntax.arrayAccess(x, idx), "=", val);
 		return val;
 	}
 }

+ 16 - 14
std/python/internal/StringImpl.hx

@@ -7,7 +7,7 @@ class StringImpl {
 
 
 
-	static inline function builtin ():Dynamic return untyped __python__("_hx_builtin");
+	static inline function builtin ():Dynamic return Syntax.untypedPython("_hx_builtin");
 
 
 	public static function split (s:String, d:String) {
@@ -15,19 +15,21 @@ class StringImpl {
 	}
 
 	public static function charCodeAt(s:String, index:Int) {
-		return if (s == null || s.length == 0 || index < 0 || index >= s.length) null else untyped ord(untyped __python_array_get__(s, index));
+		return
+			if (s == null || s.length == 0 || index < 0 || index >= s.length) null
+			else Syntax.callField(builtin(), "ord", Syntax.arrayAccess(s, index));
 	}
 	public static inline function charAt(s:String, index:Int) {
-		return if (index < 0 || index >= s.length) "" else untyped __python_array_get__(s,index);
+		return if (index < 0 || index >= s.length) "" else Syntax.arrayAccess(s,index);
 	}
 	public static inline function lastIndexOf(s:String, str:String, ?startIndex:Int):Int {
 		if (startIndex == null) {
-			return (untyped s.rfind)(str, 0, s.length);
+			return Syntax.callField(s, "rfind", str, 0, s.length);
 		} else {
 
-			var i = (untyped s.rfind)(str, 0, startIndex+1);
-			var startLeft = i == -1 ? Syntax.field(builtin(), "max")(0,startIndex+1-str.length) : i+1;
-			var check = (untyped s.find)(str, startLeft, s.length);
+			var i = Syntax.callField(s, "rfind", str, 0, startIndex+1);
+			var startLeft = i == -1 ? Syntax.callField(builtin(), "max", 0,startIndex+1-str.length) : i+1;
+			var check = Syntax.callField(s,"find", str, startLeft, s.length);
 			if (check > i && check <= startIndex) {
 				return check;
 			} else {
@@ -63,33 +65,33 @@ class StringImpl {
 		return "";
 		#else
 		var c = code;
-		return (Syntax.field('', "join")(Syntax.field(builtin(), "map")(Syntax.field(builtin(), "chr"), cast [c])):String); // TODO: check cast
+		return Syntax.callField('', "join",
+			Syntax.callField(builtin(), "map", Syntax.field(builtin(), "chr"), [c])); // TODO: check cast
 		#end
 	}
 
 	public static function substring( s:String, startIndex : Int, ?endIndex : Int ) : String {
 		if (startIndex < 0) startIndex = 0;
 		if (endIndex == null) {
-			return untyped __python__("s[startIndex:]");
+			return Syntax.arrayAccessWithLeadingColon(s, startIndex);
 		} else {
 			if (endIndex < 0) endIndex = 0;
 			if (endIndex < startIndex) {
 
-				return untyped __python__("s[endIndex:startIndex]");
+				return Syntax.arrayAccess(s, endIndex, startIndex);
 			} else {
 
-				return untyped __python__("s[startIndex:endIndex]");
+				return Syntax.arrayAccess(s, startIndex, endIndex);
 			}
-
 		}
 	}
 
 	public static function substr( s:String, startIndex : Int, ?len : Int ) : String {
 		if (len == null) {
-			return untyped __python__("s[startIndex:]");
+			return Syntax.arrayAccessWithLeadingColon(s, startIndex);
 		} else {
 			if (len == 0) return "";
-			return untyped __python__("s[startIndex:startIndex+len]");
+			return Syntax.arrayAccess(s, startIndex, startIndex+len);
 		}
 
 	}