* static.el (static-defconst): New function.
[elisp/apel.git] / static.el
1 ;;; static.el --- tools for static evaluation.
2
3 ;; Copyright (C) 1999 Tanaka Akira <akr@jaist.ac.jp>
4
5 ;; Author: Tanaka Akira <akr@jaist.ac.jp>
6 ;; Keywords: byte compile, evaluation
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (put 'static-if 'lisp-indent-function 2)
28 (defmacro static-if (cond then &rest else)
29   "`if' expression but COND is evaluated at compile-time."
30   (if (eval cond)
31       then
32     (` (progn  (,@ else)))))
33
34 (put 'static-when 'lisp-indent-function 1)
35 (defmacro static-when (cond &rest body)
36   "`when' expression but COND is evaluated at compile-time."
37   (if (eval cond)
38       (` (progn (,@ body)))))
39
40 (put 'static-unless 'lisp-indent-function 1)
41 (defmacro static-unless (cond &rest body)
42   "`unless' expression but COND is evaluated at compile-time."
43   (if (eval cond)
44       nil
45     (` (progn (,@ body)))))
46
47 (put 'static-condition-case 'lisp-indent-function 2)
48 (defmacro static-condition-case (var bodyform &rest handlers)
49   "`condition-case' expression but BODYFORM is evaluated at compile-time."
50   (eval (` (condition-case (, var)
51                (list (quote quote) (, bodyform))
52              (,@ (mapcar
53                   (if var
54                       (lambda (h)
55                         (` ((, (car h))
56                             (list (quote funcall)
57                                   (lambda ((, var)) (,@ (cdr h)))
58                                   (list (quote quote) (, var))))))
59                     (lambda (h)
60                       (` ((, (car h)) (quote (progn (,@ (cdr h))))))))
61                   handlers))))))
62
63 (defmacro static-defconst (symbol initvalue docstring)
64   "`defconst' expression but INITVALUE is evaluated at compile-time.
65
66 The variable SYMBOL can be referenced at either compile-time or run-time."
67   (let ((value (eval initvalue)))
68     (eval (` (defconst (, symbol) (quote (, value)) (, docstring))))
69     (` (defconst (, symbol) (quote (, value)) (, docstring)))))
70
71
72 ;;; @ end
73 ;;;
74
75 (provide 'static)
76
77 ;;; static.el ends here