Added some testing functions, apply and currification

This commit is contained in:
Ivy 2021-01-30 20:21:25 +01:00
parent f7119b1dd3
commit 8043186001
3 changed files with 23 additions and 7 deletions

View File

@ -8,7 +8,9 @@ data Enviroment = Enviroment (M.Map String Expr) (Maybe Enviroment)
base :: Enviroment
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
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 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)

View File

@ -8,12 +8,18 @@ import Data.Maybe
-- TODO: create a BuiltinProc or something like that in data Expr, and make + a builtin proc
eval :: Enviroment -> Expr -> (Enviroment, Expr)
eval env i@(IntE _) = (env, i)
eval env (VarE v) = eval env $ fromJust $ lookupVar env v
eval env (SetE v expr) = (insertVar env v expr, NilE)
eval env i@(IntE _) = (env, i)
eval env (VarE v) = eval env $ fromJust $ lookupVar env v
eval env (SetE v expr) = (insertVar env v expr, NilE)
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)
apply :: Expr -> Expr -> Expr
apply car cdr = undefined
apply :: Expr -> Expr -> (Enviroment, Expr)
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)

View File

@ -4,6 +4,7 @@ data Expr = IntE Integer
| VarE String
| SetE String Expr
| ConsE Expr Expr
| LambdaE String Expr
| QuotedE Expr
| NilE
deriving (Show)