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