]> git.lizzy.rs Git - metalua.git/commitdiff
fixed metabug mode
authorFabien Fleutot <fabien@MacFabien.home>
Sun, 22 Feb 2009 17:46:21 +0000 (18:46 +0100)
committerFabien Fleutot <fabien@MacFabien.home>
Sun, 22 Feb 2009 17:46:21 +0000 (18:46 +0100)
src/compiler/metalua.mlua
src/compiler/mlc.mlua
src/lib/metalua/mlc_xcall.lua

index 2b2f08459287074b4424cecb50e270254cb04b76..b3bdf0325c501795df37ca7709fd50fa91a2fb59 100644 (file)
@@ -131,6 +131,7 @@ local function main (...)
 
    -------------------------------------------------------------------
    -- Get ASTs from sources
+   mlc.metabugs = cfg.metabugs
    local last_file
    for x in values(chunks) do
       verb_print("Compiling %s", table.tostring(x))
@@ -185,6 +186,8 @@ local function main (...)
       end
    end
 
+   -- FIXME: canonize/check AST
+
    -------------------------------------------------------------------
    -- Insert runtime loader
    if cfg['no-runtime'] then
@@ -193,18 +196,17 @@ local function main (...)
       table.insert(code, 1, +{require'metalua.runtime'})
    end
 
-   -- FIXME: check for failures
-   mlc.metabugs = cfg.metabugs
    local bytecode = mlc.luacstring_of_ast (code)
    code = nil
 
    -------------------------------------------------------------------
    -- Insert #!... command
    if cfg.sharpbang then
-      verb_print ("Adding sharp-bang directive %q", cfg.sharpbang)
-      if not cfg.sharpbang:strmatch'^#!' then cfg.sharpbang='#!'..cfg.sharpbang end
-      if not cfg.sharpbang:strmatch'\n$' then cfg.sharpbang=cfg.sharpbang..'\n' end
-      bytecode = cfg.sharpbang..bytecode
+      local shbang = cfg.sharpbang
+      verb_print ("Adding sharp-bang directive %q", shbang)
+      if not shbang :strmatch'^#!' then shbang = '#!' .. shbang end
+      if not shbang :strmatch'\n$' then shbang = shbang .. '\n' end
+      bytecode = shbang .. bytecode
    end
 
    -------------------------------------------------------------------
index 6f82ac9a8028b24a6262ae41ee1d3cbe354f8567..e6e7f5d8189b57de1d1cd48bdb8dbec3a6f37447 100644 (file)
@@ -90,9 +90,9 @@ function mlc.convert (x, src_fmt, dst_fmt, name)
    local status -- status = compilation success
    local lx=x
    if mlc.metabugs
-   -- If SHOW_METABUGS is true, errors should be attributed to a parser bug.
+   -- If metabugs is true, errors should be attributed to a parser bug.
    then status, x = true, mlp.chunk (lx)
-   -- If SHOW_METABUGS is false, errors should be attributed to an invalid entry.
+   -- If metabugs is false, errors should be attributed to an invalid entry.
    else status, x = pcall (mlp.chunk, lx) end
    -- FIXME: this test seems wrong ??? Or is it the message?
    if status and lx:peek().tag ~= "Eof"
index 359bedc76a264528df7bb131adae77ac0567c99e..8af05f3917aac8587681642c75fdb697d7eb9fd9 100644 (file)
@@ -1,33 +1,61 @@
--- lua -l mlc_xcall -e 'luafile_to_astfile ("/tmp/tmp12345.lua", "/tmp/tmp54321.ast")'
--- lua -l mlc_xcall -e 'lua_to_astfile ("/tmp/tmp54321.ast")'
+--------------------------------------------------------------------------------
+-- Execute an `mlc.ast_of_*()' in a separate lua process.
+-- Communication between processes goes through temporary files,
+-- for the sake of portability.
+--------------------------------------------------------------------------------
 
 mlc_xcall = { }
 
+--------------------------------------------------------------------------------
+-- Number of lines to remove at the end of a traceback, should it be
+-- dumped due to a compilation error in metabugs mode.
+--------------------------------------------------------------------------------
+local STACK_LINES_TO_CUT = 7
 
+--------------------------------------------------------------------------------
+-- (Not intended to be called directly by users)
+--
 -- This is the back-end function, called in a separate lua process
 -- by `mlc_xcall.client_*()' through `os.execute()'.
 --  * inputs:
 --     * the name of a lua source file to compile in a separate process
 --     * the name of a writable file where the resulting ast is dumped
 --       with `serialize()'.
+--     * metabugs: if true and an error occurs during compilation,
+--       the compiler's stacktrace is printed, allowing meta-programs
+--       debugging.
 --  * results:
 --     * an exit status of 0 or -1, depending on whethet compilation
 --       succeeded;
 --     * the ast file filled will either the serialized ast, or the
 --       error message.
-function mlc_xcall.server (luafilename, astfilename)
+--------------------------------------------------------------------------------
+function mlc_xcall.server (luafilename, astfilename, metabugs)
 
    -- We don't want these to be loaded when people only do client-side business
    require 'metalua.compiler'
    require 'serialize'
 
+   mlc.metabugs = metabugs
+
    -- compile the content of luafile name in an AST, serialized in astfilename
    --local status, ast = pcall (mlc.luafile_to_ast, luafilename)
    local status, ast
    local function compile() return mlc.luafile_to_ast (luafilename) end
-   if mlc.metabugs or true then 
+   if mlc.metabugs then 
       print 'mlc_xcall.server/metabugs'
-      status, ast = xpcall (compile, debug.traceback)
+      --status, ast = xpcall (compile, debug.traceback)
+      --status, ast = xpcall (compile, debug.traceback)
+      local function tb(msg)
+         local r = debug.traceback(msg)
+
+         -- Cut superfluous end lines
+         local line_re = '\n[^\n]*'
+         local re =  "^(.-)" .. (line_re) :rep (STACK_LINES_TO_CUT) .. "$"
+         return r :strmatch (re) or r
+      end
+      --status, ast = xpcall (compile, debug.traceback)
+      status, ast = xpcall (compile, tb)
    else status, ast = pcall (compile) end
    local out = io.open (astfilename, 'w')
    if status then -- success
@@ -41,19 +69,21 @@ function mlc_xcall.server (luafilename, astfilename)
    end      
 end
 
+--------------------------------------------------------------------------------
 -- Compile the file whose name is passed as argument, in a separate process,
 -- communicating through a temporary file.
 -- returns:
 --  * true or false, indicating whether the compilation succeeded
 --  * the ast, or the error message.
+--------------------------------------------------------------------------------
 function mlc_xcall.client_file (luafile)
 
    -- printf("\n\nmlc_xcall.client_file(%q)\n\n", luafile)
 
    local tmpfilename = os.tmpname()
    local cmd = string.format (
-      [=[lua -l metalua.mlc_xcall -e "mlc_xcall.server([[%s]], [[%s]])"]=], 
-      luafile, tmpfilename)
+      [=[lua -l metalua.mlc_xcall -e "mlc_xcall.server([[%s]], [[%s]], %s)"]=], 
+      luafile, tmpfilename, mlc.metabugs and "true" or "false")
 
    -- printf("os.execute [[%s]]\n\n", cmd)
 
@@ -70,9 +100,11 @@ function mlc_xcall.client_file (luafile)
    return status, result
 end
 
+--------------------------------------------------------------------------------
 -- Compile a source string into an ast, by dumping it in a tmp
 -- file then calling `mlc_xcall.client_file()'.
 -- returns: the same as `mlc_xcall.client_file()'.
+--------------------------------------------------------------------------------
 function mlc_xcall.client_literal (luasrc)
    local srcfilename = os.tmpname()
    local srcfile, msg = io.open (srcfilename, 'w')