scsh-users
[Top] [All Lists]

Re: 0.3 awk macro question

To: scsh@martigny.ai.mit.edu
Subject: Re: 0.3 awk macro question
From: shivers@lcs.mit.edu (Olin Shivers)
Date: 13 Oct 1995 04:33:22 GMT
Organization: Lab for Computer Science, MIT
Reply-to: shivers@lcs.mit.edu
   From: glenn@mathcs.emory.EDU (Glenn Barry)
   Newsgroups: alt.lang.scheme.scsh
   Date: 12 Oct 1995 02:17:11 -0400

   Got a question about the awk macro in version 0.3:  Why does

   (define (lockd-pid)
     (awk (read-line) (line) ()
          ("lockd"  (display  (car  ((field-splitter ) line))))))

   (run (| (ps -ae) ; solaris2 (sysVR4) ps
           (begin (lockd-pid))))
   (newline)

   work fine but

   (define proc "lockd")

   (define (lockd-pid-bad)
     (awk (read-line) (line) ()
          (proc  (display  (car  ((field-splitter ) line))))))

   (run (| (ps -ae) ; solaris2 (sysVR4) ps
           (begin (lockd-pid-bad))))
   (newline)

   pg 86-87 of the man says the awk test form can be an integer or string
   or expression but does this mean it can't be a var with a string value?

Glenn-

You wrote the answer to your question in that last sentence above. The test
form must be *syntactically* a string to get the special-case regexp
interpretation. PROC is, grammatically speaking, a *variable*, which for the
purposes of the AWK macro falls under the "general Scheme expression"
heading. So it is simply evaluated for a boolean result (which is always true
in this case, since PROC's value, "lockd" is a true value).

If you want to do it this way, go ahead and write the real Scheme expression
for the test you want (i.e., a STRING-MATCH application):

       (awk (read-line) (line) ()
         ((string-match proc line)
          (display  (car ((field-splitter) line))))
          
I would also suggest pulling out the loop invariant (field-splitter)
as follows:

     (let ((split-line (field-splitter)))
       (awk (read-line) (line) ()
         ((string-match proc line)
          (display (car (split line))))))

    Is this a general "macro thang"?

No, I actually had to do gross low-level macro hackery to be able
to make awk's macro expander do different things depending
on whether a form was a numeral, string, or other. The R4RS high-level
macros won't let you do this kind of thing. So you'd have to do some
work to port my implementation of the AWK macro to another Scheme
implementation.

On the other hand, it *is* only about 250 lines of code. Add in another
250 lines of code for the field-parser library, and you still are a long
way from the 22,000 lines of code needed for the C Awk. Less code but
it does more. That's the point of advanced languages.
        -Olin

<Prev in Thread] Current Thread [Next in Thread>