Skip to content

Instantly share code, notes, and snippets.

@mmhelloworld
Created June 28, 2013 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmhelloworld/5882797 to your computer and use it in GitHub Desktop.
Save mmhelloworld/5882797 to your computer and use it in GitHub Desktop.
JSR 223 - Scripting support for Frege
package helloworld;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
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..]"));
//Pass some objects from host to scripting environment
engine.put("foo::String", "Foo");
engine.put("bar::Integer", new java.math.BigInteger("12234234232322"));
//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
System.out.println(engine.eval("show $ take 6 fib")); //use compiled script
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment