// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class EmbeddedScript {
    public static void test(String language, String source) throws ScriptException {
        System.out.println(language + ":");
        System.out.print(source);
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine se = sem.getEngineByName(language);
        se.eval(source);
    }
    public static void main(String[] args) throws ScriptException {
        test("javascript", "for(i = 0; i < 3; i++) {\r\n" +
                           "    print(\"Hi from JavaScript!\")\r\n" +
                           "}\r\n");
        test("python", "for i in range(3):\r\n" +
                       "    print(\"Hi from Python!\")\r\n");
        test("ruby", "for i in 1..3 do\r\n" +
                     "    puts 'Hi from Ruby!'\r\n" +
                     "end\r\n");
        test("php", "<?php\r\n" +
                    "for($i = 0; $i < 3; $i++) {\r\n" +
                    "    echo \"Hi from PHP!\\r\\n\";\r\n" +
                    "}\r\n" +
                    "?>\r\n");
        test("groovy", "for(i in 1..3) {\r\n" +
                       "    println \"Hi from Groovy!\"\r\n" +
                       "}\r\n");
        test("beanshell", "for(int i = 0; i < 3; i++) {\r\n" +
                          "    System.out.println(\"Hi from BeanShell!\");\r\n"+
                          "}\r\n");
        test("lua", "for i = 1,3\r\n" +
                    "do\r\n" +
                    "    print(\"Hi from Lua!\")\r\n" +
                    "end\r\n");
        // abcl fails because not on *nix or Windows
        test("tcl", "for {set i 0} {$i < 3} {incr i} {\r\n" +
                     "    puts \"Hi from Tcl!\"\r\n" +
                     "}\r\n");
        test("Renjin", "for (i in 1:3) {\r\n" +
                       "    print(\"Hi from R!\")\r\n" +
                       "}\r\n");
        test("NetRexx", "loop i=1 to 3\r\n" +
                        "  say 'Hi from Rexx!'\r\n" +
                        "end i\r\n");
    }
}
