~ft/zabava

1c9251220ace1946f62a90c8cda930108ac3054d — Sigrid Solveig Haflínudóttir 2 years ago
```wip
```
```2 files changed, 62 insertions(+), 2 deletions(-)

M source/Uxn/Op.hs
M source/Uxn/Stmt.hs
```
`M source/Uxn/Op.hs => source/Uxn/Op.hs +1 -1`
```@@ 20,7 20,7 @@ data Op = Op { opCode  ∷ OpCode
, opRet   ∷ Bool
, opKeep  ∷ Bool
}
-          deriving (Eq)
+             deriving (Eq)

data OpCode =
Ibrk |                                                  -- stop

```
`M source/Uxn/Stmt.hs => source/Uxn/Stmt.hs +61 -1`
```@@ 1,11 1,34 @@
module Uxn.Stmt
-( Stmt
+( StackStat(..)
+, StatStack(..)
+, Stmt(..)
, opToStmt
) where

import Uxn.Op
import Prelude.Unicode

+data StackStat = StackStat { ws ∷ Int
+                           , rs ∷ Int
+                           }
+                           deriving (Show)
+
+zs = StackStat { ws = 0, rs = 0 }
+
+instance Num StackStat where
+    (+) a b = StackStat { ws = ws a + ws b, rs = rs a + rs b }
+    (-) = undefined
+    (*) = undefined
+    negate = undefined
+    abs = undefined
+    signum = undefined
+    fromInteger = undefined
+
+class StatStack a where
+    bytesPushed ∷ a → StackStat
+    bytesPopped ∷ a → StackStat
+    bytesPeeped ∷ a → StackStat
+
data V = A | B | C
deriving (Eq, Show)
data S = Ws | Rs

@@ 42,6 65,43 @@ data Stmt = Push W S [Expr]
| Break
deriving (Eq, Show)

+instance StatStack W where
+    bytesPushed W8  = zs { ws = 1, rs = 1 }
+    bytesPushed W16 = zs { ws = 2, rs = 2 }
+    bytesPopped = bytesPushed
+    bytesPeeped = bytesPushed
+
+instance StatStack Ref where
+    bytesPushed (Memory _ e) = bytesPushed e
+    bytesPushed (Device _ e) = bytesPushed e
+    bytesPopped (Memory _ e) = bytesPopped e
+    bytesPopped (Device _ e) = bytesPopped e
+    bytesPeeped (Memory _ e) = bytesPeeped e
+    bytesPeeped (Device _ e) = bytesPeeped e
+
+instance StatStack Expr where
+    bytesPushed _ = 0
+    bytesPopped e = case e of
+        Signed e   → bytesPopped e
+        Pop W8 Ws  → zs { ws = 1 }
+        Pop W16 Ws → zs { ws = 2 }
+        Pop W8 Rs  → zs { rs = 1 }
+        Pop W16 Rs → zs { rs = 2 }
+        Ar _ a b   → bytesPopped a + bytesPopped b
+        Succ e     → bytesPopped e
+        Ref r      → bytesPopped r
+        _          → zs
+    bytesPeeped e = case e of
+        Signed e   → bytesPeeped e
+        Peep W8 Ws  → zs { ws = 1 }
+        Peep W16 Ws → zs { ws = 2 }
+        Peep W8 Rs  → zs { rs = 1 }
+        Peep W16 Rs → zs { rs = 2 }
+        Ar _ a b    → bytesPeeped a + bytesPeeped b
+        Succ e      → bytesPeeped e
+        Ref r       → bytesPeeped r
+        _           → zs
+
opToStmt ∷ Op → [Stmt]
opToStmt o =
case opCode o of

```