mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-10 14:00:21 +02:00
Implemented macros in elisp compiler.
* module/language/elisp/README: Document it. * module/language/elisp/compile-tree-il.scm: Implement defmacro and expansion. * module/language/elisp/runtime/macro-slot.scm: New module to keep definitions. * test-suite/Makefile.am: Add elisp-compiler.test to list of tests. * test-suite/tests/elisp-compiler.test: Basic macro tests.
This commit is contained in:
parent
b6b9d59604
commit
74c009dadc
5 changed files with 78 additions and 1 deletions
|
@ -14,6 +14,7 @@ Already implemented:
|
|||
* lambda expressions, function calls using list notation
|
||||
* some built-ins (mainly numbers/arithmetic)
|
||||
* defconst, defvar, defun
|
||||
* macros
|
||||
|
||||
Especially still missing:
|
||||
* other progX forms, will be done in macros
|
||||
|
@ -22,7 +23,6 @@ Especially still missing:
|
|||
* catch/throw, unwind-protect
|
||||
* real elisp reader instead of Scheme's
|
||||
* set, makunbound, boundp functions
|
||||
* macros
|
||||
* more general built-ins
|
||||
* funcall and apply functions
|
||||
* fset & friends, defalias functions
|
||||
|
@ -30,3 +30,5 @@ Especially still missing:
|
|||
* defsubst and inlining
|
||||
* real quoting
|
||||
* need fluids for function bindings?
|
||||
* recursive macros
|
||||
* anonymous macros
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
(define-module (language elisp compile-tree-il)
|
||||
#:use-module (language tree-il)
|
||||
#:use-module (system base pmatch)
|
||||
#:use-module (system base compile)
|
||||
#:export (compile-tree-il))
|
||||
|
||||
|
||||
|
@ -46,6 +47,7 @@
|
|||
(define runtime '(language elisp runtime))
|
||||
(define value-slot '(language elisp runtime value-slot))
|
||||
(define function-slot '(language elisp runtime function-slot))
|
||||
(define macro-slot '(language elisp runtime macro-slot))
|
||||
|
||||
|
||||
; Build a call to a primitive procedure nicely.
|
||||
|
@ -282,6 +284,23 @@
|
|||
(else #t)))
|
||||
|
||||
|
||||
; Handle macro bindings.
|
||||
|
||||
(define (is-macro? sym)
|
||||
(module-defined? (resolve-interface macro-slot) sym))
|
||||
|
||||
(define (define-macro! loc sym definition)
|
||||
(let ((resolved (resolve-module macro-slot)))
|
||||
(if (is-macro? sym)
|
||||
(report-error loc "macro is already defined" sym)
|
||||
(begin
|
||||
(module-define! resolved sym definition)
|
||||
(module-export! resolved (list sym))))))
|
||||
|
||||
(define (get-macro sym)
|
||||
(module-ref (resolve-module macro-slot) sym))
|
||||
|
||||
|
||||
; Compile a symbol expression. This is a variable reference or maybe some
|
||||
; special value like nil.
|
||||
|
||||
|
@ -470,9 +489,24 @@
|
|||
(compile-lambda loc args body))
|
||||
(make-const loc name)))))
|
||||
|
||||
; Define a macro (this is done directly at compile-time!).
|
||||
; FIXME: Recursive macros don't work!
|
||||
((defmacro ,name ,args . ,body)
|
||||
(if (not (symbol? name))
|
||||
(error "expected symbol as macro name" name)
|
||||
(let* ((tree-il (compile-lambda loc args body))
|
||||
(object (compile tree-il #:from 'tree-il #:to 'value)))
|
||||
(define-macro! loc name object)
|
||||
(make-const loc name))))
|
||||
|
||||
(('quote ,val)
|
||||
(make-const loc val))
|
||||
|
||||
; Macro calls are simply expanded and recursively compiled.
|
||||
((,macro . ,args) (guard (and (symbol? macro) (is-macro? macro)))
|
||||
(let ((expander (get-macro macro)))
|
||||
(compile-expr (apply expander args))))
|
||||
|
||||
; Function calls using (function args) standard notation; here, we have to
|
||||
; take the function value of a symbol if it is one. It seems that functions
|
||||
; in form of uncompiled lists are not supported in this syntax, so we don't
|
||||
|
|
27
module/language/elisp/runtime/macro-slot.scm
Normal file
27
module/language/elisp/runtime/macro-slot.scm
Normal file
|
@ -0,0 +1,27 @@
|
|||
;;; Guile Emac Lisp
|
||||
|
||||
;; Copyright (C) 2001 Free Software Foundation, Inc.
|
||||
|
||||
;; This program is free software; you can redistribute it and/or modify
|
||||
;; it under the terms of the GNU General Public License as published by
|
||||
;; the Free Software Foundation; either version 2, or (at your option)
|
||||
;; any later version.
|
||||
;;
|
||||
;; This program is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
;;
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program; see the file COPYING. If not, write to
|
||||
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
;; Boston, MA 02111-1307, USA.
|
||||
|
||||
;;; Code:
|
||||
|
||||
(define-module (language elisp runtime macro-slot))
|
||||
|
||||
; This module contains the macro definitions of elisp symbols. In contrast to
|
||||
; the other runtime modules, those are used directly during compilation, of
|
||||
; course, so not really in runtime. But I think it fits well to the others
|
||||
; here.
|
|
@ -32,6 +32,7 @@ SCM_TESTS = tests/alist.test \
|
|||
tests/common-list.test \
|
||||
tests/continuations.test \
|
||||
tests/elisp.test \
|
||||
tests/elisp-compiler.text \
|
||||
tests/environments.test \
|
||||
tests/eval.test \
|
||||
tests/exceptions.test \
|
||||
|
|
|
@ -211,6 +211,19 @@
|
|||
(zerop a)))))
|
||||
|
||||
|
||||
; Macros.
|
||||
; =======
|
||||
|
||||
(with-test-prefix/compile "Macros"
|
||||
|
||||
(pass-if-equal "defmacro value" 'magic-number
|
||||
(defmacro magic-number () 42))
|
||||
|
||||
(pass-if-equal "macro expansion" 1
|
||||
(progn (defmacro take-first (a b) a)
|
||||
(take-first 1 (/ 1 0)))))
|
||||
|
||||
|
||||
; Test the built-ins.
|
||||
; ===================
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue