فهرست منبع

added macro $p{Array<String>} to generate an EField chain

Simon Krajewski 12 سال پیش
والد
کامیت
d1c1f9e6b5
2فایلهای تغییر یافته به همراه7 افزوده شده و 1 حذف شده
  1. 2 0
      ast.ml
  2. 5 1
      std/haxe/macro/ExprTools.hx

+ 2 - 0
ast.ml

@@ -819,6 +819,8 @@ let reify in_macro =
 				(ECall ((EField ((EField ((EField ((EConst (Ident "haxe"),p),"macro"),p),"Context"),p),"makeExpr"),p),[e; to_pos (pos e)]),p)
 			| "$i" ->
 				(ECall ((EField ((EField ((EField ((EConst (Ident "haxe"),p),"macro"),p),"ExprTools"),p),"asIdent"),p),[e; to_pos (pos e)]),p)
+			| "$p" ->
+				(ECall ((EField ((EField ((EField ((EConst (Ident "haxe"),p),"macro"),p),"ExprTools"),p),"toFieldExpr"),p),[e]),p)
 			| _ ->
 				expr "EMeta" [to_obj [("name",to_string m p);("params",to_expr_array ml p);("pos",to_pos p)] p;loop e1]
 	in

+ 5 - 1
std/haxe/macro/ExprTools.hx

@@ -23,6 +23,7 @@
 package haxe.macro;
 
 import haxe.macro.Expr;
+using Lambda;
 
 /**
 	This class provides some utility methods to work with expressions. It is
@@ -37,6 +38,9 @@ class ExprTools {
 	static public function asIdent( s : String, p:Position ) : Expr
 		return { expr : EConst(CIdent(s)), pos : p }
 		
+	static public function toFieldExpr ( sl : Array<String> ) : Expr
+		return sl.fold(function(s, e) return e == null ? (macro $i{s}) : (macro $e.$s), null)
+	
 	/**
 		Converts expression [e] to a human-readable String representation.
 		
@@ -219,7 +223,7 @@ class ExprTools {
 		return e == null ? null : f(e)
 		
 	static inline function opt2(e:Null<Expr>, f : Expr -> Void):Void
-		if (e != null) f(e)		
+		if (e != null) f(e)
 }
 
 /**