[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How the backquote and the comma really work?
From: |
Marcin Borkowski |
Subject: |
Re: How the backquote and the comma really work? |
Date: |
Sun, 12 Jul 2015 22:33:37 +0200 |
On 2015-07-12, at 21:55, Marcin Borkowski <mbork@mbork.pl> wrote:
> On 2015-07-12, at 17:54, Michael Heerdegen <michael_heerdegen@web.de> wrote:
>
>> There is a problem though when the read expression is nested. I tried
>> to `mci/read' this string for example:
>>
>> "(defun fac (x) (if (< 2 x) 1 (* x (fac (1- x)))))"
>>
>> and got
>>
>> (defun fac :open-paren x)
>>
>> as result. If you Edebug your functions, you can see what goes wrong.
>> Please tell me if you need more hints...
>
> Good catch, thanks. No need for edebug, I guess – I haven’t looked at
> my code, but I guess I know the problem already. Stupid me.
So, what about this? It seems to work. OTOH, I think it's not the most
elegant thing possible, since there is some code duplication: mci/read
has this: (:open-paren (mci/read-list-contents)) in a (a)case statement,
and mci/read-list-contents has this: (:open-paren (setq next
(mci/read-list-contents))). Something tells my mathematical mind that
there probably exists a cleaner approach.
--8<---------------cut here---------------start------------->8---
(require 'anaphora) ; we'll need acase
;; Reader
(defun mci/next-token ()
"Get the next token from the current buffer at point position.
A token can be: an integer, a symbol, a parenthesis, a comma,
a backquote or a quote. Return a number (in case of an integer),
a symbol (in case of a symbol), or one of the symbols: :open-paren,
:close-paren, :quote, :quasi-quote, :unquote, :eob. (Of course, if
someone is devious enough to include one of these symbols in the
expression being read, he'll get what he deserves: a chaos.)"
(skip-chars-forward " \t\n")
(cond ((eq (char-after) ?\()
(forward-char)
:open-paren)
((eq (char-after) ?\))
(forward-char)
:close-paren)
((eq (char-after) ?\')
(forward-char)
:quote)
((eq (char-after) ?\`)
(forward-char)
:quasi-quote)
((eq (char-after) ?\,)
(forward-char)
:unquote)
((looking-at "\\([-+]?[[:digit:]]+\\)[ \t\n)]")
(skip-chars-forward "[:digit:]")
(string-to-number (match-string 1)))
((looking-at "[^ \t\n)]+")
(goto-char (match-end 0))
(intern (match-string-no-properties 0)))
((eobp)
:eob)))
(defun mci/read ()
"Read one Elisp expression from the buffer at point."
(acase (mci/next-token)
(:open-paren (mci/read-list-contents))
(:close-paren
(error "Unexpected closing paren at line %d encountered -- mci/read"
(line-number-at-pos)))
(:quote (list 'quote (mci/read)))
(:quasi-quote (list 'quasi-quote (mci/read)))
(:unquote (list 'unquote (mci/read)))
(:eob nil)
(t it)))
(defun mci/read-list-contents ()
"Read list contents (until the closing paren), gobble the
closing paren."
(let ((next (mci/next-token))
list)
(while (not (eq next :close-paren))
(case next
(:open-paren (setq next (mci/read-list-contents)))
(:eob (error "Unexpected EOB while reading a list --
mci/read-list-contents"))
(t (push next list)
(setq next (mci/next-token)))))
(nreverse list)))
--8<---------------cut here---------------end--------------->8---
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University