require 'metalua.compiler'
require 'metalua.clopts'
-
-do
- local mfast = os.getenv 'LUA_NOSPRINGS'
- local has_springs = mfast~='yes' and mfast~='true' and pcall(require, 'springs')
- if has_springs then
- function spring_pcall(...)
- local ring = springs.new()
- ring:dostring (INIT_COMPILATION_RING)
- st, ast = ring:pcall(...)
- ring:close()
- return st, ast
- end
- else
- function spring_pcall(f, ...)
- if type(f)=='string' then f = loadstring("return "..f)() end
- return pcall(f, ...)
- end
- end
-end
+require 'serialize'
AST_COMPILE_ERROR_NUMBER = -1
RUNTIME_ERROR_NUMBER = -3
-{ extension 'match' }
+function spring_pcall (f, arg, name)
+ -- FIXME: won't work if `arg' or `name' contain a backslash-escaped quot char.
+ arg = arg :gsub ("'", "\\'")
+ name = name :gsub ("'", "\\'")
+ local pattern = [[lua -l metalua.mlc -l serialize -e "print(serialize(%s('%s', '%s')))"]]
+ local cmd = string.format (pattern, f, arg, name)
+ --print ("Running the following process: " .. cmd)
+ local fd = io.popen (cmd)
+ local data = fd:read '*a'
+ fd:close()
+ --print (data)
+ local ast_builder, msg = lua_loadstring(data)
+ if not ast_builder then error ("can't compile data: "..msg) end
+ local ast = ast_builder()
+ return true, ast
+end
+
local chunks = { }
local runargs = { }
-local acc_chunk = |kind| function (arg)
- table.insert (chunks, { tag=kind, arg })
-end
+local acc_chunk = |kind| |arg| table.insert (chunks, { tag=kind, arg })
parser = clopts {
-- Chunk loading
forbids it.
]]}
-
-INIT_COMPILATION_RING = [[require 'metalua.compiler']]
-
local function main (...)
local cfg = parser(...)
end
end
if not st then
- printf ("Cannot compile %s: %s", table.tostring(x), ast)
+ printf ("Cannot compile %s: %s", table.tostring(x), ast or "no msg")
os.exit (AST_COMPILE_ERROR_NUMBER)
end
ast.origin = x
verb_print "Starting REPL loop"
require 'metalua.metaloop'
metaloop.run()
--- print ("*":rep(70))
--- print "*** !!! Interactive loop not implemented !!!"
--- print ("*":rep(70))
end
verb_print "Done"
----------------------------------------------------------------------
--- Execute a metalua module sources compilation in a separate ring.
+-- Execute a metalua module sources compilation in a separate process
----------------------------------------------------------------------
local function spring_load(filename)
- local env_fast = os.getenv 'LUA_NOSPRINGS'
- local try_springs = env_fast=='yes' or env_fast=='true'
- local has_springs = try_springs and pcall(require, 'springs')
- if has_springs then
- local r = springs.new()
- r:dostring [[require 'metalua.compiler']]
- local f = r:call('mlc.function_of_luafile', filename)
- r:close()
- return f
- else
- --print "LUA_NOSPRINGS mode"
- return mlc.function_of_luafile(filename)
- end
+ local pattern =
+ [[lua -l metalua.mlc -l -e "print(mlc.luacstring_of_luafile('%s', '%s'))"]]
+ local cmd = string.format (pattern, f, filename, filename)
+ print ("running command: ``" .. cmd .. "''")
+ local fd = io.popen (cmd)
+ local bytecode = fd:read '*a'
+ fd:close()
+ return string.undump (bytecode)
end
----------------------------------------------------------------------