git @ Cat's Eye Technologies Robin / 2ae4e4f
Language.Robin.Facilities module, rename type FacilityHandler. Chris Pressey 5 years ago
4 changed file(s) with 18 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
66 import Language.Robin.Expr
77 import Language.Robin.Eval
88 import Language.Robin.Reactor
9 import Language.Robin.Facilities
910
1011
11 type Facility = Expr -> IO [Expr]
12 type WaitForEvents = IO (Either String [Expr])
13
14
15 eventLoop :: Bool -> [Facility] -> WaitForEvents -> [Reactor] -> IO ()
12 eventLoop :: Bool -> [FacilityHandler] -> WaitForEvents -> [Reactor] -> IO ()
1613 eventLoop showEvents facilities waitForEvents reactors = do
1714 let (reactors', events') = updateMany reactors (List [(Symbol "init"), (Number 0)])
1815 e reactors' events'
3027 e reactors (event@(List [eventType, eventPayload]):events) = do
3128 -- An event on the queue. Allow all facilities and reactors to handle it.
3229 showEvent event
33 newFacilityEvents <- runFacilityHandlers facilities event []
30 newFacilityEvents <- runFacilityHandlers facilities event
3431 let (reactors', newReactorEvents) = updateMany reactors event
3532 e reactors' (events ++ newFacilityEvents ++ newReactorEvents)
3633
4643 Left err -> return ()
4744 Right events -> e reactors events
4845
49 runFacilityHandlers [] event acc = return acc
50 runFacilityHandlers (handler:handlers) event acc = do
46 runFacilityHandlers [] event = return []
47 runFacilityHandlers (handler:handlers) event = do
5148 newEvents <- handler event
52 runFacilityHandlers handlers event (acc ++ newEvents)
49 rest <- runFacilityHandlers handlers event
50 return $ newEvents ++ rest
5351
5452 showEvent event = case showEvents of
5553 True -> hPutStrLn stderr ("*** " ++ show event)
33 import System.IO
44
55 import Language.Robin.Expr
6 import Language.Robin.Facilities
67
7 type WaitForEvents = IO (Either String [Expr])
88
99 waitForLineTerminalEvent :: WaitForEvents
1010 waitForLineTerminalEvent = do
2020 True -> return $ Left "stop"
2121 False -> return $ Left "stop"
2222
23 handleLineTerminalEvent :: Expr -> IO [Expr]
23
24 handleLineTerminalEvent :: FacilityHandler
2425 handleLineTerminalEvent (List [Symbol "write", payload]) = do
2526 let List l = payload
2627 let s = map (\(Number x) -> Char.chr $ fromIntegral $ x) l
22 import System.Random
33
44 import Language.Robin.Expr
5 import Language.Robin.Facilities
56
67
7 handleRandomSourceEvent :: Expr -> IO [Expr]
8 handleRandomSourceEvent :: FacilityHandler
89 handleRandomSourceEvent (List [Symbol "obtain-random-u16", payload]) = do
910 v <- randomRIO (0, 65535)
1011 return $ [List [Symbol "random-u16", Number v]]
0 module Language.Robin.Facilities where
1
2 import Language.Robin.Expr
3
4 type FacilityHandler = Expr -> IO [Expr]
5 type WaitForEvents = IO (Either String [Expr])