git @ Cat's Eye Technologies Emmental / 73a8176
Allow IO functions to be dependency-injected; use ffi for them. Chris Pressey 5 years ago
2 changed file(s) with 27 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
2323
2424 data State = State {
2525 stack :: [Symbol],
26 queue :: [Symbol]
26 queue :: [Symbol],
27 getCh :: IO Char,
28 putCh :: Char -> IO ()
2729 }
2830
2931 instance Show State where
128130 -- I/O.
129131 --
130132
131 opInput state interpreter = do
132 symbol <- getChar
133 opInput state@State{ getCh=getCh } interpreter = do
134 symbol <- getCh
133135 do return (push state symbol, interpreter)
134136
135 opOutput state interpreter =
137 opOutput state@State{ putCh=putCh } interpreter =
136138 let
137139 (symbol, state') = pop state
138140 in do
139 putChar symbol
141 putCh symbol
140142 return (state', interpreter)
141143
142144 --
293295 ]
294296 )
295297
296 initialState = State [] []
298 initialState = State { stack=[], queue=[], getCh=getChar, putCh=putChar }
297299
298300 emmental string = do
299301 (state, interpreter) <- execute string initialState initialInterpreter debugNop
300302 return state
303
304 emmentalWithIO getCh putCh string =
305 let
306 i = initialState
307 i' = i{ getCh=getCh, putCh=putCh }
308 in do
309 (state, interpreter) <- execute string i' initialInterpreter debugNop
310 return state
301311
302312 debug string = do
303313 (state, interpreter) <- execute string initialState initialInterpreter debugPrintState
0 {-# LANGUAGE OverloadedStrings #-}
1
02 module Main where
13
24 import Haste.DOM (withElems, getValue, setProp)
35 import Haste.Events (onEvent, MouseEvent(Click))
6 import Haste.Foreign (ffi)
47
5 import Emmental
8 import Emmental (emmentalWithIO)
69
10
11 getCh :: IO Char
12 getCh = ffi "(function() {return 'A';})"
13
14 putCh :: Char -> IO ()
15 putCh = ffi "(function(c) {console.log(c);})"
716
817 main = withElems ["prog", "result", "run-button"] driver
918
1019 driver [progElem, resultElem, runButtonElem] =
1120 onEvent runButtonElem Click $ \_ -> do
1221 Just prog <- getValue progElem
13 r <- emmental prog
22 r <- emmentalWithIO (getCh) (putCh) prog
1423 setProp resultElem "textContent" $ show $ r