From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: How the backquote and the comma really work? Date: Fri, 10 Jul 2015 13:36:01 +0200 Message-ID: <87615sxn1a.fsf@mbork.pl> References: <87vbebg1fs.fsf@mbork.pl> <87r3ozy9pf.fsf@web.de> <87si9ffys0.fsf@mbork.pl> <87d20jbqbj.fsf@web.de> <87pp4jfx9y.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1436528200 20763 80.91.229.3 (10 Jul 2015 11:36:40 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 10 Jul 2015 11:36:40 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jul 10 13:36:34 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZDWbZ-0003Hg-6A for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Jul 2015 13:36:33 +0200 Original-Received: from localhost ([::1]:44173 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZDWbY-0001d4-Fv for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Jul 2015 07:36:32 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59034) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZDWbM-0001cy-6a for help-gnu-emacs@gnu.org; Fri, 10 Jul 2015 07:36:21 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZDWbH-00076r-Rf for help-gnu-emacs@gnu.org; Fri, 10 Jul 2015 07:36:19 -0400 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:33889) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZDWbH-00076I-F0 for help-gnu-emacs@gnu.org; Fri, 10 Jul 2015 07:36:15 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 34B514FA03B for ; Fri, 10 Jul 2015 13:36:12 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 39US0aZreciL for ; Fri, 10 Jul 2015 13:36:10 +0200 (CEST) Original-Received: from localhost (unknown [109.232.24.146]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 022044F607B for ; Fri, 10 Jul 2015 13:36:08 +0200 (CEST) In-reply-to: <87pp4jfx9y.fsf@mbork.pl> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:5e00:2:52::8 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105577 Archived-At: On 2015-06-25, at 20:39, Marcin Borkowski wrote: > On 2015-06-25, at 20:22, Michael Heerdegen wrote: > >> Marcin Borkowski 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