Moved Enviroment into a separate file
This commit is contained in:
parent
65e4502799
commit
f7119b1dd3
|
@ -22,7 +22,8 @@ executable cherry
|
|||
Parser,
|
||||
TokenType,
|
||||
Lexer,
|
||||
Evaluator
|
||||
Evaluator,
|
||||
Enviroment
|
||||
|
||||
build-depends: base >= 4.7 && < 5,
|
||||
containers,
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
module Enviroment where
|
||||
|
||||
import qualified Data.Map as M
|
||||
import ExprType
|
||||
import Data.Maybe
|
||||
|
||||
data Enviroment = Enviroment (M.Map String Expr) (Maybe Enviroment)
|
||||
|
||||
base :: Enviroment
|
||||
base = Enviroment (M.fromList [
|
||||
("test-var", IntE 10)
|
||||
]) Nothing
|
||||
|
||||
lookupVar :: Enviroment -> String -> Maybe Expr
|
||||
lookupVar (Enviroment menv upperEnv) var = let mExpr = M.lookup var menv in
|
||||
if isNothing mExpr then upperEnv >>= (`lookupVar` var) else mExpr
|
||||
|
||||
insertVar :: Enviroment -> String -> Expr -> Enviroment
|
||||
insertVar (Enviroment menv u) var expr = Enviroment (M.insert var expr menv) u
|
|
@ -1,22 +1,16 @@
|
|||
module Evaluator where
|
||||
|
||||
import qualified Data.Map as M
|
||||
import ExprType
|
||||
|
||||
type Env = M.Map String Expr
|
||||
import Enviroment
|
||||
import Data.Maybe
|
||||
|
||||
-- TODO: create a separated file for builtinProcs
|
||||
-- TODO: create a BuiltinProc or something like that in data Expr, and make + a builtin proc
|
||||
|
||||
base :: Env
|
||||
base = M.fromList [
|
||||
("test-var", IntE 10)
|
||||
]
|
||||
|
||||
eval :: Env -> Expr -> (Env, Expr)
|
||||
eval :: Enviroment -> Expr -> (Enviroment, Expr)
|
||||
eval env i@(IntE _) = (env, i)
|
||||
eval env (VarE v) = eval env $ env M.! v
|
||||
eval env (SetE v expr) = (M.insert v expr env, NilE)
|
||||
eval env (VarE v) = eval env $ fromJust $ lookupVar env v
|
||||
eval env (SetE v expr) = (insertVar env v expr, NilE)
|
||||
eval env (QuotedE e) = (env, e)
|
||||
eval env (ConsE car cdr) = (env, apply car cdr)
|
||||
eval env NilE = (env, NilE)
|
||||
|
|
|
@ -5,7 +5,8 @@ data Expr = IntE Integer
|
|||
| SetE String Expr
|
||||
| ConsE Expr Expr
|
||||
| QuotedE Expr
|
||||
| NilE deriving (Show)
|
||||
| NilE
|
||||
deriving (Show)
|
||||
|
||||
-- instance Show Expr where
|
||||
-- show (IntE x) = show x
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module Main where
|
||||
|
||||
import Evaluator
|
||||
import Enviroment
|
||||
import Parser
|
||||
import Control.Monad
|
||||
import Data.Maybe
|
||||
|
|
Loading…
Reference in New Issue