-- Test file for a Composite, Interpreter and Visitor pattern -- Sergio Antoy -- Tue Jul 17 08:32:17 PDT 2001 -- Updated Mon Apr 18 09:03:37 PDT 2011 -- Updated Thu Jun 29 10:34:55 PDT 2017 import Expr import Statement -- Sample program that computes and prints the factorial of 6. -- -- begin -- n := 1; -- i := 6; -- while (i > 1) do -- begin -- n := n * i; -- i := i -1 -- end; -- print n -- end fact6 = Compound [ Assign "n" (Num 1), Assign "i" (Num 6), While (Gt (Var "i") (Num 1)) ( Compound [ Assign "n" (Mul (Var "n") (Var "i")), Assign "i" (Sub (Var "i") (Num 1)) ]), Print (Var "n") ] -- Test the execution of the previous program test1 = do putStrLn "Should print 720" run fact6 -- Sample program that computes and prints the the first 9 -- Fibonacci numbers. The sequence starts with 0 and 1. -- -- begin -- i := 9; -- f := 0; -- g := 1; -- while (i > 0) do -- begin -- i := i - 1; -- print f; -- h := f + g; -- f := g; -- g := h; -- end -- end fibo9 = Compound [ Assign "i" (Num 9), Assign "f" (Num 0), Assign "g" (Num 1), While (Gt (Var "i") (Num 0)) ( Compound [ Assign "i" (Sub (Var "i") (Num 1)), Print (Var "f"), Assign "h" (Add (Var "f") (Var "g")), Assign "f" (Var "g"), Assign "g" (Var "h") ] ) ] -- Test the execution of the previous program test2 = do putStrLn "Should print 0 1 1 2 ... 21 one per line" run fibo9