CS 214: Programming Languages
Spring 2009

Home|Syllabus|Schedule
<<|>>|ANTLR API|CITkit API|PolyD API|EasyMock API

Command-Line Drivers
Interpreters, Iteration 3

Running the tests is fine and good, but do you really have something practical now? Well, you have something that works, but "practical" may be pushing it. In this iteration, you'll get two command-line drivers up and running.

Getting Prepared

Driver for Procedural Hobbes

Here's a class definition for ProceduralHobbes:

public class ProceduralHobbes {

    public static void main(String[] args) throws IOException,
            RecognitionException {
        new ProceduralHobbes(new HobbesFrontEnd(new File(args[0])),
                new ProceduralHobbesInterpreter()).process();
    }

    private final IHobbesFrontEnd             myFrontEnd;
    private final ProceduralHobbesInterpreter myInterpreter;

    public ProceduralHobbes(IHobbesFrontEnd frontEnd,
            ProceduralHobbesInterpreter interpreter) {
        myFrontEnd = frontEnd;
        myInterpreter = interpreter;
    }

    public void process() throws RecognitionException {
        System.out.println(myInterpreter.interpret(myFrontEnd.parse()));
    }

}

Enter this code.

Driver for Object-Oriented Hobbes

Here's a class definition for OOHobbes:

public class OOHobbes {

    public static void main(String[] args) throws IOException,
            RecognitionException {
        new OOHobbes(new HobbesFrontEnd(new File(args[0])),
                new OOHobbesInterpreter()).process();
    }

    private final IHobbesFrontEnd     myFrontEnd;
    private final OOHobbesInterpreter myInterpreter;

    public OOHobbes(IHobbesFrontEnd frontEnd, OOHobbesInterpreter interpreter) {
        myFrontEnd = frontEnd;
        myInterpreter = interpreter;
    }

    public void process() throws RecognitionException {
        System.out.println(myInterpreter.interpret((IntegerETIR) myFrontEnd
                .buildTIR()));
    }

}

Enter this code.

Running These Drivers

Poor Windows users, if someone comes up with a batch file for Windows to run the drivers, I'd be happy to include it here. However, I suspect you'll find it easier to install Cygwin in the long run.

Eclipse will run these drivers for you, but it's a little awkward. The main() methods use the first command-line argument as a file name of Hobbes code to interpret. It's awkward trying to do this in Eclipse. (Hard coding a file name in the code isn't any better since you have to keep editing the same file over and over again.)

Since these drivers are command-line based anyway, why not actually run them there? To do this, you should have a folder for command-line scripts and a folder for Hobbes programs.

The hobbes script collects jar files from your lib folder for the classpath as well as the project's bin folder. Then it runs both interpreters.

#!/bin/sh

for jar in lib/*.jar; do
        export CLASSPATH=$CLASSPATH:${jar}
done
export CLASSPATH=$CLASSPATH:./bin/

java edu.calvin.jdfrens.cs214.interpreters.hobbes.drivers.ProceduralHobbes $1
java edu.calvin.jdfrens.cs214.interpreters.hobbes.drivers.OOHobbes $1

Enter this code into the hobbes script. You'll need to change one word in two places to match the names of your packages. Then in a terminal, give yourself permission to execute the file (chmod 700 ./script/hobbes), and then run it on some files.

Here's what it looks like when I run it:

$ ./scripts/hobbes hobbes/integer5.hob 
5
5

Can you guess what I have in that Hobbes file?