module Parser (parseExp, parseProgram) where

import AST (Exp (..), Val (ValBool, ValInt),)
import Control.Applicative ((<|>))
import Lexer
  ( identifier,
    integer,
    reserved,
    reservedOp,
    symbol,
    whitespace,
    parens,
  )
import Text.Parsec (ParseError, eof, parse, many, notFollowedBy, noneOf, try)
import Text.Parsec.String (Parser)
import Control.Monad (void)

parseExp :: Parser Exp
parseExp =
  parseConst
    <|> parseLambda
    <|> parseCond
    <|> parseLetRec
    <|> parseApp
    <|> parseVar
    <|> parens parseExp

parseNonAppExp :: Parser Exp
parseNonAppExp =
  parseConst
    <|> parseLambda
    <|> parseCond
    <|> parseLetRec
    <|> parens parseExp
    <|> parseVar

parseApp :: Parser Exp
parseApp = do
  e1 <- parseNonAppExp
  es <- many (try (notFollowedBy (reserved "in" <|> reserved "then" <|> reserved "else") *> parseNonAppExp))
  return $ foldl App e1 es

parseConst :: Parser Exp
parseConst =
  Const . ValInt . fromIntegral <$> integer
    <|> (reserved "True" >> return (Const (ValBool True)))
    <|> (reserved "False" >> return (Const (ValBool False)))


parseVar :: Parser Exp
parseVar = Var <$> identifier

parseLambda :: Parser Exp
parseLambda = do
  symbol "\\"
  v <- identifier
  reservedOp "->"
  Lambda v <$> parseExp

parseCond :: Parser Exp
parseCond = do
  reserved "if"
  e1 <- parseExp
  reserved "then"
  e2 <- parseExp
  reserved "else"
  Cond e1 e2 <$> parseExp

parseLetRec :: Parser Exp
parseLetRec = do
  reserved "letrec"
  f <- identifier
  x <- identifier
  reservedOp "="
  e1 <- parseExp
  reserved "in"
  LetRec f x e1 <$> parseExp

parseProgram :: String -> Either ParseError Exp
parseProgram = parse (whitespace *> parseExp <* eof) "<input>"