001: /*
002: * CompiledFunction.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: CompiledFunction.java,v 1.6 2003/11/15 11:03:31 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 class CompiledFunction extends Closure {
025: public CompiledFunction(String name, LispObject lambdaList,
026: LispObject body, Environment env) throws ConditionThrowable {
027: super (name, lambdaList, body, env);
028: }
029:
030: public LispObject typep(LispObject typeSpecifier)
031: throws ConditionThrowable {
032: if (typeSpecifier == Symbol.COMPILED_FUNCTION)
033: return T;
034: return super .typep(typeSpecifier);
035: }
036:
037: public LispObject execute(LispObject arg) throws ConditionThrowable {
038: LispObject[] args = new LispObject[1];
039: args[0] = arg;
040: return execute(args);
041: }
042:
043: public LispObject execute(LispObject first, LispObject second)
044: throws ConditionThrowable {
045: LispObject[] args = new LispObject[2];
046: args[0] = first;
047: args[1] = second;
048: return execute(args);
049: }
050:
051: public LispObject execute(LispObject first, LispObject second,
052: LispObject third) throws ConditionThrowable {
053: LispObject[] args = new LispObject[3];
054: args[0] = first;
055: args[1] = second;
056: args[2] = third;
057: return execute(args);
058: }
059:
060: public LispObject execute(LispObject[] args)
061: throws ConditionThrowable {
062: throw new ConditionThrowable(new LispError("not implemented"));
063: }
064:
065: public String toString() {
066: return unreadableString("COMPILED-FUNCTION");
067: }
068:
069: // ### make-compiled-function
070: // make-compiled-function name lambda-list body => object
071: private static final Primitive3 MAKE_COMPILED_FUNCTION = new Primitive3(
072: "make-compiled-function", PACKAGE_SYS, false) {
073: public LispObject execute(LispObject first, LispObject second,
074: LispObject third) throws ConditionThrowable {
075: String name;
076: if (first == NIL)
077: name = null;
078: else
079: name = LispString.getValue(first);
080: LispObject lambdaList = second;
081: LispObject body = third;
082: return new CompiledFunction(name, lambdaList, body,
083: new Environment());
084: }
085: };
086:
087: // ### load-compiled-function
088: private static final Primitive1 LOAD_COMPILED_FUNCTION = new Primitive1(
089: "load-compiled-function", PACKAGE_SYS, false) {
090: public LispObject execute(LispObject arg)
091: throws ConditionThrowable {
092: String className = ((LispString) arg).getValue();
093: if (className.endsWith(".class")) {
094: try {
095: JavaClassLoader loader = new JavaClassLoader();
096: Class c = loader.loadClassFromFile(className);
097: if (c != null) {
098: Class[] parameterTypes = new Class[0];
099: java.lang.reflect.Constructor constructor = c
100: .getConstructor(parameterTypes);
101: Object[] initargs = new Object[0];
102: LispObject obj = (LispObject) constructor
103: .newInstance(initargs);
104: return obj;
105: }
106: } catch (VerifyError e) {
107: throw new ConditionThrowable(new LispError(
108: "class verification failed: "
109: + e.getMessage()));
110: } catch (Throwable t) {
111: Debug.trace(t);
112: }
113: }
114: throw new ConditionThrowable(new LispError(
115: "unable to load ".concat(className)));
116: }
117: };
118:
119: private static final Primitive1 VARLIST = new Primitive1("varlist",
120: PACKAGE_SYS, false) {
121: public LispObject execute(LispObject arg)
122: throws ConditionThrowable {
123: if (arg instanceof CompiledFunction)
124: return ((CompiledFunction) arg).getVariableList();
125: throw new ConditionThrowable(new TypeError(arg,
126: "compiled function"));
127: }
128: };
129: }
|