Browse Source

[typer] add some utility functions

Simon Krajewski 6 years ago
parent
commit
1eacebad50
2 changed files with 55 additions and 1 deletions
  1. 34 1
      src/core/texpr.ml
  2. 21 0
      src/core/type.ml

+ 34 - 1
src/core/texpr.ml

@@ -499,4 +499,37 @@ let dump_with_pos tabs e =
 			loop e1
 			loop e1
 	in
 	in
 	loop' tabs e;
 	loop' tabs e;
-	Buffer.contents buf
+	Buffer.contents buf
+
+let collect_captured_vars e =
+	let known = Hashtbl.create 0 in
+	let unknown = ref [] in
+	let accesses_this = ref false in
+	let declare v = Hashtbl.add known v.v_id () in
+	let rec loop e = match e.eexpr with
+		| TLocal ({v_capture = true; v_id = id} as v) when not (Hashtbl.mem known id) ->
+			Hashtbl.add known id ();
+			unknown := v :: !unknown
+		| TConst (TThis | TSuper) ->
+			accesses_this := true;
+		| TVar(v,eo) ->
+			Option.may loop eo;
+			declare v
+		| TFor(v,e1,e2) ->
+			declare v;
+			loop e1;
+			loop e2;
+		| TFunction tf ->
+			List.iter (fun (v,_) -> declare v) tf.tf_args;
+			loop tf.tf_expr
+		| TTry(e1,catches) ->
+			loop e1;
+			List.iter (fun (v,e) ->
+				declare v;
+				loop e;
+			) catches
+		| _ ->
+			Type.iter loop e
+	in
+	loop e;
+	List.rev !unknown,!accesses_this

+ 21 - 0
src/core/type.ml

@@ -2827,6 +2827,27 @@ module ExtType = struct
 		| TAbstract({a_path=[],"Void"},_) -> true
 		| TAbstract({a_path=[],"Void"},_) -> true
 		| _ -> false
 		| _ -> false
 
 
+	let is_int t = match t with
+		| TAbstract({a_path=[],"Int"},_) -> true
+		| _ -> false
+
+	let is_float t = match t with
+		| TAbstract({a_path=[],"Float"},_) -> true
+		| _ -> false
+
+	let is_numeric t = match t with
+		| TAbstract({a_path=[],"Float"},_) -> true
+		| TAbstract({a_path=[],"Int"},_) -> true
+		| _ -> true
+
+	let is_string t = match t with
+		| TInst({cl_path=[],"String"},_) -> true
+		| _ -> false
+
+	let is_bool t = match t with
+		| TAbstract({a_path=[],"Bool"},_) -> true
+		| _ -> false
+
 	type semantics =
 	type semantics =
 		| VariableSemantics
 		| VariableSemantics
 		| ReferenceSemantics
 		| ReferenceSemantics