Commit 32db377e authored by pm3g19's avatar pm3g19
Browse files

Added semantics for let

parent 00a56b7c
......@@ -49,7 +49,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
Contains -> case args of
(mainString:containsWhat:_) -> case (eval' mainString , eval' containsWhat) of
(String a, String b) -> b `sublist` a
(String a, String b) -> Boolean $ b `isSubList` a
Plus -> let (e1:e2:_) = args in case (eval' e1, eval' e2) of
(String a, String b) -> String (a ++ b)
......@@ -67,25 +67,33 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
(Var name) -> eval' $ findVar env name
(Let _ expr) -> expr
control@(Control lastResult exprs) -> if null exprs then lastResult else eval' $ evalControl1 env control
control@(Control lastResult exprs) -> if null exprs then lastResult else
let (newEnv, newControl) = evalControl1 env control in
eval newEnv newControl
_ -> expr
evalControl1 :: Environment -> Expr -> Expr
evalControl1 env (Control last (currentExpr:exprs)) = let output = eval env currentExprFull in Control output exprs
where
currentExprFull = case currentExpr of
evalControl1 :: Environment -> Expr -> (Environment, Expr)
evalControl1 env (Control last (currentExpr:exprs)) = (newEnv, Control newLast exprs)
where
newLast = eval env $ case currentExpr of
(FuncCall func [] args) -> FuncCall func [last] args
_ -> currentExpr
newEnv = case currentExpr of
(Let var expr) -> (var,expr):env
_ -> env
evalFull = eval
--evalFull env (Set xs) = Set (map (eval env) xs) -- evaluates expression fully (not just weak head normal form)
--evalFull _ e = e
--TODO implement properly
concatRecord (Record r1) (Record r2) = Record (r1 ++ r2)
sublist = notImplemented
\ No newline at end of file
listStartsWith, isSubList :: [a] -> [a] -> Bool
listStartsWith = notImplemented -- check if first list starts with second list
isSubList = notImplemented -- check if first list contains second list anywhere in it
\ No newline at end of file
......@@ -35,6 +35,7 @@ data Expr = Control Expr [Expr] -- result of last computation, sequence of instr
| Int Int
| String String
| Boolean Bool
| Let SymbolName Expr
-- | Nat Int
deriving (Show, Eq)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment