From 7a67ef5e2af69cb14011be201fe67f755b91a788 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 7 Nov 2016 21:15:46 +0100 Subject: [PATCH] tests --- mTask.dcl | 4 ++-- mTask.icl | 2 +- mTaskInterpret.dcl | 6 ++++-- mTaskInterpret.icl | 44 ++++++++++++++++++++++++++++++-------------- mTaskSimulation.dcl | 24 ++++++++++++------------ mTaskSimulation.icl | 24 ++++++++++++------------ 6 files changed, 61 insertions(+), 43 deletions(-) diff --git a/mTask.dcl b/mTask.dcl index 6613e2a..7380ebc 100644 --- a/mTask.dcl +++ b/mTask.dcl @@ -16,7 +16,7 @@ todo: //import iTasks import iTasks._Framework.Generic -import iTasks._Framework.Task +from iTasks._Framework.Task import :: Task import StdClass from iTasks.API.Core.Types import :: Display import gdynamic, gCons, GenEq, StdMisc, StdArray @@ -182,7 +182,7 @@ instance typeSelector a :: In a b = In infix 0 a b -read` :: Int (ReadWrite a) State -> (a,State) | dyn a +read` :: Int (ReadWrite a) State` -> (a,State`) | dyn a // ----- long ----- // diff --git a/mTask.icl b/mTask.icl index f0249c9..34703d2 100644 --- a/mTask.icl +++ b/mTask.icl @@ -84,7 +84,7 @@ instance typeSelector Char where typeSelector = c ".c" instance typeSelector Bool where typeSelector = c ".b" instance typeSelector a where typeSelector = c ".w" -read` :: Int (ReadWrite a) State -> (a,State) | dyn a +read` :: Int (ReadWrite a) State` -> (a,State`) | dyn a read` n Rd s = (fromJust (fromDyn (s.store !! n)), s) read` n (Wrt a) s = (a,{s&store=updateAt n (toDyn a) s.store}) read` n (Updt f) s=:{store} diff --git a/mTaskInterpret.dcl b/mTaskInterpret.dcl index 557225f..32e7b9b 100644 --- a/mTaskInterpret.dcl +++ b/mTaskInterpret.dcl @@ -1,10 +1,12 @@ definition module mTaskInterpret +from Data.Functor.Identity import :: Identity +from Control.Monad.State import :: State, :: StateT import mTask :: BC = BCNop - | BCPush Int + | BCPush String | BCPop //Unary ops | BCNeg @@ -15,7 +17,7 @@ import mTask | BCMul | BCDiv -:: ByteCode a p = BC ((ReadWrite a) BCState -> ([BC], BCState)) +:: ByteCode a p = BC ((ReadWrite (ByteCode a Expr)) BCState -> ([BC], BCState)) :: BCState = { a::() } diff --git a/mTaskInterpret.icl b/mTaskInterpret.icl index eecea1d..52d7fd4 100644 --- a/mTaskInterpret.icl +++ b/mTaskInterpret.icl @@ -1,10 +1,17 @@ implementation module mTaskInterpret -import iTasks +//import iTasks import gdynamic, gCons, GenEq, StdMisc, StdArray import GenPrint import mTask +from StdFunc import o +import StdTuple +import Data.Tuple +import Control.Monad +import Control.Monad.State +from Data.Func import $ + toByteVal :: BC -> String toByteVal a = undef @@ -14,19 +21,28 @@ derive gPrint BC toReadableByteVal :: BC -> String toReadableByteVal a = printToString a +//:: ByteCode a p = BC (BCState -> ([BC], BCState)) instance arith ByteCode where - lit _ = undef - (+.) _ _ = undef - (-.) _ _ = undef - (*.) _ _ = undef - (/.) _ _ = undef + lit a = BC \_ s->([BCPush $ toCode a], s) + (+.) _ _ = undef + (-.) _ _ = undef + (*.) _ _ = undef + (/.) _ _ = undef instance serial ByteCode where - serialAvailable = undef - serialPrint _ = undef - serialPrintln _ = undef - serialRead = undef - serialParseInt = undef - -Start :: Main (ByteCode Int Expr) -Start = {main=serialPrint (lit 36)} + serialAvailable = undef + serialPrint _ = undef + serialPrintln _ = undef + serialRead = undef + serialParseInt = undef + +instance zero BCState where + zero = {a=()} + +runByteCode :: (ByteCode Int Expr) BCState -> [BC] +runByteCode (BC f) s = fst (f Rd s) + +//Start :: Main (ByteCode Int Expr) +Start :: [BC] +//Start :: ByteCode Int Expr +Start = runByteCode (lit 36) zero diff --git a/mTaskSimulation.dcl b/mTaskSimulation.dcl index 496bd52..5e502e2 100644 --- a/mTaskSimulation.dcl +++ b/mTaskSimulation.dcl @@ -2,11 +2,11 @@ definition module mTaskSimulation import mTask -instance zero State +instance zero State` eval :: (Main (Eval t p)) -> [String] | toString t -:: State = - { tasks :: [(Int, State->State)] +:: State` = + { tasks :: [(Int, State`->State`)] , store :: [Dyn] , dpins :: [(DigitalPin, Bool)] , apins :: [(AnalogPin, Int)] @@ -14,11 +14,11 @@ eval :: (Main (Eval t p)) -> [String] | toString t , millis:: Int } -//:: TaskSim :== (Int, State->State) -:: Eval t p = E ((ReadWrite t) State -> (t, State)) -toS2S :: (Eval t p) -> (State->State) +//:: TaskSim :== (Int, State`->State`) +:: Eval t p = E ((ReadWrite t) State` -> (t, State`)) +toS2S :: (Eval t p) -> (State`->State`) -unEval :: (Eval t p) -> ((ReadWrite t) State -> (t, State)) +unEval :: (Eval t p) -> ((ReadWrite t) State` -> (t, State`)) :: ReadWrite t = Rd | Wrt t | Updt (t->t) @@ -66,13 +66,13 @@ instance arg (Eval t p, Eval u q, Eval v r, Eval w s) | type t & type u & type v instance + String readPinA :: AnalogPin [(AnalogPin, Int)] -> Int -writePinA :: AnalogPin Int State -> State +writePinA :: AnalogPin Int State` -> State` class readPinD p :: p [(DigitalPin,Bool)] [(AnalogPin,Int)] -> Bool instance readPinD DigitalPin instance readPinD AnalogPin -class writePinD p :: p Bool State -> State +class writePinD p :: p Bool State` -> State` instance writePinD DigitalPin instance writePinD AnalogPin @@ -81,8 +81,8 @@ instance writePinD AnalogPin derive class iTask DigitalPin, AnalogPin, Dyn, StateInterface, DisplayVar, Pin simulate :: (Main (Eval a p)) -> Task () -toView :: State -> StateInterface -mergeView :: State StateInterface -> State +toView :: State` -> StateInterface +mergeView :: State` StateInterface -> State` :: StateInterface = { serialOut :: Display [String] , analogPins :: [(AnalogPin, Int)] @@ -102,7 +102,7 @@ fromDisplayVar :: DisplayVar Dyn -> Dyn | LCD16x2 String String | DisplayVar [String] -step` :: State -> State +step` :: State` -> State` class stringQuotes t | type t :: (Code t p) -> Code t p instance stringQuotes String diff --git a/mTaskSimulation.icl b/mTaskSimulation.icl index 3a94811..6fed084 100644 --- a/mTaskSimulation.icl +++ b/mTaskSimulation.icl @@ -7,8 +7,8 @@ import mTask eval :: (Main (Eval t p)) -> [String] | toString t eval {main=(E f)} = [toString (fst (f Rd zero))] -:: State = - { tasks :: [(Int, State->State)] +:: State` = + { tasks :: [(Int, State`->State`)] , store :: [Dyn] , dpins :: [(DigitalPin, Bool)] , apins :: [(AnalogPin, Int)] @@ -16,15 +16,15 @@ eval {main=(E f)} = [toString (fst (f Rd zero))] , millis:: Int } -instance zero State where +instance zero State` where zero = {store = [], tasks = [], serial = [], millis = 0, dpins = [] , apins = []} -//:: TaskSim :== (Int, State->State) -:: Eval t p = E ((ReadWrite t) State -> (t, State)) -toS2S :: (Eval t p) -> (State->State) +//:: TaskSim :== (Int, State`->State`) +:: Eval t p = E ((ReadWrite t) State` -> (t, State`)) +toS2S :: (Eval t p) -> (State`->State`) toS2S (E f) = \state.snd (f Rd state) -unEval :: (Eval t p) -> ((ReadWrite t) State -> (t, State)) +unEval :: (Eval t p) -> ((ReadWrite t) State` -> (t, State`)) unEval (E f) = f :: ReadWrite t = Rd | Wrt t | Updt (t->t) @@ -126,7 +126,7 @@ readPinA p lista [] = 0 [a:x] = a -writePinA :: AnalogPin Int State -> State +writePinA :: AnalogPin Int State` -> State` writePinA p x s = {s & apins = [(p, x):[(q, y) \\ (q, y) <- s.apins | p <> q]]} @@ -141,7 +141,7 @@ instance readPinD AnalogPin where = case [b \\ (q,b) <- lista | p == q] of [] = False [a:x] = a <> 0 -class writePinD p :: p Bool State -> State +class writePinD p :: p Bool State` -> State` instance writePinD DigitalPin where writePinD p b s=:{dpins} = {s & dpins = [(p, b):[(q, c) \\ (q, c) <- dpins | p <> q]]} instance writePinD AnalogPin where @@ -172,7 +172,7 @@ simulate {main=(E f)} = setup zero where ] ] -toView :: State -> StateInterface +toView :: State` -> StateInterface toView s = { serialOut = Display s.serial , analogPins = s.apins @@ -182,7 +182,7 @@ toView s = , taskCount = Display (length s.tasks) } -mergeView :: State StateInterface -> State +mergeView :: State` StateInterface -> State` mergeView s si = { s & store = [fromDisplayVar new old \\ new <- si.var2iables & old <- s.store] @@ -229,7 +229,7 @@ fromDisplayVar (DisplayVar l) dyn = Dyn l | DisplayVar [String] -step` :: State -> State +step` :: State` -> State` step` s = foldr appTask {s & millis = s.millis + delta, tasks = []} [(w - delta, f) \\ (w, f) <- s.tasks] -- 2.20.1