package a5; import java.util.InputMismatchException; /** Test program for the Interpreter class of Assignment 5, CS 152, Sections 5 & 6, Fall 2006 */ public class A5 { /** Prints true or false, depending on the value of a given boolean expression. If the expression is ill-formed, an error message is printed. @param p an interpreter that uses that grammar of the Interpreter class @param expr the expresssion, represented as a string of tokens separated by whitespace characters as recognized by the Character.isWhitespace predicate. */ public static void test(Interpreter p, String expr) { try { int result = p.evaluate(expr); System.out.println("result: " + result); } catch(InputMismatchException e) { System.out.println(e.getMessage()); } } /** The main test program @param args is ignored */ public static void main(String args[]) { Interpreter p = new Interpreter(); // no errors under dynamic scoping test(p, "10"); test(p, "( + 5 6 )"); test(p, "( + ( * 10 1 ) 2 ) "); test(p, "define f ( aa bb ) 13 end ( f 8 9 ) "); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( + 10 4 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( + 1 ( f 1 5 ) )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( f ( * 10 2 ) 9 )"); test(p, "define f ( x y ) " + " define g ( x y ) " + " define h ( x y )" + " define k ( x y ) ( + x y ) end " + " ( + 1 ( k x y ) ) end " + " ( + 2 ( h x y ) ) end " + " ( + 3 ( g x y ) ) end " + "( f 101 102 )"); test(p, "define f ( x y ) ( * 10 x ) end " + "define g ( u v ) v end " + "( + ( f 1 2 ) ( g 3 7 ) )"); // the next case is illegal under static scoping test(p, "define f ( y z ) " + " define g ( u v ) ( + x ( + u v ) ) end " + " ( + 10 ( g y z ) ) end " + "define h ( x y ) ( f ( + x 1 ) ( + y 2 ) ) end " + "( h 1 3 )"); // these cases give different results under dynamic & static scoping test(p, "define f ( x y ) " + " define g ( u v ) ( + ( * 10 y ) v ) end " + " define h ( y z ) ( g y 0 ) end " + " ( h x y ) end " + "( f 1 2 )"); test(p, "define f ( x y ) " + " define g ( u v ) ( + y u ) end " + " define h ( y z ) ( g 1 3 ) end " + " ( * ( * 10 ( g x y ) ) ( h x y ) ) end " + "( f 2 5 )"); System.out.println(); // compile-time errors test(p, "+ 3 4 "); test(p, "( + 3 4 "); test(p, "( + )"); test(p, "( + 3 )"); test(p, "( + 3 4 5 )"); test(p, "( + 3 4 ) (* 3 4)"); test(p, "( * x3 4 )"); test(p, "( * 3x 4 )"); test(p, "( 3 4 5 )"); test(p, "define f x y ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define 6 ( x y ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ( 7 y ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ( x 8 ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ( x y9 ) ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end + 1 5 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( f 1 5"); test(p, "define f ( x y ( + ( * 10 x ) y ) end ( f 1 5 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( f 1 5 ) 6"); test(p, "define f ( ) ( + ( * 10 x ) y ) end ( + 10 4 )"); test(p, "define f ( x ) ( + ( * 10 x ) y ) end ( + 10 4 )"); test(p, "define f ( x y z ) ( + ( * y x ) z ) end ( f 1 2 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) ( * 10 ( f 1 2 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( * 10 ( f 1 2 )"); System.out.println(); // run-time errors test(p, "x"); test(p, "( + x 4 )"); test(p, "( f 3 4 )"); test(p, "define f ( x y ) ( + ( * 10 x ) y ) end ( g 1 5 )"); test(p, "define f ( x y ) " + " define g ( u v ) ( + u v ) end " + " ( g 6 7 ) end " + "define h ( p q ) ( g 2 3 ) end " + "( * 10 ( h 1 4 ) )"); test(p, "define f ( y z ) " + " define g ( u v ) ( + x ( + u v ) ) end " + "( * 10 ( g y z ) ) end " + "define h ( x y ) ( f ( + x 1 ) ( + y 1 ) ) end " + "( f 1 3 )"); } }