From 8e75c00c7b278b70d3f42dc6be90e09a0ae652c0 Mon Sep 17 00:00:00 2001 From: Janneke Nieuwenhuizen Date: Sat, 8 Jul 2023 11:11:38 +0200 Subject: [PATCH] repl: Fix include, load, use MODULE, and mes-use-module. Reported by Irvise via IRC. * mes/module/mes/repl.mes (repl)[load-env, mes-load-module-env]: New inner defines. [use]: Use mes-load-module-env, do not return content of module. Special-case 'include' and 'load'. --- mes/module/mes/repl.mes | 56 ++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/mes/module/mes/repl.mes b/mes/module/mes/repl.mes index 45833288..fb38df29 100644 --- a/mes/module/mes/repl.mes +++ b/mes/module/mes/repl.mes @@ -126,6 +126,26 @@ along with GNU Mes. If not, see . (newline)) (core:macro-expand sexp)))) + (define (load-env file-name a) + (push! *input-ports* (current-input-port)) + (set-current-input-port + (open-input-file file-name)) + (let ((x (core:eval (append2 (cons 'begin (read-input-file-env a)) + '((current-module))) + a))) + (set-current-input-port (pop! *input-ports*)) + x)) + + (define (mes-load-module-env module a) + (push! *input-ports* (current-input-port)) + (set-current-input-port + (open-input-file (string-append %moduledir (module->file module)))) + (let ((x (core:eval (append2 (cons 'begin (read-input-file-env a)) + '((current-module))) + a))) + (set-current-input-port (pop! *input-ports*)) + x)) + (define (help . x) (display help-commands) *unspecified*) (define (show . x) (define topic-alist `((#\newline . ,show-commands) @@ -140,7 +160,8 @@ along with GNU Mes. If not, see . (define (use a) (lambda () (let ((module (read))) - (mes-load-module-env module a)))) + (mes-load-module-env module a) + module))) (define (meta command a) (let ((command-alist `((expand . ,(expand a)) (help . ,help) @@ -163,19 +184,26 @@ along with GNU Mes. If not, see . (display sexp) (display "]") (newline)) - (if (and (pair? sexp) (eq? (car sexp) 'mes-use-module)) - (loop (mes-load-module-env (cadr sexp) a)) - (let ((e (if (and (pair? sexp) (eq? (car sexp) (string->symbol "unquote"))) - (meta (cadr sexp) a) - (core:eval sexp a)))) - (if (eq? e *unspecified*) (loop a) - (let ((id (string->symbol (string-append "$" (number->string count))))) - (set! count (+ count 1)) - (display id) - (display " = ") - (write e) - (newline) - (loop (acons id e a))))))))) + (cond + ((and (pair? sexp) (eq? (car sexp) 'mes-use-module)) + (let ((module (cadr sexp))) + (mes-load-module-env module a) + (loop a))) + ((and (pair? sexp) (memq (car sexp) '(include load))) + (load-env (cadr sexp) a) + (loop a)) + (else + (let ((e (if (and (pair? sexp) (eq? (car sexp) (string->symbol "unquote"))) + (meta (cadr sexp) a) + (core:eval sexp a)))) + (if (eq? e *unspecified*) (loop a) + (let ((id (string->symbol (string-append "$" (number->string count))))) + (set! count (+ count 1)) + (display id) + (display " = ") + (write e) + (newline) + (loop (acons id e a)))))))))) (lambda (key . args) (if (defined? 'with-output-to-string) (simple-format (current-error-port) "exception:~a:~a\n" key args)