Parcourir la source

use Std.format

Nicolas Cannasse il y a 14 ans
Parent
commit
22b1633070
1 fichiers modifiés avec 23 ajouts et 57 suppressions
  1. 23 57
      std/haxe/macro/DefaultJSGenerator.hx

+ 23 - 57
std/haxe/macro/DefaultJSGenerator.hx

@@ -69,43 +69,9 @@ class DefaultJSGenerator {
 	}
 
 	@:macro static function fprint( e : Expr ) {
-		switch( e.expr ) {
-		case EConst(c):
-			switch( c ) {
-			case CString(str):
-				var exprs = [];
-				var r = ~/%((\([^\)]+\))|([A-Za-z_][A-Za-z0-9_]*))/;
-				var pos = e.pos;
-				var inf = Context.getPosInfos(pos);
-				inf.min++; // string quote
-				while( r.match(str) ) {
-					var left = r.matchedLeft();
-					if( left.length > 0 ) {
-						exprs.push( { expr : EConst(CString(left)), pos : pos } );
-						inf.min += left.length;
-					}
-					var v = r.matched(1);
-					if( v.charCodeAt(0) == "(".code ) {
-						var pos = Context.makePosition( { min : inf.min + 2, max : inf.min + v.length, file : inf.file } );
-						exprs.push(Context.parse(v.substr(1, v.length-2), pos));
-					} else {
-						var pos = Context.makePosition( { min : inf.min + 1, max : inf.min + 1 + v.length, file : inf.file } );
-						exprs.push( { expr : EConst(CIdent(v)), pos : pos } );
-					}
-					inf.min += v.length + 1;
-					str = r.matchedRight();
-				}
-				exprs.push({ expr : EConst(CString(str)), pos : pos });
-				var ret = null;
-				for( e in exprs )
-					if( ret == null ) ret = e else ret = { expr : EBinop(OpAdd, ret, e), pos : pos };
-				return { expr : ECall({ expr : EConst(CIdent("print")), pos : pos },[ret]), pos : pos };
-			default:
-			}
-		default:
-		}
-		Context.error("Expression should be a constant string", e.pos);
-		return null;
+		var pos = haxe.macro.Context.currentPos();
+		var ret = haxe.macro.Format.format(e);
+		return { expr : ECall({ expr : EConst(CIdent("print")), pos : pos },[ret]), pos : pos };
 	}
 
 	function field(p) {
@@ -121,10 +87,10 @@ class DefaultJSGenerator {
 				continue;
 			packages.set(full, true);
 			if( prev == null )
-				fprint("if(typeof %x=='undefined') %x = {}");
+				fprint("if(typeof $x=='undefined') $x = {}");
 			else {
 				var p = prev + field(x);
-				fprint("if(!%p) %p = {}");
+				fprint("if(!$p) $p = {}");
 			}
 			newline();
 		}
@@ -142,7 +108,7 @@ class DefaultJSGenerator {
 	function genClassField( c : ClassType, p : String, f : ClassField ) {
 		checkFieldName(c, f);
 		var field = field(f.name);
-		fprint("%p.prototype%field = ");
+		fprint("$p.prototype$field = ");
 		if( f.expr == null )
 			print("null");
 		else {
@@ -156,11 +122,11 @@ class DefaultJSGenerator {
 		checkFieldName(c, f);
 		var field = field(f.name);
 		if( f.expr == null ) {
-			fprint("%p%field = null");
+			fprint("$p$field = null");
 			newline();
 		} else switch( f.kind ) {
 		case FMethod(_):
-			fprint("%p%field = ");
+			fprint("$p$field = ");
 			api.setDebugInfos(c, f.name, true);
 			genExpr(f.expr);
 			newline();
@@ -172,7 +138,7 @@ class DefaultJSGenerator {
 	function genClass( c : ClassType ) {
 		genPackage(c.pack);
 		var p = getPath(c);
-		fprint("%p = ");
+		fprint("$p = ");
 		api.setDebugInfos(c, "new", false);
 		if( c.constructor != null )
 			print(api.generateConstructor(c.constructor.get().expr));
@@ -180,13 +146,13 @@ class DefaultJSGenerator {
 			print("function() { }");
 		newline();
 		var name = p.split(".").map(api.quoteString).join(",");
-		fprint("%p.__name__ = [%name]");
+		fprint("$p.__name__ = [$name]");
 		newline();
 		if( c.superClass != null ) {
 			var psup = getPath(c.superClass.t.get());
-			fprint("%p.__super__ = %psup");
+			fprint("$p.__super__ = $psup");
 			newline();
-			fprint("for(var k in %psup.prototype ) %p.prototype[k] = %psup.prototype[k]");
+			fprint("for(var k in $psup.prototype ) $p.prototype[k] = $psup.prototype[k]");
 			newline();
 		}
 		for( f in c.statics.get() )
@@ -199,12 +165,12 @@ class DefaultJSGenerator {
 			}
 			genClassField(c, p, f);
 		}
-		fprint("%p.prototype.__class__ = %p");
+		fprint("$p.prototype.__class__ = $p");
 		newline();
 		if( c.interfaces.length > 0 ) {
 			var me = this;
 			var inter = c.interfaces.map(function(i) return me.getPath(i.t.get())).join(",");
-			fprint("%p.__interfaces__ = [%inter]");
+			fprint("$p.__interfaces__ = [$inter]");
 			newline();
 		}
 	}
@@ -214,28 +180,28 @@ class DefaultJSGenerator {
 		var p = getPath(e);
 		var names = p.split(".").map(api.quoteString).join(",");
 		var constructs = e.names.map(api.quoteString).join(",");
-		fprint("%p = { __ename__ : [%names], __constructs__ : [%constructs] }");
+		fprint("$p = { __ename__ : [$names], __constructs__ : [$constructs] }");
 		newline();
 		for( c in e.constructs.keys() ) {
 			var c = e.constructs.get(c);
 			var f = field(c.name);
-			fprint("%p%f = ");
+			fprint("$p$f = ");
 			switch( c.type ) {
 			case TFun(args, _):
 				var sargs = args.map(function(a) return a.name).join(",");
-				fprint('function(%sargs) { var $x = ["%(c.name)",%(c.index),%sargs]; $x.__enum__ = %p; $x.toString = $estr; return $x; }');
+				fprint('function($sargs) { var $$x = ["${c.name}",${c.index},$sargs]; $$x.__enum__ = $p; $$x.toString = $$estr; return $$x; }');
 			default:
 				print("[" + api.quoteString(c.name) + "," + c.index + "]");
 				newline();
-				fprint("%p%f.toString = $estr");
+				fprint("$p$f.toString = $$estr");
 				newline();
-				fprint("%p%f.__enum__ = %p");
+				fprint("$p$f.__enum__ = $$p");
 			}
 			newline();
 		}
 		var meta = api.buildMetaData(e);
 		if( meta != null ) {
-			fprint("%p.__meta__ = ");
+			fprint("$p.__meta__ = ");
 			genExpr(meta);
 			newline();
 		}
@@ -245,7 +211,7 @@ class DefaultJSGenerator {
 	function genStaticValue( c : ClassType, cf : ClassField ) {
 		var p = getPath(c);
 		var f = field(cf.name);
-		fprint("%p%f = ");
+		fprint("$p$f = ");
 		genExpr(cf.expr);
 		newline();
 	}
@@ -281,9 +247,9 @@ class DefaultJSGenerator {
 		print("js.Boot.__res = {}");
 		newline();
 		if( Context.defined("debug") ) {
-			fprint("%(api.stackVar) = []");
+			fprint("${api.stackVar} = []");
 			newline();
-			fprint("%(api.excVar) = []");
+			fprint("${api.excVar} = []");
 			newline();
 		}
 		print("js.Boot.__init()");