X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=mTaskInterpret.icl;h=6c9fd52461a2d1e5c04ad990d36622d3d6b7f92a;hb=ff56601fe3bbca4a5d507de43f31de10a11dfdb5;hp=52d7fd40666bb7ccd2a0168214eafeef8d5ac4a3;hpb=7a67ef5e2af69cb14011be201fe67f755b91a788;p=mTask.git diff --git a/mTaskInterpret.icl b/mTaskInterpret.icl index 52d7fd4..6c9fd52 100644 --- a/mTaskInterpret.icl +++ b/mTaskInterpret.icl @@ -1,48 +1,105 @@ implementation module mTaskInterpret //import iTasks -import gdynamic, gCons, GenEq, StdMisc, StdArray +import gdynamic, gCons, GenEq, StdMisc, StdArray, GenBimap import GenPrint import mTask +import StdFile +import StdString + from StdFunc import o +import StdBool import StdTuple import Data.Tuple -import Control.Monad -import Control.Monad.State +import StdList from Data.Func import $ - +from Text import class Text(join,toUpperCase), instance Text String toByteVal :: BC -> String toByteVal a = undef -derive gPrint BC +derive gPrint BC, AnalogPin +derive consIndex BC +derive consName BC toReadableByteVal :: BC -> String toReadableByteVal a = printToString a -//:: ByteCode a p = BC (BCState -> ([BC], BCState)) instance arith ByteCode where - lit a = BC \_ s->([BCPush $ toCode a], s) - (+.) _ _ = undef - (-.) _ _ = undef - (*.) _ _ = undef - (/.) _ _ = undef + lit x = BC [BCPush $ toCode x] + (+.) x y = x <++> y <+-> [BCAdd] + (-.) x y = x <++> y <+-> [BCSub] + (*.) x y = x <++> y <+-> [BCMul] + (/.) x y = x <++> y <+-> [BCDiv] + +instance boolExpr ByteCode where + (&.) x y = x <++> y <+-> [BCAnd] + (|.) x y = x <++> y <+-> [BCOr] + Not x = x <+-> [BCNot] + (==.) x y = x <++> y <+-> [BCEq] + (!=.) x y = x <++> y <+-> [BCNeq] + (<.) x y = x <++> y <+-> [ BCLes] + (>.) x y = x <++> y <+-> [BCGre] + (<=.) x y = x <++> y <+-> [BCLeq] + (>=.) x y = x <++> y <+-> [BCGeq] + +instance analogIO ByteCode where + analogRead p = BC [BCAnalogRead $ toCode p] + analogWrite p b = b <+-> [BCAnalogWrite $ toCode p] + +instance digitalIO ByteCode where + digitalRead p = BC [BCDigitalRead $ toCode p] + digitalWrite p b = b <+-> [BCDigitalWrite $ toCode p] + +instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e +instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e +instance If ByteCode Stmt Stmt e where If b t e = BCIfStmt b t e +instance If ByteCode x y Expr where If b t e = BCIfStmt b t e +instance IF ByteCode where + IF b t e = BCIfStmt b t e + (?) b t = BCIfStmt b t $ BC [] +BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t + <+-> [BCJmp $ length <$> e] <++> e + +instance noOp ByteCode where noOp = BC [] instance serial ByteCode where - serialAvailable = undef - serialPrint _ = undef - serialPrintln _ = undef - serialRead = undef - serialParseInt = undef + serialAvailable = BC [BCSerialAvail] + serialPrint s = BC [BCSerialPrint] + serialPrintln s = BC [BCSerialPrintln] + serialRead = BC [BCSerialRead] + serialParseInt = BC [BCSerialParseInt] + +(<++>) infixl 7 +(<++>) (BC x) (BC y) = BC $ x ++ y +(<+->) infixl 7 +(<+->) (BC x) y = BC $ x ++ y +(<-+>) infixl 7 +(<-+>) x (BC y) = BC $ x ++ y + +(<$>) infixl 9 +(<$>) f (BC x) = f x 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 +//Start = (lit 36 +. lit 42) +. lit 84 + +//Run test programma en pretty print +Start :: ByteCode Int Expr +Start = If (lit True) (analogRead A1) (analogRead A0) +//Start = If ((lit 36) ==. (lit 42)) (noOp) (noOp) + +//Generate header file +//Start w +//# (io, w) = stdio w +//# io = io <<< "#ifndef MTASK_H\n#define MTASK_H\n" +//# io = io <<< join "\n" ["#define " <+ toUpperCase (consName{|*|} x) <+ " " <+ consIndex{|*|} x\\x<-allBC] +// with +// allBC :: [BC] +// allBC = conses{|*|} +//# (ok, w) = fclose (io <<< "\n#endif\n") w +//| not ok = abort "Couldn't close stdio" +//= w