diff options
author | Daniel Kraft <d@domob.eu> | 2009-07-16 15:23:38 +0200 |
---|---|---|
committer | Daniel Kraft <d@domob.eu> | 2009-07-16 15:23:38 +0200 |
commit | 74c009dadc1e8f580727d2c85bf72ec90e82d15a (patch) | |
tree | 8c75d6c4dc1751149716772b5be4171e2df9b2d7 | |
parent | b6b9d59604aa320cf2bdb7d4934315febab3f6dc (diff) | |
download | guile-74c009dadc1e8f580727d2c85bf72ec90e82d15a.tar.gz |
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.
-rw-r--r-- | module/language/elisp/README | 4 | ||||
-rw-r--r-- | module/language/elisp/compile-tree-il.scm | 34 | ||||
-rw-r--r-- | module/language/elisp/runtime/macro-slot.scm | 27 | ||||
-rw-r--r-- | test-suite/Makefile.am | 1 | ||||
-rw-r--r-- | test-suite/tests/elisp-compiler.test | 13 |
5 files changed, 78 insertions, 1 deletions
diff --git a/module/language/elisp/README b/module/language/elisp/README index 4431bbdb0..5f0b7c81e 100644 --- a/module/language/elisp/README +++ b/module/language/elisp/README @@ -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 diff --git a/module/language/elisp/compile-tree-il.scm b/module/language/elisp/compile-tree-il.scm index 19ca5ae9b..cd0cc7458 100644 --- a/module/language/elisp/compile-tree-il.scm +++ b/module/language/elisp/compile-tree-il.scm @@ -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 diff --git a/module/language/elisp/runtime/macro-slot.scm b/module/language/elisp/runtime/macro-slot.scm new file mode 100644 index 000000000..d17b6f414 --- /dev/null +++ b/module/language/elisp/runtime/macro-slot.scm @@ -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. diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 7bfef16c9..17e5f1bc1 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -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 \ diff --git a/test-suite/tests/elisp-compiler.test b/test-suite/tests/elisp-compiler.test index fdb677195..677f14dc0 100644 --- a/test-suite/tests/elisp-compiler.test +++ b/test-suite/tests/elisp-compiler.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. ; =================== |