소스 검색

allowed optional args in function types

Nicolas Cannasse 13 년 전
부모
커밋
2454f800a0
6개의 변경된 파일32개의 추가작업 그리고 15개의 파일을 삭제
  1. 1 0
      ast.ml
  2. 1 0
      doc/CHANGES.txt
  3. 4 0
      interp.ml
  4. 19 14
      parser.ml
  5. 1 0
      std/haxe/macro/Expr.hx
  6. 6 1
      typeload.ml

+ 1 - 0
ast.ml

@@ -152,6 +152,7 @@ and complex_type =
 	| CTAnonymous of class_field list
 	| CTParent of complex_type
 	| CTExtend of type_path * class_field list
+	| CTOptional of complex_type
 
 and func = {
 	f_params : type_param list;

+ 1 - 0
doc/CHANGES.txt

@@ -19,6 +19,7 @@
 	flash : output traces to native trace() when using -D fdb or -D nativeTrace
 	all : allowed abitrary string fields in anonymous objects
 	all : structure fields which are Null<X> are now optional (for constant values)
+	all : allowed optional args in functions types (?Int -> Void)
 
 2011-09-25: 2.08
 	js : added js.JQuery

+ 4 - 0
interp.ml

@@ -3050,6 +3050,8 @@ and encode_type t =
 		3, [encode_type t]
 	| CTExtend (t,fields) ->
 		4, [encode_path t; enc_array (List.map encode_field fields)]
+	| CTOptional t ->
+		5, [encode_type t]
 	in
 	enc_enum ICType tag pl
 
@@ -3321,6 +3323,8 @@ and decode_ctype t =
 		CTParent (decode_ctype t)
 	| 4, [t;fl] ->
 		CTExtend (decode_path t, List.map decode_field (dec_array fl))
+	| 5, [t] ->
+		CTOptional (decode_ctype t)
 	| _ ->
 		raise Invalid_expr
 

+ 19 - 14
parser.ml

@@ -331,21 +331,26 @@ and parse_type_opt = parser
 	| [< '(DblDot,_); t = parse_complex_type >] -> Some t
 	| [< >] -> None
 
-and parse_complex_type = parser
-	| [< '(POpen,_); t = parse_complex_type; '(PClose,_); s >] -> parse_complex_type_next (CTParent t) s
+and parse_complex_type s =
+	let t = parse_complex_type_inner s in
+	parse_complex_type_next t s
+
+and parse_complex_type_inner = parser
+	| [< '(POpen,_); t = parse_complex_type; '(PClose,_) >] -> CTParent t
 	| [< '(BrOpen,p1); s >] ->
-		let t = (match s with parser
-			| [< name, p = any_ident >] -> CTAnonymous (parse_type_anonymous_resume name p s)
-			| [< '(Binop OpGt,_); t = parse_type_path; '(Comma,_); s >] ->
-				(match s with parser
-				| [< name, p = any_ident; l = parse_type_anonymous_resume name p >] -> CTExtend (t,l)
-				| [< l, _ = parse_class_fields true p1 >] -> CTExtend (t,l)
-				| [< >] -> serror())
-			| [< l, _ = parse_class_fields true p1 >] -> CTAnonymous l
-			| [< >] -> serror()
-		) in
-		parse_complex_type_next t s
-	| [< t = parse_type_path; s >] -> parse_complex_type_next (CTPath t) s
+		(match s with parser
+		| [< name, p = any_ident >] -> CTAnonymous (parse_type_anonymous_resume name p s)
+		| [< '(Binop OpGt,_); t = parse_type_path; '(Comma,_); s >] ->
+			(match s with parser
+			| [< name, p = any_ident; l = parse_type_anonymous_resume name p >] -> CTExtend (t,l)
+			| [< l, _ = parse_class_fields true p1 >] -> CTExtend (t,l)
+			| [< >] -> serror())
+		| [< l, _ = parse_class_fields true p1 >] -> CTAnonymous l
+		| [< >] -> serror())
+	| [< '(Question,_); t = parse_complex_type_inner >] ->
+		CTOptional t
+	| [< t = parse_type_path >] ->
+		CTPath t
 
 and parse_type_path s = parse_type_path1 [] s
 

+ 1 - 0
std/haxe/macro/Expr.hx

@@ -124,6 +124,7 @@ enum ComplexType {
 	TAnonymous( fields : Array<Field> );
 	TParent( t : ComplexType );
 	TExtend( p : TypePath, fields : Array<Field> );
+	TOptional( t : ComplexType );
 }
 
 typedef TypePath = {

+ 6 - 1
typeload.ml

@@ -167,6 +167,7 @@ and load_complex_type ctx p t =
 	match t with
 	| CTParent t -> load_complex_type ctx p t
 	| CTPath t -> load_instance ctx t p false
+	| CTOptional _ -> error "Optional type not allowed outside function arguments" p
 	| CTExtend (t,l) ->
 		(match load_complex_type ctx p (CTAnonymous l) with
 		| TAnon a ->
@@ -259,7 +260,10 @@ and load_complex_type ctx p t =
 		| [CTPath { tpackage = []; tparams = []; tname = "Void" }] ->
 			TFun ([],load_complex_type ctx p r)
 		| _ ->
-			TFun (List.map (fun t -> "",false,load_complex_type ctx p t) args,load_complex_type ctx p r)
+			TFun (List.map (fun t ->
+				let t, opt = (match t with CTOptional t -> t, true | _ -> t,false) in
+				"",opt,load_complex_type ctx p t
+			) args,load_complex_type ctx p r)
 
 let hide_types ctx =
 	let old_locals = ctx.local_types in
@@ -1203,6 +1207,7 @@ let init_class ctx c p herits fields =
 							| CTFunction (tl,t) -> List.for_all is_qualified tl && is_qualified t
 							| CTAnonymous fl -> List.for_all is_qual_field fl
 							| CTExtend (t,fl) -> is_qual_name t && List.for_all is_qual_field fl
+							| CTOptional t -> is_qualified t
 						and is_qual_field f =
 							match f.cff_kind with
 							| FVar (t,_) -> is_qual_opt t