diff --git a/a3/a3-handout/src/APL/Parser.hs b/a3/a3-handout/src/APL/Parser.hs index 413602a..cbfd1b9 100644 --- a/a3/a3-handout/src/APL/Parser.hs +++ b/a3/a3-handout/src/APL/Parser.hs @@ -49,6 +49,10 @@ lVName = lexeme $ try $ do then fail "Unexpected keyword" else pure v +lStringLit :: Parser String +lStringLit = + lexeme $ read <$> some (satisfy isAlpha) <* notFollowedBy (satisfy isAlphaNum) + lInteger :: Parser Integer lInteger = lexeme $ read <$> some (satisfy isDigit) <* notFollowedBy (satisfy isAlphaNum) @@ -66,6 +70,10 @@ lBool = const False <$> lKeyword "false" ] +pStringLit :: Parser String +pStringLit = + lString "\"" *> lStringLit <* lString "\"" + pAtom :: Parser Exp pAtom = choice @@ -93,6 +101,9 @@ pLExp = <$> (lKeyword "if" *> pExp) <*> (lKeyword "then" *> pExp) <*> (lKeyword "else" *> pExp), + Print + <$> (lKeyword "print" *> pStringLit) + <*> pExp, pFExp ] @@ -101,8 +112,7 @@ pPExp = pLExp >>= chain where chain x = choice - [ - do + [ do lString "**" y <- pPExp chain $ Pow x y, diff --git a/a3/a3-handout/src/APL/Parser_Tests.hs b/a3/a3-handout/src/APL/Parser_Tests.hs index 5ee19eb..985faa6 100644 --- a/a3/a3-handout/src/APL/Parser_Tests.hs +++ b/a3/a3-handout/src/APL/Parser_Tests.hs @@ -82,5 +82,9 @@ tests = [ 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") ] ]