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 = [(ident, toByteCode val):s.sdss]})
208 instance sds ByteCode where
209 sds f = {main = withSDS \sds->
210 let (v In body) = f $ retrn [BCSdsFetch sds]
211 in setSDS sds v <++> unMain body
214 pub x = fmp makePub x
216 instance assign ByteCode where
217 (=.) v e = e <++> fmp makeStore v
220 makePub [x:xs] = case x of
221 BCSdsFetch i = [BCSdsPublish i:xs]
225 makeStore [x:xs] = case x of
226 BCSdsFetch i = [BCSdsStore i:xs]
229 instance seq ByteCode where
230 (>>=.) _ _ = abort "undef on >>=."
233 instance serial ByteCode where
234 serialAvailable = retrn [BCSerialAvail]
235 serialPrint s = retrn [BCSerialPrint]
236 serialPrintln s = retrn [BCSerialPrintln]
237 serialRead = retrn [BCSerialRead]
238 serialParseInt = retrn [BCSerialParseInt]
240 instance userLed ByteCode where
241 ledOn l = retrn [BCLedOn $ toByteCode l]
242 ledOff l = retrn [BCLedOff $ toByteCode l]
244 instance zero BCState where
245 zero = {freshl=[1..], freshs=[1..], sdss=[]}
247 toRealByteCode :: (ByteCode a b) BCState -> (String, BCState)
249 # (bc, st) = runBC x s
250 # (bc, gtmap) = computeGotos bc 1
251 = (concat $ map (toString o toByteVal) (map (implGotos gtmap) bc), st)
253 implGotos map (BCJmp t) = BCJmp $ fromJust ('DM'.get t map)
254 implGotos map (BCJmpT t) = BCJmpT $ fromJust ('DM'.get t map)
255 implGotos map (BCJmpF t) = BCJmpF $ fromJust ('DM'.get t map)
258 computeGotos :: [BC] Int -> ([BC], 'DM'.Map Int Int)
259 computeGotos [] _ = ([], 'DM'.newMap)
260 computeGotos [BCLab l:xs] i = appSnd ('DM'.put l i) (computeGotos xs i)
261 computeGotos [x:xs] i = appFst (\bc->[x:bc]) (computeGotos xs (i+(bclength x)))
263 readable :: BC -> String
264 readable (BCPush d) = "BCPush " +++ concat (map safe d)
267 | isControl c = "\\d" +++ toString (toInt c)
269 readable b = printToString b
271 toReadableByteCode :: (ByteCode a b) -> (String, BCState)
273 # (bc, st) = runBC x zero
274 # (bc, gtmap) = computeGotos bc 0
275 = (join "\n" $ map readable (map (implGotos gtmap) bc), st)
277 toMessages :: MTaskInterval (String, BCState) -> ([MTaskMSGSend], BCState)
278 toMessages interval (bytes, st=:{sdss}) = ([MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes], st)
280 toSDSUpdate :: Int Int -> [MTaskMSGSend]
281 toSDSUpdate i v = [MTUpd i (to16bit v)]
283 Start = toMessages (OnInterval 500) $ toRealByteCode (unMain bc) zero
287 {main = If (y ==. lit 0) (pub x) (x =. x *. y :. y =. y -. lit 1)}
289 to16bit :: Int -> String
290 to16bit i = toString (toChar (i/256)) +++ toString (toChar (i rem 256))
292 from16bit :: String -> Int
293 from16bit s = toInt s.[0] * 256 + toInt s.[1]