Last active
February 27, 2018 12:03
-
-
Save ilmoeuro/2d59db7a168bcb95a764d8c3b2416f55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
makeList :: IORef Env -> [Expression] -> IO Value | |
makeList env elemExprs = do | |
elems <- traverse (eval env) elemExprs | |
ListValue <$> newIORef elems | |
makeFunction :: IORef Env -> [String] -> Block -> IO Value | |
makeFunction _ _ _ = pure . FunctionValue . const $ pure NullValue | |
applyFunction :: IORef Env -> Expression -> [Expression] -> IO Value | |
applyFunction env f args = do | |
args' <- traverse (eval env) args | |
(FunctionValue f') <- eval env f | |
f' args' | |
eval :: IORef Env -> Expression -> IO Value | |
eval _ NullLiteral = pure NullValue | |
eval _ (NumericLiteral val) = pure $ NumericValue val | |
eval _ (StringLiteral val) = pure $ StringValue val | |
eval e (Variable s) = (Map.! s) . variables <$> readIORef e | |
eval e (Negate val) = liftNumeric negate <$> eval e val | |
eval e (a :+ b) = liftNumeric2 (+) <$> eval e a <*> eval e b | |
eval e (a :- b) = liftNumeric2 (-) <$> eval e a <*> eval e b | |
eval e (a :* b) = liftNumeric2 (*) <$> eval e a <*> eval e b | |
eval e (a :/ b) = liftNumeric2 (/) <$> eval e a <*> eval e b | |
eval e (FunctionCall f args) = applyFunction e f args | |
eval e (Function args body) = makeFunction e args body | |
eval e (List elems) = makeList e elems |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment