diff options
author | Marvin Borner | 2022-04-12 14:20:22 +0200 |
---|---|---|
committer | Marvin Borner | 2022-04-12 14:20:22 +0200 |
commit | af953120359d9d65f2841f4ec67c4e2178d282af (patch) | |
tree | e0ee80be02ffa37c6f51cfdcf4ffe7a548e4adc4 /src/Eval.hs | |
parent | 32515bf8bf04958f22ce2cfe98edebc7b892774c (diff) |
OK
Diffstat (limited to 'src/Eval.hs')
-rw-r--r-- | src/Eval.hs | 54 |
1 files changed, 50 insertions, 4 deletions
diff --git a/src/Eval.hs b/src/Eval.hs index 72c8985..4e73778 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -4,22 +4,68 @@ module Eval import Control.Exception import Control.Monad.State +import Debug.Trace +import Parser import System.Console.Haskeline import System.Environment import System.Exit import System.IO +import Text.Parsec hiding ( State + , try + ) -type Environment = [String] +type Environment = [(String, Expression)] +type Program = State Environment -eval :: String -> IO () -eval code = putStrLn "ok" +eval :: [String] -> Environment -> IO Environment +eval [] env = pure env +eval (line : ls) env = case parse parseLine "Evaluator" line of + Left err -> print err >> pure env + Right instr -> case instr of + Define name exp -> + let (res, env') = evalDefine name exp `runState` env + in case res of + Left err -> print err >> eval ls env' + Right _ -> eval ls env' + Evaluate exp -> putStrLn "ok" >> pure env + _ -> eval ls env + +evalVar :: String -> Program (Failable Expression) +evalVar var = state $ \e -> + ( case lookup var e of + Nothing -> Left $ UndeclaredFunction var + Just x -> Right x + , e + ) + +evalApp :: Expression -> Expression -> Program (Failable Expression) +evalApp f g = + evalExp f + >>= (\case + Left e -> pure $ Left e + Right f' -> fmap (Application f') <$> evalExp g + ) + +evalExp :: Expression -> Program (Failable Expression) +evalExp idx@(Bruijn _ ) = pure $ Right idx +evalExp ( Variable var) = evalVar var +evalExp ( Abstraction exp) = evalExp exp +evalExp ( Application f g) = evalApp f g + +evalDefine :: String -> Expression -> Program (Failable Expression) +evalDefine name exp = + evalExp exp + >>= (\case + Left e -> pure $ Left e + Right f -> modify ((name, f) :) >> pure (Right f) + ) evalFile :: String -> IO () evalFile path = do file <- try $ readFile path :: IO (Either IOError String) case file of Left exception -> print (exception :: IOError) - Right file -> eval file + Right file -> eval (lines file) [] >> putStrLn "Done" evalRepl :: String -> Environment -> InputT IO Environment evalRepl line env = outputStrLn (show env) >> pure env |