PureScriptでREPLを作る
PurispでREPL作っているので実装見れば参考にはなるはずmrsekut.icon こんなかんじや
code:purs(hs)
module Main where
import Prelude
import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Effect.Class (liftEffect)
import Effect.Console (log)
import Readline (readLine)
main :: Effect Unit
main = do
launchAff_ loop
loop :: Aff Unit
loop = do
line <- readLine "user> "
case line of
":q" -> pure unit
":Q" -> pure unit
_ -> do
liftEffect $ log line
loop
read :: String -> String
read s = s
eval :: String -> String
eval s = s
print :: String -> String
print s = s
rep :: String -> String
rep = read >>> eval >>> print
code:purs(hs)
module Readline where
import Prelude
import Data.Either (Either(..))
import Effect (Effect)
import Effect.Aff (Aff, Canceler, effectCanceler, makeAff)
import Effect.Exception (Error)
import Node.ReadLine (close, createConsoleInterface, noCompletion, prompt, setLineHandler, setPrompt)
readLine :: String -> Aff String
readLine pm = makeAff handler
where
handler :: (Either Error String -> Effect Unit) -> Effect Canceler
handler next = do
interface <- createConsoleInterface noCompletion
setPrompt pm interface
prompt interface
interface
# setLineHandler \str -> do
close interface
next $ Right str
pure $ effectCanceler $ close interface
参考
これは使ってないが、コードを参考にしたmrsekut.icon