General refactor for legibility

Priming to update all source to lhs and document extensively
This commit is contained in:
2025-01-19 14:41:25 -06:00
committed by James Eversole
parent 63aa977efd
commit e6e05b607a
6 changed files with 127 additions and 171 deletions

View File

@ -17,18 +17,20 @@ type AltParser = Parsec Void String
parseTricu :: String -> [TricuAST]
parseTricu input
| null tokens = []
| otherwise = map parseSingle tokens
| otherwise = map parseSingle tokens
where
tokens = case lexTricu input of
[] -> []
tokens -> lines input
tokens
| null (lexTricu input) = []
| otherwise = lines input
parseSingle :: String -> TricuAST
parseSingle input = case lexTricu input of
[] -> SEmpty
tokens -> case runParser parseExpression "" tokens of
Left err -> error $ handleParseError err
Right ast -> ast
parseSingle input
| null tokens = SEmpty
| Left err <- parsed = error $ handleParseError err
| Right ast <- parsed = ast
where
tokens = lexTricu input
parsed = runParser parseExpression "" tokens
parseExpression :: Parser TricuAST
parseExpression = choice
@ -115,10 +117,10 @@ parseTreeTerm = do
rest <- many parseTreeLeafOrParenthesized
pure $ foldl combine base rest
where
combine acc next = case acc of
TLeaf -> TStem next
TStem t -> TFork t next
TFork _ _ -> TFork acc next
combine acc next
| TLeaf <- acc = TStem next
| TStem t <- acc = TFork t next
| TFork _ _ <- acc = TFork acc next
parseTreeLeafOrParenthesized :: Parser TricuAST
parseTreeLeafOrParenthesized = choice
@ -181,9 +183,9 @@ parseSingleItem :: Parser TricuAST
parseSingleItem = do
token <- satisfy isListItem
case token of
LIdentifier name -> return (SVar name)
LKeywordT -> return TLeaf
_ -> fail "Unexpected token in list item"
_ | LIdentifier name <- token -> return (SVar name)
| LKeywordT <- token -> return TLeaf
| otherwise -> fail "Unexpected token in list item"
isListItem :: LToken -> Bool
isListItem (LIdentifier _) = True
@ -254,9 +256,11 @@ parseTernaryFork = do
pure $ TFork term1 term2
parseTernary :: String -> Either String TricuAST
parseTernary input = case runParser (parseTernaryTerm <* eof) "" input of
Left err -> Left (errorBundlePretty err)
Right ast -> Right ast
parseTernary input
| Left err <- result = Left (errorBundlePretty err)
| Right ast <- result = Right ast
where
result = runParser (parseTernaryTerm <* eof) "" input
-- Error Handling
handleParseError :: ParseErrorBundle [LToken] Void -> String