@@ -6,7 +6,8 @@ module Lapse.Operators where
66import Control.Monad.IO.Class (liftIO )
77import Control.Monad.State (get , lift , put )
88import Data.Function (fix )
9- import Data.Map.Strict (Map , empty , insert , (!) )
9+ import Data.Map.Strict (Map , empty , insert , (!?) )
10+ import Data.Maybe (fromMaybe )
1011import Lapse.Eval (eval , lmap' )
1112import Lapse.Parser (parse )
1213import Lapse.Scopes (changeValue , dropScope , newScope )
@@ -237,18 +238,50 @@ ldict (Pair (Pair k (Pair v Nil)) rest) = do
237238 _ -> error " Key should be either string or name"
238239ldict _ = error " Wrong dict expression"
239240
241+ or' :: String -> String -> String
242+ or' " " a = a
243+ or' a " " = a
244+ or' a _ = a
245+
240246llkp :: Func
241247llkp (Pair k' (Pair d' Nil )) = do
242248 a <- eval d'
243249 let d = case a of
244250 Dict x -> x
245251 _ -> error " Wrong lookup expression. Syntax: lookup <key> <dict>"
252+ ek <- eval k'
253+ let sk = case ek of
254+ String x -> x
255+ Name x -> x
256+ _ -> " "
246257 case k' of
247- String k -> pure $ d ! k
248- Name k -> pure $ d ! k
258+ String k -> d ?! (sk `or'` k)
259+ Name k -> d ?! (sk `or'` k)
249260 _ -> llkp Nil
261+ where
262+ (?!) d k = pure $ fromMaybe Nil (d !? k)
250263llkp _ = error " Wrong lookup expression. Syntax: lookup <key> <dict>"
251264
265+ lins :: Func
266+ lins (Pair k' (Pair v' (Pair d' Nil ))) = do
267+ a <- eval d'
268+ let d = case a of
269+ Dict x -> x
270+ _ -> error " Wrong insert expression. Syntax: insert <key> <value> <dict>"
271+ v <- eval v'
272+ ek <- eval k'
273+ let sk = case ek of
274+ String x -> x
275+ Name x -> x
276+ _ -> " "
277+ case k' of
278+ String k -> pdict $ insert (sk `or'` k) v d
279+ Name k -> pdict $ insert (sk `or'` k) v d
280+ _ -> lins Nil
281+ where
282+ pdict = pure . Dict
283+ lins _ = error " Wrong insert expression. Syntax: insert <key> <value> <dict>"
284+
252285lthr :: Func
253- lthr (Pair (String s) Nil ) = error s
286+ lthr (Pair (String s) Nil ) = error $ " \ESC [0;31m " ++ s ++ " \ESC [0m "
254287lthr _ = error " throw syntax: (throw <string>)"
0 commit comments