Import hs-plugins cvs
This commit is contained in:
8
examples/shell/shell/API.hs
Normal file
8
examples/shell/shell/API.hs
Normal file
@ -0,0 +1,8 @@
|
||||
module API where
|
||||
|
||||
-- the interface between the app and the plugin
|
||||
data Interface = Interface { function :: String -> String }
|
||||
|
||||
-- default values for the interface
|
||||
plugin :: Interface
|
||||
plugin = Interface { function = id }
|
85
examples/shell/shell/Main.hs
Normal file
85
examples/shell/shell/Main.hs
Normal file
@ -0,0 +1,85 @@
|
||||
--
|
||||
-- a simple shell for loading plugins and evaluating their functions
|
||||
--
|
||||
|
||||
import Plugins
|
||||
import API
|
||||
|
||||
import Data.Either
|
||||
import Data.Char
|
||||
import Control.Monad ( when )
|
||||
import System.Console.Readline ( readline )
|
||||
import System.Exit ( ExitCode(..), exitWith )
|
||||
|
||||
|
||||
source = "Plugin.hs"
|
||||
stub = "Plugin.stub"
|
||||
|
||||
sym = "resource"
|
||||
|
||||
main = do
|
||||
status <- makeWith source stub []
|
||||
p <- case status of
|
||||
MakeFailure e -> mapM_ putStrLn e >> error "failed to compile"
|
||||
MakeSuccess _ obj -> do
|
||||
m_v <- load obj ["."] [] sym
|
||||
case m_v of
|
||||
LoadSuccess m v -> return (m,v)
|
||||
LoadFailure e -> do mapM_ putStrLn e
|
||||
error "failed to load"
|
||||
shell p
|
||||
|
||||
where
|
||||
shell p@(m,v) = do
|
||||
|
||||
s <- readline "> "
|
||||
cmd <- case s of
|
||||
Nothing -> exitWith ExitSuccess
|
||||
Just ":q" -> exitWith ExitSuccess
|
||||
Just s -> return (chomp s)
|
||||
|
||||
status <- makeWith source stub []
|
||||
case status of
|
||||
MakeFailure e -> do
|
||||
mapM_ putStrLn e
|
||||
shell p -- print error and back to prompt
|
||||
|
||||
MakeSuccess NotReq o -> do
|
||||
p' <- eval cmd p
|
||||
shell p' -- eval str again
|
||||
|
||||
MakeSuccess ReComp o -> do
|
||||
m_v' <- reload m sym
|
||||
case m_v' of
|
||||
LoadFailure e -> mapM_ putStrLn e >> error "failed to load"
|
||||
LoadSuccess _ v' -> do
|
||||
let p' = (m,v')
|
||||
p'' <- eval cmd p'
|
||||
shell p''
|
||||
|
||||
--
|
||||
-- shell commands
|
||||
--
|
||||
eval "" p = return p
|
||||
|
||||
eval ":clear" p = do
|
||||
let loop i = when (i < 40) (do putStr "\n" ; loop $! i+1)
|
||||
loop 0
|
||||
return p
|
||||
|
||||
eval ":?" p = do
|
||||
putStrLn$"\":?\"\n" ++
|
||||
"\":quit\"\n" ++
|
||||
"\":clear\"\n" ++
|
||||
"\"foo\""
|
||||
return p
|
||||
|
||||
eval s (m,v) = putStrLn ((function v) s) >> return (m,v)
|
||||
|
||||
--
|
||||
-- strip trailing whitespace
|
||||
--
|
||||
chomp :: String -> String
|
||||
chomp [] = []
|
||||
chomp s | isSpace (last s) = chomp $! init s
|
||||
| otherwise = s
|
2
examples/shell/shell/Makefile
Normal file
2
examples/shell/shell/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
TOP=../../..
|
||||
include ../../eval.mk
|
5
examples/shell/shell/Plugin.hs
Normal file
5
examples/shell/shell/Plugin.hs
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
resource = plugin {
|
||||
function = map toUpper
|
||||
}
|
||||
|
19
examples/shell/shell/Plugin.stub
Normal file
19
examples/shell/shell/Plugin.stub
Normal file
@ -0,0 +1,19 @@
|
||||
--
|
||||
-- this is a "stub" file, containing default syntax we don't
|
||||
-- want the user to have to write
|
||||
--
|
||||
-- for example, it constrains the module name and force the API to be
|
||||
-- imported
|
||||
|
||||
module Plugin ( resource ) where
|
||||
|
||||
import API
|
||||
import Data.Char
|
||||
import Data.List
|
||||
|
||||
-- this is a default definition of 'resource'. it will be overridden
|
||||
-- by anything the user writes. useful for default values
|
||||
|
||||
resource :: Interface
|
||||
resource = plugin
|
||||
|
23
examples/shell/shell/README
Normal file
23
examples/shell/shell/README
Normal file
@ -0,0 +1,23 @@
|
||||
$ make
|
||||
$ ./a.out
|
||||
Compiling plugin ... done
|
||||
Loading package base ... linking ... done
|
||||
Loading objects API Plugin ... done
|
||||
> ?
|
||||
"?"
|
||||
"quit"
|
||||
"clear"
|
||||
"filter foo"
|
||||
> filter adf adsf
|
||||
fsda fda
|
||||
> filter asd faSDFADSF
|
||||
FSDAFDSaf dsa
|
||||
|
||||
-- at this point I edit the plugin and save the source
|
||||
|
||||
> filter asfdaSDFASD
|
||||
Compiling plugin ... done
|
||||
Reloading Plugin ... done
|
||||
DSAFDSADFSA
|
||||
|
||||
-- it compiled and reloaded it for me. nice.
|
0
examples/shell/shell/dont_test
Normal file
0
examples/shell/shell/dont_test
Normal file
41
examples/shell/simple/Main.hs
Normal file
41
examples/shell/simple/Main.hs
Normal file
@ -0,0 +1,41 @@
|
||||
import Plugins
|
||||
import StringProcessorAPI
|
||||
import System.Console.Readline
|
||||
import System.Exit
|
||||
|
||||
source = "Plugin.hs"
|
||||
stub = "Plugin.stub"
|
||||
symbol = "resource"
|
||||
|
||||
main = do s <- makeWith source stub []
|
||||
o <- case s of
|
||||
MakeSuccess _ obj -> do
|
||||
ls <- load obj ["."] [] symbol
|
||||
case ls of LoadSuccess m v -> return (m,v)
|
||||
LoadFailure err -> error "load failed"
|
||||
MakeFailure e -> mapM_ putStrLn e >> error "compile failed"
|
||||
shell o
|
||||
|
||||
shell o@(m,plugin) = do
|
||||
s <- readline "> "
|
||||
cmd <- case s of
|
||||
Nothing -> exitWith ExitSuccess
|
||||
Just (':':'q':_) -> exitWith ExitSuccess
|
||||
Just s -> addHistory s >> return s
|
||||
|
||||
s <- makeWith source stub [] -- maybe recompile the source
|
||||
o' <- case s of
|
||||
MakeSuccess ReComp o -> do
|
||||
ls <- reload m symbol
|
||||
case ls of LoadSuccess m' v' -> return (m',v')
|
||||
LoadFailure err -> error "reload failed"
|
||||
MakeSuccess NotReq _ -> return o
|
||||
MakeFailure e -> mapM_ putStrLn e >> shell o
|
||||
eval cmd o'
|
||||
shell o'
|
||||
|
||||
eval ":?" _ = putStrLn ":?\n:q\n<string>"
|
||||
|
||||
eval s (_,plugin) = let fn = (stringProcessor plugin) in putStrLn (fn s)
|
||||
|
||||
|
6
examples/shell/simple/Makefile
Normal file
6
examples/shell/simple/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
OBJS=StringProcessorAPI.o
|
||||
TOP=../../..
|
||||
include ../../eval.mk
|
||||
|
||||
#all:
|
||||
# @echo test disabled
|
5
examples/shell/simple/Plugin.hs
Normal file
5
examples/shell/simple/Plugin.hs
Normal file
@ -0,0 +1,5 @@
|
||||
import Char
|
||||
|
||||
resource = plugin {
|
||||
stringProcessor = map toUpper
|
||||
}
|
19
examples/shell/simple/Plugin.stub
Normal file
19
examples/shell/simple/Plugin.stub
Normal file
@ -0,0 +1,19 @@
|
||||
--
|
||||
-- this is a "stub" file, containing default syntax we don't
|
||||
-- want the user to have to write
|
||||
--
|
||||
-- for example, it constrains the module name and force the API to be
|
||||
-- imported
|
||||
|
||||
module Plugin ( resource ) where
|
||||
|
||||
import StringProcessorAPI
|
||||
import Data.Char
|
||||
import Data.List
|
||||
|
||||
-- this is a default definition of 'resource'. it will be overridden
|
||||
-- by anything the user writes. useful for default values
|
||||
|
||||
resource :: Interface
|
||||
resource = plugin
|
||||
|
23
examples/shell/simple/README
Normal file
23
examples/shell/simple/README
Normal file
@ -0,0 +1,23 @@
|
||||
$ make
|
||||
$ ./a.out
|
||||
Compiling plugin ... done
|
||||
Loading package base ... linking ... done
|
||||
Loading objects API Plugin ... done
|
||||
> ?
|
||||
"?"
|
||||
"quit"
|
||||
"clear"
|
||||
"filter foo"
|
||||
> filter adf adsf
|
||||
fsda fda
|
||||
> filter asd faSDFADSF
|
||||
FSDAFDSaf dsa
|
||||
|
||||
-- at this point I edit the plugin and save the source
|
||||
|
||||
> filter asfdaSDFASD
|
||||
Compiling plugin ... done
|
||||
Reloading Plugin ... done
|
||||
DSAFDSADFSA
|
||||
|
||||
-- it compiled and reloaded it for me. nice.
|
8
examples/shell/simple/StringProcessorAPI.hs
Normal file
8
examples/shell/simple/StringProcessorAPI.hs
Normal file
@ -0,0 +1,8 @@
|
||||
module StringProcessorAPI where
|
||||
|
||||
data Interface = Interface {
|
||||
stringProcessor :: String -> String
|
||||
}
|
||||
|
||||
plugin :: Interface
|
||||
plugin = Interface { stringProcessor = id }
|
0
examples/shell/simple/dont_test
Normal file
0
examples/shell/simple/dont_test
Normal file
Reference in New Issue
Block a user