Explorar o código

added neko instanceof.

Nicolas Cannasse %!s(int64=20) %!d(string=hai) anos
pai
achega
7cee84dd71
Modificáronse 3 ficheiros con 41 adicións e 35 borrados
  1. 10 34
      genneko.ml
  2. 1 1
      std/Std.hx
  3. 30 0
      std/neko/Boot.hx

+ 10 - 34
genneko.ml

@@ -85,33 +85,6 @@ let gen_constant p c =
 	| TThis -> this p 
 	| TSuper -> assert false
 
-let op_str op =
-	match op with
-	| OpAdd -> "+"
-	| OpMult -> "*"
-	| OpDiv -> "/"
-	| OpSub -> "-"
-	| OpAssign -> "="
-	| OpEq -> "=="
-	| OpNotEq -> "!="
-	| OpGt -> ">"
-	| OpGte -> ">="
-	| OpLt -> "<"
-	| OpLte -> "<="
-	| OpAnd -> "&"
-	| OpOr -> "|"
-	| OpXor -> "^"
-	| OpBoolAnd -> "&&"
-	| OpBoolOr -> "||"
-	| OpShl -> "<<"
-	| OpShr -> ">>"
-	| OpUShr -> ">>>"
-	| OpMod -> "%"
-	| OpPhysEq
-	| OpPhysNotEq
-	| OpAssignOp _
-	| OpInterval -> assert false
-
 let rec gen_binop p op e1 e2 =
 	let gen_op str =
 		(EBinop (str,gen_expr e1,gen_expr e2),p)
@@ -119,9 +92,7 @@ let rec gen_binop p op e1 e2 =
 	match op with
 	| OpPhysEq -> (EBinop ("==", call p (builtin p "pcompare") [gen_expr e1; gen_expr e2], int p 0),p)
 	| OpPhysNotEq ->  (EBinop ("!=", call p (builtin p "pcompare") [gen_expr e1; gen_expr e2], int p 0),p)
-	| OpAssignOp op -> gen_op (op_str op ^ "=")
-	| OpInterval -> assert false (* handled by typer *)
-	| _ -> gen_op (op_str op)
+	| _ -> gen_op (Ast.s_binop op)
 
 and gen_unop p op flag e =	
 	match op with
@@ -245,6 +216,7 @@ and gen_expr e =
 					List.map (fun (e1,e2) -> gen_expr e1, gen_expr e2) cases,
 					(match eo with None -> None | Some e -> Some (gen_expr e))
 				),p)
+
 let gen_method c acc =
 	match c.cf_expr with
 	| None -> acc
@@ -255,6 +227,8 @@ let gen_method c acc =
 
 let gen_class p c =	
 	let clpath = gen_type_path null_pos (fst p,"@" ^ snd p) in
+	let stpath = gen_type_path null_pos p in
+	let esuper = match c.cl_super with None -> null null_pos | Some (c,_) -> gen_type_path null_pos (fst c.cl_path,"@" ^ snd c.cl_path) in
 	let fnew = (try
 		let f = PMap.find "new" c.cl_statics in
 		match follow f.cf_type with
@@ -281,22 +255,24 @@ let gen_class p c =
 		| _ -> []
 	with Not_found -> 
 		[]
-	) in
+	) in	
 	let estat = (EBinop ("=",
-		gen_type_path null_pos p,
+		stpath,
 		(EObject (PMap.fold gen_method c.cl_statics fnew),null_pos)
 	),null_pos) in
 	let p = null_pos in
 	let eclass = (EBinop ("=",
 		clpath,
-		call p (builtin p "new") [match c.cl_super with None -> null p | Some (c,_) -> gen_type_path p (fst c.cl_path,"@" ^ snd c.cl_path)]
+		call p (builtin p "new") [esuper]
 	),null_pos) in
+	let interf = array p (List.map (fun (c,_) -> gen_type_path p c.cl_path) c.cl_implements) in
+	let magic = ("__class__", call p (builtin p "array") [stpath; interf; match c.cl_super with None -> null p | Some _ -> field p esuper "__class__"]) in
 	let methods = PMap.fold gen_method c.cl_fields fstring in
 	(EBlock (
 		estat ::
 		eclass ::
 		(EVars ["@tmp", Some clpath],p) ::
-		(List.map (fun (f,e) -> (EBinop ("=",field p (ident p "@tmp") f,e),p)) methods)
+		(List.map (fun (f,e) -> (EBinop ("=",field p (ident p "@tmp") f,e),p)) (magic :: methods))
 	),p)
 
 let gen_enum_constr c =

+ 1 - 1
std/Std.hx

@@ -7,7 +7,7 @@ class Std {
 		#flash
 		Boot.__instanceof(obj,vclass);
 		#else neko
-		null;
+		Boot.__instanceof(obj,vclass);
 		#else error
 		#end
 	}

+ 30 - 0
std/neko/Boot.hx

@@ -1,10 +1,40 @@
 class Boot {
 
 
+	private static function __instanceof(o,cl) {
+		untyped {
+			switch __dollar__typeof(o) {
+			case __dollar__tint: return (cl == Int || cl == Float);
+			case __dollar__tfloat: return cl == Float;
+			case __dollar__tbool: return cl == Bool;
+			case __dollar__tobject:
+				var c = o.__class__;
+				while( c != null ) {
+					if( cl == c[0] )
+						return true;
+					var a = c[1];
+					var i = 0;
+					var l = __dollar__asize(a);
+					while( i < l ) {
+						if( cl == a[i] )
+							return true;
+						i += 1;
+					}
+					c = c[2];
+				}
+				return false;
+			default:
+				return false;
+			}
+		}
+	}
+
 	private static function __init() {
 		untyped {
 			String = NekoString__;
 			Array = NekoArray__;
+			Int = __dollar__new(null);
+			Float = __dollar__new(null);
 		}
 	}