-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.rkt
34 lines (31 loc) · 1017 Bytes
/
repl.rkt
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
#lang racket/base
(require (prefix-in r: readline/readline)
racket/match racket/pretty
data/either
megaparsack
"parse.rkt"
"eval.rkt")
(define (readline prompt)
(if (member (getenv "TERM") '("dumb"))
(begin
(write-string "> ")
(flush-output)
(read-line))
(let ([input (r:readline prompt)])
(unless (eof-object? input)
(r:add-history input))
input)))
(define (repl)
(let rec ()
(let ([input (readline "> ")])
(unless (or (eof-object? input)
(member input '("q" "quit" "exit")))
(match (parse-program input)
[(success ast)
(with-handlers ([exn:fail? (lambda (exn)
(display "Error: ")
(displayln (exn-message exn)))])
(pretty-print (value-of-program ast)))]
[(failure message) (displayln (parse-error->string message))])
(rec)))))
(repl)