Additional test cases and bug fixes for subtle issues

This commit is contained in:
James Eversole 2024-12-19 19:53:32 -06:00
parent 602048dc53
commit d13b2f52e7
2 changed files with 30 additions and 2 deletions

View File

@ -20,6 +20,7 @@ data SaplingAST
deriving (Show, Eq, Ord) deriving (Show, Eq, Ord)
parseSapling :: String -> SaplingAST parseSapling :: String -> SaplingAST
parseSapling "" = error "Empty input provided to parseSapling"
parseSapling input = case runParser parseExpression "" (lexSapling input) of parseSapling input = case runParser parseExpression "" (lexSapling input) of
Left err -> error "Failed to parse input" Left err -> error "Failed to parse input"
Right ast -> ast Right ast -> ast
@ -122,8 +123,14 @@ parseListLiteral = do
satisfy (== LCloseBracket) satisfy (== LCloseBracket)
return (SList elements) return (SList elements)
--parseListItem :: Parser SaplingAST
--parseListItem = parseGroupedItem <|> parseSingleItem
parseListItem :: Parser SaplingAST parseListItem :: Parser SaplingAST
parseListItem = parseGroupedItem <|> parseSingleItem parseListItem = choice
[ parseGroupedItem -- Handle expressions inside parentheses
, parseListLiteral -- Allow nested lists
, parseSingleItem -- Handle single tokens like `t` or identifiers
]
parseGroupedItem :: Parser SaplingAST parseGroupedItem :: Parser SaplingAST
parseGroupedItem = do parseGroupedItem = do

View File

@ -5,6 +5,7 @@ import Lexer
import Parser import Parser
import Research import Research
import Control.Exception (evaluate, try, SomeException)
import qualified Data.Map as Map import qualified Data.Map as Map
import Test.Tasty import Test.Tasty
import Test.Tasty.HUnit import Test.Tasty.HUnit
@ -58,6 +59,27 @@ parserTests = testGroup "Parser Tests"
let input = "t t t" let input = "t t t"
let expected = TFork TLeaf TLeaf let expected = TFork TLeaf TLeaf
parseSapling input @?= expected parseSapling input @?= expected
, testCase "Parse mixed list literals" $ do
-- You must put non-listliterals in parentheses
let input = "[t (\"hello\") t]"
let expected = SList [TLeaf, SStr "hello", TLeaf]
parseSapling input @?= expected
, testCase "Parse function with applications" $ do
let input = "f x = t x"
let expected = SFunc "f" ["x"] (SApp TLeaf [SVar "x"])
parseSapling input @?= expected
, testCase "Parse nested lists" $ do
let input = "[t [(t t)]]"
let expected = SList [TLeaf, SList [TStem TLeaf]]
parseSapling input @?= expected
, testCase "Parse complex parentheses" $ do
let input = "t (t t (t t))"
let expected = TStem (TFork TLeaf (TStem TLeaf))
parseSapling input @?= expected
] ]
integrationTests :: TestTree integrationTests :: TestTree
@ -138,4 +160,3 @@ propertyTests = testGroup "Property Tests"
Left _ -> property True Left _ -> property True
Right ast -> parseSapling input === ast Right ast -> parseSapling input === ast
] ]