added debug statements, dynamic allocation is working for tasks
[mTask.git] / mTaskInterpret.dcl
index 1baf878..7ddff19 100644 (file)
@@ -1,25 +1,58 @@
 definition module mTaskInterpret
 
+import mTask
+
 from Data.Functor.Identity import :: Identity
 from Control.Monad.State import :: State, :: StateT
-from Data.Monoid import class Semigroup, class Monoid
-import mTask
+from Control.Monad.RWST import :: RWST, :: RWS
+from Data.Either import :: Either
+from iTasks._Framework.Generic.Defaults import generic gDefault
+from GenPrint import generic gPrint
+from Generics.gCons import class gCons, generic conses, generic consName, generic consIndex, generic consNum
 
-:: MTaskMessage 
-       = MTSds Int String
-       | MTTask Int String
-       | MTPub Int String
-       | MTUpd Int String
+:: MTaskMSGRecv
+       = MTTaskAck Int
+       | MTTaskDelAck Int
+       | MTSDSAck Int
+       | MTSDSDelAck Int
+       | MTPub Int BCValue
+       | MTMessage String
+       | MTDevSpec MTaskDeviceSpec
        | MTEmpty
 
-instance toString MTaskMessage
-encode :: MTaskMessage -> String
-decode :: String -> MTaskMessage
+:: MTaskMSGSend
+       = MTTask MTaskInterval String
+       | MTTaskDel Int
+       | MTSds Int BCValue
+       | MTUpd Int BCValue
+       | MTSpec
+
+:: MTaskInterval
+       = OneShot
+       | OnInterval Int
+       | OnInterrupt Int
+
+:: MTaskDeviceSpec =
+               {haveLed :: Bool
+               ,haveAio :: Bool
+               ,haveDio :: Bool
+               ,maxTask :: Int //Should be number of bytes reserved in total for shares, tasks and functions
+               ,maxSDS  :: Int
+       }
+
+:: BCValue = E.e: BCValue e & mTaskType e
+
+instance toString MTaskInterval
+instance toString MTaskMSGRecv
+instance toString MTaskMSGSend
+encode :: MTaskMSGSend -> String
+decode :: String -> MTaskMSGRecv
 
 :: BC
        = BCNop
        | BCLab Int
-       | BCPush [Char]
+       | BCPush BCValue
+//     | BCPush String
        | BCPop
        //SDS functions
        | BCSdsStore Int
@@ -45,6 +78,9 @@ decode :: String -> MTaskMessage
        | BCJmp Int
        | BCJmpT Int
        | BCJmpF Int
+       //UserLED
+       | BCLedOn
+       | BCLedOff
        //Serial
        | BCSerialAvail
        | BCSerialPrint
@@ -58,34 +94,54 @@ decode :: String -> MTaskMessage
        | BCDigitalWrite Pin
        | BCTest AnalogPin
 
-:: ByteCode a p = BC (BCState -> ([BC], BCState))
-instance Semigroup (ByteCode a p)
-instance Monoid (ByteCode a p)
+derive gPrint BCValue, MTaskDeviceSpec
+derive consIndex BCValue
+derive consName BCValue
+derive conses BCValue
+derive consNum BCValue
+
+derive class gCons Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin
+derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode, MTaskDeviceSpec
+derive gEditor BCValue
+derive gText BCValue
+derive JSONEncode BCValue
+derive JSONDecode BCValue
+derive gDefault BCValue
+derive gEq BCValue
+
+:: ByteCode a p = BC (RWS () [BC] BCState ())
+
+:: BCShare = {
+               sdsi :: Int,
+               sdspub :: Bool,
+               sdsval :: BCValue
+       }
 
 :: BCState = {
                freshl :: [Int],
                freshs :: [Int],
-               sdss :: [(Int, [Char])]
+               sdss :: [BCShare]
        }
 instance zero BCState
 
-class toByteCode a :: a -> [Char]
-instance toByteCode Int
-instance toByteCode Bool
-instance toByteCode Char
-instance toByteCode String
-instance toByteCode Long
-instance toByteCode Button
+class toByteCode a :: a -> String
+class fromByteCode a :: String -> a
+class mTaskType a | toByteCode, fromByteCode, iTask, TC a
+
+instance toByteCode Int, Bool, Char, Long, String, Button, UserLED, BCValue
+instance fromByteCode Int, Bool, Char, Long, String, Button, UserLED, BCValue
+instance toByteCode MTaskInterval
+instance fromByteCode MTaskInterval, MTaskDeviceSpec
 
-instance toChar Pin
 instance arith ByteCode
 instance boolExpr ByteCode
 instance analogIO ByteCode
 instance digitalIO ByteCode
+instance userLed ByteCode
 instance If ByteCode Stmt Stmt Stmt
 instance If ByteCode e Stmt Stmt
 instance If ByteCode Stmt e Stmt
-instance If ByteCode x y Expr
+instance If ByteCode x y Stmt
 instance IF ByteCode
 instance noOp ByteCode
 
@@ -94,10 +150,8 @@ instance assign ByteCode
 instance seq ByteCode
 instance serial ByteCode
 
-//pub :: (ByteCode a b) -> ByteCode a b
-
-toMessages :: Int (String, BCState) -> [MTaskMessage]
+toMessages :: MTaskInterval (Main (ByteCode a b)) BCState -> ([MTaskMSGSend], BCState)
 
-toByteVal :: BC -> [Char]
-toReadableByteCode :: (ByteCode a b) -> (String, BCState)
-toRealByteCode :: (ByteCode a b) -> (String, BCState)
+toByteVal :: BC -> String
+toReadableByteCode :: (ByteCode a b) BCState -> (String, BCState)
+toRealByteCode :: (ByteCode a b) BCState -> (String, BCState)