1 -------------------------------------------------------------------------------
2 -- Copyright (c) 2006-2013 Fabien Fleutot and others.
4 -- All rights reserved.
6 -- This program and the accompanying materials are made available
7 -- under the terms of the Eclipse Public License v1.0 which
8 -- accompanies this distribution, and is available at
9 -- http://www.eclipse.org/legal/epl-v10.html
11 -- This program and the accompanying materials are also made available
12 -- under the terms of the MIT public license which accompanies this
13 -- distribution, and is available at http://www.lua.org/license.html
16 -- Fabien Fleutot - API and implementation
18 -------------------------------------------------------------------------------
26 local pp=require 'metalua.pprint'
29 mlc = require 'metalua.compiler'.new()
33 do -- set readline() to a line reader, either editline otr a default
34 local status, _ = pcall(require, 'editline')
36 local rl_handle = editline.init 'metalua'
37 readline = |p| rl_handle:read(p)
39 local status, rl = pcall(require, 'readline')
41 rl.set_options{histfile='~/.metalua_history', keeplines=100, completion=false }
42 readline = rl.readline
43 else -- neither editline nor readline available
53 local function reached_eof(lx, msg)
54 return lx:peek().tag=='Eof' or msg:find "token `Eof"
59 pp.printf ("Metalua, interactive REPLoop.\n"..
60 "(c) 2006-2013 <metalua@gmail.com>")
63 local src, lx, ast, f, results, success
65 local line = readline(next(lines) and PROMPT2 or PROMPT)
66 if not line then print(); os.exit(0) end -- line==nil iff eof on stdin
67 if not next(lines) then
68 line = line:gsub('^%s*=', 'return ')
70 table.insert(lines, line)
71 src = table.concat (lines, "\n")
73 lx = mlc :src_to_lexstream(src)
74 success, ast = pcall(mlc.lexstream_to_ast, mlc, lx)
76 success, f = pcall(mlc.ast_to_function, mlc, ast, '=stdin')
78 results = { xpcall(f, debug.traceback) }
79 success = table.remove (results, 1)
82 for _, x in ipairs(results) do
83 pp.print(x, {line_max=LINE_WIDTH, metalua_tag=true})
87 print "Evaluation error:"
92 print "Can't compile into bytecode:"
97 -- If lx has been read entirely, try to read
98 -- another line before failing.
99 if not reached_eof(lx, ast) then
100 print "Can't compile source into AST:"