001: /*
002: * Function.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: Function.java,v 1.38 2004/07/09 17:38:29 piso 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 Symbol symbol;
026:
027: private int callCount;
028:
029: protected Function() {
030: symbol = null;
031: }
032:
033: public Function(String name) {
034: if (name != null) {
035: symbol = Symbol.addFunction(name.toUpperCase(), this );
036: if (cold)
037: symbol.setBuiltInFunction(true);
038: setLambdaName(symbol);
039: } else
040: symbol = null;
041: }
042:
043: public Function(String name, String arglist) {
044: this (name);
045: setArglist(new SimpleString(arglist));
046: }
047:
048: public Function(String name, Package pkg) {
049: this (name, pkg, false);
050: }
051:
052: public Function(String name, Package pkg, boolean exported) {
053: this (name, pkg, exported, null, null);
054: }
055:
056: public Function(String name, Package pkg, boolean exported,
057: String arglist) {
058: this (name, pkg, exported, arglist, null);
059: }
060:
061: public Function(String name, Package pkg, boolean exported,
062: String arglist, String docstring) {
063: if (arglist instanceof String)
064: setArglist(new SimpleString(arglist));
065: if (name != null) {
066: symbol = pkg.intern(name.toUpperCase());
067: symbol.setSymbolFunction(this );
068: if (cold)
069: symbol.setBuiltInFunction(true);
070: setLambdaName(symbol);
071: if (exported) {
072: try {
073: pkg.export(symbol);
074: } catch (ConditionThrowable t) {
075: Debug.assertTrue(false);
076: }
077: }
078: if (docstring != null) {
079: try {
080: symbol.setFunctionDocumentation(docstring);
081: } catch (ConditionThrowable t) {
082: Debug.assertTrue(false);
083: }
084: }
085: } else
086: symbol = null;
087: }
088:
089: public Function(Symbol symbol) {
090: this .symbol = symbol;
091: }
092:
093: public LispObject typeOf() {
094: return Symbol.FUNCTION;
095: }
096:
097: public LispClass classOf() {
098: return BuiltInClass.FUNCTION;
099: }
100:
101: public LispObject typep(LispObject typeSpecifier)
102: throws ConditionThrowable {
103: if (typeSpecifier == Symbol.FUNCTION)
104: return T;
105: if (typeSpecifier == Symbol.COMPILED_FUNCTION)
106: return T;
107: if (typeSpecifier == BuiltInClass.FUNCTION)
108: return T;
109: return super .typep(typeSpecifier);
110: }
111:
112: public Symbol getSymbol() {
113: return symbol;
114: }
115:
116: public final String getName() {
117: return symbol != null ? symbol.getName() : null;
118: }
119:
120: // Primitive0
121: public LispObject execute() throws ConditionThrowable {
122: return signal(new WrongNumberOfArgumentsException(this ));
123: }
124:
125: // Primitive1
126: public LispObject execute(LispObject arg) throws ConditionThrowable {
127: return signal(new WrongNumberOfArgumentsException(this ));
128: }
129:
130: // Primitive2
131: public LispObject execute(LispObject first, LispObject second)
132: throws ConditionThrowable {
133: return signal(new WrongNumberOfArgumentsException(this ));
134: }
135:
136: // Primitive3
137: public LispObject execute(LispObject first, LispObject second,
138: LispObject third) throws ConditionThrowable {
139: return signal(new WrongNumberOfArgumentsException(this ));
140: }
141:
142: // Primitive4
143: public LispObject execute(LispObject first, LispObject second,
144: LispObject third, LispObject fourth)
145: throws ConditionThrowable {
146: return signal(new WrongNumberOfArgumentsException(this ));
147: }
148:
149: // Primitive
150: public LispObject execute(LispObject[] args)
151: throws ConditionThrowable {
152: return signal(new WrongNumberOfArgumentsException(this ));
153: }
154:
155: public String writeToString() throws ConditionThrowable {
156: LispObject name = getLambdaName();
157: if (name == null)
158: name = getSymbol();
159: if (name != null) {
160: StringBuffer sb = new StringBuffer("#<FUNCTION");
161: sb.append(' ');
162: sb.append(name.writeToString());
163: sb.append('>');
164: return sb.toString();
165: } else
166: return unreadableString("FUNCTION");
167: }
168:
169: // Profiling.
170: public final int getCallCount() {
171: return callCount;
172: }
173:
174: public void setCallCount(int n) {
175: callCount = n;
176: }
177:
178: public final void incrementCallCount() {
179: ++callCount;
180: }
181: }
|