comparison src/luan/modules/Io.luan @ 1192:e15a41a8b4b2

remove Io.repr
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 26 Feb 2018 14:51:44 -0700
parents 73d754b1889f
children 5d0cbd908582
comparison
equal deleted inserted replaced
1191:dada70cb57c1 1192:e15a41a8b4b2
79 out.close() 79 out.close()
80 return string_uri.read_text() 80 return string_uri.read_text()
81 end 81 end
82 82
83 83
84 -- repr
85
86 local function do_repr(out,obj,strict,done)
87 local tp = type(obj)
88 if tp == "table" then
89 if done[obj] == true then
90 strict and error "circular reference"
91 out.write "<circular reference>"
92 return
93 end
94 done[obj] = true
95 out.write( "{" )
96 local is_first = true
97 local in_list = {}
98 for key, value in ipairs(obj) do
99 if is_first then is_first = false else out.write ", " end
100 do_repr(out,value,strict,done)
101 in_list[key] = true
102 end
103 for key, value in pairs(obj) do
104 if in_list[key] ~= true then
105 if is_first then is_first = false else out.write ", " end
106 if type(key) == "string" and matches(key,"^[a-zA-Z_][a-zA-Z_0-9]*$") ~= nil then
107 out.write( key )
108 elseif type(key) == "table" then
109 out.write( "[<", key, ">]" )
110 else
111 out.write "["
112 do_repr(out,key,strict,done)
113 out.write "]"
114 end
115 out.write "="
116 do_repr(out,value,strict,done)
117 end
118 end
119 out.write "}"
120 elseif tp == "string" then
121 out.write( '"', encode(obj), '"' )
122 elseif tp == "nil" or tp == "boolean" or tp == "number" then
123 out.write( obj )
124 else
125 strict and error("can't repr type '"..tp.."' of "..obj)
126 out.write( "<", obj, ">" )
127 end
128 end
129
130 function Io.repr(obj,strict)
131 local string_uri = uri "string:"
132 local out = string_uri.text_writer()
133 do_repr(out,obj,strict or false,{})
134 out.close()
135 return string_uri.read_text()
136 end
137
138
139 -- debug 84 -- debug
140 85
141 function Io.debug(prompt) 86 function Io.debug(prompt)
142 prompt = prompt or "luan_debug> " 87 prompt = prompt or "luan_debug> "
143 local function console() 88 local function console()