]> git.lizzy.rs Git - metalua.git/blob - src/samples/weaver.mlua
fixes in the weaver sample
[metalua.git] / src / samples / weaver.mlua
1 require 'metalua.mlc'
2 require 'metalua.walk'
3
4 function weave_ast (src, ast, name)
5
6    -------------------------------------------------------------------
7    -- translation: associate an AST node to its recomposed source
8    -- ast_children: associate an AST node to the list of its children
9    -- ast_parent: associate an AST node to the list of its parent
10    -- weavable: whether an AST node supports weaving of its children
11    -- node: common walker config for exprs, stats & blocks
12    -------------------------------------------------------------------
13    local translation, ast_children, ast_parent, weaveable, node =
14       { }, { }, { }, { }, { }
15
16    -------------------------------------------------------------------
17    -- Build up the parent/children relationships. This is not the same
18    -- as inclusion between tables: the relation we're building only
19    -- relates blocks, expressions and statements; in the AST, some
20    -- tables don't represent any of these node kinds.
21    -- For instance in `Local{ { `Id "x" }, { } }, `Id"x" is a child of
22    -- the `Local{ } node, although it's not directly included in it.
23    -------------------------------------------------------------------
24    function node.down(ast, parent)
25       if not ast.lineinfo then 
26          weaveable [ast] = false, false
27          if parent then weaveable [parent] = false end
28       else
29          weaveable [ast] = true
30       end
31       ast_children [ast] = { }
32       ast_parent [ast] = parent
33       if parent then table.insert (ast_children [parent], ast) end
34    end
35
36    -------------------------------------------------------------------
37    -- Visit up, from leaves to upper-level nodes, and weave leaves
38    -- back into the text of their parent node, recursively.  Since the
39    -- visitor is imperative, we can't easily make it return a value
40    -- (the resulting recomposed source, here). Therefore we
41    -- imperatively store results in the association table
42    -- `translation'.
43    -------------------------------------------------------------------
44    function node.up(ast)
45       local _acc = { }
46       local function acc(x) table.insert (_acc, x) end
47       
48       -- ast Can't be weaved normally, try something else --
49       local function synthetize (ast)         
50          acc "-{expr: "
51          acc (table.tostring (ast, 'nohash', 80, 8))
52          acc " }"
53       end
54
55       -- regular weaving of chidren in the parent's sources --
56       local function weave (ast)
57          local li = ast.lineinfo
58          if not li then return synthetize (ast) end
59          local a, d = li.first[3], li.last[3]
60          for child in ivalues (ast_children [ast]) do
61             local li = child.lineinfo
62             local b, c = li.first[3], li.last[3]
63             acc (src:sub (a, b-1))
64             acc (translation [child])
65             a = c+1
66          end
67          acc (src:sub (a, d))
68       end
69
70       -- compute the translation from the children's ones --
71       if not translation [ast] then
72          if weaveable [ast] then weave (ast) else synthetize (ast) end
73          translation [ast] = table.concat (_acc)
74       end
75    end
76
77    local cfg = { expr=node; stat=node; block=node }
78    walk.block (cfg, ast)
79
80    return translation [ast]
81 end
82
83 -- Get the source. If none is given, use itself as an example. --
84 local filename = arg[2] or arg[1] or arg[0]
85 local f = io.open (filename, 'r')
86 local src = f:read '*a'
87 f:close()
88
89 local ast = mlc.luastring_to_ast (src, name)
90
91 print (weave_ast (src, ast))