Mercurial Hosting > luan
diff core/src/luan/modules/Io.luan @ 680:ecd436959855
improve template statements
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Apr 2016 16:38:30 -0600 |
parents | 35dde32c02ab |
children | ca169567ce07 |
line wrap: on
line diff
--- 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