[repl] support multiple line input

This commit is contained in:
Yuichi Nishiwaki 2014-09-01 01:00:30 +09:00
parent 9fefa80466
commit a290bb0b04
1 changed files with 28 additions and 24 deletions

View File

@ -23,30 +23,34 @@
(define-readline) (define-readline)
(define (repl) (define (repl)
(let ((line (readline "> "))) (let loop ((buf ""))
(let ((line (readline (if (equal? buf "") "> " "* "))))
(if (eof-object? line) (if (eof-object? line)
(newline) ; exit (newline) ; exit
(begin (let ((str (string-append buf line "\n")))
(add-history line) (add-history line)
(call/cc (call/cc
(lambda (exit) (lambda (exit)
(with-exception-handler (with-exception-handler
(lambda (condition) (lambda (condition)
(unless (equal? (error-object-message condition) "unexpected EOF")
(display (error-object-message condition) (current-error-port)) (display (error-object-message condition) (current-error-port))
(newline) (newline)
(set! str ""))
(exit)) (exit))
(lambda () (lambda ()
;; FIXME ;; FIXME
;; non-local exception jump from inside call-with-port ;; non-local exception jump from inside call-with-port
;; fails with segv, though i don't know why... ;; fails with segv, though i don't know why...
(let ((port (open-input-string line))) (let ((port (open-input-string str)))
(let loop ((expr (read port))) (let next ((expr (read port)))
(unless (eof-object? expr) (unless (eof-object? expr)
(write (eval expr '(picrin user))) (write (eval expr '(picrin user)))
(newline) (newline)
(loop (read port)))) (set! str "")
(next (read port))))
(close-port port)))))) (close-port port))))))
(repl))))) (loop str))))))
(export repl)) (export repl))