+#! /bin/sh
+
# --- BEGINNING OF USER-EDITABLE PART ---
# Metalua sources
# 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"
echo
echo "Build completed, proceed to installation with './make-install.sh' or 'sudo ./make-install.sh'"
-echo
\ No newline at end of file
+echo
-- 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
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 " }"
-- 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
-- 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