/**
 * Derived class that represents an assignment statement in the SILLY language.
 * 
 * @author Dave Reed
 * @version 2/9/07
 */
public class Assignment extends Statement
{
    private Token vbl;
    private Expression expr;
    
    /**
     * Reads in a assignment statement from the specified program
     *   @param program the source code of the program to be read from
     */
	public void read(SourceCode program) {
        vbl = program.next();
        Token op = program.next();
        
        if (vbl.getType() != Token.Type.IDENTIFIER || !op.toString().equals("=")) {
            System.out.println("SYNTAX ERROR: Malformed assignment statement");
            System.exit(0);
        }
        
        expr = new Expression();
        expr.read(program);
	}
    
	/**
	 * Executes the current assignment statement.
	 *   @param variables the table representing the current bindings for all variables
	 */
	public void execute(VariableTable variables) {
	    variables.assign(vbl.toString(), expr.evaluate(variables));
	}
    
	/**
	 * Accesses the type of the current statement.
	 *   @return this statement's type, i.e., Statement.Type.ASSIGNMENT
	 */
	public Statement.Type getType() {
	    return Statement.Type.ASSIGNMENT;
	}
	
	/**
	 * Converts the current assignment statement into a String.
	 *   @return the String representation of this statement
	 */
    public String toString() {
        return vbl + " = " + expr;
    }
}
