changeset 422:af82b266fe89

add Io.repr()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2015 16:13:52 -0600
parents b31d614343e8
children 1eafb11a150d
files core/src/luan/LuanFunction.java core/src/luan/LuanTable.java core/src/luan/modules/BasicLuan.java core/src/luan/modules/Io.luan
diffstat 4 files changed, 65 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r b31d614343e8 -r af82b266fe89 core/src/luan/LuanFunction.java
--- a/core/src/luan/LuanFunction.java	Fri May 01 15:13:14 2015 -0600
+++ b/core/src/luan/LuanFunction.java	Fri May 01 16:13:52 2015 -0600
@@ -8,7 +8,7 @@
 	public static final Object[] NOTHING = new Object[0];
 
 	@Override public String toString() {
-		return "function:" + Integer.toHexString(hashCode());
+		return "function: " + Integer.toHexString(hashCode());
 	}
 
 	@Override public String repr(LuanState luan) {
diff -r b31d614343e8 -r af82b266fe89 core/src/luan/LuanTable.java
--- a/core/src/luan/LuanTable.java	Fri May 01 15:13:14 2015 -0600
+++ b/core/src/luan/LuanTable.java	Fri May 01 16:13:52 2015 -0600
@@ -391,7 +391,7 @@
 	}
 
 	@Override public final String toString() {
-		return "table:" + Integer.toHexString(hashCode());
+		return "table: " + Integer.toHexString(hashCode());
 	}
 
 }
diff -r b31d614343e8 -r af82b266fe89 core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Fri May 01 15:13:14 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Fri May 01 16:13:52 2015 -0600
@@ -220,7 +220,7 @@
 		return obj instanceof LuanFunction ? (LuanFunction)obj : null;
 	}
 
-	public static void try_(LuanState luan,LuanTable blocks) throws LuanException {
+	public static Object try_(LuanState luan,LuanTable blocks) throws LuanException {
 		Utils.checkNotNull(luan,blocks);
 		Object obj = blocks.get(1);
 		if( obj == null )
@@ -243,11 +243,11 @@
 			finallyFn = (LuanFunction)obj;
 		}
 		try {
-			luan.call(tryFn);
+			return luan.call(tryFn);
 		} catch(LuanException e) {
 			if( catchFn == null )
 				throw e;
-			luan.call(catchFn,new Object[]{e});
+			return luan.call(catchFn,new Object[]{e});
 		} finally {
 			if( finallyFn != null )
 				luan.call(finallyFn);
diff -r b31d614343e8 -r af82b266fe89 core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Fri May 01 15:13:14 2015 -0600
+++ b/core/src/luan/modules/Io.luan	Fri May 01 16:13:52 2015 -0600
@@ -13,6 +13,10 @@
 
 local Luan = require "luan:Luan"
 local to_string = Luan.to_string
+local type = Luan.type
+local try = Luan.try
+local ipairs = Luan.ipairs
+local pairs = Luan.pairs
 local Table = require "luan:Table"
 
 function print_to(out,...)
@@ -34,6 +38,62 @@
 end
 
 
+-- repr
+
+local function do_repr(obj,done)
+	local tp = type(obj)
+	if tp == "table" then
+		if done[obj] == true then
+			%><circular reference><%
+			return
+		end
+		done[obj] = true
+		%>{<%
+		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,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 type(key) == "string" and key.match "^[a-zA-Z_][a-zA-Z_0-9]*$" ~= nil then
+					%><%=key%><%
+				else
+					%>[<%do_repr(key,done)%>]<%
+				end
+				%>=<% do_repr(value,done)
+			end
+		end
+		%>}<%
+	elseif tp == "string" then
+		%>"<%=obj.encode()%>"<%
+	elseif tp == "nil" or tp == "boolean" or tp == "number" then
+		%><%=obj%><%
+	else
+		%><<%=obj%>><%
+	end
+end
+
+function repr(obj)
+	local old_out = stdout
+	return try {
+		function()
+			local string_uri = Uri "string:"
+			stdout = string_uri.text_writer()
+			do_repr(obj,{})
+			stdout.close()
+			return string_uri.read_text()
+		end;
+		finally = function()
+			stdout = old_out
+		end;
+	}
+end
+
+
 -- useful for SimplyHTML responsiveness
 
 NO = {}