ソースを参照

a little bit more cleanup, but we should get rid of HxOverrides if possible

frabbit 11 年 前
コミット
ac06eda128
2 ファイル変更45 行追加36 行削除
  1. 1 6
      std/python/Syntax.hx
  2. 44 30
      std/python/internal/HxOverrides.hx

+ 1 - 6
std/python/Syntax.hx

@@ -8,9 +8,8 @@ import haxe.macro.Context;
 import haxe.macro.ExprTools;
 #end
 
-
-
 class Syntax {
+
     #if macro
     static var self = macro python.Syntax;
     #end
@@ -21,14 +20,10 @@ class Syntax {
 
 	@: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{
             $self.untypedPython($v{e});
             $self.untypedPython($v{e1});

+ 44 - 30
std/python/internal/HxOverrides.hx

@@ -1,76 +1,90 @@
 package python.internal;
 
+import python.Syntax;
+
+import python.Syntax.untypedPython in py;
+
 @:keep
 @:native("HxOverrides")
 class HxOverrides {
 	static public function iterator(x) {
 		if (Std.is(x, Array)) {
-			return untyped __python__(" _hx_c.python_internal_ArrayImpl.iterator(x)");
+			return Syntax.untypedPython(" _hx_c.python_internal_ArrayImpl.iterator(x)");
 		} else {
-			return untyped __python__("x.iterator()");
+			return Syntax.untypedPython("x.iterator()");
 		}
 	}
 
 	static public function shift(x) {
 		if (Std.is(x, Array)) {
-			return untyped __python__(" _hx_c.python_internal_ArrayImpl.shift(x)");
+			return Syntax.untypedPython(" _hx_c.python_internal_ArrayImpl.shift(x)");
 		} else {
-			return untyped __python__("x.shift()");
+			return Syntax.untypedPython("x.shift()");
 		}
 	}
 
 	static public function filter(x, f) {
 		if (Std.is(x, Array)) {
-			return untyped __python__(" _hx_c.python_internal_ArrayImpl.filter(x, f)");
+			return (x:Array<Dynamic>).filter(f);
 		} else {
-			return untyped __python__("x.filter(f)");
+			return Syntax.untypedPython("x.filter(f)");
 		}
 	}
 
 	static public function map(x, f) {
 		if (Std.is(x, Array)) {
-			return untyped __python__(" _hx_c.python_internal_ArrayImpl.map(x, f)");
+			return Syntax.untypedPython(" _hx_c.python_internal_ArrayImpl.map(x, f)");
 		} else {
-			return untyped __python__("x.map(f)");
+			return Syntax.untypedPython("x.map(f)");
 		}
 	}
 
-	static public function length(x) {
-		if (Std.is(x, Array) || Std.is(x, String)) {
-			return untyped __python__(" _hx_builtin.len(x)");
-		} else {
-			return untyped __python__("x.length");
+	static public function length(x:Dynamic) {
+		return if (Std.is(x, Array))
+		{
+			(x:Array<Dynamic>).length;
+		}
+		else if (Std.is(x, String))
+		{
+			(x:String).length;
+		}
+		else
+		{
+			Reflect.field(x, "length");
 		}
 	}
 
 	static public function hx_rshift(val, n) {
-		return untyped __python__("(val % 0x100000000) >> n");
+		return Syntax.binop(Syntax.binop(val, "%", Syntax.untypedPython("0x100000000")), ">>", n);
+
 	}
 
 	static public function hx_modf(a, b) {
-		return untyped __python__("float('nan') if (b == 0.0) else a % b if a > 0 else -(-a % b)");
+		return Syntax.untypedPython("float('nan') if (b == 0.0) else a % b if a > 0 else -(-a % b)");
 	}
 
-	static public function hx_array_get(a, i) {
-		return untyped __python__("a[i] if (i < len(a) and i > -1) else None");
+	static public function hx_array_get<T>(a:Array<T>, i:Int):T {
+		return if (i < a.length && i > -1) Syntax.arrayAccess(a, i) else null;
 	}
 
-	static public function hx_array_set(a, i, v) {
-		untyped __python__("
-			l = len(a)
-			while l < i:
-				a.append(None)
-				l+=1
-			if l == i:
-				a.append(v)
-			else:
-				a[i] = v
-			return v");
+	static public function hx_array_set<X>(a:Array<X>, i:Int, v:X) {
+		var l = a.length;
+		while (l < i) {
+			a.push(null);
+			l+=1;
+		}
+		if (l == i) {
+			a.push(v);
+		} else {
+			Syntax.assign(Syntax.arrayAccess(a, i), v);
+		}
+		return v;
+
 	}
 
 	static public function hx_toUpperCase(x) {
 		if (Std.is(x, String)) {
-			return x.upper();
+			return (x:String).toUpperCase();
 		} else {
 			return x.toUpperCase();
 		}
@@ -78,7 +92,7 @@ class HxOverrides {
 
 	static public function hx_toLowerCase(x) {
 		if (Std.is(x, String)) {
-			return x.lower();
+			return (x:String).toLowerCase();
 		} else {
 			return x.toLowerCase();
 		}