Implemented line editing in the REPL via linenoise.
h3rald h3rald@h3rald.com
Mon, 08 Dec 2014 17:15:21 +0100
3 files changed,
12 insertions(+),
6 deletions(-)
M
interpreter.nim
→
interpreter.nim
@@ -1,5 +1,5 @@
import streams, strutils, tables -import parser +import parser, linenoise type TMinInterpreter* = object
M
linenoise.nim
→
linenoise.nim
@@ -54,7 +54,7 @@ #
# Sets the current tab completion handler and returns the previous one, or NULL # if no prior one has been set. # - proc linenoiseSetCompletionCallback(a: linenoiseCompletionCallback): linenoiseCompletions; + proc linenoiseSetCompletionCallback*(a: linenoiseCompletionCallback): linenoiseCompletions; # # Adds a copy of the given string to the given completion list. The copy is owned # by the linenoiseCompletions object.
M
minim.nim
→
minim.nim
@@ -51,6 +51,11 @@ stderr.writeln("Error - Cannot read from "& filename)
stderr.flushFile() minimStream(stream, filename) +proc completionCallback*(str: cstring, completions: ptr linenoiseCompletions) = + for s in SYMBOLS.keys: + if startsWith(s, $str): + linenoiseAddCompletion completions, s + proc minimRepl*() = var i = newMinInterpreter(debugging) var s = newStringStream("")@@ -60,12 +65,13 @@ echo "MiNiM v"&version&" - REPL initialized."
i.eval prelude echo "Prelude loaded." echo "-> Press Ctrl+C to exit." - var line: string + discard linenoiseSetCompletionCallback completionCallback + var line: cstring while true: - stdout.write(": ") - line = stdin.readLine() + line = linenoise(": ") + discard linenoiseHistoryAdd line s.writeln(line) - i.parser.buf = $i.parser.buf & line + i.parser.buf = $i.parser.buf & $line i.parser.bufLen = i.parser.buf.len discard i.parser.getToken() try: