From bc1a62120c68e1ccd36d37058cd8d476a5f21d81 Mon Sep 17 00:00:00 2001 From: crumbtoo Date: Mon, 13 Nov 2023 21:55:18 -0700 Subject: [PATCH] other arith --- src/Core.hs | 3 +++ src/TI.hs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Core.hs b/src/Core.hs index e77bedf..067d7d9 100644 --- a/src/Core.hs +++ b/src/Core.hs @@ -19,6 +19,9 @@ data Expr = Var Name data Prim = IntP Int | IntAddP + | IntSubP + | IntMulP + | IntDivP | IntNegP deriving (Show, Eq) diff --git a/src/TI.hs b/src/TI.hs index 82432d2..fd960a3 100644 --- a/src/TI.hs +++ b/src/TI.hs @@ -78,6 +78,9 @@ primitives :: [(Name, Prim)] primitives = [ ("negate#", IntNegP) , ("+#", IntAddP) + , ("-#", IntSubP) + , ("*#", IntMulP) + , ("/#", IntDivP) ] instantiate :: Expr -> TiHeap -> [(Name, Addr)] -> (TiHeap, Addr) @@ -230,6 +233,9 @@ step st = arg = hLookupUnsafe argAddr h primStep _ IntAddP st = primBinOp (+) st + primStep _ IntSubP st = primBinOp (-) st + primStep _ IntMulP st = primBinOp (*) st + primStep _ IntDivP st = primBinOp (div) st primBinOp :: (Int -> Int -> Int) -> TiState -> TiState primBinOp f (TiState s d h g sts) = @@ -356,6 +362,12 @@ negExample3 = Program "twice" :$ Prim IntNegP :$ Prim (IntP 3) ] +arithExample1 :: Program +arithExample1 = Program + [ ScDef "main" [] $ + "+#" :$ (Prim $ IntP 3) :$ ("negate#" :$ (Prim $ IntP 2)) + ] + ---------------------------------------------------------------------------------- instance Pretty TiState where