mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-20 22:29:16 +02:00
adding more featureful repl module
This commit is contained in:
parent
b5f29def78
commit
1b14ac40a6
2 changed files with 63 additions and 0 deletions
5
lib/chibi/repl.module
Normal file
5
lib/chibi/repl.module
Normal file
|
@ -0,0 +1,5 @@
|
|||
|
||||
(define-module (chibi repl)
|
||||
(export repl)
|
||||
(import-immutable (scheme) (chibi process))
|
||||
(include "repl.scm"))
|
58
lib/chibi/repl.scm
Normal file
58
lib/chibi/repl.scm
Normal file
|
@ -0,0 +1,58 @@
|
|||
|
||||
(define (run-repl module env)
|
||||
(if module (display module))
|
||||
(display "> ")
|
||||
(flush-output)
|
||||
(let lp ()
|
||||
(let ((ch (peek-char)))
|
||||
(cond ((eof-object? ch)
|
||||
(exit 0))
|
||||
((and (char? ch) (char-whitespace? ch))
|
||||
(read-char)
|
||||
(lp)))))
|
||||
(cond
|
||||
((eq? #\@ (peek-char))
|
||||
(read-char)
|
||||
(let ((sym (read)))
|
||||
(if (not (symbol? sym))
|
||||
(error "repl: invalid @ syntax: @" sym)
|
||||
(case sym
|
||||
((config)
|
||||
(let ((res (eval (read) *config-env*)))
|
||||
(cond
|
||||
((not (eq? res (if #f #f)))
|
||||
(write res)
|
||||
(newline)))
|
||||
(run-repl module env)))
|
||||
((in)
|
||||
(let ((mod (read)))
|
||||
(if (or (not mod) (equal? mod '(scheme)))
|
||||
(run-repl #f (interaction-environment))
|
||||
(let ((env (eval `(module-env (load-module ',mod))
|
||||
*config-env*)))
|
||||
(run-repl mod env)))))
|
||||
(else
|
||||
(error "repl: unknown @ escape" sym))))))
|
||||
(else
|
||||
(let ((expr (read)))
|
||||
(cond
|
||||
((eof-object? expr)
|
||||
(exit 0))
|
||||
(else
|
||||
(let ((res (eval expr env)))
|
||||
(cond
|
||||
((not (eq? res (if #f #f)))
|
||||
(write res)
|
||||
(newline)))
|
||||
(run-repl module env))))))))
|
||||
|
||||
(define (repl)
|
||||
(set-signal-action! signal/interrupt
|
||||
(lambda (n info)
|
||||
(newline)
|
||||
(run-repl #f (interaction-environment))))
|
||||
(current-exception-handler
|
||||
(lambda (exn)
|
||||
(print-exception exn (current-error-port))
|
||||
(run-repl #f (interaction-environment))))
|
||||
(run-repl #f (interaction-environment)))
|
Loading…
Add table
Reference in a new issue