Added State monad for better readability & manintenance
This commit is contained in:
parent
38e55f5258
commit
3ac3f8f11a
|
@ -28,4 +28,5 @@ executable cherry
|
||||||
build-depends: base >= 4.7 && < 5,
|
build-depends: base >= 4.7 && < 5,
|
||||||
containers,
|
containers,
|
||||||
parsec,
|
parsec,
|
||||||
haskeline
|
haskeline,
|
||||||
|
mtl
|
||||||
|
|
|
@ -13,17 +13,17 @@ base = Enviroment (M.fromList [
|
||||||
("const", LambdaE "x" $ LambdaE "y" $ VarE "x") -- Given two expressions, returns the first expression
|
("const", LambdaE "x" $ LambdaE "y" $ VarE "x") -- Given two expressions, returns the first expression
|
||||||
]) Nothing
|
]) Nothing
|
||||||
|
|
||||||
lookupVar :: Enviroment -> String -> Maybe Expr
|
lookupVar :: String -> Enviroment -> Maybe Expr
|
||||||
lookupVar (Enviroment menv upperEnv) var = let mExpr = M.lookup var menv in
|
lookupVar var (Enviroment menv upperEnv) = let mExpr = M.lookup var menv in
|
||||||
if isNothing mExpr then upperEnv >>= (`lookupVar` var) else mExpr
|
if isNothing mExpr then upperEnv >>= lookupVar var else mExpr
|
||||||
|
|
||||||
insertVar :: Enviroment -> String -> Expr -> Enviroment
|
insertVar :: String -> Expr -> Enviroment -> Enviroment
|
||||||
insertVar (Enviroment menv u) var expr = Enviroment (M.insert var expr menv) u
|
insertVar var expr (Enviroment menv u) = Enviroment (M.insert var expr menv) u
|
||||||
|
|
||||||
emptyEnv :: Enviroment
|
emptyEnv :: Enviroment
|
||||||
emptyEnv = Enviroment M.empty Nothing
|
emptyEnv = Enviroment M.empty Nothing
|
||||||
|
|
||||||
-- Returns a copy of the first enviroment whose upper enviroment is the second
|
-- Returns a copy of the second enviroment whose upper enviroment is the first
|
||||||
extendEnv :: Enviroment -> Enviroment -> Enviroment
|
extendEnv :: Enviroment -> Enviroment -> Enviroment
|
||||||
extendEnv (Enviroment menv Nothing) upper = Enviroment menv (Just upper)
|
extendEnv upper (Enviroment menv Nothing) = Enviroment menv (Just upper)
|
||||||
extendEnv (Enviroment menv (Just upperMenv)) upper = Enviroment menv (Just $ extendEnv upperMenv upper)
|
extendEnv upper (Enviroment menv (Just upperMenv)) = Enviroment menv (Just $ extendEnv upperMenv upper)
|
||||||
|
|
|
@ -3,23 +3,30 @@ module Evaluator where
|
||||||
import Expression
|
import Expression
|
||||||
import Enviroment
|
import Enviroment
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
import Control.Monad.State
|
||||||
|
|
||||||
-- TODO: create a separated file for builtinProcs
|
-- TODO: create a separated file for builtinProcs
|
||||||
-- TODO: create a BuiltinProc or something like that in data Expr, and make + a builtin proc
|
-- TODO: create a BuiltinProc or something like that in data Expr, and make + a builtin proc
|
||||||
|
|
||||||
eval :: Enviroment -> Expr -> (Enviroment, Expr)
|
evalS :: Expr -> State Enviroment Expr
|
||||||
eval env i@(IntE _) = (env, i)
|
evalS i@(IntE _) = return i
|
||||||
eval env (VarE v) = eval env $ fromJust $ lookupVar env v
|
evalS (VarE v) = do
|
||||||
eval env (SetE v expr) = (insertVar env v expr, NilE)
|
get >>= evalS . fromJust . lookupVar v
|
||||||
eval env (QuotedE e) = (env, e)
|
evalS (SetE v expr) = do
|
||||||
eval env (ConsE p args) = (fEnv, fExpr)
|
get >>= put . insertVar v expr
|
||||||
where (uEnv, ap) = eval env p
|
return NilE
|
||||||
(aEnv, aExpr) = apply ap args
|
evalS (QuotedE e) = return e
|
||||||
(fEnv, fExpr) = eval (extendEnv aEnv uEnv) aExpr
|
evalS (ConsE pr args) = do
|
||||||
eval env (LambdaE arg expr) = (env, LambdaE arg expr)
|
evaluatedProc <- evalS pr
|
||||||
eval env NilE = (env, NilE)
|
resExpr <- applyS evaluatedProc args
|
||||||
|
finalExpr <- evalS resExpr
|
||||||
|
return finalExpr
|
||||||
|
evalS l@(LambdaE _ _) = return l
|
||||||
|
evalS NilE = return NilE
|
||||||
|
|
||||||
apply :: Expr -> Expr -> (Enviroment, Expr)
|
applyS :: Expr -> Expr -> State Enviroment Expr
|
||||||
apply (LambdaE p expr) (ConsE x xs) = (extendEnv (insertVar emptyEnv p x) nEnv, nExp)
|
applyS (LambdaE p expr) (ConsE x xs) = do
|
||||||
where (nEnv, nExp) = apply expr xs
|
e <- applyS expr xs
|
||||||
apply e NilE = (emptyEnv, e)
|
get >>= put . (`extendEnv` insertVar p x emptyEnv)
|
||||||
|
return e
|
||||||
|
applyS e NilE = return e
|
||||||
|
|
|
@ -6,6 +6,7 @@ import Parser
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import System.Console.Haskeline
|
import System.Console.Haskeline
|
||||||
|
import Control.Monad.State
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
@ -19,10 +20,10 @@ repl = do
|
||||||
line <- getInputLine "cherry> "
|
line <- getInputLine "cherry> "
|
||||||
unless (isNothing line) $ do
|
unless (isNothing line) $ do
|
||||||
let expr = parseExpression $ fromJust line
|
let expr = parseExpression $ fromJust line
|
||||||
let (nEnv, out) = case expr of
|
let (out, nEnv) = case expr of
|
||||||
(Left err) -> (env, show err)
|
(Left err) -> (show err, env)
|
||||||
(Right expr') -> let (env', nExp) = eval env expr'
|
(Right expr') -> let (nExp, env') = runState (evalS expr') env
|
||||||
in (env', show nExp)
|
in (show nExp, env')
|
||||||
outputStrLn out
|
outputStrLn out
|
||||||
repl' nEnv
|
repl' nEnv
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue