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