aboutsummaryrefslogtreecommitdiff
path: root/src/Env.hs
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-08-13 13:57:44 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-08-13 13:57:44 +0100
commit241b6af0794e020a29da9205c3843554ce0a43b5 (patch)
tree2a155cf826d0efedbfba1369f030704c591908dd /src/Env.hs
parentcecdf2f0e9f7ba159b592f0eeacedf34a14b6ca6 (diff)
downloadmicro-lang-hs-241b6af0794e020a29da9205c3843554ce0a43b5.tar.gz
micro-lang-hs-241b6af0794e020a29da9205c3843554ce0a43b5.zip
Improved documentation
Diffstat (limited to 'src/Env.hs')
-rw-r--r--src/Env.hs21
1 files changed, 12 insertions, 9 deletions
diff --git a/src/Env.hs b/src/Env.hs
index bf81899..678903e 100644
--- a/src/Env.hs
+++ b/src/Env.hs
@@ -14,8 +14,8 @@ data Env = Env SymMap (Maybe Env) deriving (Show)
emptyEnv = Env Map.empty Nothing
--- first parameter tells if we look on the local environment
--- or if we should check also in the parent(s)
+-- | @getSymB local env ident@ checks @local@ parameter to tell if we look on
+-- the local environment or if we should check also in the parent(s).
getSymB :: Bool -> Env -> A.Ident -> Maybe Sym
getSymB local (Env m parent) id =
case (local, Map.lookup id m) of
@@ -24,23 +24,25 @@ getSymB local (Env m parent) id =
getSym p id
(_, s) -> s
--- get symbol
+-- | Gets a symbol checking all the environments.
getSym :: Env -> A.Ident -> Maybe Sym
getSym = getSymB False
--- get symbol local
+-- | Gets a symbol checking the local environment.
getSyml :: Env -> A.Ident -> Maybe Sym
getSyml = getSymB True
--- if a symbol exists
+-- | Checks if a symbol exists.
existsSym :: Env -> A.Ident -> Bool
existsSym env sym = isJust $ getSym env sym
--- if a local symbol exists
+-- | Checks if a local symbol exists in the local environment.
existsSyml :: Env -> A.Ident -> Bool
existsSyml env sym = isJust $ getSyml env sym
--- add symbol
+-- | @addSym e s@ add symbol @s@ to enviroment @e@ and returns the modified
+-- environment. It will create a new enviroment if the symbol already exists
+-- (shadowing).
addSym :: Env -> Sym -> Env
addSym (Env m parent) (id, typ, pos) = case getSym env id of
Nothing -> Env (Map.insert id sym m) parent
@@ -49,11 +51,12 @@ addSym (Env m parent) (id, typ, pos) = case getSym env id of
env = (Env m parent)
sym = (id, typ, pos)
--- adds a new local environment
+-- | @addEnv e@ adds a new local environment using @e@ as parent.
addEnv :: Env -> Env
addEnv env = Env Map.empty $ Just env
--- add a local symbol if it doesn't exist
+-- | @addSymUniq e s@ add a local symbol @s@ to the enviroment @e@ if it
+-- doesn't exist.
addSymUniq :: Env -> Sym -> Either Error Env
addSymUniq ev (id, typ, pos) = case getSyml ev id of
Nothing -> Right $ addSym ev sym