我刚刚开始研究ABCL,以便在Java语言中混合使用一些Lisp语言。现在,从文件中加载一些Lisp就足够了,我一直在研究examples。在每种情况下,模式都是:
Interpreter interpreter = Interpreter.createInstance();
interpreter.eval("(load \"lispfunctions.lisp\")");但是,假设我正在构建一个Maven项目,以将其打包为JAR:我如何从src/main/resources加载lispfunctions.lisp?我可以很容易地得到一个InputStream-can,我可以带着它去某个地方?或者,对于从这样的资源加载Lisp源代码,我在这里遗漏了另一个习惯用法吗?
发布于 2020-07-06 03:44:42
我已经让以下内容生效了。我正在使用MacOS上的ABCL1.7.0,尽管我非常确定这不是特定于版本的。
/* load_lisp_within_jar.java -- use ABCL to load Lisp file as resource in jar
* copyright 2020 by Robert Dodier
* I release this work under terms of the GNU General Public License
*/
/* To run this example:
$ javac -cp /path/to/abcl.jar -d . load_lisp_within_jar.java
$ cat << EOF > foo.lisp
(defun f (x) (1+ x))
EOF
$ jar cvf load_lisp_within_jar.jar load_lisp_within_jar.class foo.lisp
$ java -cp load_lisp_within_jar.jar:/path/to/abcl.jar load_lisp_within_jar
*
* Expected output:
(F 100) => 101
*/
import org.armedbear.lisp.*;
import java.io.*;
public class load_lisp_within_jar {
public static void main (String [] args) {
try {
// It appears that interpreter instance is required even though
// it isn't used directly; I guess it arranges global resources.
Interpreter I = Interpreter.createInstance ();
LispObject LOAD_function = Symbol.LOAD.getSymbolFunction ();
// Obtain an input stream for Lisp source code in jar.
ClassLoader L = load_lisp_within_jar.class.getClassLoader ();
InputStream f = L.getResourceAsStream ("foo.lisp");
Stream S = new Stream (Symbol.SYSTEM_STREAM, f, Symbol.CHARACTER);
// Call COMMON-LISP:LOAD with input stream as argument.
LOAD_function.execute (S);
// Verify that function F has been defined.
Symbol F = Packages.findPackage ("COMMON-LISP-USER").findAccessibleSymbol ("F");
LispObject F_function = F.getSymbolFunction ();
LispObject x = F_function.execute (LispInteger.getInstance (100));
System.out.println ("(F 100) => " + x.javaInstance ());
}
catch (Exception e) {
System.err.println ("oops: " + e);
e.printStackTrace ();
}
}
}如您所见,程序首先获取与符号加载相关联的函数。(为方便起见,许多通用LISP符号都有静态定义,因此您可以直接使用Symbol.LOAD,而不是通过findAccessibleSymbol查找符号。)然后,输入流被提供给加载函数。然后,我们验证我们的函数F确实是定义的。
我知道这些东西可能有点晦涩难懂;我很乐意尝试回答任何问题。
https://stackoverflow.com/questions/62469847
复制相似问题