[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Adding wc to Bournish
From: |
Efraim Flashner |
Subject: |
Re: Adding wc to Bournish |
Date: |
Thu, 26 May 2016 20:50:09 +0300 |
User-agent: |
Mutt/1.6.1 (2016-04-27) |
On Thu, May 26, 2016 at 10:46:21AM +0200, Ludovic Courtès wrote:
>
> As Ricardo suggests, you could use ‘port->stream’ and ‘stream-fold’ to
> iterate over the characters read from the port. I suspect that’d be
> rather slow though, at least on 2.0, so another option is something
> like:
>
> (define (lines+chars port)
> ;; Return the number of lines and number of chars read from PORT.
> (let loop ((lines 1) (chars 0))
> (match (read-char port)
> ((? eof-object?) ;done!
> (values lines port))
> (#\newline ;recurse
> (loop (+ 1 lines) (+ 1 chars)))
> (_ ;recurse
> (loop lines (+ 1 chars))))))
>
> (define (wc-command file)
> (let-values (((lines chars)
> (call-with-input-file file lines+chars)))
> (format #t "~a ~a ~a~%" lines chars file)))
>
Are you suggesting just dropping the word count part of `wc'? I've been
thinking about it, and the simplest way I can think of to describe a
word is a space followed by a character, or, to use the char-sets from
the guile manual, a character from char-set:whitespace followed by a
character from char-set:graphic. I can compare (read-char port) and
(peek-char port) to get a word count (possibly).
> > +(define (wc-command file)
> > + (if (and (file-exists? file) (access? file 4))
>
> This check is not needed and is subject to a race condition (“TOCTTOU”);
> just let ‘call-with-input-file’ error out if the file cannot be read.
>
> Bonus point: catch ‘system-error’ exceptions and report the inability to
> open the file in a nice user-friendly way (but really, don’t bother
> about it for now.)
>
I'm still wrapping my head around the following part. My wife says when
I work I scowl at the computer a lot and mutter :)
>
> Remember that Bournish is a compiler that compiles Bash to Scheme.
> So we must distinguish the support functions that are used at run time,
> such as ‘ls-command-implementation’, from what the Scheme code that the
> compiler emits (compile time).
>
> In the case of ‘ls’, when the compiler encounters ‘ls’ in the input, it
> emits this code:
>
> ((@@ (guix build bournish) ls-command-implementation))
>
> ‘ls-command-implementation’ is the implementation that is called when we
> run the compiled program.
>
> Thus, you must similarly distinguish those two stages by providing:
>
> 1. A ‘wc-command-implementation’ procedure that implements ‘wc’;
>
> 2. A ‘wc-command’ procedure that emits the code that calls
> ‘wc-command-implementation’; so something like:
>
> (define (wc-command args)
> `((@@ (guix build bournish) wc-command-implementation)
> ,@args))
>
> Better yet, ‘wc-command’ could check for the presence of “-l” or
> “-c” at compile time and emit a call to the right thing.
I checked with coreutil's 'wc', and it emits in its particular order
whether you call 'wc -l -c' or 'wc -lc' or 'wc -cl'
>
> HTH!
>
> Ludo’.
--
Efraim Flashner <address@hidden> אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted
signature.asc
Description: PGP signature