1

The frege-scripting project on github contains the ScriptEngineFactory as required for JSR223 but it appears that is neither packaged in the Frege language jar itself nor in the REPL or any of its dependencies.

Is there such a jar somewhere or does it require an extra build?

Dierk
  • 1,308
  • 7
  • 13
  • 1
    Unfortunately the jar is not made available as a github release so far. I was supposed to bundle it along with REPL but somehow missed it. For now, the only option is to build one from the source. I will soon upload the jar once the REPL changes for the latest Frege are done. – Marimuthu Madasamy Sep 27 '13 at 23:54
  • ok, thanks! Please notify, when it is available ;-) – Dierk Sep 30 '13 at 02:59
  • It is now available, @Dierk – Ingo Oct 06 '13 at 09:28

1 Answers1

2

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]
Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • Groovy integration looks clean! One thing that is not yet implemented is that the engine is not currently `Invokable` and we can also add more of REPL's features to the script engine like type of an expression. – Marimuthu Madasamy Oct 07 '13 at 14:17
  • @MarimuthuMadasamy, how would I pre-load a module in Frege so that a function it provides could be called repeatedly? And, the JAR you provide on GitHub is from 2014 and it says there that it uses an older version of Frege; will it work with the latest Frege version? If not, is this something I can take care of myself? – 0dB Feb 25 '16 at 08:36
  • @0dB I just uploaded a new release [1.3-SNAPSHOT](https://github.com/Frege/frege-interpreter/releases/tag/1.3-SNAPSHOT) and posted to the Frege mailing list describing the script engine. I think it answers your question about module as well. You might want to take a look at it [here](https://groups.google.com/forum/#!msg/frege-programming-language/Oxs6isJR6d0/KkNSYh-gAwAJ). – Marimuthu Madasamy Feb 25 '16 at 09:13