1 require 'metalua.compiler'
3 module ('metaloop', package.seeall)
10 do -- set readline() to a line reader, either editline otr a default
11 local status, _ = pcall(require, 'editline')
13 local rl_handle = editline.init 'metalua'
14 readline = |p| rl_handle:read(p)
16 readline = |p| io.write(p) and io.read '*l'
20 function reached_eof(lx, msg)
21 return lx:peek().tag=='Eof' or msg:find "token `Eof"
24 printf ("Metalua, interactive REPLoop.\n"..
25 "(c) 2006-2008 <metalua@gmail.com>")
30 local src, lx, ast, f, results, success
32 local line = readline(next(lines) and PROMPT2 or PROMPT)
33 if not line then print(); os.exit(0) end -- line==nil iff eof on stdin
34 if not next(lines) then
35 line = line:gsub('^%s*=', 'return ')
37 table.insert(lines, line)
38 src = table.concat (lines, "\n")
41 lx = mlc.lexstream_of_luastring(src)
42 success, ast = pcall(mlc.ast_of_lexstream, lx)
44 success, f = pcall(mlc.function_of_ast, ast, '=stdin')
46 results = { pcall(f) }
47 success = table.remove (results, 1)
50 table.iforeach(|x| table.print(x, LINE_WIDTH), results)
53 print "Evaluation error:"
58 print "Can't compile into bytecode:"
63 -- If lx has been read entirely, try to read another
64 -- line before failing.
65 if not reached_eof(lx, ast) then
66 print "Can't compile source into AST:"