diff --git a/a1/src/APL/AST.hs b/a1/src/APL/AST.hs index 2f4c316..9172de6 100644 --- a/a1/src/APL/AST.hs +++ b/a1/src/APL/AST.hs @@ -25,4 +25,17 @@ data Exp deriving (Eq, Show) printExp :: Exp -> String -printExp = undefined -- TODO +printExp (CstBool b) = show b +printExp (CstInt i) = show i +printExp (Add e1 e2) = "("++(printExp e1)++" + "++(printExp e2)++")" +printExp (Mul e1 e2) = "("++(printExp e1)++" * "++(printExp e2)++")" +printExp (Sub e1 e2) = "("++(printExp e1)++" - "++(printExp e2)++")" +printExp (Div e1 e2) = "("++(printExp e1)++" / "++(printExp e2)++")" +printExp (Pow e1 e2) = "("++(printExp e1)++" ** "++(printExp e2)++")" +printExp (If e1 e2 e3) = "(if "++(printExp e1)++" then "++(printExp e2)++" else "++(printExp e3)++")" +printExp (Var var) = var +printExp (Let var e1 e2) = "(let "++var++" = "++(printExp e1)++" in "++(printExp e2)++")" +printExp (Lambda var e) = "(\\"++var++" -> "++(printExp e)++")" +printExp (Apply e1 e2) = "("++(printExp e1)++" "++(printExp e2)++")" +printExp (TryCatch e1 e2) = "(try "++(printExp e1)++" catch "++(printExp e2)++")" +printExp _ = "error" diff --git a/a1/src/APL/AST_Tests.hs b/a1/src/APL/AST_Tests.hs index 826229e..82d953a 100644 --- a/a1/src/APL/AST_Tests.hs +++ b/a1/src/APL/AST_Tests.hs @@ -1,11 +1,44 @@ module APL.AST_Tests (tests) where --- import APL.AST (Exp (..)) +import APL.AST (Exp (..), printExp) import Test.Tasty (TestTree, testGroup) --- import Test.Tasty.HUnit (testCase, (@?=)) +import Test.Tasty.HUnit (testCase, (@?=)) tests :: TestTree tests = testGroup "Prettyprinting" - [] + [ + testCase "CstBool" $ + printExp (CstBool True) @?= "True", + testCase "CstInt" $ + printExp (CstInt 500) @?= "500", + testCase "Add" $ + printExp (Add (CstInt 1) (CstInt 9999)) @?= "(1 + 9999)", + testCase "Mul" $ + printExp (Mul (CstInt 1) (CstInt 9999)) @?= "(1 * 9999)", + testCase "Sub" $ + printExp (Sub (CstInt 1) (CstInt 9999)) @?= "(1 - 9999)", + testCase "Div" $ + printExp (Div (CstInt 1) (CstInt 9999)) @?= "(1 / 9999)", + testCase "Pow" $ + printExp (Pow (CstInt 1) (CstInt 9999)) @?= "(1 ** 9999)", + testCase "If" $ + printExp (If (CstBool True) (CstInt 2) (CstInt 3)) + @?= "(if True then 2 else 3)", + testCase "Var" $ + printExp (Var "missing") + @?= "missing", + testCase "Let" $ + printExp (Let "x" (CstInt 2) (Var "x")) + @?= "(let x = 2 in x)", + testCase "Lambda" $ + printExp (Lambda "x" (Var "x")) + @?= "(\\x -> x)", + testCase "Apply" $ + printExp (Apply (Lambda "x" (Var "x")) (CstInt 1)) + @?= "((\\x -> x) 1)", + testCase "TryCatch" $ + printExp (TryCatch (CstInt 0) (CstInt 1)) + @?= "(try 0 catch 1)" + ]