diff mbox

Adding wc to Bournish

Message ID 20160615202814.GB25828@debian-netbook
State New
Headers show

Commit Message

Efraim Flashner June 15, 2016, 8:28 p.m. UTC
On Wed, Jun 15, 2016 at 03:56:43PM +0200, Ludovic Courtès wrote:
> Efraim Flashner <efraim@flashner.co.il> skribis:
> 
> > From 09eef9cd841a7d212e024be0609168611923696b Mon Sep 17 00:00:00 2001
> > From: Efraim Flashner <efraim@flashner.co.il>
> > Date: Sun, 22 May 2016 14:56:06 +0300
> > Subject: [PATCH] bournish: Add `wc' command.
> >
> > * guix/build/bournish.scm (lines+chars, only-files, wc-commands,
> > wc-command-implementation, wc-l-command-implementation,
> > wc-c-command-implementation): New variables.
> 
> s/variables/procedures/ :-)
> 
> > (%commands): Add wc command.
> 
> [...]
> 
> > +(define (only-files files)
> > +  (filter (lambda (file)
> > +            (catch 'system-error
> > +              (lambda ()
> > +                (stat file))
> > +              (lambda args
> > +                (let ((errno (system-error-errno args)))
> > +                  (format (current-error-port) "~a: ~a~%"
> > +                          file (strerror errno))
> > +                  #f))))
> > +          files))
> > +
> > +(define (wc-command-implementation . files)
> > +  (for-each
> > +    (lambda (file)
> > +      (let-values (((lines chars)
> > +                    (call-with-input-file file lines+chars)))
> > +                  (format #t "~a ~a ~a~%" lines chars file)))
> > +    ((@@ (guix build bournish) only-files) files)))
> 
> I prefer the approach Ricardo suggested, I think it’s more concise and
> clearer:
> 
>   https://lists.gnu.org/archive/html/guix-devel/2016-06/msg00525.html
> 
> Also, note that @@ would not be needed here; @@ serves to access private
> bindings within a specific module:
> 
>   https://www.gnu.org/software/guile/manual/html_node/Using-Guile-Modules.html
> 
> Last, do not miss the bit about docstrings at:
> 
>   https://www.gnu.org/software/guix/manual/html_node/Formatting-Code.html
> 
> :-)
> 
> With these changes, we’re all set.  Thanks!

I've attached another patch

> 
> From a GSoC viewpoint, I think we must move to the compilation part
> now.  Specifically, I think the next step is to start parsing Bash
> code.

This part took much longer than I thought it would. Going forward I'll
try to be more pro-active on IRC about asking for help.

> 
> For that we could use SILex + parse-lalr, but these are not the nicest
> tools for the job.  Better tools would be “parsing expression grammars”
> (the (ice-9 peg) module in Guile 2.1) or parser combinators, though I
> don’t think there’s a directly usable Guile library for that.  Maybe
> Eric or David can comment?
> 

I get lalr as look-ahead left->right parser and found this¹, but what's SILex?
I also found peg in the master branch of the guile manual².

> The goal is to have a parser that returns an abstract syntax tree (AST)
> as an sexp:
> 
>   (parse "(cd /foo; ls $HOME) && echo *.a ; echo done")
>   =>
>   '(sequence
>      (success-sequence
>        (subshell
>          (sequence (command "cd" "/foo")
>                    (command "ls" (variable-ref "HOME"))))
>        (command "echo" (glob "*.a")))
>      (command "echo" "done"))
> 
> Thoughts?
> 
> Ludo’.

¹ https://www.gnu.org/software/guile/manual/html_node/LALR_00281_0029-Parsing.html#LALR_00281_0029-Parsing
² https://www.gnu.org/software/guile/docs/master/guile.html/PEG-Parsing.html#PEG-Parsing

Comments

Ludovic Courtès June 23, 2016, 8:34 a.m. UTC | #1
Hello!

Efraim Flashner <efraim@flashner.co.il> skribis:


[...]

>> I prefer the approach Ricardo suggested, I think it’s more concise and
>> clearer:
>> 
>>   https://lists.gnu.org/archive/html/guix-devel/2016-06/msg00525.html
>> 
>> Also, note that @@ would not be needed here; @@ serves to access private
>> bindings within a specific module:
>> 
>>   https://www.gnu.org/software/guile/manual/html_node/Using-Guile-Modules.html
>> 
>> Last, do not miss the bit about docstrings at:
>> 
>>   https://www.gnu.org/software/guix/manual/html_node/Formatting-Code.html
>> 
>> :-)
>> 
>> With these changes, we’re all set.  Thanks!
>
> I've attached another patch

Sorry for taking so long!  I’ve applied the patch with minor cosmetic
changes: procedure names, docstrings, and formatting (please take a
look).

>> From a GSoC viewpoint, I think we must move to the compilation part
>> now.  Specifically, I think the next step is to start parsing Bash
>> code.
>
> This part took much longer than I thought it would. Going forward I'll
> try to be more pro-active on IRC about asking for help.
>
>> 
>> For that we could use SILex + parse-lalr, but these are not the nicest
>> tools for the job.  Better tools would be “parsing expression grammars”
>> (the (ice-9 peg) module in Guile 2.1) or parser combinators, though I
>> don’t think there’s a directly usable Guile library for that.  Maybe
>> Eric or David can comment?
>> 
>
> I get lalr as look-ahead left->right parser and found this¹, but what's SILex?

SILex is a lexer: it turns character sequences into tokens, that can
then be passed to the parser.

I think SILex no longer has a home on the Internet, but you can find a
copy at
<http://git.savannah.gnu.org/cgit/guile-rpc.git/tree/modules/silex>.
Guile-RPC uses both SILex and ‘parse-lalr’, so it could serve as an
example, but again, PEG is probably preferable (Dave’s
guile-parser-combinator library is not ready for prime time, per a
recent discussion on guile-user@gnu.org.)

>> The goal is to have a parser that returns an abstract syntax tree (AST)
>> as an sexp:
>> 
>>   (parse "(cd /foo; ls $HOME) && echo *.a ; echo done")
>>   =>
>>   '(sequence
>>      (success-sequence
>>        (subshell
>>          (sequence (command "cd" "/foo")
>>                    (command "ls" (variable-ref "HOME"))))
>>        (command "echo" (glob "*.a")))
>>      (command "echo" "done"))
>> 
>> Thoughts?

So let’s focus on this part now.  Feel free to ping me/us while
researching the best approach or tools to work on it!

Thanks,
Ludo’.
diff mbox

Patch

diff --git a/guix/build/bournish.scm b/guix/build/bournish.scm
index 1f17e0a..97c1b43 100644
--- a/guix/build/bournish.scm
+++ b/guix/build/bournish.scm
@@ -1,5 +1,6 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2016 Efraim Flashner <efraim@flashner.co.il>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -25,6 +26,7 @@ 
   #:use-module (ice-9 match)
   #:use-module (ice-9 ftw)
   #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
   #:export (%bournish-language))
 
@@ -103,6 +105,57 @@  characters."
        ((@ (guix build utils) dump-port) port (current-output-port))
        *unspecified*)))
 
+(define (lines+chars port)
+  ;; Return the number of lines and number of chars read from PORT.
+  ;; TODO: Also return the number of words.
+  (let loop ((lines 0) (chars 0))
+    (match (read-char port) ; get the next char ready
+      ((? eof-object?)              ;done!
+       (values lines chars))
+      (#\newline                    ;recurse
+       (loop (1+ lines) (1+ chars)))
+      (_                            ;recurse
+       (loop lines (1+ chars))))))
+
+(define (only-files file)
+  (catch 'system-error
+    (lambda ()
+      (stat file))
+    (lambda args
+      (let ((errno (system-error-errno args)))
+        (format (current-error-port) "~a: ~a~%"
+                file (strerror errno))
+        #f))))
+
+(define (wc-print file)
+  (let-values (((lines chars)
+                (call-with-input-file file lines+chars)))
+              (format #t "~a ~a ~a~%" lines chars file)))
+
+(define (wc-l-print file)
+  (let-values (((lines chars)
+                (call-with-input-file file lines+chars)))
+              (format #t "~a ~a~%" lines file)))
+
+(define (wc-c-print file)
+  (let-values (((lines chars)
+                (call-with-input-file file lines+chars)))
+              (format #t "~a ~a~%" chars file)))
+
+(define (wc-command-implementation . files)
+  (for-each wc-print (filter only-files files)))
+
+(define (wc-l-command-implementation . files)
+  (for-each wc-l-print (filter only-files files)))
+
+(define (wc-c-command-implementation . files)
+  (for-each wc-c-print (filter only-files files)))
+
+(define (wc-commands . args)
+  (cond ((member "-l" args) `((@@ (guix build bournish) wc-l-command-implementation) ,@(delete "-l" args)))
+        ((member "-c" args) `((@@ (guix build bournish) wc-c-command-implementation) ,@(delete "-c" args)))
+        (else `((@@ (guix build bournish) wc-command-implementation) ,@args))))
+
 (define (help-command . _)
   (display "\
 Hello, this is Bournish, a minimal Bourne-like shell in Guile!
@@ -129,7 +182,8 @@  commands such as 'ls' and 'cd'; it lacks globbing, pipes---everything.\n"))
     ("help"   ,help-command)
     ("ls"     ,ls-command)
     ("which"  ,which-command)
-    ("cat"    ,cat-command)))
+    ("cat"    ,cat-command)
+    ("wc"     ,wc-commands)))
 
 (define (read-bournish port env)
   "Read a Bournish expression from PORT, and return the corresponding Scheme