1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
local utils = require('utils')
local repr, stringify, min, max, equivalent, set, is_list, sum
repr, stringify, min, max, equivalent, set, is_list, sum = utils.repr, utils.stringify, utils.min, utils.max, utils.equivalent, utils.set, utils.is_list, utils.sum
local immutable = require('immutable')
local insert, remove, concat
do
local _obj_0 = table
insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
end
local Lua, Nomsu, Source
do
local _obj_0 = require("code_obj")
Lua, Nomsu, Source = _obj_0.Lua, _obj_0.Nomsu, _obj_0.Source
end
local MAX_LINE = 80
local Types = { }
Types.is_node = function(n)
return type(n) == 'userdata' and getmetatable(n) and Types[n.type] == getmetatable(n)
end
local Tree
Tree = function(name, fields, methods)
methods = methods or { }
local is_multi = true
for _index_0 = 1, #fields do
local f = fields[_index_0]
is_multi = is_multi and (f ~= "value")
end
do
methods.type = name
methods.name = name
methods.__new = methods.__new or function(self, source, ...)
assert(source)
if type(source) == 'string' then
source = Source:from_string(source)
end
return source, ...
end
methods.is_multi = is_multi
methods.map = function(self, fn)
if type(fn) == 'table' then
if not (next(fn)) then
return self
end
local _replacements = fn
fn = function(k)
return _replacements[k]
end
end
return self:_map(fn)
end
if is_multi then
methods.__tostring = function(self)
return tostring(self.name) .. "(" .. tostring(table.concat((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #self do
local v = self[_index_0]
_accum_0[_len_0] = repr(v)
_len_0 = _len_0 + 1
end
return _accum_0
end)(), ', ')) .. ")"
end
methods._map = function(self, fn)
do
local ret = fn(self)
if ret then
return ret
end
end
local new_vals
do
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #self do
local v = self[_index_0]
_accum_0[_len_0] = v._map and v:_map(fn) or v
_len_0 = _len_0 + 1
end
new_vals = _accum_0
end
local ret = getmetatable(self)(self.source, unpack(new_vals))
return ret
end
else
methods.__tostring = function(self)
return tostring(self.name) .. "(" .. tostring(repr(self.value)) .. ")"
end
methods._map = function(self, fn)
return fn(self) or self
end
end
end
Types[name] = immutable(fields, methods)
end
Tree("Block", {
"source"
})
Tree("EscapedNomsu", {
"source"
})
Tree("Text", {
"source"
})
Tree("List", {
"source"
})
Tree("Dict", {
"source"
})
Tree("DictEntry", {
"source"
})
Tree("IndexChain", {
"source"
})
Tree("Number", {
"source",
"value"
})
Tree("Var", {
"source",
"value"
})
Tree("Action", {
"source",
"stub"
}, {
__new = function(self, source, ...)
assert(source)
if type(source) == 'string' then
source = Source:from_string(source)
end
local stub_bits = { }
for i = 1, select("#", ...) do
local a = select(i, ...)
stub_bits[i] = type(a) == 'string' and a or "%"
end
local stub = concat(stub_bits, " ")
return source, stub, ...
end,
get_spec = function(self)
return concat((function()
local _accum_0 = { }
local _len_0 = 1
for _index_0 = 1, #self do
local a = self[_index_0]
_accum_0[_len_0] = type(a) == "string" and a or "%" .. tostring(a.value)
_len_0 = _len_0 + 1
end
return _accum_0
end)(), " ")
end
})
return Types
|