]> Joshua Wise's Git repositories - jwcc.git/blob - lib/deparser.lua
Initial commit
[jwcc.git] / lib / deparser.lua
1 function errprint(s)
2         io.stderr:write(s.."\n")
3 end
4
5 function deparse(parsetree)
6         function printtable(spaces, tbl)
7                 if table.getn(tbl) == 1 and tbl[1] then
8                         printtable(spaces, tbl[1])
9                         return
10                 end
11                 if tbl.type == "function" then
12                         errprint(spaces.."function: "..tbl.name)
13                         errprint(spaces.."   returns: "..tbl.returntype)
14                         errprint(spaces.."   args: ")
15                         for k,v in pairs(tbl.args) do
16                                 printtable(spaces.."        ",v)
17                         end
18                         errprint(spaces.."   body: ")
19                         printtable(spaces.."         ", tbl.body)
20                 elseif tbl.type == "stmtlist" then
21                         errprint(spaces.."stmtlist:")
22                         for k,v in pairs(tbl.body) do
23                                 errprint(spaces.."   "..k..":")
24                                 printtable(spaces.."      ", v)
25                         end
26                 elseif tbl.type == "variable" then
27                         errprint(spaces.."variable: "..tbl.vtype.." "..tbl.name.." = "..tostring(tbl.initializer))
28                 elseif tbl.type == "expression" then
29                         errprint(spaces.."expression")
30                         printtable(spaces.."   ",tbl.value)
31                 elseif tbl.type == "operator" then
32                         errprint(spaces.."operator \""..tbl.value.."\"")
33                         errprint(spaces.."   left: ")
34                         printtable(spaces.."      ", tbl.left)
35                         errprint(spaces.."   right: ")
36                         printtable(spaces.."      ", tbl.right)
37                 elseif tbl.type == "number" or tbl.type == "identifier" then
38                         errprint(spaces..tbl.type.." "..tbl.value)
39                 elseif tbl.type == "while" then
40                         errprint(spaces.."while")
41                         errprint(spaces.."   expression:")
42                         printtable(spaces.."      ", tbl.expression)
43                         errprint(spaces.."   chunk:")
44                         printtable(spaces.."      ", tbl.chunk)
45                 else
46                         ignored = { ["parent"] = true }
47                         for k,v in pairs(tbl) do
48                                 if type(v) ~= "table" then
49                                         errprint(spaces..k..": "..v)
50                                 end
51                         end
52                         for k,v in pairs(tbl) do
53                                 if type(v) == "table" and not ignored[k] then
54                                         if k == "v" then
55                                                 printtable(spaces, v)
56                                         else
57                                                 errprint(spaces..k..":")
58                                                 printtable(spaces.."   ",v)
59                                         end
60                                 end
61                         end
62                 end
63         end
64         printtable("", parsetree)
65 end
This page took 0.026299 seconds and 4 git commands to generate.