import Generics.gCons
+from iTasks.UI.Editor.Common import emptyEditor
+
import GenEq, StdMisc, StdArray, GenBimap
import GenPrint
import StdEnum
toByteVal :: BC -> String
toByteVal b = {toChar $ consIndex{|*|} b} +++
case b of
- (BCPush i) = toByteCode i
+ (BCPush (BCValue i)) = toByteCode i
(BCLab i) = {toChar i}
(BCSdsStore i) = to16bit i
(BCSdsFetch i) = to16bit i
(BCSdsPublish i) = to16bit i
- (BCAnalogRead i) = {toChar i}
- (BCAnalogWrite i) = {toChar i}
- (BCDigitalRead i) = {toChar i}
- (BCDigitalWrite i) = {toChar i}
+ (BCAnalogRead i) = {toChar $ consIndex{|*|} i}
+ (BCAnalogWrite i) = {toChar $ consIndex{|*|} i}
+ (BCDigitalRead i) = {toChar $ consIndex{|*|} i}
+ (BCDigitalWrite i) = {toChar $ consIndex{|*|} i}
(BCJmp i) = {toChar i}
(BCJmpT i) = {toChar i}
(BCJmpF i) = {toChar i}
_ = ""
-instance toByteCode BCValue where toByteCode (BCValue v) = toByteCode v
-instance toByteCode Bool where toByteCode b = toByteCode $ if b 1 0
-instance toByteCode Int where toByteCode n = {toChar $ n/256,toChar $ n rem 256}
-instance toByteCode Long where toByteCode (L n) = toByteCode n
-instance toByteCode Char where toByteCode c = toByteCode $ toInt c
+parseBCValue :: Char String -> BCValue
+parseBCValue c s = case c of
+ 'b' = BCValue $ castfbc True s
+ 'i' = BCValue $ castfbc 0 s
+ 'l' = BCValue $ castfbc (L 0) s
+ 'c' = BCValue $ castfbc ('0') s
+ 'B' = BCValue $ castfbc (NoButton) s
+ 'L' = BCValue $ castfbc (LED1) s
+
+castfbc :: a -> (String -> a) | mTaskType a
+castfbc _ = fromByteCode
+
+instance toByteCode Bool where toByteCode b = {#'b','\0',if b '\x01' '\0'}
+instance toByteCode Int where toByteCode n = {'i',toChar $ n/256,toChar $ n rem 256}
+instance toByteCode Long where toByteCode (L n) = {'l',toChar $ n/256,toChar $ n rem 256}
+instance toByteCode Char where toByteCode c = {'c','\0',c}
instance toByteCode String where toByteCode s = abort $ "Undef on toBytecode String" +++ s
-instance toByteCode Button where toByteCode s = toByteCode $ consIndex{|*|} s
-instance toByteCode UserLED where toByteCode s = toByteCode $ consIndex{|*|} s
-instance toByteCode MTaskInterval where
- toByteCode OneShot = toByteCode 0
- //Intervals have the first bit 0 and the rest is a 15 bit unsigned int
- toByteCode (OnInterval i) = {toChar $ i/256 bitand 127, toChar $ i rem 256}
- //Intervals have the first bit 1 and the rest is a 15 bit unsigned int
- toByteCode (OnInterrupt i) = {toChar $ i/256 bitor 127, toChar $ i rem 256}
-
-//parseByteCode :: String -> BCValue
-//parseByteCode "b" = BCValue True
-//parseByteCode "i" = BCValue 0
+instance toByteCode Button where toByteCode s = {'B','\0',toChar $ consIndex{|*|} s}
+instance toByteCode UserLED where toByteCode s = {'L','\0',toChar $ consIndex{|*|} s}
+instance toByteCode BCValue where toByteCode (BCValue v) = toByteCode v
instance fromByteCode Bool where fromByteCode s = fromByteCode s == 1
instance fromByteCode Int where fromByteCode s = (toInt s.[0])*256 + toInt s.[1]
instance fromByteCode String where fromByteCode s = abort $ "Undef on fromBytecode String" +++ s
instance fromByteCode Button where fromByteCode s = conses{|*|} !! fromByteCode s
instance fromByteCode UserLED where fromByteCode s = conses{|*|} !! fromByteCode s
-instance fromByteCode BCValue
- where
- fromByteCode s = let tail = subString 1 (size s) s in case s.[0] of
- 'b' = BCValue $ castfbc True tail
- 'i' = BCValue $ castfbc 0 tail
- 'l' = BCValue $ castfbc (L 0) tail
- 'c' = BCValue $ castfbc ('0') tail
- 'B' = BCValue $ castfbc (NoButton) tail
- 'L' = BCValue $ castfbc (LED1) tail
- where
- castfbc :: a -> (String -> a) | mTaskType a
- castfbc _ = fromByteCode
-
+instance fromByteCode BCValue where fromByteCode s = parseBCValue s.[0] s
+
+instance toByteCode MTaskInterval where
+ toByteCode OneShot = toByteCode 0
+ //Intervals have the first bit 0 and the rest is a 15 bit unsigned int
+ toByteCode (OnInterval i) = {toChar $ i/256 bitand 127, toChar $ i rem 256}
+ //Intervals have the first bit 1 and the rest is a 15 bit unsigned int
+ toByteCode (OnInterrupt i) = {toChar $ i/256 bitor 127, toChar $ i rem 256}
instance fromByteCode MTaskInterval
where
fromByteCode s
i = OnInterval i
= OnInterrupt $ fromByteCode s bitand 127
-instance toChar Pin where
- toChar (Digital p) = toChar $ consIndex{|*|} p
- toChar (Analog p) = toChar $ consIndex{|*|} p
-
derive gPrint Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin, BC
derive class gCons BC
consNum{|BCValue|} _ = 1
gPrint{|BCValue|} v ps = gPrint{|*|} (readable $ BCPush v) ps
-gEditor{|BCValue|} = undef
+gEditor{|BCValue|} = emptyEditor
gText{|BCValue|} fm Nothing = []
gText{|BCValue|} fm (Just (BCValue e)) = gText{|*|} fm (Just e)
JSONEncode{|BCValue|} b (BCValue e) = JSONEncode{|*|} b (toByteCode e)
JSS :: (Bool [JSONNode] -> (Maybe String, [JSONNode]))
JSS = JSONDecode{|*|}
gDefault{|BCValue|} = BCValue 0
-gEq{|BCValue|} (BCValue e) (BCValue f) = False
+gEq{|BCValue|} (BCValue e) (BCValue f) = toByteCode e == toByteCode f
derive class gCons Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin
derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode
ledOn (BC l) = BC $ l >>| tell [BCLedOn]
ledOff (BC l) = BC $ l >>| tell [BCLedOff]
-func :: (a -> BC) [BC] -> [BC] | mTaskType a
-func f b = abort ('Text'.join "\n" (map printToString b))
-
instance zero BCState where
zero = {freshl=[1..], freshs=[1..], sdss=[]}
implGotos map (BCJmpF t) = BCJmpF $ fromJust ('DM'.get t map)
implGotos _ i = i
-import StdDebug
bclength :: BC -> Int
bclength (BCPush s) = 1 + size (toByteCode s)
bclength (BCSdsStore _) = 3
toReadableByteCode :: (ByteCode a b) BCState -> (String, BCState)
toReadableByteCode x s
# (s, bc) = runBC x s
-| not (trace_tn $ ('Text'.join "\n" $ lineNumbers numbers bc) +++ "\n") = undef
# (bc, gtmap) = computeGotos bc 0
= ('Text'.join "\n" $ lineNumbers numbers (map (implGotos gtmap) bc), s)
where