repl: added simple completion and history file

This commit is contained in:
hellerve 2017-10-22 23:34:06 +02:00
parent f93fa5fd9e
commit 333fca84fb
2 changed files with 68 additions and 2 deletions

View File

@ -53,6 +53,7 @@ executable carp
build-depends: base
, CarpHask
, containers
, directory
, haskeline
, process
default-language: Haskell2010

View File

@ -2,7 +2,16 @@ module Main where
import Control.Monad
import Control.Monad.IO.Class
import System.Console.Haskeline (getInputLine, InputT, runInputT, defaultSettings)
import Data.List (isPrefixOf)
import System.Console.Haskeline ( getInputLine
, InputT
, runInputT
, Settings(..)
, Completion
, simpleCompletion
, completeWordWithPrev
)
import System.Directory (getHomeDirectory)
import qualified System.Environment as SystemEnvironment
import System.IO (stdout)
import System.Info (os)
@ -27,6 +36,61 @@ defaultProject = Project { projectTitle = "Untitled"
, projectPrompt = if os == "darwin" then "" else "> "
}
completeKeywords :: Monad m => String -> String -> m [Completion]
completeKeywords _ word = return $ findKeywords word keywords []
where
findKeywords match [] res = res
findKeywords match (x : xs) res =
if isPrefixOf match x
then findKeywords match xs (res ++ [simpleCompletion x])
else findKeywords match xs res
keywords = [ "Int" -- we should probably have a list of those somewhere
, "Float"
, "Double"
, "Bool"
, "String"
, "Char"
, "Array"
, "Fn"
, "def"
, "defn"
, "let"
, "do"
, "if"
, "while"
, "ref"
, "address"
, "set!"
, "the"
, "defmacro"
, "dynamic"
, "quote"
, "car"
, "cdr"
, "cons"
, "list"
, "array"
, "deftype"
, "register"
, "true"
, "false"
]
readlineSettings :: Monad m => IO (Settings m)
readlineSettings = do
home <- getHomeDirectory
return $ Settings {
complete = completeWordWithPrev Nothing ['(', ')', '[', ']', ' ', '\t', '\n'] completeKeywords,
historyFile = Just $ home ++ "/.carp_history",
autoAddHistory = True
}
repl :: Context -> String -> InputT IO ()
repl context readSoFar =
do let prompt = strWithColor Yellow (if null readSoFar then (projectPrompt (contextProj context)) else " ") -- 鲤 / 鲮
@ -106,5 +170,6 @@ main = do putStrLn "Welcome to Carp 0.2.0"
context <- foldM executeCommand (Context startingGlobalEnv (TypeEnv startingTypeEnv) [] projectWithCarpDir "")
(map Load (preludeModules (projectCarpDir projectWithCarpDir)))
context' <- foldM executeCommand context (map Load args)
runInputT defaultSettings (repl context' "")
settings <- readlineSettings
runInputT settings (repl context' "")