JSR 223 implementation for Frege can now be downloaded from here. Here is a small program demonstrating Frege script engine.
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"));
}
}
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
5
Hello World, I am foo
1234567890123456794
[0, 1, 1, 2, 3, 5]