function errprint(s) io.stderr:write(s.."\n") end function deparse(parsetree) function printtable(spaces, tbl) if table.getn(tbl) == 1 and tbl[1] then printtable(spaces, tbl[1]) return end if tbl.type == "function" then errprint(spaces.."function: "..tbl.name) errprint(spaces.." returns: "..tbl.returntype) errprint(spaces.." args: ") for k,v in pairs(tbl.args) do printtable(spaces.." ",v) end errprint(spaces.." body: ") printtable(spaces.." ", tbl.body) elseif tbl.type == "stmtlist" then errprint(spaces.."stmtlist:") for k,v in pairs(tbl.body) do errprint(spaces.." "..k..":") printtable(spaces.." ", v) end elseif tbl.type == "variable" then errprint(spaces.."variable: "..tbl.vtype.." "..tbl.name.." = "..tostring(tbl.initializer)) elseif tbl.type == "expression" then errprint(spaces.."expression") printtable(spaces.." ",tbl.value) elseif tbl.type == "operator" then errprint(spaces.."operator \""..tbl.value.."\"") errprint(spaces.." left: ") printtable(spaces.." ", tbl.left) errprint(spaces.." right: ") printtable(spaces.." ", tbl.right) elseif tbl.type == "number" or tbl.type == "identifier" then errprint(spaces..tbl.type.." "..tbl.value) elseif tbl.type == "while" then errprint(spaces.."while") errprint(spaces.." expression:") printtable(spaces.." ", tbl.expression) errprint(spaces.." chunk:") printtable(spaces.." ", tbl.chunk) else ignored = { ["parent"] = true } for k,v in pairs(tbl) do if type(v) ~= "table" then errprint(spaces..k..": "..v) end end for k,v in pairs(tbl) do if type(v) == "table" and not ignored[k] then if k == "v" then printtable(spaces, v) else errprint(spaces..k..":") printtable(spaces.." ",v) end end end end end printtable("", parsetree) end