I am doing a project where I have to make a Facile program that reads and executes statements line by line.

Example would be like:
LET A 5
ADD A 8
PRINT A
END

and that would print 13 to the console

So I have a class for each of the function methods(like ADD, SUB, GOTO,PRINT,MULTI,etc)

I have a ProgramState class that holds all my methods for the function classes.
Below is just the first part of the class, I didnt think it was necessary to paste all
the methods for the function statements

private int programCounter;
	private int[] variables;
	private Stack<Integer> stack;


	public ProgramState()
	{
		programCounter = 1;
		variables = new int[26];
		stack = new Stack<Integer>();
	}

I also have an abstract class called Statement that all my classes for the function methods extend to

public abstract class Statement
{
	
	public abstract void execute(ProgramState state)
	throws FatalException;
}

I have a Parser class that takes in a file, parses it, and returns the info as a arraylist of statements. I included a brief overview of the class

// this reads and parses the file then stores it in an arrayList
public static ArrayList<Statement> parseProgram(String filename)
	throws IOException
 
// this method just contains what to do with the info from the file
// like if the line says "LET" do this if it says "PRINT" then do this, etc.
private static Statement parseLine(String line)

I have a Stack class which is like how you would build a linked list but you can only access the top one at a time. The stack is, at least I think, just for making a stack in the ProgramState to keep track of the line numbers with GOSUB's and REUTRN's

I also have a main class that I am suppose to use to create an Interperator object among other things but obviously I'm not there yet.

So I have to write an Interperator class and I am a bit lost on how to start. Do I create objects of some of my other classes (like Parser / ProgramState) and call some of there methods? Do I write some new methods? I'm not looking for the actual syntax of how to do it, hence why I left out the majority of my code, but just the logic behind it and how to generally start.

Consider using:

HashMap<String, Statement> cmds = new HashMap<String, Statement>();

If you're not familiar with HashMaps, you can do:

cmds.put("ADD", new ADD());  // assuming ADD() extends Statement.

When you parse your file, you can then do:

if (cmds.KeySet().contains(line)) { cmds.get(line).execute(state); }
//assuming you have already parsed "ADD" from the file to the String line.

You don't necessarily need to write any new methods...open a file with BufferedReader, read one line, parse line, call execute as described above, continuing reading lines until end of file, close file.

I wouldn't recommend reading the entire file into an arrayList, generally not good design. What if the file was 10k lines? That's a huge slice of memory. BufferedReader is generally the best method since it reads chunks of the file, the downside is that the file needs to be open for the duration of the call. However, that is easily mitigated with proper execution catching.

o ok, I have some idea of what to do now, kinda. I'll see what I can do. Thanks for the help and advice.

You can do it with other data structures....array, arrayList, LinkedList, etc (see Collection), but HashMap is the most efficient lookup, which is what you will be using it for.

This article has been dead for over six months. Start a new discussion instead.