-- -- Copyright (c) 2004 Don Stewart - http://www.cse.unsw.edu.au/~dons -- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html) -- import Plugins import API import System.Exit ( ExitCode(..), exitWith ) import System.IO import Control.Exception ( evaluate ) import System.Console.Readline ( readline, addHistory ) source = "Plugin.hs" symbol = "resource" newtype State = State { modules :: [String] } newState :: State newState = State [] main = do putStrLn banner -- preload the base package putStr "Loading package base" >> hFlush stdout loadPackage "base" putStr " ... linking ... " >> hFlush stdout resolveObjs putStrLn "done" shell newState shell :: State -> IO State shell state = do s <- readline "> " cmd <- case s of Nothing -> exitWith ExitSuccess Just (':':'q':_) -> exitWith ExitSuccess Just s -> addHistory s >> return s state' <- eval cmd state shell state' eval :: String -> State -> IO State eval "" s = return s eval ":?" s = putStrLn help >> return s eval (':':'l':'o':'a':'d':[]) _ = return $ State [] eval (':':'l':'o':'a':'d':' ':s) state@(State { modules = ms }) = return $ state { modules = (s:ms)} eval (':':_) s = putStrLn "unknown command" >> return s eval s state = do writeOut s state (o,_,err) <- make source ["-Onot"] -- fast as possible if not $ null err then mapM_ putStrLn err >> return state else do (m,v) <- load o ["."] Nothing symbol v' <- evaluate (code v) putStrLn v' unload m return state writeOut :: String -> State -> IO FilePath writeOut expr state = do f <- openFile source WriteMode hPutStr f $ "module Plugin ( resource ) where" ++ "\nimport API\n" ++ concatMap (\m-> "import "++m++"\n") (modules state) ++ "\nresource :: Interface" ++ "\nresource = plugin { code = f_ }" ++ "\nf_ :: String" ++ "\nf_ = let { expr = (\n" ++ "{-# LINE 1 \"\" #-}\n" ++ expr ++ "\n); } in show expr" hClose f return source banner = "\ \ __ \n\ \ ____ / /_ ______ ______ \n\ \ / __ \\/ / / / / __ `/ ___/ PLugin User's Gofer System, for Haskell 98\n\ \ / /_/ / / /_/ / /_/ (__ ) http://www.cse.unsw.edu.au/~dons/hs-plugins\n\ \ / .___/_/\\__,_/\\__, /____/ Type :? for help \n\ \/_/ /____/ \n" help = "\ \Commands :\n\ \ evaluate expression\n\ \ :type show type of expression [unimplemented]\n\ \ :quit quit\n\ \ :load mod load module (not filename)\n\ \ :load clear all modules\n\ \ :? display this list of commands"