changeset 680:ecd436959855

improve template statements
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Apr 2016 16:38:30 -0600
parents 43522473599d
children f1c935be546d
files core/src/luan/impl/LuanParser.java core/src/luan/modules/Io.luan
diffstat 2 files changed, 36 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
diff -r 43522473599d -r ecd436959855 core/src/luan/impl/LuanParser.java
--- a/core/src/luan/impl/LuanParser.java	Thu Apr 14 15:19:25 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Thu Apr 14 16:38:30 2016 -0600
@@ -183,6 +183,7 @@
 	private Frame frame;
 	private final Parser parser;
 	private final Stmts top;
+	private boolean hasTemplateWrite = false;
 
 	LuanParser(String sourceName,String sourceText) {
 //		this.source = source;
@@ -372,14 +373,14 @@
 		Expr exprs = TemplateExpressions(In.NOTHING);
 		if( exprs == null )
 			return null;
-		Expr requireCall = new Expr(Val.SINGLE,false);
-		requireCall.add( "PackageLuan.require(luan,\"luan:Io\")" );
-		Expr stdoutExp = indexExpStr( requireCall.single(), constExpStr("stdout") );
-		Expr writeExp = indexExpStr( stdoutExp, constExpStr("write") );
-		Expr writeCall = callExpStr( writeExp, exprs );
+		if( !hasTemplateWrite ) {
+			top.add(0,"final LuanFunction template_write = Luan.checkFunction(luan.index(PackageLuan.require(luan,\"luan:Io\"),\"template_write\"));  ");
+			hasTemplateWrite = true;
+		}
 		Stmts stmt = new Stmts();
-		stmt.addAll( writeCall );
-		stmt.add( ";  " );
+		stmt.add( "template_write.call(luan," );
+		stmt.addAll( exprs.array() );
+		stmt.add( ");  " );
 		return stmt;
 	}
 
diff -r 43522473599d -r ecd436959855 core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Thu Apr 14 15:19:25 2016 -0600
+++ b/core/src/luan/modules/Io.luan	Thu Apr 14 16:38:30 2016 -0600
@@ -29,6 +29,12 @@
 local matches = String.matches or error()
 
 
+-- do not change
+function M.template_write(...)
+	return M.stdout.write(...)
+end
+
+
 function M.print_to(out,...)
 	local list = {}
 	for v in values(...) do
@@ -72,49 +78,56 @@
 
 -- repr
 
-local function do_repr(obj,strict,done)
+local function do_repr(out,obj,strict,done)
 	local tp = type(obj)
 	if tp == "table" then
 		if done[obj] == true then
 			strict and error "circular reference"
-			%><circular reference><%
+			out.write "<circular reference>"
 			return
 		end
 		done[obj] = true
-		%>{<%
+		out.write( "{" )
 		local is_first = true
 		local in_list = {}
 		for key, value in ipairs(obj) do
-			if is_first then is_first = false else %>, <% end
-			do_repr(value,strict,done)
+			if is_first then is_first = false else out.write ", " end
+			do_repr(out,value,strict,done)
 			in_list[key] = true
 		end
 		for key, value in pairs(obj) do
 			if in_list[key] ~= true then
-				if is_first then is_first = false else %>, <% end
+				if is_first then is_first = false else out.write ", " end
 				if type(key) == "string" and matches(key,"^[a-zA-Z_][a-zA-Z_0-9]*$") ~= nil then
-					%><%=key%><%
+					out.write( key )
 				elseif type(key) == "table" then
-					%>[<<%=key%>>]<%
+					out.write( "[<", key, ">]" )
 				else
-					%>[<%do_repr(key,strict,done)%>]<%
+					out.write "["
+					do_repr(out,key,strict,done)
+					out.write "]"
 				end
-				%>=<% do_repr(value,strict,done)
+				out.write "="
+				do_repr(out,value,strict,done)
 			end
 		end
-		%>}<%
+		out.write "}"
 	elseif tp == "string" then
-		%>"<%=encode(obj)%>"<%
+		out.write( '"', encode(obj), '"' )
 	elseif tp == "nil" or tp == "boolean" or tp == "number" then
-		%><%=obj%><%
+		out.write( obj )
 	else
 		strict and error("can't repr type '"..tp.."' of "..obj)
-		%><<%=obj%>><%
+		out.write( "<", obj, ">" )
 	end
 end
 
 function M.repr(obj,strict)
-	return M.output_of(do_repr,obj,strict or false,{})
+	local string_uri = uri "string:"
+	local out = string_uri.text_writer()
+	do_repr(out,obj,strict or false,{})
+	out.close()
+	return string_uri.read_text()
 end