>From 454af1f4326d600d6044de903b12812dfd9310ad Mon Sep 17 00:00:00 2001 From: Nala Ginrut Date: Sat, 30 Mar 2013 21:48:35 +0800 Subject: [PATCH] Add src command in REPL to show Scheme code of loaded module * system/repl/command.scm: Add inspection command "source (,src)" which shows Scheme code of loaded module. --- module/system/repl/command.scm | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/module/system/repl/command.scm b/module/system/repl/command.scm index 8ad00da..bda6dfe 100644 --- a/module/system/repl/command.scm +++ b/module/system/repl/command.scm @@ -65,7 +65,7 @@ (tracepoint tp) (traps) (delete del) (disable) (enable) (registers regs)) - (inspect (inspect i) (pretty-print pp)) + (inspect (inspect i) (pretty-print pp) (source src)) (system (gc) (statistics stat) (option o) (quit q continue cont)))) @@ -869,6 +869,40 @@ Pretty-print the result(s) of evaluating EXP." (pp x)) args)))) +(define (get-src source) + (define any (@ (srfi srfi-1) any)) + (define (skip-lines port n) + (cond + ((zero? n) port) + (else (read-line port) (skip-lines port (1- n))))) + + (let* ((file (source:file source)) + (line (source:line source)) + (fp (any (lambda (x) + (let ((f (string-append x "/" file))) + (if (file-exists? f) (open-input-file f) #f))) %load-path))) + (skip-lines fp line) + (let ((src (read fp))) + (close fp) + src))) + +(define (print-src p) + (define (get-program-src p) + (let ((source (program-source p 0))) + (cond + ((not source) "It's inner procedure implemented with C") + ((not (source:file source)) #f) + (else (get-src source))))) + (let ((src (and (program? p) (get-program-src p)))) + (and src (pp src)))) + +(define-meta-command (source repl (form)) + "source PROC +Pretty-print the source code of PROC" + (call-with-values (repl-prepare-eval-thunk repl (repl-parse repl form)) + (lambda args + (for-each print-src args)))) + ;;; ;;; System commands -- 1.7.10.4