help-gnu-emacs
[Top][All Lists]
Advanced

[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: Fri, 10 Jul 2015 13:36:01 +0200

On 2015-06-25, at 20:39, Marcin Borkowski <mbork@mbork.pl> wrote:

> On 2015-06-25, at 20:22, Michael Heerdegen <michael_heerdegen@web.de> wrote:
>
>> Marcin Borkowski <mbork@mbork.pl> writes:
>>
>>> Seeing a simplistic (though working in typical/correct cases) version
>>> might be rather illuminating, no?
>>
>> Yes.  Want to give it a try?
>
> Sure.  I'll get back here with some code (notice: it might make some
> time, from a week to a few months - it's not the only thing I have to
> do;-)) to discuss.

OK, so -- as I said -- I'm back.  I don't have my metacircular
interpreter (yet), and I want to make it rather a simplistic one (no
assignments, for instance -- just evaluating functions, conditionals and
(maybe) while loops), but I concentrated on the reader to start with.
So here's my humble attempt at the reader itself.  It does nothing with
ticks, backticks and commas -- AFAIUC, it shouldn't be done at this
level anyway -- it just translates them to special forms (quote ...),
(quasi-quote ...) and (unquote ...).  Do I get it correctly that it's
the eval function which should handle these?

--8<---------------cut here---------------start------------->8---
;; A simple metacircular interpreter for (a subset of) Emacs Lisp

(require 'anaphora)                     ; we'll use acase

(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."
  (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))
      (if (eq next :eob)
          (error "Unexpected EOB while reading a list -- 
mci/read-list-contents")
        (push next list)
        (setq next (mci/next-token))))
    (nreverse list)))
--8<---------------cut here---------------end--------------->8---

I'd be thankful for any input, either on correctness of the above code,
or on its elegance and `lispy-ness', or on ways to make it better for
novices to understand.

TIA for your help!  (And I'm feeling a bit guilty that I ask a fair
share of simple questions -- but my mission here is to try to understand
this stuff as well as I can, and then write about it, so that it will be
easier for others to `get it' -- so that makes my conscience easier;-).)

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



reply via email to

[Prev in Thread] Current Thread [Next in Thread]