diff options
author | David Walter Seikel | 2012-01-23 21:58:02 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-23 21:58:02 +1000 |
commit | 2d1df4714e2736dbde7855ddcd76b4c1de822fa5 (patch) | |
tree | c80e4622631c92dbe8dd6375d187702d084c2f2b /LuaSL/testLua/ilua.lua | |
parent | Remove the conflict, and the comment. lol (diff) | |
download | SledjHamr-2d1df4714e2736dbde7855ddcd76b4c1de822fa5.zip SledjHamr-2d1df4714e2736dbde7855ddcd76b4c1de822fa5.tar.gz SledjHamr-2d1df4714e2736dbde7855ddcd76b4c1de822fa5.tar.bz2 SledjHamr-2d1df4714e2736dbde7855ddcd76b4c1de822fa5.tar.xz |
Added a big bunch of example lua scripts for testing the speed of lua compiling.
Diffstat (limited to '')
-rw-r--r-- | LuaSL/testLua/ilua.lua | 381 |
1 files changed, 381 insertions, 0 deletions
diff --git a/LuaSL/testLua/ilua.lua b/LuaSL/testLua/ilua.lua new file mode 100644 index 0000000..f58505a --- /dev/null +++ b/LuaSL/testLua/ilua.lua | |||
@@ -0,0 +1,381 @@ | |||
1 | -- ilua.lua | ||
2 | -- A more friendly Lua interactive prompt | ||
3 | -- doesn't need '=' | ||
4 | -- will try to print out tables recursively, subject to the pretty_print_limit value. | ||
5 | -- Steve Donovan, 2007 | ||
6 | -- | ||
7 | local pretty_print_limit = 20 | ||
8 | local max_depth = 7 | ||
9 | local table_clever = true | ||
10 | local prompt = '> ' | ||
11 | local verbose = false | ||
12 | local strict = true | ||
13 | -- suppress strict warnings | ||
14 | _ = true | ||
15 | |||
16 | -- imported global functions | ||
17 | local sub = string.sub | ||
18 | local match = string.match | ||
19 | local find = string.find | ||
20 | local push = table.insert | ||
21 | local pop = table.remove | ||
22 | local append = table.insert | ||
23 | local concat = table.concat | ||
24 | local floor = math.floor | ||
25 | local write = io.write | ||
26 | local read = io.read | ||
27 | |||
28 | local savef | ||
29 | local collisions = {} | ||
30 | local G_LIB = {} | ||
31 | local declared = {} | ||
32 | local line_handler_fn, global_handler_fn | ||
33 | local print_handlers = {} | ||
34 | |||
35 | ilua = {} | ||
36 | local num_prec | ||
37 | local num_all | ||
38 | |||
39 | local jstack = {} | ||
40 | |||
41 | local function oprint(...) | ||
42 | if savef then | ||
43 | savef:write(concat({...},' '),'\n') | ||
44 | end | ||
45 | print(...) | ||
46 | end | ||
47 | |||
48 | local function join(tbl,delim,limit,depth) | ||
49 | if not limit then limit = pretty_print_limit end | ||
50 | if not depth then depth = max_depth end | ||
51 | local n = #tbl | ||
52 | local res = '' | ||
53 | local k = 0 | ||
54 | -- very important to avoid disgracing ourselves with circular referencs... | ||
55 | if #jstack > depth then | ||
56 | return "..." | ||
57 | end | ||
58 | for i,t in ipairs(jstack) do | ||
59 | if tbl == t then | ||
60 | return "<self>" | ||
61 | end | ||
62 | end | ||
63 | push(jstack,tbl) | ||
64 | -- this is a hack to work out if a table is 'list-like' or 'map-like' | ||
65 | -- you can switch it off with ilua.table_options {clever = false} | ||
66 | local is_list | ||
67 | if table_clever then | ||
68 | local index1 = n > 0 and tbl[1] | ||
69 | local index2 = n > 1 and tbl[2] | ||
70 | is_list = index1 and index2 | ||
71 | end | ||
72 | if is_list then | ||
73 | for i,v in ipairs(tbl) do | ||
74 | res = res..delim..val2str(v) | ||
75 | k = k + 1 | ||
76 | if k > limit then | ||
77 | res = res.." ... " | ||
78 | break | ||
79 | end | ||
80 | end | ||
81 | else | ||
82 | for key,v in pairs(tbl) do | ||
83 | if type(key) == 'number' then | ||
84 | key = '['..tostring(key)..']' | ||
85 | else | ||
86 | key = tostring(key) | ||
87 | end | ||
88 | res = res..delim..key..'='..val2str(v) | ||
89 | k = k + 1 | ||
90 | if k > limit then | ||
91 | res = res.." ... " | ||
92 | break | ||
93 | end | ||
94 | end | ||
95 | end | ||
96 | pop(jstack) | ||
97 | return sub(res,2) | ||
98 | end | ||
99 | |||
100 | |||
101 | function val2str(val) | ||
102 | local tp = type(val) | ||
103 | if print_handlers[tp] then | ||
104 | local s = print_handlers[tp](val) | ||
105 | return s or '?' | ||
106 | end | ||
107 | if tp == 'function' then | ||
108 | return tostring(val) | ||
109 | elseif tp == 'table' then | ||
110 | if val.__tostring then | ||
111 | return tostring(val) | ||
112 | else | ||
113 | return '{'..join(val,',')..'}' | ||
114 | end | ||
115 | elseif tp == 'string' then | ||
116 | return "'"..val.."'" | ||
117 | elseif tp == 'number' then | ||
118 | -- we try only to apply floating-point precision for numbers deemed to be floating-point, | ||
119 | -- unless the 3rd arg to precision() is true. | ||
120 | if num_prec and (num_all or floor(val) ~= val) then | ||
121 | return num_prec:format(val) | ||
122 | else | ||
123 | return tostring(val) | ||
124 | end | ||
125 | else | ||
126 | return tostring(val) | ||
127 | end | ||
128 | end | ||
129 | |||
130 | function _pretty_print(...) | ||
131 | for i,val in ipairs(arg) do | ||
132 | oprint(val2str(val)) | ||
133 | end | ||
134 | _G['_'] = arg[1] | ||
135 | end | ||
136 | |||
137 | function compile(line) | ||
138 | if verbose then oprint(line) end | ||
139 | local f,err = loadstring(line,'local') | ||
140 | return err,f | ||
141 | end | ||
142 | |||
143 | function evaluate(chunk) | ||
144 | local ok,res = pcall(chunk) | ||
145 | if not ok then | ||
146 | return res | ||
147 | end | ||
148 | return nil -- meaning, fine! | ||
149 | end | ||
150 | |||
151 | function eval_lua(line) | ||
152 | if savef then | ||
153 | savef:write(prompt,line,'\n') | ||
154 | end | ||
155 | -- is the line handler interested? | ||
156 | if line_handler_fn then | ||
157 | line = line_handler_fn(line) | ||
158 | -- returning nil here means that the handler doesn't want | ||
159 | -- Lua to see the string | ||
160 | if not line then return end | ||
161 | end | ||
162 | -- is it an expression? | ||
163 | local err,chunk = compile('_pretty_print('..line..')') | ||
164 | if err then | ||
165 | -- otherwise, a statement? | ||
166 | err,chunk = compile(line) | ||
167 | end | ||
168 | -- if compiled ok, then evaluate the chunk | ||
169 | if not err then | ||
170 | err = evaluate(chunk) | ||
171 | end | ||
172 | -- if there was any error, print it out | ||
173 | if err then | ||
174 | oprint(err) | ||
175 | end | ||
176 | end | ||
177 | |||
178 | local function quit(code,msg) | ||
179 | io.stderr:write(msg,'\n') | ||
180 | os.exit(code) | ||
181 | end | ||
182 | |||
183 | -- functions available in scripts | ||
184 | function ilua.precision(len,prec,all) | ||
185 | if not len then num_prec = nil | ||
186 | else | ||
187 | num_prec = '%'..len..'.'..prec..'f' | ||
188 | end | ||
189 | num_all = all | ||
190 | end | ||
191 | |||
192 | function ilua.table_options(t) | ||
193 | if t.limit then pretty_print_limit = t.limit end | ||
194 | if t.depth then max_depth = t.depth end | ||
195 | if t.clever ~= nil then table_clever = t.clever end | ||
196 | end | ||
197 | |||
198 | -- inject @tbl into the global namespace | ||
199 | function ilua.import(tbl,dont_complain,lib) | ||
200 | lib = lib or '<unknown>' | ||
201 | if type(tbl) == 'table' then | ||
202 | for k,v in pairs(tbl) do | ||
203 | local key = rawget(_G,k) | ||
204 | -- NB to keep track of collisions! | ||
205 | if key and k ~= '_M' and k ~= '_NAME' and k ~= '_PACKAGE' and k ~= '_VERSION' then | ||
206 | append(collisions,{k,lib,G_LIB[k]}) | ||
207 | end | ||
208 | _G[k] = v | ||
209 | G_LIB[k] = lib | ||
210 | end | ||
211 | end | ||
212 | if not dont_complain and #collisions > 0 then | ||
213 | for i, coll in ipairs(collisions) do | ||
214 | local name,lib,oldlib = coll[1],coll[2],coll[3] | ||
215 | write('warning: ',lib,'.',name,' overwrites ') | ||
216 | if oldlib then | ||
217 | write(oldlib,'.',name,'\n') | ||
218 | else | ||
219 | write('global ',name,'\n') | ||
220 | end | ||
221 | end | ||
222 | end | ||
223 | end | ||
224 | |||
225 | function ilua.print_handler(name,handler) | ||
226 | print_handlers[name] = handler | ||
227 | end | ||
228 | |||
229 | function ilua.line_handler(handler) | ||
230 | line_handler_fn = handler | ||
231 | end | ||
232 | |||
233 | function ilua.global_handler(handler) | ||
234 | global_handler_fn = handler | ||
235 | end | ||
236 | |||
237 | function ilua.print_variables() | ||
238 | for name,v in pairs(declared) do | ||
239 | print(name,type(_G[name])) | ||
240 | end | ||
241 | end | ||
242 | -- | ||
243 | -- strict.lua | ||
244 | -- checks uses of undeclared global variables | ||
245 | -- All global variables must be 'declared' through a regular assignment | ||
246 | -- (even assigning nil will do) in a main chunk before being used | ||
247 | -- anywhere. | ||
248 | -- | ||
249 | local function set_strict() | ||
250 | local mt = getmetatable(_G) | ||
251 | if mt == nil then | ||
252 | mt = {} | ||
253 | setmetatable(_G, mt) | ||
254 | end | ||
255 | |||
256 | local function what () | ||
257 | local d = debug.getinfo(3, "S") | ||
258 | return d and d.what or "C" | ||
259 | end | ||
260 | |||
261 | mt.__newindex = function (t, n, v) | ||
262 | declared[n] = true | ||
263 | rawset(t, n, v) | ||
264 | end | ||
265 | |||
266 | mt.__index = function (t, n) | ||
267 | if not declared[n] and what() ~= "C" then | ||
268 | local lookup = global_handler_fn and global_handler_fn(n) | ||
269 | if not lookup then | ||
270 | error("variable '"..n.."' is not declared", 2) | ||
271 | else | ||
272 | return lookup | ||
273 | end | ||
274 | end | ||
275 | return rawget(t, n) | ||
276 | end | ||
277 | |||
278 | end | ||
279 | |||
280 | --- Initial operations which may not succeed! | ||
281 | -- try to bring in any ilua configuration file; don't complain if this is unsuccessful | ||
282 | pcall(function() | ||
283 | require 'ilua-defs' | ||
284 | end) | ||
285 | |||
286 | -- Unix readline support, if readline.so is available... | ||
287 | local rl,readline,saveline | ||
288 | err = pcall(function() | ||
289 | rl = require 'readline' | ||
290 | readline = rl.readline | ||
291 | saveline = rl.add_history | ||
292 | end) | ||
293 | if not rl then | ||
294 | readline = function(prompt) | ||
295 | write(prompt) | ||
296 | return read() | ||
297 | end | ||
298 | saveline = function(s) end | ||
299 | end | ||
300 | |||
301 | -- process command-line parameters | ||
302 | if arg then | ||
303 | local i = 1 | ||
304 | |||
305 | local function parm_value(opt,parm,def) | ||
306 | local val = parm:sub(3) | ||
307 | if #val == 0 then | ||
308 | i = i + 1 | ||
309 | if i > #arg then | ||
310 | if not def then | ||
311 | quit(-1,"expecting parameter for option '-"..opt.."'") | ||
312 | else | ||
313 | return def | ||
314 | end | ||
315 | end | ||
316 | val = arg[i] | ||
317 | end | ||
318 | return val | ||
319 | end | ||
320 | |||
321 | while i <= #arg do | ||
322 | local v = arg[i] | ||
323 | local opt = v:sub(1,1) | ||
324 | if opt == '-' then | ||
325 | opt = v:sub(2,2) | ||
326 | if opt == 'h' then | ||
327 | quit(0,"ilua (-l lib) (-L lib) (lua files)") | ||
328 | elseif opt == 'l' then | ||
329 | require (parm_value(opt,v)) | ||
330 | elseif opt == 'L' then | ||
331 | local lib = parm_value(opt,v) | ||
332 | local tbl = require (lib) | ||
333 | -- we cannot always trust require to return the table! | ||
334 | if type(tbl) ~= 'table' then | ||
335 | tbl = _G[lib] | ||
336 | end | ||
337 | ilua.import(tbl,true,lib) | ||
338 | elseif opt == 't' or opt == 'T' then | ||
339 | local file | ||
340 | if opt == 'T' then | ||
341 | file = 'ilua_'..os.date ('%y_%m_%d_%H_%M')..'.log' | ||
342 | else | ||
343 | file = parm_value(opt,v,"ilua.log") | ||
344 | end | ||
345 | print('saving transcript "'..file..'"') | ||
346 | savef = io.open(file,'w') | ||
347 | savef:write('! ilua ',concat(arg,' '),'\n') | ||
348 | elseif opt == 's' then | ||
349 | strict = false | ||
350 | elseif opt == 'v' then | ||
351 | verbose = true | ||
352 | end | ||
353 | else -- a plain file to be executed immediately | ||
354 | dofile(v) | ||
355 | end | ||
356 | i = i + 1 | ||
357 | end | ||
358 | end | ||
359 | |||
360 | print 'ILUA: Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio\n"quit" to end' | ||
361 | |||
362 | -- any import complaints? | ||
363 | ilua.import() | ||
364 | |||
365 | -- enable 'not declared' error | ||
366 | if strict then | ||
367 | set_strict() | ||
368 | end | ||
369 | |||
370 | local line = readline(prompt) | ||
371 | while line do | ||
372 | if line == 'quit' then break end | ||
373 | eval_lua(line) | ||
374 | saveline(line) | ||
375 | line = readline(prompt) | ||
376 | end | ||
377 | |||
378 | if savef then | ||
379 | savef:close() | ||
380 | end | ||
381 | |||