Browse Source

load core + better getenv

lisp-env-for-cmd
parent
commit
a0ca892fef
Signed by: yogsototh GPG Key ID: 7B19A4C650D59646
  1. 4
      lish/core.lsh
  2. 12
      src/Lish/Core.hs
  3. 30
      src/Lish/InternalCommands.hs

4
lish/core.lsh

@ -10,4 +10,6 @@
(if (empty? lst)
[]
(cons (f (first lst))
(map f (rest lst))))))
(map f (rest lst))))))
(prn "LISH core loaded")

12
src/Lish/Core.hs

@ -25,8 +25,13 @@ import Lish.Types
runLish :: IO ()
runLish = do
env <- toEnv <$> getEnvironment
-- load core
runInputT (defaultSettings { historyFile = Just ".lish-history" })
(mainLoop Nothing env "")
(do
-- load lish core
fileContent <- liftIO $ readFile "lish/core.lsh"
newEnv <- eval env (fmap unFix (parseCmd ("(do " <> fileContent <> ")")))
mainLoop Nothing newEnv "")
-- | System Environment -> LISH Env
toEnv :: [(String,String)] -> Env
@ -52,9 +57,8 @@ mainLoop mc env previousPartialnput = do
, Just "exit"
, Just "logout"] -> outputStrLn "bye bye!"
Just rawLine -> do
let line = takeWhile (/= ';') rawLine -- remove comments
exprs = previousPartialnput
Just line -> do
let exprs = previousPartialnput
<> (if isJust mc then " " else "")
<> toS line
case checkBalanced exprs empty of

30
src/Lish/InternalCommands.hs

@ -57,16 +57,6 @@ undef ((Atom name):[]) = do
return Void
undef x = evalErr $ "undef wait an atom got" <> toS (show x)
-- | retrieve the value of a var
getenv :: ReduceUnawareCommand
getenv ((Atom varname):[]) = do
hm <- get
return $ fromMaybe Void (Map.lookup varname hm)
getenv ((Str varname):[]) = do
hm <- get
return $ fromMaybe Void (Map.lookup varname hm)
getenv _ = evalErr "getenv need on atom or a string as argument"
-- | replace à la `sed s/old/new/g text`
replace :: ReduceUnawareCommand
replace ((Str old) : (Str new) : (Str text) : []) =
@ -151,8 +141,6 @@ strictCommands = [ ("prn", prn)
, (">", toWaitingStream)
, ("replace", replace)
, ("undef",undef)
, ("getenv",getenv)
, ("$",getenv)
, ("str",str)
, ("atom",atom)
-- binary operators
@ -278,6 +266,22 @@ evalStr r (x@(Lambda _):[]) = do
evalStr r (reduced:[])
evalStr _ _ = evalErr "evalStr error"
-- | retrieve the value of a var
getenv :: Command
getenv _ ((Atom varname):[]) = do
hm <- get
return $ fromMaybe Void (Map.lookup varname hm)
getenv _ ((Str varname):[]) = do
hm <- get
return $ fromMaybe Void (Map.lookup varname hm)
getenv r (expr:_) = do
reduced <- r expr
hm <- get
case reduced of
(Str varname) -> return $ fromMaybe Void (Map.lookup varname hm)
_ -> evalErr "getenv need on atom or a string as argument"
getenv _ _ = evalErr "getenv need on atom or a string as argument"
unstrictCommands :: [(Text,InternalCommand)]
unstrictCommands = [ ("if", InternalCommand "if" lishIf)
, ("def", InternalCommand "def" def)
@ -286,6 +290,8 @@ unstrictCommands = [ ("if", InternalCommand "if" lishIf)
, ("=", InternalCommand "=" equal)
, ("export", InternalCommand "export" export)
, ("eval", InternalCommand "eval" evalStr)
, ("getenv", InternalCommand "getenv" getenv)
, ("$", InternalCommand "$" getenv)
-- list ops
, ("empty?",InternalCommand "empty?" emptyCmd)
, ("first",InternalCommand "first" firstCmd)

Loading…
Cancel
Save