I have no idea how to start the interpreter for this java project.
I need some hint on the LET statement in order to write other code such as ADD, MINUS, GOTO...etc
Thanks

public abstract class Statement
{
	// execute() takes a ProgramState and executes this statement, by making
	// any necessary changes to the ProgramState (e.g. a new value for the
	// program counter, changing the value of some variable, pushing or
	// popping from the line number stack).
	//
	// In the event that the execution of the statement causes a fatal error
	// that should terminate the Facile program, such as division by zero or
	// a RETURN statement without a corresponding GOSUB, a FatalException is
	// thrown.
	public abstract void execute(ProgramState state)
	throws FatalException;
}
public class LetStatement extends Statement
{
	private char variableName;
	private int value;


	public LetStatement(char variableName, int value)
	{
		this.variableName = variableName;
		this.value = value;
	}
	

	// The LetStatement version of execute() should make two changes to the
	// state of the program:
	//
	//    * set the value of the appropriate variable
	//    * increment the program counter
	//
	// There is no way for a LET statement to fail, so there is no reason for
	// it to ever throw a FatalException.  Nevertheless, the signature of the
	// method must match the signature of the execute() method in Statement,
	// so we'll leave the "throws" clause alone.

        This part is where i need help
	public void execute(ProgramState state)
	throws FatalException
	{
		
	}
}

Recommended Answers

All 11 Replies

What does the ProgramState class look like???

ProgramState looks like this

// The ProgramState class encapsulates the state of an executing Facile
// program.  The state of a Facile program consists of three parts:
//
//    * The program counter, which specifies the line number of the
//      statement that will execute next.
//    * An array of 26 integers, holding the current values of the 26
//      variables.
//    * A stack of integers, which is used to hold return lines for GOSUB
//      statements.
//
// I've provided you a start on this class, but you'll need to add methods
// to it as you go along.  In particular, you'll need methods to access and
// modify the state; these methods will be called primarily by the various
// execute() methods in the Statement subclasses, to allow the execution of
// a statement to change the state of a program.  For example, executing a
// GOTO statement will cause the program counter to be changed.



public class ProgramState
{
	private int programCounter;
	private int[] variables;
	private Stack<Integer> stack;


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

	// You'll need to add a variety of methods here.  Rather than trying to
	// think of what you'll need to add ahead of time, add them as you find
	// that you need them.

}

OK, if you look at the comment in your first post, you should see what you are supposed to do.

// * set the value of the appropriate variable
// * increment the program counter

What you need to do inside the execute() is...

state.addVariable(this.value);
state.increaseProgramCounter();

Then in ProgramState, you need to add 2 methods to match whatever they are in the execute().

public void addVariable(int val) {
  // Put the value from val into variables array
  // However, I am not sure how you would associate variable name
  // with is value inside ProgramState. If I were to implement,
  // I will change the variables from integer array to a hashmap,
  // but if I need to keep the same data type, I would add another
  // array for variable name. Though, it is your program, so
  // add whatever you want.
}
public void increaseProgramCounter() {
  programCounter++;
}

I do not know what is hashmap though
By the way, where is the stack in ProgramState() need to be used?
Moreover, the LetStatement is not to add a variable but is to set a variable to a value, such as LET A 3, means let a =3
so basically what you are saying is that

public ProgramState()
{
variableName = new char[26];
variableValue = new int[];
stack = new Stack<Integer>();
}
public void addVariable(int val, char variableName)
{
//don't know what to put here then...
}
}

I do not know what is hashmap though
By the way, where is the stack in ProgramState() need to be used?
Moreover, the LetStatement is not to add a variable but is to set a variable to a value, such as LET A 3, means let a =3
so basically what you are saying is that

public ProgramState()
{
variableName = new char[26];
variableValue = new int[];
stack = new Stack<Integer>();
}
public void addVariable(int val, char variableName)
{
//don't know what to put here then...
}
}

Since LetStatement takes a single character variable name and since the variables array in ProgramState is 26 integers, I would assume that each variable is a letter of the alphabet and you need to map letters to integers 0-25. That way there's no need to store the variable names in a separate array.

So here is my program, but I am not sure is right

public class LetStatement extends Statement
{
	private char variableName;
	private int value;


	public LetStatement(char variableName, int value)
	{
		this.variableName = variableName;
		this.value = value;
	}
	
public void execute(ProgramState state)
	throws FatalException
	{
		state.addVariable(variableName, value);
		state.increaseProgramCounter();
		
		
	}
}
public class ProgramState
{
	private int programCounter;
	private int[] variables;
	private Stack<Integer> stack;


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

	public void addVariable(char variableName, int value)
	{
		if("a".equals(variableName))
			variables[1] = value; 
                //...etc, so on so for like "b".equal(variableName) variable[2] = value;
		
	}

So here is my program, but I am not sure is right

public class LetStatement extends Statement
{
	private char variableName;
	private int value;


	public LetStatement(char variableName, int value)
	{
		this.variableName = variableName;
		this.value = value;
	}
	
public void execute(ProgramState state)
	throws FatalException
	{
		state.addVariable(variableName, value);
		state.increaseProgramCounter();
		
		
	}
}
public class ProgramState
{
	private int programCounter;
	private int[] variables;
	private Stack<Integer> stack;


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

	public void addVariable(char variableName, int value)
	{
		if("a".equals(variableName))
			variables[1] = value; 
                //...etc, so on so for like "b".equal(variableName) variable[2] = value;
		
	}

Characters in Java are represented by ASCII codes. That means you can treat a character like an integer and do arithmetic with it. You can find a table of ASCII codes in most programming books or search on the web. The letters 'a' to 'z' will always be listed sequentially. So to map a letter of the alphabet to the numbers 0-25, you can subtract the value 'a' from the letter.

int index = variableName - 'a';

If variableName contains the letter 'a', then index will be 0. If variableName contains the letter 'b', then index will be 1, etc.

I think I figure it out
Thanks you all

Oh didn't look at what argument type for LetStatement constructor is taken. :P Sorry about that.

You may need to modify the constructor to make sure that the variable name is valid or at least throw an exception when the variable name is NOT lower case or it is not a~z in order to use variableName - 'a' (or uppder case and A-Z if you use 'A'). Unless you have another class to control it (e.g. LetStatementGenerator). Also, the prerequisite for this is that you are not allow to have the same variable name for the whole program or it will mess up.

Is this class compiler? It looks a bit like it even though it is not exactly. Anyway, LetStatement needs for storing variable value associated with name; however, it is an individual. ProgramStatement will hold all program's variables and keep the stack. The stack is used to hold state of the program. For example, when you do goto, you need to know where to get back and all variable values that are still live before the jump. You don't need it until you get to the GOSUB.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.