1 | /* |
---|
2 | * AutoloadMacro.java |
---|
3 | * |
---|
4 | * Copyright (C) 2003-2004 Peter Graves |
---|
5 | * $Id: AutoloadMacro.java 12513 2010-03-02 22:35:36Z ehuelsmann $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public final class AutoloadMacro extends Autoload |
---|
39 | { |
---|
40 | private AutoloadMacro(Symbol symbol) |
---|
41 | { |
---|
42 | super(symbol); |
---|
43 | } |
---|
44 | |
---|
45 | private AutoloadMacro(Symbol symbol, String fileName) |
---|
46 | { |
---|
47 | super(symbol, fileName, null); |
---|
48 | } |
---|
49 | |
---|
50 | static void installAutoloadMacro(Symbol symbol, String fileName) |
---|
51 | |
---|
52 | { |
---|
53 | AutoloadMacro am = new AutoloadMacro(symbol, fileName); |
---|
54 | if (symbol.getSymbolFunction() instanceof SpecialOperator) |
---|
55 | put(symbol, Symbol.MACROEXPAND_MACRO, am); |
---|
56 | else |
---|
57 | symbol.setSymbolFunction(am); |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public void load() |
---|
62 | { |
---|
63 | Load.loadSystemFile(getFileName(), true); |
---|
64 | } |
---|
65 | |
---|
66 | @Override |
---|
67 | public String writeToString() |
---|
68 | { |
---|
69 | StringBuffer sb = new StringBuffer("#<AUTOLOAD-MACRO "); |
---|
70 | sb.append(getSymbol().writeToString()); |
---|
71 | sb.append(" \""); |
---|
72 | sb.append(getFileName()); |
---|
73 | sb.append("\">"); |
---|
74 | return sb.toString(); |
---|
75 | } |
---|
76 | |
---|
77 | // ### autoload-macro |
---|
78 | private static final Primitive AUTOLOAD_MACRO = |
---|
79 | new Primitive("autoload-macro", PACKAGE_EXT, true) |
---|
80 | { |
---|
81 | @Override |
---|
82 | public LispObject execute(LispObject first) |
---|
83 | { |
---|
84 | if (first instanceof Symbol) { |
---|
85 | Symbol symbol = (Symbol) first; |
---|
86 | installAutoloadMacro(symbol, null); |
---|
87 | return T; |
---|
88 | } |
---|
89 | if (first instanceof Cons) { |
---|
90 | for (LispObject list = first; list != NIL; list = list.cdr()) { |
---|
91 | Symbol symbol = checkSymbol(list.car()); |
---|
92 | installAutoloadMacro(symbol, null); |
---|
93 | } |
---|
94 | return T; |
---|
95 | } |
---|
96 | return error(new TypeError(first)); |
---|
97 | } |
---|
98 | @Override |
---|
99 | public LispObject execute(LispObject first, LispObject second) |
---|
100 | |
---|
101 | { |
---|
102 | final String fileName = second.getStringValue(); |
---|
103 | if (first instanceof Symbol) { |
---|
104 | Symbol symbol = (Symbol) first; |
---|
105 | installAutoloadMacro(symbol, fileName); |
---|
106 | return T; |
---|
107 | } |
---|
108 | if (first instanceof Cons) { |
---|
109 | for (LispObject list = first; list != NIL; list = list.cdr()) { |
---|
110 | Symbol symbol = checkSymbol(list.car()); |
---|
111 | installAutoloadMacro(symbol, fileName); |
---|
112 | } |
---|
113 | return T; |
---|
114 | } |
---|
115 | return error(new TypeError(first)); |
---|
116 | } |
---|
117 | }; |
---|
118 | } |
---|