aboutsummaryrefslogtreecommitdiff
path: root/.mbs/bin/lua.lua
blob: eed7aa9ff9daddad11c864ce182e649a84c742c3 (plain) (blame)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
if select('#', ...) > 0 then
  print("This is an interactive Lua prompt.")
  print("To run a lua program, just type its name.")
  return
end

local input_colour,  output_colour, text_colour,          keyword_colour, comment_colour, string_colour =
      colours.green, colours.cyan,  term.getTextColour(), colours.yellow, colours.grey,   colours.red
local number_colour,   extra_colour, object_colour =
      colours.magenta, colours.grey, colours.lightGrey

local keywords = {
  ["and"] = keyword_colour, ["break"] = keyword_colour, ["do"] = keyword_colour,
  ["else"] = keyword_colour, ["elseif"] = keyword_colour, ["end"] = keyword_colour,
  ["false"] = object_colour, ["for"] = keyword_colour, ["function"] = keyword_colour,
  ["if"] = keyword_colour, ["in"] = keyword_colour, ["local"] = keyword_colour,
  ["nil"] = object_colour, ["not"] = keyword_colour, ["or"] = keyword_colour,
  ["repeat"] = keyword_colour, ["return"] = keyword_colour, ["then"] = keyword_colour,
  ["true"] = object_colour, ["until"] = keyword_colour, ["while"] = keyword_colour,
}

local tokens = {
  { "^%s+", text_colour },

  -- Identifiers and keywords
  { "^[%a_][%w_]*", function(match) return keywords[match] or text_colour end },

  -- TODO: Exponents + hex, partial strings and comments

  { "^%-%-%[%[.-%]%]", comment_colour },
  { "^%-%-.*",         comment_colour },

  { [[^".-[^\]"]], string_colour }, -- Complete strings
  { [[^"[^"]*"?]], string_colour }, -- Incomplete strings
  { [[^'.-[^\]']], string_colour }, -- Complete strings
  { [[^'[^"]*'?]], string_colour }, -- Incomplete strings
  { "^%[%[.-%]%]", string_colour },

  { "^0x[a-fA-F0-9]*", number_colour }, -- Hexadecimal

  { "^%d+%.%d*e[-+]?%d*", number_colour }, -- 23.4e+2
  { "^%d+%.%d*", number_colour },          -- 23.2
  { "^%d+e[-+]?%d*", number_colour },      -- 23e+2
  { "^%d+", number_colour },               -- 23

  { "^%.%d*e[-+]?%d*", number_colour },    -- .23e+2
  { "^%.%d*", number_colour },             -- .23

  { "^[^%w_]", text_colour }, -- Consume some unknown input
}

--- A basic highlighting function:
local function highlight(line, start)
  local find, type = string.find, type
  for i = 1, #tokens do
    local token = tokens[i]
    local pat_start, pat_finish = find(line, token[1], start)
    if pat_finish then
      if type(token[2]) == "function" then
        return pat_finish, token[2](line:sub(pat_start, pat_finish))
      else
        return pat_finish, token[2]
      end
    end
  end

  return #line, text_colour
end

local function write_with(colour, text)
  term.setTextColour(colour)
  write(text)
end

local function pretty_sort(a, b)
  local ta, tb = type(a), type(b)

  if ta == "string" then return tb ~= "string" or a < b
  elseif tb == "string" then return false
  end

  if ta == "number" then return tb ~= "number" or a < b end
  return false
end

local debug_info = type(debug) == "table" and type(debug.getinfo) == "function" and debug.getinfo
local debug_local = type(debug) == "table" and type(debug.getlocal) == "function" and debug.getlocal
local function pretty_function(fn)
  local info = debug_info and debug_info(fn, "Su")

  -- Include function source position if available
  local name
  if info and info.short_src and info.linedefined and info.linedefined >= 1 then
    name = "function<" .. info.short_src .. ":" .. info.linedefined .. ">"
  else
    name = tostring(fn)
  end

  -- Include arguments if a Lua function and if available. Lua will report "C"
  -- functions as variadic.
  if info and info.what == "Lua" and info.nparams and debug_local then
    local args = {}
    for i = 1, info.nparams do args[i] = debug_local(fn, i) or "?" end
    if info.isvararg then args[#args + 1] = "..." end
    name = name .. "(" .. table.concat(args, ", ") .. ")"
  end

  return name
end

local function pretty_size(obj, tracking, limit)
  local obj_type = type(obj)
  if obj_type == "string" then return #string.format("%q", obj):gsub("\\\n", "\\n")
  elseif obj_type == "function" then return #pretty_function(obj)
  elseif obj_type ~= "table" or tracking[obj] then return #tostring(obj) end

  local count = 2
  tracking[obj] = true
  for k, v in pairs(obj) do
    count = count + pretty_size(k, tracking, limit) + pretty_size(v, tracking, limit)
    if count >= limit then break end
  end
  tracking[obj] = nil
  return count
end

local function pretty_impl(obj, tracking, width, height, indent, tuple_length)
  local obj_type = type(obj)
  if obj_type == "string" then
    local formatted = string.format("%q", obj):gsub("\\\n", "\\n")

    -- Strings are limited to the size of the current buffer with a bit of padding
    local limit = math.max(8, math.floor(width * height * 0.8))
    if #formatted > limit then
      write_with(string_colour, formatted:sub(1, limit - 3))
      write_with(extra_colour, "...")
    else
      write_with(string_colour, formatted)
    end
    return
  elseif obj_type == "number" then
    return write_with(number_colour, tostring(obj))
  elseif obj_type == "function" then
    return write_with(object_colour, pretty_function(obj))
  elseif obj_type ~= "table" or tracking[obj] then
    return write_with(object_colour, tostring(obj))
  elseif (getmetatable(obj) or {}).__tostring then
    return write_with(text_colour, tostring(obj))
  end

  local open, close = "{", "}"
  if tuple_length then open, close = "(", ")" end

  if (tuple_length == nil or tuple_length == 0) and next(obj) == nil then
    return write_with(text_colour, open .. close)
  elseif width <= 7 then
    write_with(text_colour, open) write_with(extra_colour, " ... ") write_with(text_colour, close)
    return
  end

  local should_newline = false
  local length = tuple_length or #obj

  -- Compute the "size" of this object and how many children it has.
  local size, children, keys, kn = 2, 0, {}, 0
  for k, v in pairs(obj) do
    if type(k) == "number" and k >= 1 and k <= length and k % 1 == 0 then
      local vs = pretty_size(v, tracking, width)
      size = size + vs + 2
      children = children + 1
    else
      kn = kn + 1
      keys[kn] = k

      local vs, ks = pretty_size(v, tracking, width), pretty_size(k, tracking, width)
      size = size + vs + ks + 2
      children = children + 2
    end

    -- Some aribtrary scale factor to stop long lines filling too much of the
    -- screen
    if size >= width * 0.6 then should_newline = true end
  end

  -- If we want to have multiple lines, but don't fit in one then abort!
  if should_newline and height <= 1 then
    write_with(text_colour, open) write_with(extra_colour, " ... ") write_with(text_colour, close)
    return
  end

  -- Make sure our keys are in some sort of sensible order
  table.sort(keys, pretty_sort)

  local next_newline, sub_indent, child_width, child_height
  if should_newline then
    next_newline, sub_indent = ",\n", indent .. " "

    -- We split our height over multiple items. A future improvement could be to
    -- give more "height" to complex elements (such as tables)
    height = height - 2
    child_width, child_height = width - 2, math.ceil(height / children)

    -- If there's more children then we have space then
    if children > height then children = height - 2 end
  else
    next_newline, sub_indent =  ", ", ""

    -- Like multi-line elements, we share the width across multiple children
    width = width - 2
    child_width, child_height = math.ceil(width / children), 1
  end

  write_with(text_colour, open .. (should_newline and "\n" or " "))

  tracking[obj] = true
  local seen = {}
  local first = true
  for k = 1, length do
    if not first then write_with(text_colour, next_newline) else first = false end
    write_with(text_colour, sub_indent)

    seen[k] = true
    pretty_impl(obj[k], tracking, child_width, child_height, sub_indent)

    children = children - 1
    if children < 0 then
      if not first then write_with(text_colour, next_newline) else first = false end
      write_with(extra_colour, sub_indent .. "...")
      break
    end
  end

  for i = 1, kn do
    local k, v = keys[i], obj[keys[i]]
    if not seen[k] then
      if not first then write_with(text_colour, next_newline) else first = false end
      write_with(text_colour, sub_indent)

      if type(k) == "string" and not keywords[k] and string.match( k, "^[%a_][%a%d_]*$" ) then
        write_with(text_colour, k .. " = ")
        pretty_impl(v, tracking, child_width, child_height, sub_indent)
      else
        write_with(text_colour, "[")
        pretty_impl(k, tracking, child_width, child_height, sub_indent)
        write_with(text_colour, "] = ")
        pretty_impl(v, tracking, child_width, child_height, sub_indent)
      end

      children = children - 1
      if children < 0 then
        if not first then write_with(text_colour, next_newline) else first = false end
        write_with(extra_colour, sub_indent .. "...")
        break
      end
    end
  end
  tracking[obj] = nil

  write_with(text_colour, (should_newline and "\n" .. indent or " ") .. (tuple_length and ")" or "}"))
end

local function pretty(t, n)
  local width, height = term.getSize()
  local fit_height = settings.get("mbs.lua.pretty_height", true)
  if type(fit_height) == "number" then height = fit_height
  elseif fit_height == false then height = 1/0 end
  return pretty_impl(t, {}, width, height - 2, "", n)
end

local running = true
local history = {}
local counter = 1
local output = {}

local environment = setmetatable({
  exit = setmetatable({}, {
    __tostring = function() return "Call exit() to exit" end,
    __call = function() running = false end,
  }),

  _noTail = function(...) return ... end,

  out = output,
}, { __index = _ENV })

local autocomplete = nil
if not settings or settings.get("lua.autocomplete") then
  autocomplete = function(line)
    local start = line:find("[a-zA-Z0-9_%.:]+$")
    if start then
      line = line:sub(start)
    end
    if #line > 0 then
      return textutils.complete(line, environment)
    end
  end
end

local history_file = settings.get("mbs.lua.history_file", ".lua_history")
if history_file and fs.exists(history_file) then
  local handle = fs.open(history_file, "r")
  if handle then
    for line in handle.readLine do history[#history + 1] = line end
    handle.close()
  end
end

local function set_output(out, length)
  environment._ = out
  environment['_' .. counter] = out
  output[counter] = out

  term.setTextColour(output_colour)
  write("out[" .. counter .. "]: ")
  term.setTextColour(text_colour)

  if type(out) == "table" then
    print(pretty(out, length))
  else
    print(pretty(out))
  end
end

--- Handle the result of the function
local function handle(force_print, success, ...)
  if success then
    local len = select('#', ...)
    if len == 0 then
      if force_print then
        set_output(nil)
      end
    elseif len == 1 then
      set_output(...)
    else
      set_output({...}, len)
    end
  else
    printError(...)
  end
end

if type(package) == "table" and type(package.path) == "string" then
  -- Attempt to determine the shell directory with leading and trailing slashes
  local dir = shell.dir()
  if dir:sub(1, 1) ~= "/" then dir = "/" .. dir end
  if dir:sub(#dir, #dir) ~= "/" then dir = dir .. "/" end

  -- Strip the default "current program" package path
  local strip_path = "?;?.lua;?/init.lua;"
  local path = package.path
  if path:sub(1, #strip_path) == strip_path then path = path:sub(#strip_path + 1) end

  -- And append the current directory to the package path
  package.path = dir .. "?;" .. dir .. "?.lua;" .. dir .. "?/init.lua;" .. path
end

while running do
  term.setTextColour(input_colour)
  term.write("in [" .. counter .. "]: ")
  term.setTextColour(text_colour)

  local line
  if readline and readline.read and settings.get("mbs.lua.highlight") then
    line = readline.read {
      history = history,
      complete = autocomplete,
      highlight = highlight,
    }
  else
    line = read(nil, history, autocomplete)
  end
  if not line then break end

  if line:find("%S") then
    if line ~= history[#history] then
      -- Add item to history
      history[#history + 1] = line

      -- Remove extra items from history
      local max = tonumber(settings.get("mbs.lua.history_max", 1e4)) or 1e4
      while #history > max do table.remove(history, 1) end

      -- Write history file
      local history_file = settings.get("mbs.lua.history_file", ".lua_history")
      if history_file then
        local handle = fs.open(history_file, "w")
        if handle then
          for i = 1, #history do handle.writeLine(history[i]) end
          handle.close()
        end
      end
    end

    local force_print = true
    local func, e = load("return " .. line, "=lua", "t", environment)
    if not func then
      func, e = load(line, "=lua", "t", environment)
      force_print = false
    else
      local wrapped_func = load("return _noTail(" .. line .. ")", "=lua", "t", environment)
      if wrapped_func then func = wrapped_func end
    end

    if func then
      if settings.get("mbs.lua.traceback", true) then
        handle(force_print, stack_trace.xpcall_with(func))
      else
        handle(force_print, pcall(func))
      end
    else
      printError(e)
    end

    counter = counter + 1
  end
end