rufus-lua-moon 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,14 @@
1
- module("moonscript.types", package.seeall)
2
1
  local util = require("moonscript.util")
3
2
  local data = require("moonscript.data")
4
3
  local insert = table.insert
5
- manual_return = data.Set({
4
+ local unpack = util.unpack
5
+ local manual_return = data.Set({
6
6
  "foreach",
7
7
  "for",
8
8
  "while",
9
9
  "return"
10
10
  })
11
- cascading = data.Set({
11
+ local cascading = data.Set({
12
12
  "if",
13
13
  "unless",
14
14
  "with",
@@ -16,13 +16,7 @@ cascading = data.Set({
16
16
  "class",
17
17
  "do"
18
18
  })
19
- is_value = function(stm)
20
- local compile, transform = moonscript.compile, moonscript.transform
21
- return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
22
- end
23
- comprehension_has_value = function(comp)
24
- return is_value(comp[2])
25
- end
19
+ local ntype
26
20
  ntype = function(node)
27
21
  local _exp_0 = type(node)
28
22
  if "nil" == _exp_0 then
@@ -33,9 +27,41 @@ ntype = function(node)
33
27
  return "value"
34
28
  end
35
29
  end
30
+ local mtype
31
+ do
32
+ local moon_type = util.moon.type
33
+ mtype = function(val)
34
+ local mt = getmetatable(val)
35
+ if mt and mt.smart_node then
36
+ return "table"
37
+ end
38
+ return moon_type(val)
39
+ end
40
+ end
41
+ local has_value
42
+ has_value = function(node)
43
+ if ntype(node) == "chain" then
44
+ local ctype = ntype(node[#node])
45
+ return ctype ~= "call" and ctype ~= "colon"
46
+ else
47
+ return true
48
+ end
49
+ end
50
+ local is_value
51
+ is_value = function(stm)
52
+ local compile = require("moonscript.compile")
53
+ local transform = require("moonscript.transform")
54
+ return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
55
+ end
56
+ local comprehension_has_value
57
+ comprehension_has_value = function(comp)
58
+ return is_value(comp[2])
59
+ end
60
+ local value_is_singular
36
61
  value_is_singular = function(node)
37
62
  return type(node) ~= "table" or node[1] ~= "exp" or #node == 2
38
63
  end
64
+ local is_slice
39
65
  is_slice = function(node)
40
66
  return ntype(node) == "chain" and ntype(node[#node]) == "slice"
41
67
  end
@@ -175,7 +201,7 @@ make_builder = function(name)
175
201
  return node
176
202
  end
177
203
  end
178
- build = nil
204
+ local build = nil
179
205
  build = setmetatable({
180
206
  group = function(body)
181
207
  if body == nil then
@@ -246,25 +272,43 @@ build = setmetatable({
246
272
  return rawget(self, name)
247
273
  end
248
274
  })
249
- smart_node = function(node)
250
- local index = key_table[ntype(node)]
251
- if not index then
252
- return node
253
- end
254
- return setmetatable(node, {
255
- __index = function(node, key)
256
- if index[key] then
257
- return rawget(node, index[key])
258
- elseif type(key) == "string" then
259
- return error("unknown key: `" .. key .. "` on node type: `" .. ntype(node) .. "`")
275
+ local smart_node_mt = setmetatable({ }, {
276
+ __index = function(self, node_type)
277
+ local index = key_table[node_type]
278
+ local mt = {
279
+ smart_node = true,
280
+ __index = function(node, key)
281
+ if index[key] then
282
+ return rawget(node, index[key])
283
+ elseif type(key) == "string" then
284
+ return error("unknown key: `" .. key .. "` on node type: `" .. ntype(node) .. "`")
285
+ end
286
+ end,
287
+ __newindex = function(node, key, value)
288
+ if index[key] then
289
+ key = index[key]
290
+ end
291
+ return rawset(node, key, value)
260
292
  end
261
- end,
262
- __newindex = function(node, key, value)
263
- if index[key] then
264
- key = index[key]
265
- end
266
- return rawset(node, key, value)
267
- end
268
- })
293
+ }
294
+ self[node_type] = mt
295
+ return mt
296
+ end
297
+ })
298
+ local smart_node
299
+ smart_node = function(node)
300
+ return setmetatable(node, smart_node_mt[ntype(node)])
269
301
  end
270
- return nil
302
+ return {
303
+ ntype = ntype,
304
+ smart_node = smart_node,
305
+ build = build,
306
+ is_value = is_value,
307
+ is_slice = is_slice,
308
+ manual_return = manual_return,
309
+ cascading = cascading,
310
+ value_is_singular = value_is_singular,
311
+ comprehension_has_value = comprehension_has_value,
312
+ has_value = has_value,
313
+ mtype = mtype
314
+ }
@@ -1,9 +1,23 @@
1
- module("moonscript.util", package.seeall)
2
1
  local concat = table.concat
3
- moon = {
2
+ local unpack = unpack or table.unpack
3
+ local type = type
4
+ local moon = {
4
5
  is_object = function(value)
5
6
  return type(value) == "table" and value.__class
6
7
  end,
8
+ is_a = function(thing, t)
9
+ if not (type(thing) == "table") then
10
+ return false
11
+ end
12
+ local cls = thing.__class
13
+ while cls do
14
+ if cls == t then
15
+ return true
16
+ end
17
+ cls = cls.__parent
18
+ end
19
+ return false
20
+ end,
7
21
  type = function(value)
8
22
  local base_type = type(value)
9
23
  if base_type == "table" then
@@ -15,6 +29,7 @@ moon = {
15
29
  return base_type
16
30
  end
17
31
  }
32
+ local pos_to_line
18
33
  pos_to_line = function(str, pos)
19
34
  local line = 1
20
35
  for _ in str:sub(1, pos):gmatch("\n") do
@@ -22,14 +37,11 @@ pos_to_line = function(str, pos)
22
37
  end
23
38
  return line
24
39
  end
25
- get_closest_line = function(str, line_num)
26
- local line = get_line(str, line_num)
27
- if (not line or trim(line) == "") and line_num > 1 then
28
- return get_closest_line(str, line_num - 1)
29
- else
30
- return line, line_num
31
- end
40
+ local trim
41
+ trim = function(str)
42
+ return str:match("^%s*(.-)%s*$")
32
43
  end
44
+ local get_line
33
45
  get_line = function(str, line_num)
34
46
  for line in str:gmatch("([^\n]*)\n?") do
35
47
  if line_num == 1 then
@@ -38,6 +50,16 @@ get_line = function(str, line_num)
38
50
  line_num = line_num - 1
39
51
  end
40
52
  end
53
+ local get_closest_line
54
+ get_closest_line = function(str, line_num)
55
+ local line = get_line(str, line_num)
56
+ if (not line or trim(line) == "") and line_num > 1 then
57
+ return get_closest_line(str, line_num - 1)
58
+ else
59
+ return line, line_num
60
+ end
61
+ end
62
+ local reversed
41
63
  reversed = function(seq)
42
64
  return coroutine.wrap(function()
43
65
  for i = #seq, 1, -1 do
@@ -45,9 +67,7 @@ reversed = function(seq)
45
67
  end
46
68
  end)
47
69
  end
48
- trim = function(str)
49
- return str:match("^%s*(.-)%s*$")
50
- end
70
+ local split
51
71
  split = function(str, delim)
52
72
  if str == "" then
53
73
  return { }
@@ -55,14 +75,15 @@ split = function(str, delim)
55
75
  str = str .. delim
56
76
  return (function()
57
77
  local _accum_0 = { }
58
- local _len_0 = 0
78
+ local _len_0 = 1
59
79
  for m in str:gmatch("(.-)" .. delim) do
60
- _len_0 = _len_0 + 1
61
80
  _accum_0[_len_0] = m
81
+ _len_0 = _len_0 + 1
62
82
  end
63
83
  return _accum_0
64
84
  end)()
65
85
  end
86
+ local dump
66
87
  dump = function(what)
67
88
  local seen = { }
68
89
  local _dump
@@ -81,13 +102,10 @@ dump = function(what)
81
102
  depth = depth + 1
82
103
  local lines = (function()
83
104
  local _accum_0 = { }
84
- local _len_0 = 0
105
+ local _len_0 = 1
85
106
  for k, v in pairs(what) do
86
- local _value_0 = (" "):rep(depth * 4) .. "[" .. tostring(k) .. "] = " .. _dump(v, depth)
87
- if _value_0 ~= nil then
88
- _len_0 = _len_0 + 1
89
- _accum_0[_len_0] = _value_0
90
- end
107
+ _accum_0[_len_0] = (" "):rep(depth * 4) .. "[" .. tostring(k) .. "] = " .. _dump(v, depth)
108
+ _len_0 = _len_0 + 1
91
109
  end
92
110
  return _accum_0
93
111
  end)()
@@ -99,16 +117,17 @@ dump = function(what)
99
117
  end
100
118
  return _dump(what)
101
119
  end
120
+ local debug_posmap
102
121
  debug_posmap = function(posmap, moon_code, lua_code)
103
122
  local tuples = (function()
104
123
  local _accum_0 = { }
105
- local _len_0 = 0
124
+ local _len_0 = 1
106
125
  for k, v in pairs(posmap) do
107
- _len_0 = _len_0 + 1
108
126
  _accum_0[_len_0] = {
109
127
  k,
110
128
  v
111
129
  }
130
+ _len_0 = _len_0 + 1
112
131
  end
113
132
  return _accum_0
114
133
  end)()
@@ -117,7 +136,7 @@ debug_posmap = function(posmap, moon_code, lua_code)
117
136
  end)
118
137
  local lines = (function()
119
138
  local _accum_0 = { }
120
- local _len_0 = 0
139
+ local _len_0 = 1
121
140
  local _list_0 = tuples
122
141
  for _index_0 = 1, #_list_0 do
123
142
  local pair = _list_0[_index_0]
@@ -126,13 +145,68 @@ debug_posmap = function(posmap, moon_code, lua_code)
126
145
  local lua_text = get_line(lua_code, lua_line)
127
146
  local moon_text = get_closest_line(moon_code, moon_line)
128
147
  local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]"
129
- if _value_0 ~= nil then
130
- _len_0 = _len_0 + 1
131
- _accum_0[_len_0] = _value_0
132
- end
148
+ _accum_0[_len_0] = _value_0
149
+ _len_0 = _len_0 + 1
133
150
  end
134
151
  return _accum_0
135
152
  end)()
136
153
  return concat(lines, "\n")
137
154
  end
138
- return nil
155
+ local setfenv = setfenv or function(fn, env)
156
+ local name
157
+ local i = 1
158
+ while true do
159
+ name = debug.getupvalue(fn, i)
160
+ if not name or name == "_ENV" then
161
+ break
162
+ end
163
+ i = i + 1
164
+ end
165
+ if name then
166
+ debug.upvaluejoin(fn, i, (function()
167
+ return env
168
+ end), 1)
169
+ end
170
+ return fn
171
+ end
172
+ local getfenv = getfenv or function(fn)
173
+ local i = 1
174
+ while true do
175
+ local name, val = debug.getupvalue(fn, i)
176
+ if not (name) then
177
+ break
178
+ end
179
+ if name == "_ENV" then
180
+ return val
181
+ end
182
+ i = i + 1
183
+ end
184
+ return nil
185
+ end
186
+ local get_options
187
+ get_options = function(...)
188
+ local count = select("#", ...)
189
+ local opts = select(count, ...)
190
+ if type(opts) == "table" then
191
+ return opts, unpack({
192
+ ...
193
+ }, nil, count - 1)
194
+ else
195
+ return { }, ...
196
+ end
197
+ end
198
+ return {
199
+ moon = moon,
200
+ pos_to_line = pos_to_line,
201
+ get_closest_line = get_closest_line,
202
+ get_line = get_line,
203
+ reversed = reversed,
204
+ trim = trim,
205
+ split = split,
206
+ dump = dump,
207
+ debug_posmap = debug_posmap,
208
+ getfenv = getfenv,
209
+ setfenv = setfenv,
210
+ get_options = get_options,
211
+ unpack = unpack
212
+ }
@@ -1,7 +1,7 @@
1
1
 
2
2
  module("moonscript.version", package.seeall)
3
3
 
4
- version = "0.2.2"
4
+ version = "0.2.3"
5
5
  function print_version()
6
6
  print("MoonScript version "..version)
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-lua-moon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stas Ukolov
@@ -70,15 +70,17 @@ files:
70
70
  - vendor/lua/moon/all.moon
71
71
  - vendor/lua/moon/init.moon
72
72
  - vendor/lua/moonscript/compile.lua
73
- - vendor/lua/moonscript/compile/format.lua
74
73
  - vendor/lua/moonscript/compile/statement.lua
75
74
  - vendor/lua/moonscript/compile/value.lua
76
75
  - vendor/lua/moonscript/data.lua
77
76
  - vendor/lua/moonscript/dump.lua
78
77
  - vendor/lua/moonscript/errors.lua
79
78
  - vendor/lua/moonscript/init.lua
79
+ - vendor/lua/moonscript/line_tables.lua
80
80
  - vendor/lua/moonscript/parse.lua
81
81
  - vendor/lua/moonscript/transform.lua
82
+ - vendor/lua/moonscript/transform/destructure.lua
83
+ - vendor/lua/moonscript/transform/names.lua
82
84
  - vendor/lua/moonscript/types.lua
83
85
  - vendor/lua/moonscript/util.lua
84
86
  - vendor/lua/moonscript/version.lua
@@ -1,49 +0,0 @@
1
- module("moonscript.compile", package.seeall)
2
- local util = require("moonscript.util")
3
- local data = require("moonscript.data")
4
- local Set
5
- do
6
- local _table_0 = require("moonscript.data")
7
- Set = _table_0.Set
8
- end
9
- local ntype
10
- do
11
- local _table_0 = require("moonscript.types")
12
- ntype = _table_0.ntype
13
- end
14
- local concat, insert = table.concat, table.insert
15
- indent_char = " "
16
- user_error = function(...)
17
- return error({
18
- "user-error",
19
- ...
20
- })
21
- end
22
- moonlib = {
23
- bind = function(tbl, name)
24
- return concat({
25
- "moon.bind(",
26
- tbl,
27
- ".",
28
- name,
29
- ", ",
30
- tbl,
31
- ")"
32
- })
33
- end
34
- }
35
- has_value = function(node)
36
- if ntype(node) == "chain" then
37
- local ctype = ntype(node[#node])
38
- return ctype ~= "call" and ctype ~= "colon"
39
- else
40
- return true
41
- end
42
- end
43
- count_lines = function(str)
44
- local count = 1
45
- for _ in str:gmatch("\n") do
46
- count = count + 1
47
- end
48
- return count
49
- end