1 module Env (Env, initEnv, lookupEnv, enterEnv) where 2 3 import Ast 4 import BasicNumber 5 6 -- environment is a list of variable bindings. 7 type Env = [(String, BasicExp)] 8 9 -- initial environment 10 initEnv :: Env -> Env 11 initEnv _ = [("$prec", (Numb 20))] 12 13 -- environment lookup 14 lookupEnv :: String -> Env -> BasicExp 15 lookupEnv str [] = BSError 16 lookupEnv str ((s,bexp):es) = if s==str then bexp 17 else lookupEnv str es 18 19 -- enter a new element to the environment, remove the duplicate if exists. 20 enterEnv :: String -> BasicExp -> Env -> Env 21 enterEnv str bexp env = (str, bexp):env1 22 where 23 env1 = filter (\c@(s,e) -> s/=str) env 24 25 26 27 28 29