summaryrefslogtreecommitdiff
path: root/AST.hs
diff options
context:
space:
mode:
Diffstat (limited to 'AST.hs')
-rw-r--r--AST.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/AST.hs b/AST.hs
index 2953227..dbe8e34 100644
--- a/AST.hs
+++ b/AST.hs
@@ -1,5 +1,8 @@
module AST where
+import qualified Data.Map.Strict as Map
+import Data.Maybe
+
import Data.List
@@ -52,3 +55,22 @@ fromVNum _ = Nothing
fromVString :: Value -> Maybe String
fromVString (VString s) = Just s
fromVString _ = Nothing
+
+replaceNames :: Map.Map Name Value -> Value -> Value
+replaceNames mp origValue =
+ case origValue of
+ VList vs -> VList (map (replaceNames mp) vs)
+ VName n -> fromMaybe origValue (Map.lookup n mp)
+ VDefine n v -> VDefine n (replaceNames mp v)
+ VLambda as v -> VLambda as (replaceNames (foldr Map.delete mp as) v)
+ VLambdaRec rn as v -> VLambdaRec rn as (replaceNames (foldr Map.delete mp (rn : as)) v)
+ VLet [] v -> VLet [] (replaceNames mp v)
+ VLet ((n, d) : pairs) v ->
+ let VLet pairs' v' = replaceNames (Map.delete n mp) (VLet pairs v)
+ in VLet ((n, replaceNames mp d) : pairs') v'
+ VNum _ -> origValue
+ VString _ -> origValue
+ VQuoted _ -> origValue
+ VDeclare _ -> origValue
+ VBuiltin _ -> origValue
+ VEllipsis -> origValue