From a9a530444e426ff4357a4e88d2841799a0a19372 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Sat, 28 Sep 2024 14:09:12 +0200 Subject: [PATCH] :sunglasses: Task 4 and 5 --- a3/a3-handout/src/APL/Parser.hs | 17 ++++++++++++++-- a3/a3-handout/src/APL/Parser_Tests.hs | 29 +++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/a3/a3-handout/src/APL/Parser.hs b/a3/a3-handout/src/APL/Parser.hs index 4b53203..530eec1 100644 --- a/a3/a3-handout/src/APL/Parser.hs +++ b/a3/a3-handout/src/APL/Parser.hs @@ -51,7 +51,7 @@ lVName = lexeme $ try $ do lStringLit :: Parser String lStringLit = - lexeme $ read <$> some (satisfy isAlpha) <* notFollowedBy (satisfy isAlphaNum) + lexeme $ some (satisfy (/= '"')) lInteger :: Parser Integer lInteger = @@ -101,6 +101,16 @@ pLExp = <$> (lKeyword "if" *> pExp) <*> (lKeyword "then" *> pExp) <*> (lKeyword "else" *> pExp), + Lambda + <$> (lString "\\" *> lVName) + <*> (lString "->" *> pExp), + TryCatch + <$> (lKeyword "try" *> pExp) + <*> (lKeyword "catch" *> pExp), + Let + <$> (lKeyword "let" *> lVName) + <*> (lString "=" *> pExp) + <*> (lKeyword "in" *> pExp), pFExp ] @@ -110,7 +120,10 @@ pExp3 = [ Print <$> (lKeyword "print" *> pStringLit) - <*> pExp, + <*> pAtom, + KvGet <$> (lKeyword "get" *> pAtom), + KvPut <$> (lKeyword "put" *> pAtom) + <*> pAtom, pLExp ] diff --git a/a3/a3-handout/src/APL/Parser_Tests.hs b/a3/a3-handout/src/APL/Parser_Tests.hs index 985faa6..e737e86 100644 --- a/a3/a3-handout/src/APL/Parser_Tests.hs +++ b/a3/a3-handout/src/APL/Parser_Tests.hs @@ -69,6 +69,7 @@ tests = "FExp" [ parserTest "x y" $ Apply (Var "x") (Var "y"), parserTest "x y z" $ Apply (Apply (Var "x") (Var "y")) (Var "z"), + parserTest "x y z+5" $ Add (Apply (Apply (Var "x") (Var "y")) (Var "z")) (CstInt 5), parserTest "x (y z)" $ Apply (Var "x") (Apply (Var "y") (Var "z")) ], testGroup @@ -78,13 +79,37 @@ tests = parserTest "x + y == z * x" $ Eql (Add (Var "x") (Var "y")) (Mul (Var "z") (Var "x")) ], testGroup - "Eql" + "Pow" [ parserTest "x ** y" $ Pow (Var "x") (Var "y"), parserTest "x ** y ** z" $ Pow (Var "x") (Pow (Var "y") (Var "z")), parserTest "x + y ** z * x" $ Add (Var "x") (Mul (Pow (Var "y") (Var "z")) (Var "x")) ], testGroup "Print" - [ parserTest "print \"test\" y" $ Print "test" (Var "y") + [ parserTest "print \"test\" x" $ Print "test" (Var "x"), + parserTest "print \"7\" x" $ Print "7" (Var "x"), + parserTest "print \"cool_print\" x" $ Print "cool_print" (Var "x") + ], + testGroup + "Get" + [ parserTest "get x" $ KvGet (Var "x"), + parserTest "get 1" $ KvGet (CstInt 1) + ], + testGroup + "Put" + [ parserTest "put x y" $ KvPut (Var "x") (Var "y") + ], + testGroup + "Lambda" + [ parserTest "\\x->y" $ Lambda "x" (Var "y"), + parserTest "\\x->y+z+5" $ Lambda "x" (Add (Add (Var "y") (Var "z")) (CstInt 5)) + ], + testGroup + "TryCatch" + [ parserTest "try x catch y" $ TryCatch (Var "x") (Var "y") + ], + testGroup + "Let" + [ parserTest "let x=5 in y" $ Let "x" (CstInt 5) (Var "y") ] ]