Skip to content

Instantly share code, notes, and snippets.

@gdevanla
Created September 19, 2021 00:18
Show Gist options
  • Save gdevanla/5e4a6869ff2d2a79b445ebf3b809e6de to your computer and use it in GitHub Desktop.
Save gdevanla/5e4a6869ff2d2a79b445ebf3b809e6de to your computer and use it in GitHub Desktop.
-- Notes on how to wrap InputT over StateT to maintain the environment around a language repl.
type HaskellLineT = InputT (StateT Env IO) ()
runScriptInteractive = runStateT (runInputT defaultSettings loop) (initEnv Nothing)
where
loop :: HaskellLineT
loop = do
minput <- getInputLine "%"
when (isNothing minput) loop
env <- lift get
let lex_result = scanner (fromJust minput)
case lex_result of
Right lex -> do
let ast = P.parse loxProgram "" lex
case ast of
Right ast' -> do
(env', msg) <- liftIO $ interpretProgram ast' env
when (isJust msg) $ liftIO $ print msg
lift $ put env'
loop
Left e -> do
liftIO $ print $ "Scanner error" <> show e
loop
Left e -> do
liftIO $ print $ "Lexer error" <> show e
loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment