소스 검색

Allow `#if (some.field)` (#8005)

* [parser] allow `#if (something.field)`

* [typer] add some `target.name` etc. defines
Simon Krajewski 6 년 전
부모
커밋
2bb054b0be
4개의 변경된 파일38개의 추가작업 그리고 5개의 파일을 삭제
  1. 24 3
      src/context/common.ml
  2. 5 2
      src/core/define.ml
  3. 1 0
      src/syntax/grammar.mly
  4. 8 0
      src/syntax/parserEntry.ml

+ 24 - 3
src/context/common.ml

@@ -100,6 +100,8 @@ type platform_config = {
 	pf_uses_utf16 : bool;
 	(** target supports accessing `this` before calling `super(...)` **)
 	pf_this_before_super : bool;
+	(** target supports threads **)
+	pf_supports_threads : bool;
 }
 
 class compiler_callbacks = object(self)
@@ -279,6 +281,7 @@ let default_config =
 		pf_supports_function_equality = true;
 		pf_uses_utf16 = true;
 		pf_this_before_super = true;
+		pf_supports_threads = false;
 	}
 
 let get_config com =
@@ -308,6 +311,7 @@ let get_config com =
 			pf_static = false;
 			pf_pad_nulls = true;
 			pf_uses_utf16 = false;
+			pf_supports_threads = true;
 		}
 	| Flash when defined Define.As3 ->
 		{
@@ -337,6 +341,7 @@ let get_config com =
 			pf_capture_policy = CPWrapRef;
 			pf_pad_nulls = true;
 			pf_add_final_return = true;
+			pf_supports_threads = true;
 		}
 	| Cs ->
 		{
@@ -351,6 +356,7 @@ let get_config com =
 			pf_capture_policy = CPWrapRef;
 			pf_pad_nulls = true;
 			pf_overload = true;
+			pf_supports_threads = true;
 		}
 	| Python ->
 		{
@@ -371,6 +377,7 @@ let get_config com =
 			pf_static = false;
 			pf_pad_nulls = true;
 			pf_uses_utf16 = false;
+			pf_supports_threads = true;
 		}
 
 let memory_marker = [|Unix.time()|]
@@ -514,9 +521,23 @@ let init_platform com pf =
 	let forbid acc p = if p = name || PMap.mem p acc then acc else PMap.add p Forbidden acc in
 	com.package_rules <- List.fold_left forbid com.package_rules (List.map platform_name platforms);
 	com.config <- get_config com;
-	if com.config.pf_static then define com Define.Static;
-	if com.config.pf_sys then define com Define.Sys else com.package_rules <- PMap.add "sys" Forbidden com.package_rules;
-	if com.config.pf_uses_utf16 then define com Define.Utf16;
+	if com.config.pf_static then begin
+		raw_define_value com.defines "target.static" "true";
+		define com Define.Static;
+	end;
+	if com.config.pf_sys then begin
+		raw_define_value com.defines "target.sys" "true";
+		define com Define.Sys
+	end else
+		com.package_rules <- PMap.add "sys" Forbidden com.package_rules;
+	if com.config.pf_uses_utf16 then begin
+		raw_define_value com.defines "target.utf16" "true";
+		define com Define.Utf16;
+	end;
+	if com.config.pf_supports_threads then begin
+		raw_define_value com.defines "target.threaded" "true";
+	end;
+	raw_define_value com.defines "target.name" name;
 	raw_define com name
 
 let add_feature com f =

+ 5 - 2
src/core/define.ml

@@ -262,13 +262,16 @@ let defined_value_safe ?default ctx v =
 	try defined_value ctx v
 	with Not_found -> match default with Some s -> s | None -> ""
 
-let raw_define ctx v =
-	let k,v = try ExtString.String.split v "=" with _ -> v,"1" in
+let raw_define_value ctx k v =
 	ctx.values <- PMap.add k v ctx.values;
 	let k = String.concat "_" (ExtString.String.nsplit k "-") in
 	ctx.values <- PMap.add k v ctx.values;
 	ctx.defines_signature <- None
 
+let raw_define ctx v =
+	let k,v = try ExtString.String.split v "=" with _ -> v,"1" in
+	raw_define_value ctx k v
+
 let define_value ctx k v =
 	raw_define ctx (fst (infos k) ^ "=" ^ v)
 

+ 1 - 0
src/syntax/grammar.mly

@@ -1462,6 +1462,7 @@ let rec validate_macro_cond s e = match fst e with
 	| EUnop (op,p,e1) -> (EUnop (op, p, validate_macro_cond s e1), snd e)
 	| EBinop (op,e1,e2) -> (EBinop(op, (validate_macro_cond s e1), (validate_macro_cond s e2)), snd e)
 	| EParenthesis (e1) -> (EParenthesis (validate_macro_cond s e1), snd e)
+	| EField(e1,name) -> (EField(validate_macro_cond s e1,name), snd e)
 	| _ -> syntax_error (Custom ("Invalid conditional expression")) ~pos:(Some (pos e)) s ((EConst (Ident "false"),(pos e)))
 
 let parse_macro_ident t p s =

+ 8 - 0
src/syntax/parserEntry.ml

@@ -68,6 +68,14 @@ let rec eval ctx (e,p) =
 		| OpLt -> compare (<)
 		| OpLte -> compare (<=)
 		| _ -> error (Custom "Unsupported operation") p)
+	| EField _ ->
+		begin try
+			let sl = string_list_of_expr_path_raise (e,p) in
+			let i = String.concat "." (List.rev sl) in
+			(try TString (Define.raw_defined_value ctx i) with Not_found -> TNull)
+		with Exit ->
+			error (Custom "Invalid condition expression") p
+		end
 	| _ ->
 		error (Custom "Invalid condition expression") p