1 | /* |
---|
2 | * function_info.java |
---|
3 | * |
---|
4 | * Copyright (C) 2004-2005 Peter Graves |
---|
5 | * $Id: function_info.java 12957 2010-10-08 18:43: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 java.util.Map; |
---|
37 | import java.util.Collections; |
---|
38 | import java.util.WeakHashMap; |
---|
39 | import static org.armedbear.lisp.Lisp.*; |
---|
40 | |
---|
41 | public final class function_info |
---|
42 | { |
---|
43 | // ### TODO: Replace by a concurrent hashmap, with weak keys, ofcourse. |
---|
44 | final static Map<LispObject,LispObject> symbolToFunctionMap = |
---|
45 | Collections.synchronizedMap(new WeakHashMap()); |
---|
46 | |
---|
47 | // ### function-info name |
---|
48 | private static final Primitive FUNCTION_INFO = |
---|
49 | new Primitive("function-info", PACKAGE_SYS, false) |
---|
50 | { |
---|
51 | @Override |
---|
52 | public LispObject execute(LispObject arg) |
---|
53 | { |
---|
54 | LispObject info = symbolToFunctionMap.get(arg); |
---|
55 | return info != null ? info : NIL; |
---|
56 | } |
---|
57 | }; |
---|
58 | |
---|
59 | // ### %set-function-info name info |
---|
60 | private static final Primitive _SET_FUNCTION_INFO = |
---|
61 | new Primitive("%set-function-info", PACKAGE_SYS, false) |
---|
62 | { |
---|
63 | @Override |
---|
64 | public LispObject execute(LispObject name, LispObject info) |
---|
65 | |
---|
66 | { |
---|
67 | if (info == NIL) |
---|
68 | symbolToFunctionMap.remove(name); |
---|
69 | else |
---|
70 | symbolToFunctionMap.put(name, info); |
---|
71 | return info; |
---|
72 | } |
---|
73 | }; |
---|
74 | |
---|
75 | // ### get-function-info-value name indicator => value |
---|
76 | private static final Primitive GET_FUNCTION_INFO_VALUE = |
---|
77 | new Primitive("get-function-info-value", PACKAGE_SYS, true, |
---|
78 | "name indicator") |
---|
79 | { |
---|
80 | @Override |
---|
81 | public LispObject execute(LispObject name, LispObject indicator) |
---|
82 | |
---|
83 | { |
---|
84 | // info is an alist |
---|
85 | LispObject info = symbolToFunctionMap.get(name); |
---|
86 | if (info != null) { |
---|
87 | while (info != NIL) { |
---|
88 | LispObject cons = info.car(); |
---|
89 | if (cons instanceof Cons) { |
---|
90 | if (cons.car().eql(indicator)) { |
---|
91 | // Found it. |
---|
92 | return LispThread.currentThread().setValues(cons.cdr(), T); |
---|
93 | } |
---|
94 | } else if (cons != NIL) |
---|
95 | error(new TypeError(cons, Symbol.LIST)); |
---|
96 | info = info.cdr(); |
---|
97 | } |
---|
98 | } |
---|
99 | return LispThread.currentThread().setValues(NIL, NIL); |
---|
100 | } |
---|
101 | }; |
---|
102 | |
---|
103 | // ### set-function-info-value name indicator value => value |
---|
104 | private static final Primitive SET_FUNCTION_INFO_VALUE = |
---|
105 | new Primitive("set-function-info-value", PACKAGE_SYS, true, |
---|
106 | "name indicator value") |
---|
107 | { |
---|
108 | @Override |
---|
109 | public LispObject execute(LispObject name, LispObject indicator, |
---|
110 | LispObject value) |
---|
111 | |
---|
112 | { |
---|
113 | // info is an alist |
---|
114 | LispObject info = symbolToFunctionMap.get(name); |
---|
115 | if (info == null) |
---|
116 | info = NIL; |
---|
117 | LispObject alist = info; |
---|
118 | while (alist != NIL) { |
---|
119 | LispObject cons = alist.car(); |
---|
120 | if (cons instanceof Cons) { |
---|
121 | if (cons.car().eql(indicator)) { |
---|
122 | // Found it. |
---|
123 | cons.setCdr(value); |
---|
124 | return value; |
---|
125 | } |
---|
126 | } else if (cons != NIL) |
---|
127 | error(new TypeError(cons, Symbol.LIST)); |
---|
128 | alist = alist.cdr(); |
---|
129 | } |
---|
130 | // Not found. |
---|
131 | symbolToFunctionMap.put(name, info.push(new Cons(indicator, value))); |
---|
132 | return value; |
---|
133 | } |
---|
134 | }; |
---|
135 | } |
---|