About

Overview

Snap is an open source, optionally typed, object oriented scripting language for the Java platform. The learning curve is small for anyone with experience of Java, JavaScript, TypeScript, or Scala, amongst others. It has excellent integration with the host platform, and can do whatever can be done with Java and much more.

The language is ideal for embedding in to an existing application, and is a fraction of the size of similar languages for the Java platform. In addition to embedding it can be run as a standalone interpreter and has an development environment which allows scripts to be debugged and profiled.

Development

Snap comes with a development environment that is available over HTTP on any web browser that supports Web Sockets. To develop and debug scripts the devlopment environment can be started with the command shown below. All that is required is a web browser and Java 1.5 or greater. The development environment is a single JAR file that can be downloaded from here. In addition an embeddable version is available which can be integrated in to any Java application.

java -jar snapd.jar --directory=work --port=4457

The development environment can use hot stand-by agents to improve responsiveness, the agent pool can be configured on the command line. In addition an agent can connect through the HTTP port using the HTTP CONNECT request and begin a debug session. An example configuration is shown below.

java -jar snapd.jar --directory=work --port=4457 --agent-pool=4 --server-only=true





Interpreter

To begin it is recommended that you download the interpreter. The interpreter requires Java to be installed on the host machine. Once you have downloaded the interpreter you can begin running scripts right away. All you need to do is specify the script file relative to the current directory.

java -jar snap.jar --script=/run.snap

If your scripts are in a different directory to the current working directory, then it can be specified on the command line. The script will be loaded relative to that directory.

java -jar snap.jar --script=/run.snap --directory=/path/to/scripts

If you would like to specify arguments that will be available within the script to be executed then each argument provided without a '--' prefix will be available in the order they are provided via both the 'args' and 'arguments' variable. For example take the folllowing.

java -jar snap.jar --script=/run.snap --directory=/path/to/scripts 10 'blah' 123.0

If all you want to do is to evaluate some expression then you can specify that expression on the command line. The expression will be executed and if there is any output then that will be displayed to the console.

java -jar snap.jar --evaluate="println(1000 / 2.5)"

Embed

If you would like to embed the interpreter in to an existing application then all you need to do is create a compiler to compile a script. A script is compiled to an executable object, which is essentially an execution tree representing the script. Finally, once you have the executable object, you can provide a set of bindings that will be accessible to the script. These bindings can be provided by creating a model object.

File file = new File('/path/to/scripts'); // path used to load scripts
Store store = new FileStore(file);
Context context = new StoreContext(store);
Compiler compiler = new ResourceCompiler(context);
Map map = new HashMap();
Model model = new MapModel(map); // bindings for the script

map.put("blah", 10);
map.put("foo", "some text");
map.put("bar", 50);

Executable executable = compiler.compile('/run.snap');

executable.execute(model); // execute the script

There are a number of configuration options for creating a compiler. For more information you can browse through the code which is hosted here.