From 5410b383758ffb04868488618260d2e6aa0c7053 Mon Sep 17 00:00:00 2001 From: Nils 'Linkie Pi' Reid Date: Fri, 8 May 2020 16:14:00 +0200 Subject: [PATCH] Implemented the `while` structure in the evaluator. --- src/Eval.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Eval.hs b/src/Eval.hs index 5b1bcd75..959dfa7a 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -247,6 +247,8 @@ eval ctx xobj@(XObj o i t) = case acc of err@(Left _) -> return (ctx, err) Right _ -> eval ctx x + [XObj While _ _, cond, body] -> + specialCommandWhile ctx cond body [] -> return (ctx, dynamicNil) x -> do return (evalError ctx ("I did not understand the form `" ++ pretty xobj ++ "`") (info xobj)) @@ -471,6 +473,23 @@ specialCommandDefine ctx xobj = Left err -> return (ctx, Left err) +specialCommandWhile :: Context -> XObj -> XObj -> IO (Context, Either EvalError XObj) +specialCommandWhile ctx cond body = do + (newCtx, evd) <- eval ctx cond + case evd of + Right c -> + case obj c of + Bol b -> if b + then do + (newCtx, _) <- eval newCtx body + specialCommandWhile newCtx cond body + else + return (newCtx, dynamicNil) + _ -> + return (evalError ctx ("This `while` condition contains the non-boolean value '" ++ + pretty c ++ "`") (info c)) + Left e -> return (newCtx, Left e) + getSigFromDefnOrDef :: Context -> Env -> FilePathPrintLength -> XObj -> (Either EvalError (Maybe (Ty, XObj))) getSigFromDefnOrDef ctx globalEnv fppl xobj = let metaData = existingMeta globalEnv xobj