/**
 * Derived class that represents an assignment statement in the SILLY language.
 *   @author Dave Reed
 *   @version 2/8/12
 */
public class Assignment extends Statement {
    private Token vbl;
    private Expression expr;
    
    /**
     * Reads in a assignment statement from the specified TokenStream
     *   @param input the stream to be read from
     */
    public Assignment(TokenStream input) throws Exception {
        this.vbl = input.next();
        Token op = input.next();
        if (this.vbl.getType() != Token.Type.IDENTIFIER || !op.toString().equals("=")) {
            throw new Exception("SYNTAX ERROR: malformed assignment");
        }
        
        this.expr = new Expression(input);
    }
    
    /**
     * Executes the current assignment statement.
     *   @param bindings the current variable bindings
     */
    public void execute(Bindings bindings) throws Exception {
        bindings.assign(this.vbl, this.expr.evaluate(bindings));
    }
    
    /**
     * 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 this.vbl + " = " + this.expr;
    }
}
