Creating new Hecl commands

Creating new Hecl commands is relatively simple. The first step is to create a new class for your command in a file, say HelloCmd.java, which, for simplicity's sake, you could put in core/org/hecl/. The code would look something like this:

import org.hecl.Command;
import org.hecl.HeclException;
import org.hecl.Interp;
import org.hecl.Thing;

class HelloCmd implements Command {

    public Thing cmdCode(Interp interp, Thing[] argv)
	throws HeclException {

	System.out.println("Hello world");
	return null;
    }
}
      

The command takes an interpreter and an array of Things as arguments, where the first Thing is the name of the command itself, and the others are the arguments to it.

The "glue" that connects the name of your Hecl command with the Java code is also relatively simple:

	interp.addCommand("hello", new HelloCmd());
      

The above code would be included somewhere in commandline/Hecl.java, midp20/Hecl.java or elsewhere, depending on what platform you're working on; or core/org/hecl/Interp.java if you want to include it as part of the Hecl core.

Easy, no? There are a few other useful methods that you should be aware of, to share variables between Hecl and Java, and to return results from your Hecl commands:

There are similar methods for strings, floats (where applicable), hashes and lists.