]> git.lizzy.rs Git - metalua.git/commitdiff
Merge branch 'ag-fixes' of git://github.com/agladysh/metalua
authorFabien Fleutot <metalua@gmail.com>
Mon, 2 Feb 2009 20:29:08 +0000 (21:29 +0100)
committerFabien Fleutot <metalua@gmail.com>
Mon, 2 Feb 2009 20:29:08 +0000 (21:29 +0100)
src/make-install.sh [deleted file]
src/make.sh
src/samples/synth.mlua
src/samples/weaver.mlua

diff --git a/src/make-install.sh b/src/make-install.sh
deleted file mode 100755 (executable)
index 3a03a13..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-echo "This script is currently empty. Run ./make.sh first, then a correct make-install.sh will be generated."
-exit -1
index 5cb78c524855d06073f59a8416a33f336af42b8a..f7f87982e759e9fb422e3ccadf2da7c0bb4851d4 100755 (executable)
@@ -1,3 +1,5 @@
+#! /bin/sh
+
 # --- BEGINNING OF USER-EDITABLE PART ---
 
 # Metalua sources
@@ -5,15 +7,29 @@ BASE=${PWD}
 
 # Temporary building location.
 # Upon installation, everything will be moved to ${INSTALL_LIB} and ${INSTALL_BIN}
-BUILD=/tmp/metalua-build
-BUILD_BIN=${BUILD}/bin
-BUILD_LIB=${BUILD}/lib
+
+if [ -z "${BUILD}" ]; then
+  BUILD=/tmp/metalua-build
+fi
+
+if [ -z "${BUILD_BIN}" ]; then
+  BUILD_BIN=${BUILD}/bin
+fi
+
+if [ -z "${BUILD_LIB}" ]; then
+  BUILD_LIB=${BUILD}/lib
+fi
 
 # Where to place the final results
 # INSTALL_BIN=/usr/local/bin
 # INSTALL_LIB=/usr/local/lib/lua/5.1
-INSTALL_BIN=~/local/bin
-INSTALL_LIB=~/local/lib/lua
+if [ -z "${INSTALL_BIN}" ]; then
+  INSTALL_BIN=~/local/bin
+fi
+
+if [ -z "${INSTALL_LIB}" ]; then
+  INSTALL_LIB=~/local/lib/lua
+fi
 
 # Where to find Lua executables.
 # On many Debian-based systems, those can be installed with "sudo apt-get install lua5.1"
@@ -96,4 +112,4 @@ chmod a+x make-install.sh
 
 echo
 echo "Build completed, proceed to installation with './make-install.sh' or 'sudo ./make-install.sh'"
-echo
\ No newline at end of file
+echo
index e572b4bcf24e8b16055f4bc860144986a289f581..01090eee14161821917e170052861487b1b60377 100644 (file)
@@ -553,8 +553,8 @@ end
 -- Read a file, get its AST, use synth to regenerate sources
 -- from that AST
 --------------------------------------------------------------------------------
-require 'mlc'
+require 'metalua.compiler'
 local filename = (arg[2] or arg[1]) or arg[0]
 local ast = mlc.luafile_to_ast (filename)
 
-print(synth.run(ast)) 
\ No newline at end of file
+print(synth.run(ast))
index 202d00326311e335c4745d394695256f236f1e5d..f428a419d857e8d7141459431ea750daae6005f0 100644 (file)
@@ -22,11 +22,25 @@ function weave_ast (src, ast, name)
    -- the `Local{ } node, although it's not directly included in it.
    -------------------------------------------------------------------
    function node.down(ast, parent)
+      ----------------------------------------------------
+      -- `Do{ } blocks are processed twice:
+      --  * once as a statement
+      --  * once as a block, child of itself
+      -- This prevents them from becoming their own child.
+      ----------------------------------------------------
+      if ast==parent then return end
+
       if not ast.lineinfo then 
          weaveable [ast] = false, false
          if parent then weaveable [parent] = false end
       else
          weaveable [ast] = true
+         
+         -- normalize lineinfo
+         -- TODO: FIXME
+         if ast.lineinfo.first[3] > ast.lineinfo.last[3] then
+           ast.lineinfo.first, ast.lineinfo.last = ast.lineinfo.last, ast.lineinfo.first
+         end
       end
       ast_children [ast] = { }
       ast_parent [ast] = parent
@@ -45,8 +59,14 @@ function weave_ast (src, ast, name)
       local _acc = { }
       local function acc(x) table.insert (_acc, x) end
       
+      if not next(ast) then -- shadow node, remove from ast_children
+         local x = ast_children[ast_parent[ast]]
+         for i,a in ipairs (x) do if a==ast then table.remove (x, i); break end end
+         return "" -- no need to continue, we know that the node is empty!
+      end
+      
       -- ast Can't be weaved normally, try something else --
-      local function synthetize (ast)         
+      local function synthetize (ast)
          acc "-{expr: "
          acc (table.tostring (ast, 'nohash', 80, 8))
          acc " }"
@@ -54,15 +74,19 @@ function weave_ast (src, ast, name)
 
       -- regular weaving of chidren in the parent's sources --
       local function weave (ast)
+         -- sort children in appearence order
+         local comp = |a,b| a.lineinfo.first[3] < b.lineinfo.first[3]
+         table.sort (ast_children [ast], comp)
          local li = ast.lineinfo
          if not li then return synthetize (ast) end
          local a, d = li.first[3], li.last[3]
          for child in ivalues (ast_children [ast]) do
             local li = child.lineinfo
             local b, c = li.first[3], li.last[3]
-            acc (src:sub (a, b-1))
+            acc (src:sub (a, b - 1))
             acc (translation [child])
-            a = c+1
+            a = c + 1
          end
          acc (src:sub (a, d))
       end
@@ -82,10 +106,15 @@ end
 
 -- Get the source. If none is given, use itself as an example. --
 local filename = arg[2] or arg[1] or arg[0]
-local f = io.open (filename, 'r')
+local f = assert (io.open (filename, 'r'))
 local src = f:read '*a'
 f:close()
 
 local ast = mlc.luastring_to_ast (src, name)
-
-print (weave_ast (src, ast))
+if not next(ast) then
+   io.write (src) -- Empty ast, probably empty file, or comments only
+else
+   local before = src:sub (1, ast.lineinfo.first[3]-1)
+   local after  = src:sub (ast.lineinfo.last[3]+1, -1)
+   io.write (before .. weave_ast (src, ast) .. after)
+end