module Main where

import AST
import Eval (eval)
import Parser (parseProgram)
import System.Environment (getArgs)

type Cont = Val -> Val

isFun :: Val -> Bool
isFun (ValFun _) = True
isFun _ = False

isBool :: Val -> Bool
isBool (ValBool _) = True
isBool _ = False

isPair :: Val -> Bool
isPair (ValPair _) = True
isPair _ = False

env :: Env
env = [ ("succ", ValFun (\(ValInt n) k -> k (ValInt (n + 1))))
      , ("equal", ValFun (\ (ValInt n) k -> k (ValFun (\ (ValInt m) k' -> k' (ValBool (n == m))))))
      , ("equalB", ValFun (\ (ValBool b) k -> k (ValFun (\ (ValBool c) k' -> k' (ValBool (b == c))))))
      , ("equalS", ValFun (\ (ValString s) k -> k (ValFun (\ (ValString q) k' -> k' (ValBool (s == q))))))
      , ("fst", ValFun (\ (ValPair (v1,v2)) k -> k v1))
      , ("snd", ValFun (\ (ValPair (v1,v2)) k -> k v2))
      , ("add", ValFun (\ (ValInt n) k -> k (ValFun (\ (ValInt m) k' -> k' (ValInt (n + m))))))
      , ("minus", ValFun (\ (ValInt n) k -> k (ValFun (\ (ValInt m) k' -> k' (ValInt (n - m))))))
      , ("isValFun", ValFun (\ f k -> k (ValBool (isFun f))))
      , ("isValBool", ValFun (\ b k -> k (ValBool (isBool b))))
      , ("isValPair", ValFun (\ p k -> k (ValBool (isPair p))))
      ]


main :: IO ()
main = do
  args <- getArgs
  case args of
    [filename] -> do
      input <- readFile filename
      case parseProgram input of
        Left err -> putStrLn $ "Error: " ++ show err
        Right exp -> do
          putStrLn $ "Parsed expression: " ++ show exp
          let val = eval exp env id
          putStrLn $ "Evaluated value: " ++ show val
    _ -> putStrLn "Usage: interp4 <filename>"