mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-17 08:27:45 +03:00
repl: added simple completion and history file
This commit is contained in:
parent
f93fa5fd9e
commit
333fca84fb
@ -53,6 +53,7 @@ executable carp
|
||||
build-depends: base
|
||||
, CarpHask
|
||||
, containers
|
||||
, directory
|
||||
, haskeline
|
||||
, process
|
||||
default-language: Haskell2010
|
||||
|
69
app/Main.hs
69
app/Main.hs
@ -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' "")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user