From: David E. Young
Subject: Embedded language and the Lisp reader
Date: 
Message-ID: <921900761.637627044@news.mindspring.com>
Greetings. I'm building an embedded language and need some assistance
modifying the behavior of the Lisp reader (I think).

Among the new language's constructs are forms like the following:

  (b ?b:=(schtum))

Unfortunately, the reader fails on forms like this because it's
interpreting '?b' as a package name (note here that 'defrule' is a
macro I've written):

  LESS(23): (defrule foo (boris (b ?b:=(schtum))) => (halt))

  Error: Package "?B" not found.
    [condition type: READER-ERROR]

If my analysis is correct, my first inclination is to adjust the Lisp
reader such that tokens like '<symbol>:' aren't interpreted as package
specifiers. I suppose I could do this lexically within the 'defrule'
macro, but since my language can also be used interactively from
within Lisp, and since the language must also support Lisp syntax, I
don't see how changing the reader can work.

Perhaps I should just choose a character other that ':', but I'd like
to hear some suggestions first. Staying with ':' would maintain some
compatibility with an existing language, but that factor isn't a hard
and fast requirement. If changing the reader *is* appropriate, an
example or two would be very helpful. Thanks for your time.

FWIW, I'm developing on ACL 5.0 (Linux).

Regards,
--
-----------------------------------------------------------------
David E. Young
Fujitsu Network Communications  "The fact that ... we still
(···········@fnc.fujitsu.com)       live well cannot ease the pain of
                                                 feeling that we no longer live nobly." 
                                                  -- John Updike
"Programming should be fun,
 programs should be beautiful"
  -- P. Graham

From: Stig Hemmer
Subject: Re: Embedded language and the Lisp reader
Date: 
Message-ID: <ekv90cspjl6.fsf@gnoll.pvv.ntnu.no>
David E. Young <········@computer.org> writes:
>   (b ?b:=(schtum))
[...]
>   LESS(23): (defrule foo (boris (b ?b:=(schtum))) => (halt))

If the odd contructs always start with ? you can change the readtable
entry for that character to do the appropriate magic.

(Sorry, no examples)

Stig Hemmer,
Jack of a Few Trades.
From: David E. Young
Subject: Re: Embedded language and the Lisp reader
Date: 
Message-ID: <921971541.884398889@news.mindspring.com>
On Sat, 20 Mar 1999, Stig Hemmer wrote:
>David E. Young <········@computer.org> writes:
>>   (b ?b:=(schtum))
>[...]
>>   LESS(23): (defrule foo (boris (b ?b:=(schtum))) => (halt))
>
>If the odd contructs always start with ? you can change the readtable
>entry for that character to do the appropriate magic.

Yes, I suspected that might be an approach.

>
>(Sorry, no examples)

Ah well. Thanks anyway.

>
>Stig Hemmer,
>Jack of a Few Trades.

Regards,

--
-----------------------------------------------------------------
David E. Young
Fujitsu Network Communications  "The fact that ... we still
(···········@fnc.fujitsu.com)       live well cannot ease the pain of
                                                 feeling that we no longer live nobly."
                                                  -- John Updike
"Programming should be fun,
 programs should be beautiful"
  -- P. Graham
From: Steve Gonedes
Subject: Re: Embedded language and the Lisp reader
Date: 
Message-ID: <m2iubv5qzy.fsf@KludgeUnix.com>
David E. Young <········@computer.org> writes:

< Greetings. I'm building an embedded language and need some assistance
< modifying the behavior of the Lisp reader (I think).
<
< Among the new language's constructs are forms like the following:
<
<   (b ?b:=(schtum))
<
< Unfortunately, the reader fails on forms like this because it's
< interpreting '?b' as a package name (note here that 'defrule' is a
< macro I've written):
<
<   LESS(23): (defrule foo (boris (b ?b:=(schtum))) => (halt))
<
<   Error: Package "?B" not found.
<     [condition type: READER-ERROR]
<
< If my analysis is correct, my first inclination is to adjust the Lisp
< reader such that tokens like '<symbol>:' aren't interpreted as package
< specifiers. I suppose I could do this lexically within the 'defrule'
< macro, but since my language can also be used interactively from
< within Lisp, and since the language must also support Lisp syntax, I
< don't see how changing the reader can work.
<
< Perhaps I should just choose a character other that ':', but I'd like
< to hear some suggestions first. Staying with ':' would maintain some
< compatibility with an existing language, but that factor isn't a hard
< and fast requirement. If changing the reader *is* appropriate, an
< example or two would be very helpful. Thanks for your time.
<
< FWIW, I'm developing on ACL 5.0 (Linux).

This really depends on what you're planning to do with the
'<symbol>:', here's an example of returning it as a string.

(defvar *question-readtable*
  (let ((new-readtable (copy-readtable)))
    (set-macro-character #\?
      #'(lambda (stream char)
          (declare (ignore char))
          (with-output-to-string (buffer)
            (loop never (char= (peek-char nil stream) #\=)
                do (write-char (read-char stream) buffer))))
      nil new-readtable)
    new-readtable))

You can now do

(setq *readtable* *question-readtable*)

Lisp> ?variable:= 5
=> Error: unbound variable `='.

Dunno what you want to do with the equal sign. You can intern the
string with the colon and get symbols like `|?X:|'. Then again, you
could just toss it.

Also I don't know how well lexically bindinga readtable will work. You
can use `excl:named-readtable' in combination with
`excl:with-named-readtable' but I don't see how this could work for
the repl since the entire sexp is probably being read all at one time.

Another example you could put at the top of your rule file.

(eval-when (:compile-toplevel :load-toplevel :execute)
  (setq *readtable*
    (let ((new-readtable (copy-readtable)))
      (set-macro-character #\?
        #'(lambda (stream char)
            (declare (ignore char))
            `'(,(intern
                  (nstring-upcase
                    (with-output-to-string (buffer)
                       (loop for ch = (read-char stream)
                           until (char= ch #\:)
                           do (write-char ch buffer)))))
                   ;; There are better ways to do this than using
                   ;; read-from-string, just an example.
                  ,(read-from-string (string (read-char stream)))
                  ,(read stream)))
        nil new-readtable)
      new-readtable)))

Lisp> ?this:=12
=> (THIS = 12)

Hope this helps some...
From: David E. Young
Subject: Re: Embedded language and the Lisp reader
Date: 
Message-ID: <922038843.1256474358@news.mindspring.com>
On Sun, 21 Mar 1999, Steve Gonedes wrote:
>David E. Young <········@computer.org> writes:
>
>< Greetings. I'm building an embedded language and need some assistance
>< modifying the behavior of the Lisp reader (I think)....

>This really depends on what you're planning to do with the
>'<symbol>:', here's an example of returning it as a string...
>
>Dunno what you want to do with the equal sign. You can intern the
>string with the colon and get symbols like `|?X:|'. Then again, you
>could just toss it...
>
>Hope this helps some...

Indeed it does. Thanks very much, Steve.

Regards,

--
-----------------------------------------------------------------
David E. Young
Fujitsu Network Communications  "The fact that ... we still
(···········@fnc.fujitsu.com)       live well cannot ease the pain of
                                                 feeling that we no longer live nobly."  
                                                  -- John Updike
"Programming should be fun,
 programs should be beautiful"
  -- P. Graham