Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

113 lines (79 sloc) 2.348 kb
use("atomy")
use("doc")
condition = require("condition")
pretty = require("pretty")
require("readline")
try(source, module, debug = false) =
with-restarts(retry -> try(source, module), abort -> .ok):
{ signal(.evaluated(module eval(source, 1, debug)))
} rescue:
(e: StandardError | ScriptError) -> error(e)
basic-repl(module, debug = false) =
loop:
prompt =
with-restarts(use-prompt(p) -> p):
signal(.prompt)
">> "
input =
{ Readline readline(prompt)
} rescue:
Interrupt: signal(.quit), ""
input match:
nil -> signal(.quit)
"" -> .ok
String ? =~ r"^:[[:alnum:]]" ->
signal(.special(input [1 .. -1]))
source:
signal(.input(source))
try(source, module, debug)
signal(.loop)
condition DefaultDebugger data(ReplDebugger(@error))
ReplDebugger open:
attr-accessor(.module)
ReplDebugger run := do:
/signal(.debug(self))
show-banner
with-restarts(backtrace -> show-backtrace):
debug
ReplDebugger(e) show-banner := do:
puts("-" * 78)
(e name + ": " + e message) split("\n") each [l]:
puts("*** " + l)
ReplDebugger debug := do:
show-restarts
{ /basic-repl(@module) } bind:
.prompt -> /restart(.use-prompt, "[!]> ")
.special(n ? =~ r"\d+") ->
^(condition Restarts) [n to-i] invoke
.quit -> exit(1)
repl(history = nil, module = nil, debug = false) := do:
unless(module):
module =! Atomy Module new(."(repl)")
module use("atomy")
when(history and File exists(history)?):
File open(history, "r") [f]:
f readlines each [l]:
Readline HISTORY << l strip
sane-history = []
{ let(condition Debugger = ReplDebugger):
frame = 0
{ basic-repl(module, debug)
} bind:
.prompt ->
restart(.use-prompt, "[" + frame to-s + "]> ")
.loop ->
(frame += 1)
.quit -> exit(0)
.input(str) -> (sane-history << str)
.special("h") ->
puts(":h\thelp")
.debug(dbg) ->
(dbg module = module)
.evaluated(r) ->
let(pretty Multiline = true,
pretty Colored = true):
puts((text(" =>") <+> pretty doc(r)) render(56))
} ensuring:
when(history):
File open(history, "a") [f]:
f puts(*sane-history)
Jump to Line
Something went wrong with that request. Please try again.