1

github 上的 frege-scripting 项目包含 JSR223 所需的 ScriptEngineFactory,但它似乎既没有打包在 Frege 语言 jar 本身中,也没有打包在 REPL 或其任何依赖项中。

某处有这样的罐子还是需要额外的构建?

4

1 回答 1

2

现在可以从这里下载 Frege 的 JSR 223 实现。这是一个演示 Frege 脚本引擎的小程序。

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.math.BigInteger;

public class FregeScriptEngineTest {

    public static void main(final String[] args) throws Exception {
        //Get the Frege Script Engine
        final ScriptEngineManager factory = new ScriptEngineManager();
        final ScriptEngine engine = factory.getEngineByName("frege");

        //Evaluate an expression
        System.out.println(engine.eval("show $ take 10 [2,4..]"));

        //Bind a value
        engine.eval("x=5");
        System.out.println(engine.eval("x"));

        //Pass some objects from host to scripting environment
        engine.put("foo::String", "I am foo");
        engine.put("bar::Integer", new BigInteger("1234567890123456789"));

        //Use the objects from host environment
        System.out.println(engine.eval("\"Hello World, \" ++ foo"));
        System.out.println(engine.eval("bar + big 5"));

        /*
         * Frege Script Engine is `Compilable` too. So scripts can be compiled
         * and then executed later.
         */
        final Compilable compilableEngine = (Compilable) engine;
        final CompiledScript compiled =
                compilableEngine.compile("fib = 0 : 1 : zipWith (+) fib fib.tail");
        compiled.eval(); //Evaluate the compiled script
        //use compiled script
        System.out.println(engine.eval("show $ take 6 fib"));
    }

}

输出:

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
5
Hello World, I am foo
1234567890123456794
[0, 1, 1, 2, 3, 5]
于 2013-10-06T06:57:39.430 回答