fix: replace qualified bindings (#1125)

Previously, we couldn't replace qualified bindings in Envs. This change
updates envReplaceBindings to accept qualified paths and to replace the
binding denoted by the path, updating the module environments the binder
belongs to accordingly.

If the qualified path does not denote an existing module, it simply
returns the environment as is (just like the unqualified case).
This commit is contained in:
Scott Olsen 2021-01-12 15:37:27 -05:00 committed by GitHub
parent afa9b1223d
commit 02e04f33b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
module Env where
import Data.List (foldl')
import Data.Maybe (fromMaybe)
import qualified Map
import Obj
import Types
@ -70,7 +71,12 @@ envReplaceBinding s@(SymPath [] name) binder env =
case envParent env of
Just parent -> env {envParent = Just (envReplaceBinding s binder parent)}
Nothing -> env
envReplaceBinding (SymPath _ _) _ _ = error "TODO: cannot replace qualified bindings"
envReplaceBinding s@(SymPath (p : ps) name) binder env =
case Map.lookup p (envBindings env) of
Just b@(Binder _ (XObj (Mod innerEnv) i t)) ->
envReplaceBinding (SymPath [] p) b {binderXObj = (XObj (Mod (envReplaceBinding (SymPath ps name) binder innerEnv)) i t)} env
_ ->
fromMaybe env (envParent env >>= \parent -> Just (env {envParent = Just (envReplaceBinding s binder parent)}))
envBindingNames :: Env -> [String]
envBindingNames = concatMap select . envBindings