/*
 * Program skeleton for Parser.java
 *
 * NOTE: This incomplete fragment compiles but does not evaluate
 * all valid BE's.
 */

public class Parser
{
    private static CS211In boolExp;

    public static void main(String[] args)
    {
	if (args.length == 1)
	    boolExp = new CS211In(args[0]);
	else {
	    System.out.println("Usage: Parser <filename>");
	    return;
	}

	if (evalBoolExp())
	    System.out.println(" ==> true");
	else
	    System.out.println(" ==> false");
	// any text after a valid BE will be ignored!
    }

    // recursive descent parsing routine
    public static boolean evalBoolExp()
    {
	switch (boolExp.peekAtKind()) {
	case boolExp.WORD: {
	    String cons = boolExp.getWord();
	    if (cons.equals("T"))
		return true;
	    // else if ... COMPLETE HERE ...
	    else
		syntaxError("T or F");
	}
	case boolExp.OPERATOR: {
	    char op = boolExp.getOp();
	    if (op == '!')
		return !evalBoolExp();
	    // else ... COMPLETE HERE ...
	}
	default:
	    return syntaxError("... COMPLETE HERE ...");
	}
    }

    private static boolean syntaxError(String expectation)
    {
	System.out.println("\nSyntax error in expression: expected " + expectation);
	System.exit(1);  // abort program
	return false;  // bogus return value to make type checker happy
    }
}
