Added some testing functions, apply and currification
This commit is contained in:
parent
f7119b1dd3
commit
8043186001
|
@ -8,7 +8,9 @@ data Enviroment = Enviroment (M.Map String Expr) (Maybe Enviroment)
|
||||||
|
|
||||||
base :: Enviroment
|
base :: Enviroment
|
||||||
base = Enviroment (M.fromList [
|
base = Enviroment (M.fromList [
|
||||||
("test-var", IntE 10)
|
("test-var", IntE 10),
|
||||||
|
("id", LambdaE "x" $ VarE "x"),
|
||||||
|
("first", LambdaE "x" $ LambdaE "y" $ VarE "x")
|
||||||
]) Nothing
|
]) Nothing
|
||||||
|
|
||||||
lookupVar :: Enviroment -> String -> Maybe Expr
|
lookupVar :: Enviroment -> String -> Maybe Expr
|
||||||
|
@ -17,3 +19,10 @@ lookupVar (Enviroment menv upperEnv) var = let mExpr = M.lookup var menv in
|
||||||
|
|
||||||
insertVar :: Enviroment -> String -> Expr -> Enviroment
|
insertVar :: Enviroment -> String -> Expr -> Enviroment
|
||||||
insertVar (Enviroment menv u) var expr = Enviroment (M.insert var expr menv) u
|
insertVar (Enviroment menv u) var expr = Enviroment (M.insert var expr menv) u
|
||||||
|
|
||||||
|
emptyEnv :: Enviroment
|
||||||
|
emptyEnv = Enviroment M.empty Nothing
|
||||||
|
|
||||||
|
extendEnv :: Enviroment -> Enviroment -> Enviroment
|
||||||
|
extendEnv (Enviroment menv Nothing) upper = Enviroment menv (Just upper)
|
||||||
|
extendEnv (Enviroment menv (Just upperMenv)) upper = Enviroment menv (Just $ extendEnv upperMenv upper)
|
||||||
|
|
|
@ -8,12 +8,18 @@ import Data.Maybe
|
||||||
-- 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)
|
eval :: Enviroment -> Expr -> (Enviroment, Expr)
|
||||||
eval env i@(IntE _) = (env, i)
|
eval env i@(IntE _) = (env, i)
|
||||||
eval env (VarE v) = eval env $ fromJust $ lookupVar env v
|
eval env (VarE v) = eval env $ fromJust $ lookupVar env v
|
||||||
eval env (SetE v expr) = (insertVar env v expr, NilE)
|
eval env (SetE v expr) = (insertVar env v expr, NilE)
|
||||||
eval env (QuotedE e) = (env, e)
|
eval env (QuotedE e) = (env, e)
|
||||||
eval env (ConsE car cdr) = (env, apply car cdr)
|
eval env (ConsE p args) = (fEnv, fExpr)
|
||||||
|
where (uEnv, ap) = eval env p
|
||||||
|
(aEnv, aExpr) = apply ap args
|
||||||
|
(fEnv, fExpr) = eval (extendEnv aEnv uEnv) aExpr
|
||||||
|
eval env (LambdaE arg expr) = (env, LambdaE arg expr)
|
||||||
eval env NilE = (env, NilE)
|
eval env NilE = (env, NilE)
|
||||||
|
|
||||||
apply :: Expr -> Expr -> Expr
|
apply :: Expr -> Expr -> (Enviroment, Expr)
|
||||||
apply car cdr = undefined
|
apply (LambdaE p expr) (ConsE x xs) = (extendEnv (insertVar emptyEnv p x) nEnv, nExp)
|
||||||
|
where (nEnv, nExp) = apply expr xs
|
||||||
|
apply e NilE = (emptyEnv, e)
|
||||||
|
|
|
@ -4,6 +4,7 @@ data Expr = IntE Integer
|
||||||
| VarE String
|
| VarE String
|
||||||
| SetE String Expr
|
| SetE String Expr
|
||||||
| ConsE Expr Expr
|
| ConsE Expr Expr
|
||||||
|
| LambdaE String Expr
|
||||||
| QuotedE Expr
|
| QuotedE Expr
|
||||||
| NilE
|
| NilE
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
Loading…
Reference in New Issue