From mboxrd@z Thu Jan 1 00:00:00 1970 From: Efraim Flashner Subject: Re: Adding wc to Bournish Date: Wed, 15 Jun 2016 23:28:14 +0300 Message-ID: <20160615202814.GB25828@debian-netbook> References: <20160524184720.GA27449@debian-netbook> <20160605124033.GB859@debian-netbook> <87eg8bpe2v.fsf@gnu.org> <20160607074155.GB32264@debian-netbook> <878tyfelf6.fsf@gnu.org> <20160614092713.GA2832@debian-netbook> <20160614102029.GB10636@debian-netbook> <20160614105037.GC10636@debian-netbook> <87shwed084.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="bajzpZikUji1w+G9" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51805) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDHQS-0006bP-VP for guix-devel@gnu.org; Wed, 15 Jun 2016 16:28:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDHQQ-00050q-C2 for guix-devel@gnu.org; Wed, 15 Jun 2016 16:28:35 -0400 Content-Disposition: inline In-Reply-To: <87shwed084.fsf@gnu.org> List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: Ludovic =?utf-8?Q?Court=C3=A8s?= Cc: guix-devel@gnu.org, David Thompson --bajzpZikUji1w+G9 Content-Type: multipart/mixed; boundary="c3bfwLpm8qysLVxt" Content-Disposition: inline --c3bfwLpm8qysLVxt Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 15, 2016 at 03:56:43PM +0200, Ludovic Court=C3=A8s wrote: > Efraim Flashner skribis: >=20 > > From 09eef9cd841a7d212e024be0609168611923696b Mon Sep 17 00:00:00 2001 > > From: Efraim Flashner > > 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. >=20 > s/variables/procedures/ :-) >=20 > > (%commands): Add wc command. >=20 > [...] >=20 > > +(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))) >=20 > I prefer the approach Ricardo suggested, I think it=E2=80=99s more concis= e and > clearer: >=20 > https://lists.gnu.org/archive/html/guix-devel/2016-06/msg00525.html >=20 > Also, note that @@ would not be needed here; @@ serves to access private > bindings within a specific module: >=20 > https://www.gnu.org/software/guile/manual/html_node/Using-Guile-Modules= =2Ehtml >=20 > Last, do not miss the bit about docstrings at: >=20 > https://www.gnu.org/software/guix/manual/html_node/Formatting-Code.html >=20 > :-) >=20 > With these changes, we=E2=80=99re all set. Thanks! I've attached another patch >=20 > 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. >=20 > For that we could use SILex + parse-lalr, but these are not the nicest > tools for the job. Better tools would be =E2=80=9Cparsing expression gra= mmars=E2=80=9D > (the (ice-9 peg) module in Guile 2.1) or parser combinators, though I > don=E2=80=99t think there=E2=80=99s a directly usable Guile library for t= hat. Maybe > Eric or David can comment? >=20 I get lalr as look-ahead left->right parser and found this=C2=B9, but what'= s SILex? I also found peg in the master branch of the guile manual=C2=B2. > The goal is to have a parser that returns an abstract syntax tree (AST) > as an sexp: >=20 > (parse "(cd /foo; ls $HOME) && echo *.a ; echo done") > =3D> > '(sequence > (success-sequence > (subshell > (sequence (command "cd" "/foo") > (command "ls" (variable-ref "HOME")))) > (command "echo" (glob "*.a"))) > (command "echo" "done")) >=20 > Thoughts? >=20 > Ludo=E2=80=99. =C2=B9 https://www.gnu.org/software/guile/manual/html_node/LALR_00281_0029-= Parsing.html#LALR_00281_0029-Parsing =C2=B2 https://www.gnu.org/software/guile/docs/master/guile.html/PEG-Parsin= g.html#PEG-Parsing --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --c3bfwLpm8qysLVxt Content-Type: text/plain; charset=utf-8 Content-Disposition: attachment; filename="0001-bournish-Add-wc-command.patch" Content-Transfer-Encoding: quoted-printable =46rom 432d742639e193a29aeedc2f080c349494fa2a8f Mon Sep 17 00:00:00 2001 =46rom: Efraim Flashner Date: Sun, 22 May 2016 14:56:06 +0300 Subject: [PATCH] bournish: Add `wc' command. * guix/build/bournish.scm (lines+chars, only-files, wc-print, wc-l-print, wc-c-print, wc-commands, wc-command-implementation, wc-l-command-implementation, wc-c-command-implementation): New procedures. (%commands): Add wc command. --- guix/build/bournish.scm | 56 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 55 insertions(+), 1 deletion(-) 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 =C2=A9 2016 Ludovic Court=C3=A8s +;;; Copyright =C2=A9 2016 Efraim Flashner ;;; ;;; 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)) =20 @@ -103,6 +105,57 @@ characters." ((@ (guix build utils) dump-port) port (current-output-port)) *unspecified*))) =20 +(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-imple= mentation) ,@(delete "-l" args))) + ((member "-c" args) `((@@ (guix build bournish) wc-c-command-imple= mentation) ,@(delete "-c" args))) + (else `((@@ (guix build bournish) wc-command-implementation) ,@arg= s)))) + (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, pipe= s---everything.\n")) ("help" ,help-command) ("ls" ,ls-command) ("which" ,which-command) - ("cat" ,cat-command))) + ("cat" ,cat-command) + ("wc" ,wc-commands))) =20 (define (read-bournish port env) "Read a Bournish expression from PORT, and return the corresponding Sche= me --=20 2.8.4 --c3bfwLpm8qysLVxt-- --bajzpZikUji1w+G9 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJXYbpbAAoJEPTB05F+rO6TWXYP/iEOrjZBvHne6eD74UIJBiw9 dnW2zpNMOCMcBHhb8E9cNM+FVmpsDO4PqWljFFzS8/pU1cfEcX5KxjJE0LGtGTzN WkOUTowf8unTrqrXL8wYFoPszyzm+WrvHYs/YgJdcNb+hDHM5T+06coqK0zPOJCt AOOsyCpg5UyVOImLd/VPeChTGD3BWNt+RBRG4mflF+R0vmXIRgvD1GCfA+I4I0qg kt/b0GW+o0uzjy4SgN7SxQcrpharb0AVVUb1XgAW3nUrtYNIlj/Oi5J06RQM2Gys D78tZVjLpvSUItjyL2yC6Xps0pb53MCVOfOgfAMU9kfnuQYYMMn7hHSPFu3vlWyv hlFBnURLU55BLuy6EIpe8am0hOU+59mlCMLeaO2q0bucbSERjA7BE3CyGoTN0DNw a8suYYXBnwmiwukcog3B42/dgtFRZlxzJgiMTdfVAHJf4yK8cUpjQ9awRovaj52S MSlvn8SI0c7Oc5LBoDAaeCcORkQhCKVGryaIik3NAfT3EFA3WZbuPa3klY02/wuz N2Sxb/Hnon0XyfU/gV3PPr9Mx75xH1vVTdABXya814BePmY0crV2Dk++NvjYz5K5 lkNsgLbpXsLugmd/ZwhUOtp7jRTSlcGzwuWTo6N71jplaC8z/qLL8bBjvZ7ypw9h Jnu0bjmitunGxwjlYwjY =yRUb -----END PGP SIGNATURE----- --bajzpZikUji1w+G9--