fix bugs for uireorg
[mTask.git] / mTaskInterpret.icl
index 56d19e6..789c58a 100644 (file)
@@ -21,7 +21,7 @@ from Text import class Text(concat,join,toUpperCase), instance Text String
 
 toByteVal :: BC -> [Char]
 toByteVal b
-# bt = toBC b
+# bt = toChar $ consIndex{|*|} b + 1
 = [bt:case b of
                (BCPush i) = i
                (BCAnalogRead i) = [toChar i]
@@ -32,8 +32,6 @@ toByteVal b
                (BCJmpT i) = [toChar i]
                (BCJmpF i) = [toChar i]
                _ = []]
-       where
-               toBC b = toChar $ consIndex{|*|} b + 1
 
 instance Semigroup (ByteCode a p) where
        mappend m n = BC \s->let (b1, (b2, t)) = runBC m <$> runBC m s in (b1 ++ b2, t)
@@ -51,11 +49,13 @@ runBC (BC m) = m
 
 retrn :: ([BC] -> ByteCode a p)
 retrn = BC o tuple
+fmp :: ([BC] -> [BC]) (ByteCode a p) -> ByteCode a p
+fmp f b = BC \s->let (bc, s`) = runBC b s in (f bc, s`)
 
 instance toByteCode Bool where
        toByteCode True = [toChar 1]
        toByteCode False = [toChar 0]
-instance toByteCode Int where toByteCode n = map toChar [n/(2<<7),n rem 265]
+instance toByteCode Int where toByteCode n = map toChar [n/256,n rem 256]
 instance toByteCode Long where toByteCode (L n) = toByteCode n
 instance toByteCode Char where toByteCode c = [c]
 instance toByteCode String where toByteCode s = undef
@@ -65,7 +65,7 @@ instance toChar Pin where
        toChar (Digital p) = toChar $ consIndex{|*|} p + 1
        toChar (Analog p) = toChar $ consIndex{|*|} p + 1
 
-derive gPrint BC, AnalogPin, Pin, DigitalPin
+derive gPrint BC, AnalogPin, Pin, DigitalPin, BCState
 derive consIndex BC, Pin, Button
 derive consName BC, Pin, Button
 
@@ -107,20 +107,32 @@ BCIfStmt b t e = withLabel \else->withLabel \endif->retrn [BCJmpF else] <++> t
 
 instance noOp ByteCode where noOp = mempty
 
-
 withLabel :: (Int -> (ByteCode b q)) -> ByteCode b q
 withLabel f = BC \s->let [fresh:fs] = s.freshl
        in runBC (f fresh) {s & freshl=fs}
 
-/*
+withSDS :: (Int -> (ByteCode b q)) -> ByteCode b q
+withSDS f = BC \s->let [fresh:fs] = s.freshs
+       in runBC (f fresh) {s & freshs=fs}
+
 instance sds ByteCode where
-       sds f = undef/*{main = 
-                       let var = 42
-                           (v In body) = f var
-                       in unMain body
-               }*/
+       sds f = {main = withSDS \sds->
+                       let (v In body) = f $ retrn [BCSdsFetch sds]
+                       in retrn [BCPush $ toByteCode v,BCSdsStore sds] <++> unMain body
+               }
        con f = undef
-*/
+
+instance assign ByteCode where
+       (=.) v e = e <++> fmp makeStore v
+
+makeStore [] = []
+makeStore [x:xs] = case x of
+       BCSdsFetch i = [BCSdsStore i:xs]
+       y = [y:xs]
+
+instance seq ByteCode where
+       (>>=.) _ _ = abort "undef on >>=."
+       (:.) x y = x <++> y
 
 instance serial ByteCode where
        serialAvailable = retrn [BCSerialAvail]
@@ -130,7 +142,7 @@ instance serial ByteCode where
        serialParseInt = retrn [BCSerialParseInt]
 
 instance zero BCState where
-       zero = {freshl=[1..]}
+       zero = {freshl=[1..], freshs=[1..]}
 
 toRealByteCode :: (ByteCode a Expr) -> String
 toRealByteCode x
@@ -142,15 +154,20 @@ toReadableByteCode x
 # (bc, st) = runBC x zero
 = join "\n" $ map printToString bc
 
-toReadableByteVal :: BC -> String
-toReadableByteVal a = printToString a
-
+//Start :: String
+//Start = toReadableByteCode bc
+//     where
+//             bc :: ByteCode Int Expr
+//             bc = (lit 36 +. lit 42) +. lit 44
 
 Start :: String
-Start = toReadableByteCode bc
+Start = toReadableByteCode $ unMain bc
+//Start = toRealByteCode $ unMain bc
        where
-               bc :: ByteCode Int Expr
-               bc = (lit 36 +. lit 42) +. lit 44
+               bc :: Main (ByteCode Int Expr)
+               bc = sds \x=41 In 
+                       sds \y=1 In 
+                       {main = x =. x +. y}
 
 //to16bit :: Int -> String
 //to16bit i = toString (toChar (i/265)) +++ toString (toChar (i rem 265))