From 47ab82fbcc6f59a936d42c96c05d8630576126fe Mon Sep 17 00:00:00 2001 From: Tipbs Date: Sun, 14 Jan 2024 20:55:33 +0100 Subject: [PATCH] [DEL] Old parser file --- src/Parser.hs | 140 -------------------------------------------------- 1 file changed, 140 deletions(-) delete mode 100644 src/Parser.hs diff --git a/src/Parser.hs b/src/Parser.hs deleted file mode 100644 index bdbfca6..0000000 --- a/src/Parser.hs +++ /dev/null @@ -1,140 +0,0 @@ -{-# LANGUAGE InstanceSigs #-} -module Parser (LispVal (..), lispValP, charP, stringP, Parser (runParser)) where -import Control.Applicative (Alternative (empty, some, many), (<|>)) -import Data.Char (isDigit, isLetter, isSpace) - -data LispVal = Atom String - | List [LispVal] - | DottedList [LispVal] LispVal - | Number Int - | String String - | Bool Bool - | Func { name :: String, params :: [String], - body :: [LispVal] } - deriving (Eq) -instance Show LispVal where show = showVal - -unwordsList :: [LispVal] -> String -unwordsList = unwords . map showVal - -showVal :: LispVal -> String -showVal (String contents) = "\"" ++ contents ++ "\"" -showVal (Atom name) = name -showVal (Number contents) = show contents -showVal (Bool True) = "#t" -showVal (Bool False) = "#f" -showVal (List contents) = "(" ++ unwordsList contents ++ ")" -showVal (DottedList h t) = "(" ++ unwordsList h ++ " . " ++ showVal t ++ ")" - -newtype Parser a = Parser { - runParser :: String -> Maybe (a, String) -} - -instance Functor Parser where - fmap :: (a -> b) -> Parser a -> Parser b - fmap fun (Parser p) = Parser f - where - f input = do - (x, xs) <- p input - Just (fun x, xs) - -instance Applicative Parser where - pure :: a -> Parser a - pure x = Parser $ \input -> Just (x, input) - (<*>) :: Parser (a -> b) -> Parser a -> Parser b -- voir exemples avec Maybe - (Parser p1) <*> (Parser p2) = Parser $ \input -> do - (f, inp) <- p1 input - (second, inp') <- p2 inp - Just (f second, inp') - -instance Alternative Parser where - empty :: Parser a - empty = Parser $ const Nothing - (<|>) :: Parser a -> Parser a -> Parser a - (Parser p1) <|> (Parser p2) = Parser $ \input -> p1 input <|> p2 input - -charP :: Char -> Parser Char -charP c = Parser f - where - f (x : xs) - | c == x = Just (c, xs) - | otherwise = Nothing - f [] = Nothing - -stringP :: String -> Parser String -stringP = traverse charP - -letterP :: Parser Char -letterP = Parser f - where - f (x: xs) | isLetter x = Just (x, xs) - f _ = Nothing - --- manyP :: Parser [a] --- manyP = traverse - -spanP :: (Char -> Bool) -> Parser String -spanP f = Parser $ \input -> Just $ span f input - -notNull :: Parser [a] -> Parser [a] -notNull p = Parser $ \input -> do - (x, inp) <- runParser p input - if null x - then Nothing - else Just (x, inp) - -lispNumberP :: Parser LispVal -lispNumberP = f <$> notNull (spanP isDigit) - where - f d = Number $ read d - -stringLiteral :: Parser String -stringLiteral = spanP (/= '\"') - -lispStringP :: Parser LispVal -lispStringP = String <$> (charP '\"' *> stringLiteral <* charP '\"') - -oneOfP :: [Char] -> Parser Char -oneOfP ch = Parser f - where - f (x: xs) | x `elem` ch = Just (x, xs) - f _ = Nothing - -symbolP :: Parser Char -symbolP = oneOfP "!#$%&|*+-/:<=>?@^_~" - -lispAtomP :: Parser LispVal -lispAtomP = f <$> validAtom - where - validAtom = some (symbolP <|> letterP) - f a = case a of - "#t" -> Bool True - "#f" -> Bool False - _ -> Atom a - -skipManyP :: (Char -> Bool) -> Parser [Char] -skipManyP = spanP - -sepByP :: Parser a -> Parser b -> Parser [b] -sepByP sep element = many (sep *> element <* sep) - -lispListP :: Parser LispVal -lispListP = List <$> (charP '(' *> elements <* charP ')') - where - elements = sepByP sep lispValP - sep = skipManyP isSpace - -lispQuotedP :: Parser LispVal -lispQuotedP = f <$> (charP '\'' *> lispValP) - where - f a = List [Atom "quote", a] - -lispValP :: Parser LispVal -lispValP = lispNumberP <|> lispStringP <|> lispAtomP <|> lispListP <|> lispQuotedP - --- atomP :: Parser LispVal --- atomP str = fmap transf $ letterP str *> manyP --- where --- transf "#t" = Bool True --- transf "#f" = Bool False --- transf atom = Atom atom