1 implementation module mTaskInterpret
6 import GenEq, StdMisc, StdArray, GenBimap
15 from StdFunc import o, const
22 from Data.Func import $
23 from Text import class Text(concat,join,toUpperCase), instance Text String
25 import qualified Data.Map as DM
26 import Text.Encodings.Base64
28 encode :: MTaskMSGSend -> String
29 encode (MTTask to data) = "t" +++ tob +++ to16bit (size data) +++ data +++ "\n"
33 OnInterval i = to16bit i
34 OnInterrupt _ = abort "Interrupts not implemented yet"
35 encode (MTTaskDel i) = "d" +++ to16bit i +++ "\n"
36 encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n"
37 encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n"
39 decode :: String -> MTaskMSGRecv
41 | size x == 0 = MTEmpty
43 't' = MTTaskAck (from16bit (x % (1,3)))
44 'd' = MTTaskDelAck (from16bit (x % (1,3)))
46 's' = MTSDSAck (from16bit (x % (1,3)))
47 'a' = MTSDSDelAck (from16bit (x % (1,3)))
48 'p' = MTPub (from16bit (x % (1,3))) (x % (3,5))
51 _ = MTMessage x//abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n")
53 safePrint :== toString o toJSON
55 instance toString MTaskInterval where
56 toString OneShot = "One shot"
57 toString (OnInterrupt i) = "Interrupt: " +++ toString i
58 toString (OnInterval i) = "Every " +++ toString i +++ "ms"
60 instance toString MTaskMSGSend where
61 toString (MTSds i v) = "Sds id: " +++ toString i
62 +++ " value " +++ safePrint v
63 toString (MTTask to data) = "Task timeout: " +++ toString to
64 +++ " data " +++ safePrint data
65 toString (MTTaskDel i) = "Task delete request: " +++ toString i
66 toString (MTUpd i v) = "Update id: " +++ toString i
67 +++ " value " +++ safePrint v
69 instance toString MTaskMSGRecv where
70 toString (MTTaskAck i) = "Task added with id: " +++ toString i
71 toString (MTTaskDelAck i) = "Task deleted with id: " +++ toString i
72 toString (MTSDSAck i) = "SDS added with id: " +++ toString i
73 toString (MTSDSDelAck i) = "SDS deleted with id: " +++ toString i
74 toString (MTPub i v) = "Publish id: " +++ toString i
75 +++ " value " +++ safePrint v
76 toString (MTMessage m) = m
77 toString MTEmpty = "Empty message"
80 bclength (BCPush _) = 3
81 bclength (BCLab _) = 2
82 bclength (BCSdsStore _) = 3
83 bclength (BCSdsFetch _) = 3
84 bclength (BCSdsPublish _) = 3
85 bclength (BCAnalogRead _) = 2
86 bclength (BCAnalogWrite _) = 2
87 bclength (BCDigitalRead _) = 2
88 bclength (BCDigitalWrite _) = 2
89 bclength (BCLedOn _) = 2
90 bclength (BCLedOff _) = 2
91 bclength (BCJmp i) = 2
92 bclength (BCJmpT i) = 2
93 bclength (BCJmpF i) = 2
96 toByteVal :: BC -> [Char]
98 # bt = toChar $ consIndex{|*|} b
101 (BCLab i) = [toChar i]
102 (BCSdsStore i) = [c\\c<-:to16bit i]
103 (BCSdsFetch i) = [c\\c<-:to16bit i]
104 (BCSdsPublish i) = [c\\c<-:to16bit i]
105 (BCAnalogRead i) = [toChar i]
106 (BCAnalogWrite i) = [toChar i]
107 (BCDigitalRead i) = [toChar i]
108 (BCDigitalWrite i) = [toChar i]
111 (BCJmp i) = [toChar i]
112 (BCJmpT i) = [toChar i]
113 (BCJmpF i) = [toChar i]
116 instance Semigroup (ByteCode a p) where
117 mappend m n = BC \s->let (b1, (b2, t)) = runBC m <$> runBC m s in (b1 ++ b2, t)
119 instance Monoid (ByteCode a p) where
122 (<++>) infixl 2 :: (ByteCode a p) (ByteCode b q) -> ByteCode c r
123 (<++>) m n = BC \s->let (b1, (b2, t)) = runBC n <$> runBC m s in (b1 ++ b2, t)
126 (<+->) m n :== m <++> retrn n
130 retrn :: ([BC] -> ByteCode a p)
132 fmp :: ([BC] -> [BC]) (ByteCode a p) -> ByteCode a q
133 fmp f b = BC \s->let (bc, s`) = runBC b s in (f bc, s`)
135 instance toByteCode Bool where toByteCode b = toByteCode $ if b 1 0
136 instance toByteCode Int where toByteCode n = map toChar [n/256,n rem 256]
137 instance toByteCode Long where toByteCode (L n) = toByteCode n
138 instance toByteCode Char where toByteCode c = [c]
139 instance toByteCode String where toByteCode s = undef
140 instance toByteCode Button where toByteCode s = [toChar $ consIndex{|*|} s]
141 instance toByteCode UserLED where toByteCode s = [toChar $ consIndex{|*|} s]
142 instance toByteCode MTaskInterval where
143 toByteCode OneShot = toByteCode 0
144 //Intervals have the first bit 0 and the rest is a 15 bit unsigned int
145 toByteCode (OnInterval i) = map toChar [i/256 bitand 127, i rem 256]
146 //Intervals have the first bit 1 and the rest is a 15 bit unsigned int
147 toByteCode (OnInterrupt i) = map toChar [i/256 bitor 128, i rem 256]
149 instance toChar Pin where
150 toChar (Digital p) = toChar $ consIndex{|*|} p
151 toChar (Analog p) = toChar $ consIndex{|*|} p
153 derive gPrint BC, AnalogPin, Pin, DigitalPin
154 derive consIndex BC, Pin, Button, UserLED
155 derive consName BC, Pin, Button
157 instance arith ByteCode where
158 lit x = retrn [BCPush $ toByteCode x]
159 (+.) x y = x <++> y <+-> [BCAdd]
160 (-.) x y = x <++> y <+-> [BCSub]
161 (*.) x y = x <++> y <+-> [BCMul]
162 (/.) x y = x <++> y <+-> [BCDiv]
164 instance boolExpr ByteCode where
165 (&.) x y = x <++> y <+-> [BCAnd]
166 (|.) x y = x <++> y <+-> [BCOr]
167 Not x = x <+-> [BCNot]
168 (==.) x y = x <++> y <+-> [BCEq]
169 (!=.) x y = x <++> y <+-> [BCNeq]
170 (<.) x y = x <++> y <+-> [ BCLes]
171 (>.) x y = x <++> y <+-> [BCGre]
172 (<=.) x y = x <++> y <+-> [BCLeq]
173 (>=.) x y = x <++> y <+-> [BCGeq]
175 instance analogIO ByteCode where
176 analogRead p = retrn [BCAnalogRead $ pin p]
177 analogWrite p b = b <+-> [BCAnalogWrite $ pin p]
179 instance digitalIO ByteCode where
180 digitalRead p = retrn [BCDigitalRead $ pin p]
181 digitalWrite p b = b <+-> [BCDigitalWrite $ pin p]
183 instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
184 instance If ByteCode e Stmt Stmt where If b t e = BCIfStmt b t e
185 instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
186 instance If ByteCode x y Stmt where If b t e = BCIfStmt b t e
187 instance IF ByteCode where
188 IF b t e = BCIfStmt b t e
189 (?) b t = BCIfStmt b t $ retrn []
191 withLabel \else->withLabel \endif->
192 b <++> retrn [BCJmpF else] <++> t
193 <++> retrn [BCJmp endif,BCLab else] <++> e <++> retrn [BCLab endif]
195 instance noOp ByteCode where noOp = retrn [BCNop]
197 withLabel :: (Int -> (ByteCode b q)) -> ByteCode b q
198 withLabel f = BC \s->let [fresh:fs] = s.freshl
199 in runBC (f fresh) {s & freshl=fs}
201 withSDS :: (Int -> (ByteCode b q)) -> ByteCode b q
202 withSDS f = BC \s->let [fresh:fs] = s.freshs
203 in runBC (f fresh) {s & freshs=fs}
205 setSDS :: Int v -> ByteCode b q | toByteCode v
206 setSDS ident val = BC \s->([], {s & sdss=[
207 {BCShare|sdsi=ident,sdspub=False,sdsval=toByteCode val}:s.sdss]})
209 instance sds ByteCode where
210 sds f = {main = withSDS \sds->
211 let (v In body) = f $ retrn [BCSdsFetch sds]
212 in setSDS sds v <++> unMain body
215 pub x = BC \s-> let ((i, bc), s`) = appFst makePub $ runBC x s
216 in (bc, {s` & sdss=map (publish i) s`.sdss})
218 publish i s = if (i == s.sdsi) {s & sdspub=True} s
219 makePub [BCSdsFetch i:xs] = (i, [BCSdsPublish i:xs])
221 instance assign ByteCode where
222 (=.) v e = e <++> fmp makeStore v
223 where makeStore [BCSdsFetch i:xs] = [BCSdsStore i:xs]
225 instance seq ByteCode where
226 (>>=.) _ _ = abort "undef on >>=."
229 instance serial ByteCode where
230 serialAvailable = retrn [BCSerialAvail]
231 serialPrint s = retrn [BCSerialPrint]
232 serialPrintln s = retrn [BCSerialPrintln]
233 serialRead = retrn [BCSerialRead]
234 serialParseInt = retrn [BCSerialParseInt]
236 instance userLed ByteCode where
237 ledOn l = retrn [BCLedOn $ toByteCode l]
238 ledOff l = retrn [BCLedOff $ toByteCode l]
240 instance zero BCState where
241 zero = {freshl=[1..], freshs=[1..], sdss=[]}
243 toRealByteCode :: (ByteCode a b) BCState -> (String, BCState)
245 # (bc, st) = runBC x s
246 # (bc, gtmap) = computeGotos bc 1
247 = (concat $ map (toString o toByteVal) (map (implGotos gtmap) bc), st)
249 implGotos map (BCJmp t) = BCJmp $ fromJust ('DM'.get t map)
250 implGotos map (BCJmpT t) = BCJmpT $ fromJust ('DM'.get t map)
251 implGotos map (BCJmpF t) = BCJmpF $ fromJust ('DM'.get t map)
254 computeGotos :: [BC] Int -> ([BC], 'DM'.Map Int Int)
255 computeGotos [] _ = ([], 'DM'.newMap)
256 computeGotos [BCLab l:xs] i = appSnd ('DM'.put l i) (computeGotos xs i)
257 computeGotos [x:xs] i = appFst (\bc->[x:bc]) (computeGotos xs (i+(bclength x)))
259 readable :: BC -> String
260 readable (BCPush d) = "BCPush " +++ concat (map safe d)
263 | isControl c = "\\d" +++ toString (toInt c)
265 readable b = printToString b
267 toReadableByteCode :: (ByteCode a b) -> (String, BCState)
269 # (bc, st) = runBC x zero
270 # (bc, gtmap) = computeGotos bc 0
271 = (join "\n" $ map readable (map (implGotos gtmap) bc), st)
273 toMessages :: MTaskInterval (String, BCState) -> ([MTaskMSGSend], BCState)
274 toMessages interval (bytes, st=:{sdss}) = (
275 [MTSds s.sdsi (toString s.sdsval)\\s<-sdss] ++
276 [MTTask interval bytes], st)
278 toSDSUpdate :: Int Int -> [MTaskMSGSend]
279 toSDSUpdate i v = [MTUpd i (to16bit v)]
281 Start = toMessages (OnInterval 500) $ toRealByteCode (unMain bc) zero
285 {main = If (y ==. lit 0) (pub x) (x =. x *. y :. y =. y -. lit 1)}
287 to16bit :: Int -> String
288 to16bit i = toString (toChar (i/256)) +++ toString (toChar (i rem 256))
290 from16bit :: String -> Int
291 from16bit s = toInt s.[0] * 256 + toInt s.[1]