Skip to content

Commit

Permalink
indexed enums
Browse files Browse the repository at this point in the history
git-svn-id: http://haxe.googlecode.com/svn/trunk@2191 f16182fa-f095-11de-8f43-4547254af6c6
  • Loading branch information
ncannasse committed Oct 16, 2007
1 parent c200dd0 commit 75325cb
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 122 deletions.
3 changes: 3 additions & 0 deletions doc/CHANGES.txt
Expand Up @@ -20,6 +20,9 @@
removed haxe.Proxy
added flash.XMLRequest
fixed Type.enumParameters for Neko
guaranteed order of Type.getEnumConstructs same as code
used index-based dispatch for enums
added Type.enumIndex

2007-08-29: 1.15
fixed bug with Enum.construct when Enum have type parameters
Expand Down
12 changes: 7 additions & 5 deletions genas3.ml
Expand Up @@ -517,11 +517,11 @@ and gen_expr ctx e =
print ctx "var %s : enum = " tmp;
gen_value ctx e;
newline ctx;
print ctx "switch( %s.tag ) {" tmp;
print ctx "switch( %s.index ) {" tmp;
newline ctx;
List.iter (fun (cl,params,e) ->
List.iter (fun c ->
print ctx "case \"%s\":" c;
print ctx "case %d:" c;
newline ctx;
) cl;
let b = save_locals ctx in
Expand Down Expand Up @@ -840,7 +840,7 @@ let generate_enum ctx e =
newline ctx;
print ctx "public static const __isenum : Boolean = true";
newline ctx;
print ctx "public function %s( t : String, p : Array = null ) : void { this.tag = t; this.params = p; }" ename;
print ctx "public function %s( t : String, index : int, p : Array = null ) : void { this.tag = t; this.index = index; this.params = p; }" ename;
PMap.iter (fun _ c ->
newline ctx;
match c.ef_type with
Expand All @@ -851,11 +851,11 @@ let generate_enum ctx e =
if o then spr ctx " = null";
) args;
print ctx ") : %s {" ename;
print ctx " return new %s(\"%s\",[" ename c.ef_name;
print ctx " return new %s(\"%s\",%d,[" ename c.ef_name c.ef_index;
concat ctx "," (fun (a,_,_) -> spr ctx a) args;
print ctx "]); }";
| _ ->
print ctx "public static var %s : %s = new %s(\"%s\")" c.ef_name ename ename c.ef_name;
print ctx "public static var %s : %s = new %s(\"%s\",%d)" c.ef_name ename ename c.ef_name c.ef_index;
) e.e_constrs;
cl();
newline ctx;
Expand All @@ -872,6 +872,8 @@ let generate_base_enum ctx =
newline ctx;
spr ctx "public var tag : String";
newline ctx;
spr ctx "public var index : int";
newline ctx;
spr ctx "public var params : Array";
cl();
newline ctx;
Expand Down
15 changes: 8 additions & 7 deletions genjs.ml
Expand Up @@ -382,21 +382,21 @@ and gen_expr ctx e =
bend();
newline ctx;
spr ctx "}";
| TMatch (e,_,cases,def) ->
| TMatch (e,(estruct,_),cases,def) ->
spr ctx "var $e = ";
gen_value ctx e;
newline ctx;
spr ctx "switch( $e[0] ) {";
spr ctx "switch( $e[1] ) {";
newline ctx;
List.iter (fun (cl,params,e) ->
List.iter (fun c ->
print ctx "case \"%s\":" c;
print ctx "case %d:" c;
newline ctx;
) cl;
(match params with
| None | Some [] -> ()
| Some l ->
let n = ref 0 in
let n = ref 1 in
let l = List.fold_left (fun acc (v,_) -> incr n; match v with None -> acc | Some v -> (v,!n) :: acc) [] l in
match l with
| [] -> ()
Expand Down Expand Up @@ -637,16 +637,17 @@ let generate_class ctx c =
let generate_enum ctx e =
let p = s_path e.e_path in
generate_package_create ctx e.e_path;
print ctx "%s = { __ename__ : [%s] }" p (String.concat "," (List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst e.e_path @ [snd e.e_path])));
let ename = List.map (fun s -> Printf.sprintf "\"%s\"" (Ast.s_escape s)) (fst e.e_path @ [snd e.e_path]) in
print ctx "%s = { __ename__ : [%s], __constructs__ : [%s] }" p (String.concat "," ename) (String.concat "," (List.map (Printf.sprintf "\"%s\"") e.e_names));
newline ctx;
PMap.iter (fun _ f ->
print ctx "%s%s = " p (field f.ef_name);
(match f.ef_type with
| TFun (args,_) ->
let sargs = String.concat "," (List.map arg_name args) in
print ctx "function(%s) { var $x = [\"%s\",%s]; $x.__enum__ = %s; return $x; }" sargs f.ef_name sargs p;
print ctx "function(%s) { var $x = [\"%s\",%d,%s]; $x.__enum__ = %s; return $x; }" sargs f.ef_name f.ef_index sargs p;
| _ ->
print ctx "[\"%s\"]" f.ef_name;
print ctx "[\"%s\",%d]" f.ef_name f.ef_index;
newline ctx;
print ctx "%s%s.__enum__ = %s" p (field f.ef_name) p;
);
Expand Down
13 changes: 7 additions & 6 deletions genneko.ml
Expand Up @@ -402,7 +402,7 @@ and gen_expr ctx e =
call p (builtin p "throw") [gen_expr ctx e]
| TMatch (e,_,cases,eo) ->
let etmp = (EVars ["@tmp",Some (gen_expr ctx e)],p) in
let etag = field p (ident p "@tmp") "tag" in
let eindex = field p (ident p "@tmp") "index" in
let gen_params params e =
match params with
| None ->
Expand Down Expand Up @@ -433,10 +433,10 @@ and gen_expr ctx e =
(EBlock [
etmp;
(ESwitch (
etag,
eindex,
List.map (fun (cl,params,e2) ->
let cond = match cl with
| [s] -> str p s
| [s] -> int p s
| _ -> raise Exit
in
cond , gen_params params e2
Expand All @@ -448,12 +448,12 @@ and gen_expr ctx e =
Exit ->
(EBlock [
etmp;
(EVars ["@tag",Some etag],p);
(EVars ["@index",Some eindex],p);
List.fold_left (fun acc (cl,params,e2) ->
let cond = (match cl with
| [] -> assert false
| c :: l ->
let eq c = (EBinop ("==",ident p "@tag",str p c),p) in
let eq c = (EBinop ("==",ident p "@index",int p c),p) in
List.fold_left (fun acc c -> (EBinop ("||",acc,eq c),p)) (eq c) l
) in
EIf (cond,gen_params params e2,Some acc),p
Expand Down Expand Up @@ -572,6 +572,7 @@ let gen_enum_constr ctx path c =
(EBlock [
(EVars ["@tmp",Some (EObject [
"tag" , str p c.ef_name;
"index" , int p c.ef_index;
"args" , array p (List.map (ident p) params);
],p)],p);
call p (builtin p "objsetproto") [ident p "@tmp"; field p path "prototype"];
Expand All @@ -580,7 +581,7 @@ let gen_enum_constr ctx path c =
),p)
| _ ->
(EBlock [
(EVars ["@tmp",Some (EObject ["tag" , str p c.ef_name; "__serialize" , ident p "@tag_serialize"],p)],p);
(EVars ["@tmp",Some (EObject ["tag" , str p c.ef_name; "index", int p c.ef_index; "__serialize" , ident p "@tag_serialize"],p)],p);
call p (builtin p "objsetproto") [ident p "@tmp"; field p path "prototype"];
ident p "@tmp";
],p)
Expand Down
21 changes: 14 additions & 7 deletions genswf8.ml
Expand Up @@ -657,15 +657,15 @@ and gen_match ctx retval e cases def =
gen_expr ctx true e;
let renum = alloc_tmp ctx in
set_tmp ctx renum;
push ctx [VInt 0];
push ctx [VInt 1];
write ctx AObjGet;
let rtag = alloc_tmp ctx in
set_tmp ctx rtag;
let first = ref true in
let dispatch = List.map (fun (cl,params,e) ->
List.map (fun c ->
if !first then first := false else get_tmp ctx rtag;
push ctx [VStr (c,false)];
push ctx [VInt c];
write ctx APhysEqual;
cjmp ctx
) cl, params, e
Expand All @@ -680,7 +680,7 @@ and gen_match ctx retval e cases def =
let regs = ctx.regs in
let nregs = ctx.reg_count in
List.iter (fun j -> j()) jl;
let n = ref 0 in
let n = ref 1 in
List.iter (fun (a,t) ->
incr n;
match a with
Expand Down Expand Up @@ -1117,7 +1117,7 @@ let gen_enum_field ctx e f =
ctx.reg_count <- 1;
let no_reg = ctx.version = 6 in
let rargs = List.map (fun (n,_,_) -> if no_reg then 0, n else alloc_reg ctx , "") args in
let nregs = List.length rargs + 1 in
let nregs = List.length rargs + 2 in
let tf = func ctx false false rargs in
List.iter (fun (r,name) ->
if no_reg then begin
Expand All @@ -1126,7 +1126,7 @@ let gen_enum_field ctx e f =
end else
push ctx [VReg r]
) (List.rev rargs);
push ctx [VStr (f.ef_name,false); VInt nregs];
push ctx [VInt f.ef_index; VStr (f.ef_name,false); VInt nregs];
write ctx AInitArray;
write ctx ADup;
push ctx [VStr ("__enum__",false); VThis];
Expand All @@ -1135,9 +1135,9 @@ let gen_enum_field ctx e f =
write ctx AReturn;
tf();
| t ->
push ctx [VStr (f.ef_name,false); VInt 1];
push ctx [VInt f.ef_index; VStr (f.ef_name,false); VInt 2];
write ctx AInitArray;
ctx.stack_size <- ctx.stack_size - 1;
ctx.stack_size <- ctx.stack_size - 2;
write ctx ADup;
push ctx [VStr ("__enum__",false); VReg 0];
write ctx AObjSet;
Expand Down Expand Up @@ -1294,6 +1294,13 @@ let gen_type_def ctx t =
write ctx (ASetReg 0);
setvar ctx acc;
init_name ctx e.e_path true;
let nitems = List.length e.e_names in
push ctx [VReg 0; VStr ("__constructs__",true)];
List.iter (fun s -> push ctx [VStr (s,true)]) (List.rev e.e_names);
push ctx [VInt nitems];
write ctx AInitArray;
write ctx AObjSet;
ctx.stack_size <- ctx.stack_size - nitems;
PMap.iter (fun _ f -> gen_enum_field ctx e f) e.e_constrs
| TTypeDecl _ ->
()
Expand Down
44 changes: 31 additions & 13 deletions genswf9.ml
Expand Up @@ -130,9 +130,11 @@ let t_void = TEnum ({
e_extern = false;
e_types = [];
e_constrs = PMap.empty;
e_names = [];
},[])

let t_string = TInst (mk_class ([],"String") null_pos None false,[])
let t_int = TInst (mk_class ([],"Int") null_pos None false,[])

let lookup i w =
try
Expand Down Expand Up @@ -916,11 +918,11 @@ let rec gen_expr_content ctx retval e =
| TMatch (e0,_,cases,def) ->
let t = classify ctx e.etype in
let rparams = alloc_reg ctx KDynamic in
let rtag = alloc_reg ctx KDynamic in
let rindex = alloc_reg ctx KInt in
gen_expr ctx true e0;
write ctx A3Dup;
write ctx (A3GetProp (ident ctx "tag"));
set_reg ctx rtag;
write ctx (A3GetProp (ident ctx "index"));
set_reg ctx rindex;
write ctx (A3GetProp (ident ctx "params"));
set_reg ctx rparams;
let branch = begin_branch ctx in
Expand All @@ -931,12 +933,12 @@ let rec gen_expr_content ctx retval e =
| [] ->
assert false
| [tag] ->
write ctx (A3Reg rtag.rid);
write ctx (A3String (lookup tag ctx.strings));
write ctx (A3Reg rindex.rid);
write ctx (A3Int tag);
prev := jump ctx J3Neq;
| tag :: l ->
write ctx (A3Reg rtag.rid);
write ctx (A3String (lookup tag ctx.strings));
write ctx (A3Reg rindex.rid);
write ctx (A3Int tag);
let j = jump ctx J3Eq in
loop l;
j()
Expand Down Expand Up @@ -981,7 +983,7 @@ let rec gen_expr_content ctx retval e =
);
List.iter (fun j -> j()) jend;
branch();
free_reg ctx rtag;
free_reg ctx rindex;
free_reg ctx rparams

and gen_call ctx e el =
Expand Down Expand Up @@ -1320,10 +1322,15 @@ let generate_enum_init ctx e slot =
write ctx (A3Reg r.rid);
write ctx (A3FindPropStrict name_id);
write ctx (A3String (lookup f.ef_name ctx.strings));
write ctx (A3Int f.ef_index);
write ctx A3Null;
write ctx (A3ConstructProperty (name_id,2));
write ctx (A3ConstructProperty (name_id,3));
write ctx (A3SetSlot !nslot);
) e.e_constrs;
write ctx (A3Reg r.rid);
List.iter (fun n -> write ctx (A3String (lookup n ctx.strings))) e.e_names;
write ctx (A3Array (List.length e.e_names));
write ctx (A3SetSlot (!nslot + 1));
free_reg ctx r

let generate_field_kind ctx f c stat =
Expand Down Expand Up @@ -1441,14 +1448,18 @@ let generate_class ctx c =
let generate_enum ctx e =
let name_id = type_path ctx e.e_path in
let st_id = empty_method ctx in
let f = begin_fun ctx [("tag",false,t_string);("params",false,mk_mono())] t_void [] false in
let f = begin_fun ctx [("tag",false,t_string);("index",false,t_int);("params",false,mk_mono())] t_void [] false in
let tag_id = ident ctx "tag" in
let index_id = ident ctx "index" in
let params_id = ident ctx "params" in
write ctx (A3FindProp tag_id);
write ctx (A3Reg 1);
write ctx (A3InitProp tag_id);
write ctx (A3FindProp params_id);
write ctx (A3FindProp index_id);
write ctx (A3Reg 2);
write ctx (A3InitProp index_id);
write ctx (A3FindProp params_id);
write ctx (A3Reg 3);
write ctx (A3InitProp params_id);
write ctx A3RetVoid;
let construct = f() in
Expand All @@ -1469,6 +1480,7 @@ let generate_enum ctx e =
cl3_construct = construct;
cl3_fields = [|
{ f3_name = tag_id; f3_slot = 0; f3_kind = A3FVar { v3_type = None; v3_value = A3VNone; v3_const = false; }; f3_metas = None };
{ f3_name = index_id; f3_slot = 0; f3_kind = A3FVar { v3_type = None; v3_value = A3VNone; v3_const = false; }; f3_metas = None };
{ f3_name = params_id; f3_slot = 0; f3_kind = A3FVar { v3_type = None; v3_value = A3VNone; v3_const = false; }; f3_metas = None };
{ f3_name = ident ctx "__enum__"; f3_slot = 0; f3_kind = A3FVar { v3_type = None; v3_value = A3VBool true; v3_const = true }; f3_metas = None };
{
Expand All @@ -1495,10 +1507,11 @@ let generate_enum ctx e =
let fdata = begin_fun ctx args (TEnum (e,[])) [] true in
write ctx (A3FindPropStrict name_id);
write ctx (A3String (lookup f.ef_name ctx.strings));
write ctx (A3Int f.ef_index);
let n = ref 0 in
List.iter (fun _ -> incr n; write ctx (A3Reg !n)) args;
write ctx (A3Array (!n));
write ctx (A3ConstructProperty (name_id,2));
write ctx (A3ConstructProperty (name_id,3));
write ctx A3Ret;
let fid = fdata() in
A3FMethod {
Expand All @@ -1517,9 +1530,14 @@ let generate_enum ctx e =
st3_method = st_id;
st3_fields = Array.of_list ({
f3_name = ident ctx "__isenum";
f3_slot = !st_count + 1;
f3_slot = !st_count + 2;
f3_kind = A3FVar { v3_type = None; v3_value = A3VBool true; v3_const = true; };
f3_metas = None;
} :: {
f3_name = ident ctx "__constructs__";
f3_slot = !st_count + 1;
f3_kind = A3FVar { v3_type = None; v3_value = A3VNone; v3_const = false; };
f3_metas = None;
} :: constrs)
} in
ctx.classes <- sc :: ctx.classes;
Expand Down

0 comments on commit 75325cb

Please sign in to comment.