comparison src/luan/modules/Io.luan @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents core/src/luan/modules/Io.luan@55983a476a21
children bae2d0c2576c
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 java()
2 local IoLuan = require "java:luan.modules.IoLuan"
3 local System = require "java:java.lang.System"
4
5 local M = {}
6
7 M.ip = IoLuan.ip
8 M.my_ips = IoLuan.my_ips
9 M.read_console_line = IoLuan.read_console_line
10 M.schemes = IoLuan.newSchemes()
11 M.uri = IoLuan.uri
12 M.stdin = IoLuan.defaultStdin.table()
13 M.socket_server = IoLuan.socket_server
14 M.stdout = IoLuan.textWriter(System.out)
15 M.stderr = IoLuan.textWriter(System.err)
16
17 -- used by http and rpc
18 M.password = "password"
19
20 local Luan = require "luan:Luan.luan"
21 local error = Luan.error
22 local to_string = Luan.to_string or error()
23 local type = Luan.type or error()
24 local try = Luan.try or error()
25 local ipairs = Luan.ipairs or error()
26 local pairs = Luan.pairs or error()
27 local values = Luan.values or error()
28 local load = Luan.load or error()
29 local Table = require "luan:Table.luan"
30 local unpack = Table.unpack or error()
31 local String = require "luan:String.luan"
32 local encode = String.encode or error()
33 local matches = String.matches or error()
34
35
36 -- do not change
37 function M.template_write(...)
38 return M.stdout.write(...)
39 end
40
41
42 function M.print_to(out,...)
43 local list = {}
44 for v in values(...) do
45 list[#list+1] = to_string(v)
46 list[#list+1] = '\t'
47 end
48 if #list > 0 then
49 list[#list] = '\n'
50 out.write( unpack(list) )
51 end
52 end
53
54 function M.print(...)
55 M.print_to(M.stdout,...)
56 end
57
58
59 function M.output_to(out,fn,...)
60 local old_out = M.stdout
61 return try( {
62 function(...)
63 M.stdout = out
64 fn(...)
65 end;
66 finally = function()
67 M.stdout = old_out
68 end;
69 }, ... )
70 end
71
72 local uri = M.uri -- make local
73
74 function M.output_of(fn,...)
75 local string_uri = uri "string:"
76 local out = string_uri.text_writer()
77 M.output_to(out,fn,...)
78 out.close()
79 return string_uri.read_text()
80 end
81
82
83 -- repr
84
85 local function do_repr(out,obj,strict,done)
86 local tp = type(obj)
87 if tp == "table" then
88 if done[obj] == true then
89 strict and error "circular reference"
90 out.write "<circular reference>"
91 return
92 end
93 done[obj] = true
94 out.write( "{" )
95 local is_first = true
96 local in_list = {}
97 for key, value in ipairs(obj) do
98 if is_first then is_first = false else out.write ", " end
99 do_repr(out,value,strict,done)
100 in_list[key] = true
101 end
102 for key, value in pairs(obj) do
103 if in_list[key] ~= true then
104 if is_first then is_first = false else out.write ", " end
105 if type(key) == "string" and matches(key,"^[a-zA-Z_][a-zA-Z_0-9]*$") ~= nil then
106 out.write( key )
107 elseif type(key) == "table" then
108 out.write( "[<", key, ">]" )
109 else
110 out.write "["
111 do_repr(out,key,strict,done)
112 out.write "]"
113 end
114 out.write "="
115 do_repr(out,value,strict,done)
116 end
117 end
118 out.write "}"
119 elseif tp == "string" then
120 out.write( '"', encode(obj), '"' )
121 elseif tp == "nil" or tp == "boolean" or tp == "number" then
122 out.write( obj )
123 else
124 strict and error("can't repr type '"..tp.."' of "..obj)
125 out.write( "<", obj, ">" )
126 end
127 end
128
129 function M.repr(obj,strict)
130 local string_uri = uri "string:"
131 local out = string_uri.text_writer()
132 do_repr(out,obj,strict or false,{})
133 out.close()
134 return string_uri.read_text()
135 end
136
137
138 -- useful for SimplyHTML responsiveness
139
140 local NO = {}
141 M.NO = NO
142
143 function M.dont_write_when_no(write_fn)
144 return function(...)
145 for v in values(...) do
146 if v == NO then
147 return
148 end
149 end
150 write_fn(...)
151 end
152 end
153
154
155 -- debug
156
157 function M.debug(prompt)
158 prompt = prompt or "luan_debug> "
159 local function console()
160 return M.read_console_line(prompt)
161 end
162 local env = {}
163 for line in console do
164 try {
165 function()
166 local fn
167 try {
168 function()
169 fn = load("return "..line,"stdin",env)
170 end
171 catch = function(e)
172 fn = load(line,"stdin",env)
173 end
174 }
175 M.print( fn() )
176 end
177 catch = function(e)
178 M.print(e)
179 end
180 }
181 end
182 end
183
184
185 return M