From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nala Ginrut Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] Add inspection command "source (,src)" which shows Scheme code of loaded module Date: Sun, 31 Mar 2013 20:47:28 +0800 Organization: HFG Message-ID: <1364734048.2730.97.camel@Renee-desktop.suse> References: <1364651860.2730.71.camel@Renee-desktop.suse> <87zjxk7fjy.fsf@tines.lan> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1364734060 5912 80.91.229.3 (31 Mar 2013 12:47:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 31 Mar 2013 12:47:40 +0000 (UTC) Cc: guile-devel@gnu.org To: Mark H Weaver Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Mar 31 14:48:07 2013 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UMHg6-0001PL-Qc for guile-devel@m.gmane.org; Sun, 31 Mar 2013 14:48:07 +0200 Original-Received: from localhost ([::1]:53596 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMHfi-0002J0-Dq for guile-devel@m.gmane.org; Sun, 31 Mar 2013 08:47:42 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:60955) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMHfb-0002IY-NU for guile-devel@gnu.org; Sun, 31 Mar 2013 08:47:39 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UMHfZ-0000f1-KB for guile-devel@gnu.org; Sun, 31 Mar 2013 08:47:35 -0400 Original-Received: from mail-pa0-f53.google.com ([209.85.220.53]:52943) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UMHfZ-0000en-BF for guile-devel@gnu.org; Sun, 31 Mar 2013 08:47:33 -0400 Original-Received: by mail-pa0-f53.google.com with SMTP id bh4so932068pad.12 for ; Sun, 31 Mar 2013 05:47:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:subject:from:to:cc:date:in-reply-to :references:organization:content-type:x-mailer:mime-version :content-transfer-encoding; bh=7i9+J8OfZYJDULwGVNXiyg7pHHUY3X8AN+gK0NDS+i0=; b=aHFh2B+YblZIYlBssQbuYySwvYapgVB7UgEEIOUQjQEl+C6EbFrJBs8QzuQar60pj+ E7n8zCyFrwIy9D/5pfwnAUEMQKA8zyuXky+QkXoemxMMp1cislqcOv8J8jFr/N0kZQ72 fLD1l4WvNE9fskN5v9SwT7qtbo0bDEZSS5bIori0zO5AiH22OXdKdd6a3NekRZL7dkpo gFzJkvRCzGdWPaGG6fnjq9sC4F05PRkHu9T3PvqCuf20seMQKzynkjszMVz7nXBB0mVO Baqamj12cVAX+I7V/vmuadDt8ZIZijR/KEIyqulSkb2w7/THKEuhzIpixyEuJjaHTXOB dMoA== X-Received: by 10.66.139.198 with SMTP id ra6mr13916573pab.148.1364734052606; Sun, 31 Mar 2013 05:47:32 -0700 (PDT) Original-Received: from [192.168.100.102] ([59.40.233.115]) by mx.google.com with ESMTPS id eh5sm9914079pbc.44.2013.03.31.05.47.30 (version=SSLv3 cipher=RC4-SHA bits=128/128); Sun, 31 Mar 2013 05:47:31 -0700 (PDT) In-Reply-To: <87zjxk7fjy.fsf@tines.lan> X-Mailer: Evolution 3.4.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.220.53 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:16066 Archived-At: On Sat, 2013-03-30 at 17:17 -0400, Mark H Weaver wrote: > Nala Ginrut writes: > > > Attached patch added an inspection command "source" to display the > > Scheme code of loaded module, it'll be useful for folks: > > > > -------------Scheme proc------------ > > scheme@(guile-user)> ,use (srfi srfi-1) > > scheme@(guile-user)> ,src any > > (define (any pred ls . lists) > > (check-arg procedure? pred any) > > (if (null? lists) > > (any1 pred ls) > > (let lp ((lists (cons ls lists))) > > (cond ((any1 null? lists) #f) > > ((any1 null? (map cdr lists)) > > (apply pred (map car lists))) > > (else > > (or (apply pred (map car lists)) > > (lp (map cdr lists)))))))) > > --------------------end------------------------- > > Nice hack! :) > > I'd be glad to see something like this in Guile core. This code is a > great demonstration, but it has some problems. > > > 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))) > > This strategy of reading the code is not robust. > > * It assumes that the procedure is the first datum on the specified > line. This is not generally true. Yes, I saw that. But I don't know how to get the procedure definition line from program-source, since there's no sufficient docs for that. Someone in IRC suggested me use the first datum. Could you point me out how to do that? ;-) > > * It assumes that there are no reader directives in the file (such as > #!curly-infix) that affect the operation of the reader. As is, your > code will not work with any procedure that uses curly-infix. > > * It assumes that the procedure is written in Scheme. > > Also, as we discussed on IRC, it would be better to show the original > characters in the file instead of the sexp representation. I want to > see the comments. I want to see the programmer-chosen indentation. If > they chose to use curly-infix for some expressions, I want to see that. > More generally, I want to see the presentation that the programmer > thought was most readable, i.e. the *source* code. > For these purposes above, I think there should be a modified 'read' to read the code from source file with comments. > > + > > +(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") > > I'm not sure we can conclude that the procedure is implemented in C just > because 'program-source' returns #f. There might be other reasons why > that might happen. > Well, I'll take Daniel's suggestion to return #f if the source code is unavailable. > > + ((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 > > Regards, > Mark