// This is a version of Evaluator.java's main method
// (and a copy of the prettyPrint helper method) that
// by default prints the tokens, parse tree, value, and env
// each time through the read-eval-print loop
// Feel free to use this version instead of the one that
// checks the command-line arguments with if (args.length > 0)
// Either version is completely acceptable.
/** method: main
* inputs: usual args, ignored
* Note the boolean PRINT_ALL controls printing... .
* It is now true by default.
* outputs: none, this just tests the Evaluator
*/
public static void main(String[] args)
{
Tokenizer tokenizer = new Tokenizer();
//tokenizer = new Tokenizer("testfile.txt");
Parser parser = new Parser();
Evaluator evaluator = new Evaluator(OpenList.emptyList);
boolean PRINT_ALL = true;
while (true)
{
String[] tokens = tokenizer.nextTokens();
if (tokens == null) break;
if (PRINT_ALL)
System.out.println("The tokens are " + Tokenizer.printArray(tokens));
OpenList parseTree = parser.parse(tokens);
if (PRINT_ALL)
System.out.println("The parseTree is " + parseTree);
Quantity outputValue = evaluator.evaluate( parseTree );
if (PRINT_ALL)
System.out.println( "The value is " + outputValue );
else
System.out.println( " " + outputValue );
// print the current environment if there is the -debug flag present
// at the command line (or any flag, for that matter!)
if (PRINT_ALL)
System.out.println("\nThe current env is\n" + prettyPrint(evaluator.env));
System.out.println();
}
}
/** prettyPrint, prints OpenList association lists prettily
* inputs: an OpenList that should be an association list
* output: a string to print
*/
private static String prettyPrint( OpenList AL )
{
String s = "[\n";
for (OpenList h=AL ; !h.isEmpty() ; h=h.rest())
s += " " + h.first() + "\n"; // inefficient, but OK
return s + "]\n";
}