started with interpreter for basic language
[mTask.git] / mTaskInterpret.icl
index eecea1d..eaded85 100644 (file)
@@ -1,10 +1,22 @@
 implementation module mTaskInterpret
 
-import iTasks
+//import iTasks
 import gdynamic, gCons, GenEq, StdMisc, StdArray
 import GenPrint
 import mTask
 
+import StdFile
+import StdString
+
+from StdFunc import o
+import StdTuple
+import Data.Tuple
+import StdList
+from Data.Func import $
+from Text import class Text(join), instance Text String
+
+
+
 
 toByteVal :: BC -> String
 toByteVal a = undef
@@ -15,18 +27,69 @@ toReadableByteVal :: BC -> String
 toReadableByteVal a = printToString a
 
 instance arith ByteCode where
-  lit _ = undef
-  (+.) _ _ = 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 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 Stmt x y 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 []
+
+
+(<++>) 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 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=()}
+
+//Start :: ByteCode Int Expr
+//Start = (lit 36 +. lit 42) +. lit 84
+//
+derive consIndex BC
+derive consName BC
+
+Start w
+# (io, w) = stdio w
+# io = io <<< "#ifndef MTASK_H\n#define MTASK_H\n"
+# io = io <<< join "\n" ["#define " +++ consName{|*|} x +++ " " +++ toString (consIndex{|*|} x)\\x<-allBC]
+# (ok, w) = fclose (io <<< "\n#endif\n") w
+| not ok = abort "Couldn't close stdio"
+= w
+       where
+               allBC = [BCNop, BCPush "", BCPop, BCNeg, BCNot, BCAdd, BCSub, BCMul,
+                       BCDiv, BCAnd, BCOr, BCEq, BCNeq, BCLes, BCGre, BCLeq, BCGeq,
+                       BCJmp 0, BCJmpT 0, BCJmpF 0]