1 implementation module mTaskInterpret
4 import gdynamic, gCons, GenEq, StdMisc, StdArray, GenBimap
12 from StdFunc import o, const
19 from Data.Func import $
20 from Text import class Text(concat,join,toUpperCase), instance Text String
22 import qualified Data.Map as DM
23 import Text.Encodings.Base64
25 encode :: MTaskMSGSend -> String
26 encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n"
27 encode (MTTask to data) = "t" +++ to16bit to +++ to16bit (size data) +++ data +++ "\n"
28 encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n"
31 decode :: String -> MTaskMSGRecv
33 | size x == 0 = MTEmpty
38 't' = MTTaskAdded (from16bit (x % (1,3)))
39 'u' = MTPub (from16bit (x % (1,3))) (x % (3,5))
40 _ = MTMessage x//abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n")
42 safePrint :== toString o toJSON
44 instance toString MTaskMSGSend where
45 toString (MTSds i v) = "Sds id: " +++ toString i
46 +++ " value " +++ safePrint v
47 toString (MTTask to data) = "Task timeout: " +++ toString to
48 +++ " data " +++ safePrint data
49 toString (MTUpd i v) = "Update id: " +++ toString i
50 +++ " value " +++ safePrint v
52 instance toString MTaskMSGRecv where
53 toString (MTPub i v) = "Publish id: " +++ toString i
54 +++ " value " +++ safePrint v
55 toString (MTTaskAdded i) = "Task added with id: " +++ toString i
56 toString MTEmpty = "Empty message"
59 bclength (BCPush _) = 3
60 bclength (BCLab _) = 2
61 bclength (BCSdsStore _) = 3
62 bclength (BCSdsFetch _) = 3
63 bclength (BCSdsPublish _) = 3
64 bclength (BCAnalogRead _) = 2
65 bclength (BCAnalogWrite _) = 2
66 bclength (BCDigitalRead _) = 2
67 bclength (BCDigitalWrite _) = 2
68 bclength (BCJmp i) = 2
69 bclength (BCJmpT i) = 2
70 bclength (BCJmpF i) = 2
73 toByteVal :: BC -> [Char]
75 # bt = toChar $ consIndex{|*|} b
78 (BCLab i) = [toChar i]
79 (BCSdsStore i) = [c\\c<-:to16bit i]
80 (BCSdsFetch i) = [c\\c<-:to16bit i]
81 (BCSdsPublish i) = [c\\c<-:to16bit i]
82 (BCAnalogRead i) = [toChar i]
83 (BCAnalogWrite i) = [toChar i]
84 (BCDigitalRead i) = [toChar i]
85 (BCDigitalWrite i) = [toChar i]
86 (BCJmp i) = [toChar i]
87 (BCJmpT i) = [toChar i]
88 (BCJmpF i) = [toChar i]
91 instance Semigroup (ByteCode a p) where
92 mappend m n = BC \s->let (b1, (b2, t)) = runBC m <$> runBC m s in (b1 ++ b2, t)
94 instance Monoid (ByteCode a p) where
97 (<++>) infixl 2 :: (ByteCode a p) (ByteCode b q) -> ByteCode c r
98 (<++>) m n = BC \s->let (b1, (b2, t)) = runBC n <$> runBC m s in (b1 ++ b2, t)
101 (<+->) m n :== m <++> retrn n
105 retrn :: ([BC] -> ByteCode a p)
107 fmp :: ([BC] -> [BC]) (ByteCode a p) -> ByteCode a q
108 fmp f b = BC \s->let (bc, s`) = runBC b s in (f bc, s`)
110 instance toByteCode Bool where
111 toByteCode True = [toChar 0, toChar 1]
112 toByteCode False = [toChar 0, toChar 0]
113 instance toByteCode Int where toByteCode n = map toChar [n/256,n rem 256]
114 instance toByteCode Long where toByteCode (L n) = toByteCode n
115 instance toByteCode Char where toByteCode c = [c]
116 instance toByteCode String where toByteCode s = undef
117 instance toByteCode Button where toByteCode s = [toChar $ consIndex{|*|} s]
119 instance toChar Pin where
120 toChar (Digital p) = toChar $ consIndex{|*|} p
121 toChar (Analog p) = toChar $ consIndex{|*|} p
123 derive gPrint BC, AnalogPin, Pin, DigitalPin
124 derive consIndex BC, Pin, Button
125 derive consName BC, Pin, Button
127 instance arith ByteCode where
128 lit x = retrn [BCPush $ toByteCode x]
129 (+.) x y = x <++> y <+-> [BCAdd]
130 (-.) x y = x <++> y <+-> [BCSub]
131 (*.) x y = x <++> y <+-> [BCMul]
132 (/.) x y = x <++> y <+-> [BCDiv]
134 instance boolExpr ByteCode where
135 (&.) x y = x <++> y <+-> [BCAnd]
136 (|.) x y = x <++> y <+-> [BCOr]
137 Not x = x <+-> [BCNot]
138 (==.) x y = x <++> y <+-> [BCEq]
139 (!=.) x y = x <++> y <+-> [BCNeq]
140 (<.) x y = x <++> y <+-> [ BCLes]
141 (>.) x y = x <++> y <+-> [BCGre]
142 (<=.) x y = x <++> y <+-> [BCLeq]
143 (>=.) x y = x <++> y <+-> [BCGeq]
145 instance analogIO ByteCode where
146 analogRead p = retrn [BCAnalogRead $ pin p]
147 analogWrite p b = b <+-> [BCAnalogWrite $ pin p]
149 instance digitalIO ByteCode where
150 digitalRead p = retrn [BCDigitalRead $ pin p]
151 digitalWrite p b = b <+-> [BCDigitalWrite $ pin p]
153 //instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
154 //instance If ByteCode e Stmt Stmt where If b t e = BCIfStmt b t e
155 //instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
156 instance If ByteCode x y Stmt where If b t e = BCIfStmt b t e
157 instance IF ByteCode where
158 IF b t e = BCIfStmt b t e
159 (?) b t = BCIfStmt b t $ retrn []
161 withLabel \else->withLabel \endif->
162 b <++> retrn [BCJmpF else] <++> t
163 <++> retrn [BCJmp endif,BCLab else] <++> e <++> retrn [BCLab endif]
165 instance noOp ByteCode where noOp = retrn [BCNop]
167 withLabel :: (Int -> (ByteCode b q)) -> ByteCode b q
168 withLabel f = BC \s->let [fresh:fs] = s.freshl
169 in runBC (f fresh) {s & freshl=fs}
171 withSDS :: (Int -> (ByteCode b q)) -> ByteCode b q
172 withSDS f = BC \s->let [fresh:fs] = s.freshs
173 in runBC (f fresh) {s & freshs=fs}
175 setSDS :: Int v -> ByteCode b q | toByteCode v
176 setSDS ident val = BC \s->([], {s & sdss = [(ident, toByteCode val):s.sdss]})
178 instance sds ByteCode where
179 sds f = {main = withSDS \sds->
180 let (v In body) = f $ retrn [BCSdsFetch sds]
181 in setSDS sds v <++> unMain body
184 pub x = fmp makePub x
187 instance assign ByteCode where
188 (=.) v e = e <++> fmp makeStore v
191 makePub [x:xs] = case x of
192 BCSdsFetch i = [BCSdsPublish i:xs]
196 makeStore [x:xs] = case x of
197 BCSdsFetch i = [BCSdsStore i:xs]
200 instance seq ByteCode where
201 (>>=.) _ _ = abort "undef on >>=."
204 instance serial ByteCode where
205 serialAvailable = retrn [BCSerialAvail]
206 serialPrint s = retrn [BCSerialPrint]
207 serialPrintln s = retrn [BCSerialPrintln]
208 serialRead = retrn [BCSerialRead]
209 serialParseInt = retrn [BCSerialParseInt]
211 instance zero BCState where
212 zero = {freshl=[1..], freshs=[1..], sdss=[]}
215 toRealByteCode :: (ByteCode a b) -> (String, BCState)
217 # (bc, st) = runBC x zero
218 # (bc, gtmap) = computeGotos bc 1
219 = (concat $ map (toString o toByteVal) (map (implGotos gtmap) bc), st)
221 implGotos map (BCJmp t) = BCJmp $ fromJust ('DM'.get t map)
222 implGotos map (BCJmpT t) = BCJmpT $ fromJust ('DM'.get t map)
223 implGotos map (BCJmpF t) = BCJmpF $ fromJust ('DM'.get t map)
226 computeGotos :: [BC] Int -> ([BC], 'DM'.Map Int Int)
227 computeGotos [] _ = ([], 'DM'.newMap)
228 computeGotos [BCLab l:xs] i = appSnd ('DM'.put l i) (computeGotos xs i)
229 computeGotos [x:xs] i = appFst (\bc->[x:bc]) (computeGotos xs (i+(bclength x)))
231 readable :: BC -> String
232 readable (BCPush d) = "BCPush " +++ concat (map safe d)
235 | isControl c = "\\d" +++ toString (toInt c)
237 readable b = printToString b
239 toReadableByteCode :: (ByteCode a b) -> (String, BCState)
241 # (bc, st) = runBC x zero
242 # (bc, gtmap) = computeGotos bc 0
243 = (join "\n" $ map readable (map (implGotos gtmap) bc), st)
246 //Start = toReadableByteCode bc
248 // bc :: ByteCode Int Expr
249 // bc = (lit 36 +. lit 42) +. lit 44
250 toMessages :: Int (String, BCState) -> ([MTaskMSGSend], BCState)
251 toMessages interval (bytes, st=:{sdss}) = ([MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes], st)
253 toSDSUpdate :: Int Int -> [MTaskMSGSend]
254 toSDSUpdate i v = [MTUpd i (to16bit v)]
256 Start = toMessages 500 $ toRealByteCode (unMain bc)
257 //Start = fst $ toReadableByteCode $ unMain bc
261 {main = If (y ==. lit 0) (pub x) (x =. x *. y :. y =. y -. lit 1)}
263 //pub :: (ByteCode a b) -> ByteCode a b
264 //pub x = fmp makePub x
266 to16bit :: Int -> String
267 to16bit i = toString (toChar (i/256)) +++ toString (toChar (i rem 256))
269 from16bit :: String -> Int
270 from16bit s = toInt s.[0] * 256 + toInt s.[1]