2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2006-12-03 09:17:28 -05:00
|
|
|
#|procedure:new-cafe
|
|
|
|
synopsis:
|
|
|
|
(new-cafe [eval])
|
|
|
|
description:
|
|
|
|
The procedure new-cafe starts a new read-eval-print loop inside
|
|
|
|
the current cafe (if one exists). It prompts the user for an
|
|
|
|
expression, evaluates it, prints the result back, and repeats the
|
|
|
|
process. If new-cafe is called with an argument, eval, then that
|
|
|
|
argument must be a procedure that takes a single argument. The
|
|
|
|
eval procedure will be used to evaluate the expressions.
|
|
|
|
|
|
|
|
Every time a new cafe is started, the prompt is changed to reflect
|
|
|
|
the depth of the current cafe (i.e. how many eof objects is takes
|
|
|
|
to exit the outermost cafe).
|
|
|
|
|
|
|
|
Input and output performed by the cafe can be changed by the
|
|
|
|
console-input-port and console-output-port parameters.
|
|
|
|
|
2007-12-15 08:22:49 -05:00
|
|
|
If an die occurs during reading, evaluating, or printing an
|
|
|
|
expression, then the die is printed to the error-port and the
|
2006-12-03 09:17:28 -05:00
|
|
|
operations of the cafe resume as normal.|#
|
|
|
|
#|FIXME:new-cafe
|
|
|
|
Be specific about what the error-port is |#
|
2007-04-29 18:35:18 -04:00
|
|
|
|
|
|
|
(library (ikarus cafe)
|
2007-05-05 22:09:41 -04:00
|
|
|
(export new-cafe)
|
2007-05-06 20:29:58 -04:00
|
|
|
(import
|
2007-10-23 23:55:57 -04:00
|
|
|
(only (rnrs) with-exception-handler)
|
2007-05-06 20:43:56 -04:00
|
|
|
(except (ikarus) new-cafe))
|
2007-04-29 18:35:18 -04:00
|
|
|
|
2006-11-23 19:33:45 -05:00
|
|
|
(define eval-depth 0)
|
|
|
|
|
|
|
|
(define display-prompt
|
|
|
|
(lambda (i)
|
|
|
|
(if (fx= i eval-depth)
|
|
|
|
(display " " (console-output-port))
|
|
|
|
(begin
|
|
|
|
(display ">" (console-output-port))
|
|
|
|
(display-prompt (fx+ i 1))))))
|
|
|
|
|
2008-08-03 16:50:20 -04:00
|
|
|
(define (print-ex ex)
|
|
|
|
(flush-output-port (console-output-port))
|
|
|
|
(display "Unhandled exception\n" (console-error-port))
|
|
|
|
(print-condition ex (console-error-port)))
|
|
|
|
|
|
|
|
(define (reset k)
|
|
|
|
(reset-input-port! (console-input-port))
|
|
|
|
(k))
|
|
|
|
|
2008-12-08 02:13:40 -05:00
|
|
|
(define wait1
|
|
|
|
(lambda (eval-proc k escape-k)
|
|
|
|
(display-prompt 0)
|
|
|
|
(let ([x (with-exception-handler
|
|
|
|
(lambda (ex)
|
|
|
|
(cond
|
|
|
|
[(lexical-violation? ex)
|
|
|
|
(print-ex ex)
|
|
|
|
(reset k)]
|
|
|
|
[(interrupted-condition? ex)
|
|
|
|
(flush-output-port (console-output-port))
|
|
|
|
(newline (console-output-port))
|
|
|
|
(reset k)]
|
|
|
|
[else (raise-continuable ex)]))
|
|
|
|
(lambda ()
|
|
|
|
(read (console-input-port))))])
|
|
|
|
(cond
|
|
|
|
[(eof-object? x)
|
|
|
|
(newline (console-output-port))
|
|
|
|
(escape-k (void))]
|
|
|
|
[else
|
|
|
|
(call-with-values
|
|
|
|
(lambda ()
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (ex)
|
|
|
|
(if (non-continuable-violation? ex)
|
|
|
|
(reset k)
|
|
|
|
(raise-continuable ex)))
|
|
|
|
(lambda ()
|
2008-08-03 16:50:20 -04:00
|
|
|
(with-exception-handler
|
|
|
|
(lambda (ex)
|
2008-12-08 02:13:40 -05:00
|
|
|
(print-ex ex)
|
|
|
|
(when (serious-condition? ex)
|
|
|
|
(reset k)))
|
2008-08-03 16:50:20 -04:00
|
|
|
(lambda ()
|
2008-12-08 02:13:40 -05:00
|
|
|
(eval-proc x))))))
|
|
|
|
(lambda v*
|
|
|
|
(unless (andmap (lambda (v) (eq? v (void))) v*)
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (ex)
|
|
|
|
(cond
|
|
|
|
[(interrupted-condition? ex)
|
|
|
|
(flush-output-port (console-output-port))
|
|
|
|
(newline (console-output-port))
|
|
|
|
(reset k)]
|
|
|
|
[else (raise-continuable ex)]))
|
|
|
|
(lambda ()
|
|
|
|
(for-each
|
|
|
|
(lambda (v)
|
|
|
|
(pretty-print v (console-output-port)))
|
|
|
|
v*))))))]))))
|
2006-11-23 19:33:45 -05:00
|
|
|
|
2007-04-29 21:59:06 -04:00
|
|
|
(define do-new-cafe
|
2007-05-03 00:38:42 -04:00
|
|
|
(lambda (eval-proc)
|
2006-11-23 19:40:06 -05:00
|
|
|
(dynamic-wind
|
|
|
|
(lambda () (set! eval-depth (fxadd1 eval-depth)))
|
|
|
|
(lambda ()
|
|
|
|
(call/cc
|
|
|
|
(lambda (k)
|
2008-12-08 02:13:40 -05:00
|
|
|
(let loop ()
|
|
|
|
(call/cc
|
|
|
|
(lambda (k1)
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (ex)
|
|
|
|
(with-exception-handler k1
|
|
|
|
(lambda ()
|
|
|
|
(flush-output-port (console-output-port))
|
|
|
|
(newline (console-output-port))
|
|
|
|
(reset k1))))
|
|
|
|
(lambda () (wait1 eval-proc k1 k)))))
|
|
|
|
(loop)))))
|
2006-11-23 19:40:06 -05:00
|
|
|
(lambda () (set! eval-depth (fxsub1 eval-depth))))))
|
|
|
|
|
2007-05-03 00:38:42 -04:00
|
|
|
(define default-cafe-eval
|
|
|
|
(lambda (x)
|
2008-05-01 06:02:36 -04:00
|
|
|
(eval x (interaction-environment))))
|
2007-05-03 00:38:42 -04:00
|
|
|
|
2007-05-05 22:09:41 -04:00
|
|
|
(define new-cafe
|
2006-11-23 19:40:06 -05:00
|
|
|
(case-lambda
|
2007-05-03 00:38:42 -04:00
|
|
|
[() (do-new-cafe default-cafe-eval)]
|
2006-11-23 19:40:06 -05:00
|
|
|
[(p)
|
2007-04-29 18:35:18 -04:00
|
|
|
(unless (procedure? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'new-cafe "not a procedure" p))
|
2007-04-29 21:59:06 -04:00
|
|
|
(do-new-cafe p)]))
|
2006-11-23 19:40:06 -05:00
|
|
|
)
|
2006-11-23 19:33:45 -05:00
|
|
|
|