+ lit x = retrn [BCPush $ toByteCode 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 = retrn [BCAnalogRead $ pin p]
+ analogWrite p b = b <+-> [BCAnalogWrite $ pin p]
+
+instance digitalIO ByteCode where
+ digitalRead p = retrn [BCDigitalRead $ pin p]
+ digitalWrite p b = b <+-> [BCDigitalWrite $ pin p]
+
+//instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
+//instance If ByteCode e 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 x y Stmt 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 $ retrn []
+BCIfStmt b t e =
+ withLabel \else->withLabel \endif->
+ b <++> retrn [BCJmpF else] <++> t
+ <++> retrn [BCJmp endif,BCLab else] <++> e <++> retrn [BCLab endif]
+
+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}
+
+setSDS :: Int v -> ByteCode b q | toByteCode v
+setSDS ident val = BC \s->([], {s & sdss = [(ident, toByteCode val):s.sdss]})
+
+instance sds ByteCode where
+ sds f = {main = withSDS \sds->
+ let (v In body) = f $ retrn [BCSdsFetch sds]
+ in setSDS sds v <++> unMain body
+ }
+ con f = undef
+ pub x = fmp makePub x
+// pub _ = undef
+
+instance assign ByteCode where
+ (=.) v e = e <++> fmp makeStore v
+
+makePub [] = []
+makePub [x:xs] = case x of
+ BCSdsFetch i = [BCSdsPublish i:xs]
+ y = [y:xs]
+
+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