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 Thing
s 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:
interp.setVar(Thing varname,
Thing value);
This sets the value of varname
to
some value.
Thing interp.getVar(Thing varname);
Thing interp.getVar(String varname);
These methods take a variable name, either in string form or
as a Thing
, and return the
Thing
associated with that variable.
interp.result
is used to set the
result of a command. This oft-used variable is accessed
directly for simplicity, speed and smaller code size.
int IntThing.get(Thing thing);
Get an int from a Thing.
String StringThing.get(Thing thing);
Get a String from a Thing.
Thing IntThing.create(int i);
Creates a new thing from an int.
There are similar methods for strings, floats (where applicable), hashes and lists.