001: /*
002: * Function.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: Function.java,v 1.6 2003/11/15 11:03:32 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: public abstract class Function extends Functional {
025: private final Module module;
026: private final String name;
027: protected final int index;
028:
029: private int callCount;
030:
031: private LispObject arglist;
032:
033: protected Function() {
034: module = null;
035: name = null;
036: index = 0;
037: }
038:
039: public Function(String name) {
040: module = null;
041: this .name = name != null ? name.toUpperCase() : null;
042: index = 0;
043: if (name != null)
044: setLambdaName(Symbol.addFunction(this .name, this ));
045: }
046:
047: public Function(String name, Package pkg) {
048: this (name, pkg, false);
049: }
050:
051: public Function(String name, Package pkg, boolean exported) {
052: this (name, pkg, exported, null, null);
053: }
054:
055: public Function(String name, Package pkg, boolean exported,
056: String arglist, String docstring) {
057: module = null;
058: this .name = name != null ? name.toUpperCase() : null;
059: index = 0;
060: if (arglist instanceof String)
061: this .arglist = new LispString(arglist);
062: if (name != null) {
063: Symbol symbol = pkg.intern(this .name);
064: symbol.setSymbolFunction(this );
065: setLambdaName(symbol);
066: if (exported) {
067: try {
068: pkg.export(symbol);
069: } catch (ConditionThrowable t) {
070: Debug.assertTrue(false);
071: }
072: }
073: if (docstring != null) {
074: try {
075: symbol.setFunctionDocumentation(docstring);
076: } catch (ConditionThrowable t) {
077: Debug.assertTrue(false);
078: }
079: }
080: }
081: }
082:
083: public Function(Module module, String name, int index) {
084: this .module = module;
085: this .name = name.toUpperCase();
086: this .index = index;
087: setLambdaName(Symbol.addFunction(this .name, this ));
088: }
089:
090: public LispObject typeOf() {
091: return Symbol.FUNCTION;
092: }
093:
094: public LispClass classOf() {
095: return BuiltInClass.FUNCTION;
096: }
097:
098: public LispObject typep(LispObject typeSpecifier)
099: throws ConditionThrowable {
100: if (typeSpecifier == Symbol.FUNCTION)
101: return T;
102: if (typeSpecifier == Symbol.COMPILED_FUNCTION)
103: return T;
104: if (typeSpecifier == BuiltInClass.FUNCTION)
105: return T;
106: return super .typep(typeSpecifier);
107: }
108:
109: public final String getName() {
110: return name;
111: }
112:
113: public final LispObject getArglist() {
114: return arglist;
115: }
116:
117: public final void setArglist(LispObject obj) {
118: arglist = obj;
119: }
120:
121: // Primitive
122: public LispObject execute(LispObject[] args)
123: throws ConditionThrowable {
124: if (module != null)
125: return module.dispatch(args, index);
126: throw new ConditionThrowable(
127: new WrongNumberOfArgumentsException(name));
128: }
129:
130: // Primitive1
131: public LispObject execute(LispObject arg) throws ConditionThrowable {
132: if (module != null)
133: return module.dispatch(arg, index);
134: LispObject[] args = new LispObject[1];
135: args[0] = arg;
136: return execute(args);
137: }
138:
139: // Primitive2
140: public LispObject execute(LispObject first, LispObject second)
141: throws ConditionThrowable {
142: if (module != null)
143: return module.dispatch(first, second, index);
144: LispObject[] args = new LispObject[2];
145: args[0] = first;
146: args[1] = second;
147: return execute(args);
148: }
149:
150: public String toString() {
151: StringBuffer sb = new StringBuffer("FUNCTION");
152: if (name != null) {
153: sb.append(' ');
154: sb.append(name);
155: }
156: return unreadableString(sb.toString());
157: }
158:
159: // Profiling.
160: public final int getCallCount() {
161: return callCount;
162: }
163:
164: public void setCallCount(int n) {
165: callCount = n;
166: }
167:
168: public final void incrementCallCount() {
169: ++callCount;
170: }
171: }
|