From 551f74a3f77d5f0b7b5221fa38ef67df5790083f Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 14 Nov 2019 17:31:23 +0100 Subject: Implement 'let' --- Compiler.hs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'Compiler.hs') diff --git a/Compiler.hs b/Compiler.hs index 9f8b595..a5a1f3d 100644 --- a/Compiler.hs +++ b/Compiler.hs @@ -36,6 +36,13 @@ analyseValue = go [] | Just names <- mapM fromVName args = go scopes (VLambda names body) | otherwise = error "Invalid 'lambda' syntax: Invalid argument list" go _ (VList (VName "lambda" : _)) = error "Invalid 'lambda' syntax" + go scopes (VList [VName "let", VList args, body]) = + case sequence [if length p == 2 then Just (p !! 0, p !! 1) else Nothing | VList p <- args] of + Just pairs -> + go scopes (VList (VList [VName "lambda", VList (map fst pairs), body] : map snd pairs)) + Nothing -> + error "Invalid 'let' syntax: Invalid variable list (not all pairs)" + go _ (VList (VName "let" : _)) = error "Invalid 'let' syntax: Invalid argument list" go scopes (VList values) = TVList (map (go scopes) values) go _ (VNum n) = TVNum n go _ (VString s) = TVString s @@ -46,6 +53,7 @@ analyseValue = go [] in TVLambda args t (Set.toList (collectEscapes 0 t)) go _ (VBuiltin _) = undefined go _ VEllipsis = TVEllipsis + go _ (VLet _ _) = error "'let' should not be present yet at this point" collectEscapes :: Int -> TaggedValue -> Set.Set Name collectEscapes limit (TVList values) = Set.unions (map (collectEscapes limit) values) -- cgit v1.2.3-54-g00ecf