Browse Source

add --eval

Simon Krajewski 11 năm trước cách đây
mục cha
commit
ef4b7e33f3
2 tập tin đã thay đổi với 33 bổ sung0 xóa
  1. 6 0
      main.ml
  2. 27 0
      typer.ml

+ 6 - 0
main.ml

@@ -932,6 +932,7 @@ try
 	let pre_compilation = ref [] in
 	let pre_compilation = ref [] in
 	let interp = ref false in
 	let interp = ref false in
 	let swf_version = ref false in
 	let swf_version = ref false in
+	let evals = ref [] in
 	Common.define_value com Define.HaxeVer (float_repres (float_of_int version /. 1000.));
 	Common.define_value com Define.HaxeVer (float_repres (float_of_int version /. 1000.));
 	Common.define_value com Define.HxcppApiLevel "312";
 	Common.define_value com Define.HxcppApiLevel "312";
 	Common.raw_define com "haxe3";
 	Common.raw_define com "haxe3";
@@ -1205,6 +1206,10 @@ try
 			force_typing := true;
 			force_typing := true;
 			config_macros := e :: !config_macros
 			config_macros := e :: !config_macros
 		)," : call the given macro before typing anything else");
 		)," : call the given macro before typing anything else");
+		("--eval", Arg.String (fun s ->
+			force_typing := true;
+			evals := s :: !evals;
+		), " : evaluates argument as Haxe module code");
 		("--wait", Arg.String (fun hp ->
 		("--wait", Arg.String (fun hp ->
 			let host, port = (try ExtString.String.split hp ":" with _ -> "127.0.0.1", hp) in
 			let host, port = (try ExtString.String.split hp ":" with _ -> "127.0.0.1", hp) in
 			wait_loop com host (try int_of_string port with _ -> raise (Arg.Bad "Invalid port"))
 			wait_loop com host (try int_of_string port with _ -> raise (Arg.Bad "Invalid port"))
@@ -1400,6 +1405,7 @@ try
 		Typecore.type_expr_ref := (fun ctx e with_type -> Typer.type_expr ctx e with_type);
 		Typecore.type_expr_ref := (fun ctx e with_type -> Typer.type_expr ctx e with_type);
 		let tctx = Typer.create com in
 		let tctx = Typer.create com in
 		List.iter (Typer.call_init_macro tctx) (List.rev !config_macros);
 		List.iter (Typer.call_init_macro tctx) (List.rev !config_macros);
+		List.iter (Typer.eval tctx) !evals;
 		List.iter (fun cpath -> ignore(tctx.Typecore.g.Typecore.do_load_module tctx cpath Ast.null_pos)) (List.rev !classes);
 		List.iter (fun cpath -> ignore(tctx.Typecore.g.Typecore.do_load_module tctx cpath Ast.null_pos)) (List.rev !classes);
 		Typer.finalize tctx;
 		Typer.finalize tctx;
 		t();
 		t();

+ 27 - 0
typer.ml

@@ -351,6 +351,33 @@ let parse_string com s p inlined =
 	restore();
 	restore();
 	decls
 	decls
 
 
+let eval ctx s =
+	let p = { pfile = "--eval"; pmin = 0; pmax = String.length s; } in
+	let decls = parse_string ctx.com s p false in
+	let rec find_main current decls = match decls with
+		| (EClass c,_) :: decls ->
+			begin try
+				let cff = List.find (fun cff -> cff.cff_name = "main") c.d_data in
+				if ctx.com.main_class <> None then error "Multiple main" cff.cff_pos;
+				ctx.com.main_class <- Some ([],c.d_name);
+				Some ([],c.d_name)
+			with Not_found ->
+				find_main (if current = None then Some ([],c.d_name) else current) decls
+			end
+		| ((EEnum {d_name = s} | ETypedef {d_name = s} | EAbstract {d_name = s}),_) :: decls when current = None ->
+			find_main (Some ([],s)) decls
+		| _ :: decls ->
+			find_main current decls
+		| [] ->
+			current
+	in
+	let path_module = match find_main None decls with
+		| None -> error "Evaluated string did not define any types" p
+		| Some path -> path
+	in
+	ignore(Typeload.type_module ctx path_module "eval" decls p);
+	flush_pass ctx PBuildClass "load_module"
+
 let parse_expr_string ctx s p inl =
 let parse_expr_string ctx s p inl =
 	let head = "class X{static function main() " in
 	let head = "class X{static function main() " in
 	let head = (if p.pmin > String.length head then head ^ String.make (p.pmin - String.length head) ' ' else head) in
 	let head = (if p.pmin > String.length head then head ^ String.make (p.pmin - String.length head) ' ' else head) in