about summary refs log tree commit diff stats
path: root/mu.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-04-25 22:08:28 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-04-25 22:08:28 -0700
commit45508a440f91e893f863a0479d87109b58c74bd0 (patch)
tree410b038e74514e2926c66868d08cc60089454687 /mu.lua
parent6762fac7dbe5e41c2e480c09b80c18e452a052bf (diff)
downloadlines.love-45508a440f91e893f863a0479d87109b58c74bd0.tar.gz
repl
Diffstat (limited to 'mu.lua')
-rw-r--r--mu.lua58
1 files changed, 57 insertions, 1 deletions
diff --git a/mu.lua b/mu.lua
index 05cbdd6..8917476 100644
--- a/mu.lua
+++ b/mu.lua
@@ -1,6 +1,62 @@
 curses = require 'curses'
 
 local stdscr = curses.initscr()
+curses.echo(false) -- unclear why implicit echo can't handle newlines, regardless of stdscr:nl()
 stdscr:clear()
-stdscr:getch()
+stdscr:scrollok(true)
+
+-- unclear how Lua (post 5.2) is able to selectively print values of variables
+-- at the repl
+
+local function gather_results(success, ...)
+  local n = select('#', ...)
+  return success, { n = n, ... }
+end
+
+local function readline()
+  local result = ''
+  while true do
+    local x = stdscr:getch()
+    stdscr:addch(x)
+    local c = string.char(x)
+    result = result .. c
+    if c == '\n' then break end
+  end
+  return result
+end
+
+local new_expr = true
+local buf = ''
+while true do
+  if new_expr then
+    stdscr:addstr('> ')
+  else
+    stdscr:addstr('>> ')
+  end
+  buf = buf .. readline()
+  local f, err = load(buf, 'REPL')
+  if f then
+    buf = ''
+    new_expr = true
+    local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end))
+    if success then
+      for _, result in ipairs(results) do
+        print(result)
+      end
+    else
+      print(results[1])
+    end
+  else
+    stdscr:addstr(err..'\n')
+    if string.match(err, "'<eof>'$") or string.match(err, "<eof>$") then
+      buf = buf .. '\n'
+      new_expr = false
+    else
+      print(err)
+      buf = ''
+      new_expr = true
+    end
+  end
+end
+
 curses.endwin()