Skip to content

Adding New Languages

Borislav Iordanov edited this page Apr 8, 2016 · 4 revisions

Introduction

Seco official builds come with pre-bundled support for the BeanShell and JScheme programming languages (some of the UI elements are implemented in them). We have also implemented support for several other popular JVM based languages that you can just add to your environment. And of course, you can write support for other languages of your own choosing.

To add support for a new scripting language in Seco's notebooks, the following must be done:

  1. Add the language interpreter to the classpath: either the Seco system classpath or your runtime evaluation context (required).
  2. Add support for code formatting, syntax highlighting and code completion (optional).

Installing Other Supported Languages

The following languages are currently supported as a separate install

Language Home Page Seco Installation Jar
Groovy [http://groovy.codehaus.org/ Groovy Home] http://seco.googlecode.com/files/seco-groovy.jar
!JavaScript [http://www.mozilla.org/rhino/ JavaScript Home] http://seco.googlecode.com/files/seco-js.jar
JRuby [http://jruby.org/ JRuby Home]
Prolog [http://kobrix.com/wikishow?page=TuProlog&project=hypergraphdb Prolog Implementation] http://seco.googlecode.com/files/seco-prolog.jar

To install any of those languages:

  1. Download the Seco Installation Jar, and put in the 'lib' folder of your Seco installation.
  2. The language will henceforth be automatically recognized and added to any new niche you create.
  3. To add it to an existing niche, download the [http://kobrix.com/downloads/InstallLanguages.nb InstallLanguages Notebook], import it in your niche and evaluate the appropriate cells for your language.

The following sections provide more detail about the relevant APIs to use when adding support for additional languages.

Adding the Language Interpreter

  1. Language interpreters are added by first implementing the javax.script.ScriptEngineFactory interface. Most JVM languages already come with such an implementation. If not, take a look at beanshell implementation that's part of Seco's source code.
  2. Add an atom of type seco.rtenv.SEDescriptor in the niche. All atoms of this type are automatically loaded and associated with the runtime context. Here's an example:
    SEDescriptor desc = new SEDescriptor();
    desc.setLanguage("prolog");
    desc.setPackageNames(new String [] { "alice.tuprolog", "alice.tuprolog.lib", "alice.tuprolog.hgdb",       
                                    "alice.tuprolog.clausestore"});
    desc.setFactoryClassName("alice.tuprologx.TuScriptEngineFactory");
    niche.add(desc); // if you are within a notebook or seco.ThisNiche.graph.add(desc) in Java

The name of the language here is prolog and this is what will appear in context menus when selecting a language for a notebook or a particular cell or cell group. The packageNames property lists core package names of the language interpreter - the need for this will be explained below. Finally, the factoryClassName is the implementation of the javax.script.ScriptEngineFactory that usually comes with the language implementation distribution.

The packageNames Property

Seco supports multiple runtime contexts, each with a separate classpath. Each runtime context has a separate class loader which will try to find an already loaded class in its parent before attempting to define it locally. So normally, when a language interpreter is part of the JVM startup classpath, it will be loaded by the system class loader, rather than the Seco runtime class loader. This will cause the interpreter to try to load all classes referred to by script with the system class loader (because there's no standard way to pass a classloader to a ScriptEngine). To solve this problem, the interpreter needs to be loaded within the Seco runtime class loader. So the Seco runtime class loader needs to know what classes not to delegate to its parent, but rather define them locally. Those classes are specified with the packageNames : all classes from those packages are going to be explicitly defined by Seco context loader.

Interpreter classes/jar Location

When adding a new language, one thing you need to decide is whether to include in Seco startup classpath by modified the shell script for starting Seco or simply add it to the runtime context's classpath. In the latter case, the interpreter will obviously available only to the context where you added it.

Adding Code Assistance Support

There are a couple of aspects to code assistance: formatting, syntax highlighting and code completion.