;;; interpreter-repl.scm
;;;
;;; REPL and support procedures for interpreter based on R5RS DS implementation
;;;
;;; Copyright (C) 2002 Anton van Straaten <anton@ppsolutions.com>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License, 
;;; version 2, 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/copyleft/gpl.html
;;;
;;; -------------------------------------------------------------------------
;; Accepts program as s-expression.  Invokes semantic function 'expression-meaning' 
;; to obtain a function M, which represents the denotational meaning of the specified
;; program.  This meaning function is used to evaluate the program.  Result is printed,
;; along with internal information such as a dump of the store.
;;
;; E : expression
;; K : continuation - will have access to store
;; X : exit continuation.  Note this determines handling of (exit) and
    ;; (exit errorlevel); latter not yet supported in procedures below.
Show source file in large font In interpreter-repl: Link from eval-expression to it's cross reference table entry 4.10. Interpreter REPL
(define (eval-expression e k x) (let* ((context (initialize-global-context x)) (u (car context)) (s (cdr context))) (((expression-meaning-toplevel e) u k) s))) ;; repl - Invoke the DS Interpreter's REPL Show source file in large font In interpreter-repl: Link from repl to it's cross reference table entry 2.1. Operation of the Interpreter 4.10. Interpreter REPL
(define (repl) (call-with-current-continuation (lambda (c) (eval-expression bootstrap-source c ;; exit continuation (lambda (e k) (c e)))))) ;; The following are defined in the host Scheme environment, to support use by ds:wrong ;; Note LAML can't handle global names with characters that aren't allowed in filenames, ;; like * and <. Wanted to use *current-failure-continuation* below. Show source file in large font In interpreter-repl: Link from failure-continuation-value to it's cross reference table entry 4.10. Interpreter REPL
(define failure-continuation-value #f) Show source file in large font In interpreter-repl: Link from current-failure-continuation to it's cross reference table entry 4.10. Interpreter REPL
(define current-failure-continuation (lambda (e k) (lambda (s) (if (= (ds:length e) 1) (set! failure-continuation-value (ds:first e))) ((ds:send (ds:sequence failure-continuation-value) k) s)))) Show source file in large font In interpreter-repl: Link from ds:wrong to it's cross reference table entry 4.2. Auxiliary Functions 4.10. Interpreter REPL 5.2. Shortcomings
(define ds:wrong (lambda (x) (display x) ; allows display before store is supplied, in case of mismatches (newline) (lambda (s) (((ds:applicate failure-continuation-value (ds:sequence)) #f) s)))) ;; #f is for unused continuation ;; ;; Called from non-DS procedures when execution context can't easily support ds:wrong. ;; todo: improve error handling logic Show source file in large font In interpreter-repl: Link from ds:wrong-wrong to it's cross reference table entry 4.10. Interpreter REPL
(define ds:wrong-wrong (lambda (x) (display x) ; allows display before store is supplied, in case of mismatches (newline) (lambda (s) (display "This never executes - no store supplied.") (((ds:applicate failure-continuation-value (ds:sequence)) #f) s)) ;; return uninitialized location; bogus, since not associated with an address (ds:sequence ds:unspecified ds:false))) ;; The following is the first code evaluated by the interpreter itself. ;; This code defines the interpreter's repl, the 'load' procedure, and ;; then loads subsequent definitions from prelude.scm. Show source file in large font In interpreter-repl: Link from bootstrap-source to it's cross reference table entry 2.1. Operation of the Interpreter 4.10. Interpreter REPL
(define bootstrap-source '(begin ;; a parameterizerable repl - see chatty-repl in prelude.scm for example of use Show source file in large font In interpreter-repl: Link from main-repl to it's cross reference table entry 
(define (main-repl port prompt-proc display-proc) (let reploop () (if prompt-proc (begin (newline) (prompt-proc))) (let ((p (read port))) (if (eof-object? p) #f ; todo: use level-sensitive exit continuation? (begin (call-with-current-continuation (lambda (c) (current-failure-continuation c) (display-proc (eval p (interaction-environment))))) (reploop)))))) Show source file in large font In interpreter-repl: Link from load to it's cross reference table entry 
(define (load filename) (let ((port (open-input-file filename))) (main-repl port #f (lambda (x) #f)) (close-input-port port) #t)) (load "prelude.scm") (chatty-repl (current-input-port))))