Skip to content

Commit

Permalink
Integer division operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomatosoup97 committed Jan 7, 2020
1 parent 8ee5132 commit a01cb54
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ data Value
data BinaryOp
= BAdd
| BMul
| BDiv
| BSub
| BLte
| BLt
Expand Down
1 change: 1 addition & 0 deletions src/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ boolToInt b = if b then 1 else 0
convOp :: BinaryOp -> (Integer, Integer) -> Integer
convOp BAdd = uncurry (+)
convOp BMul = uncurry (*)
convOp BDiv = uncurry div
convOp BSub = uncurry (-)
convOp BLte = boolToInt . uncurry (<=)
convOp BLt = boolToInt . uncurry (<)
Expand Down
5 changes: 4 additions & 1 deletion src/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ binOps = [ [
Infix (reservedOp "==" >> return (VBinOp BEq )) AssocLeft,
Infix (reservedOp "!=" >> return (VBinOp BNe )) AssocLeft
]
, [Infix (reservedOp "*" >> return (VBinOp BMul )) AssocLeft]
, [
Infix (reservedOp "*" >> return (VBinOp BMul )) AssocLeft,
Infix (reservedOp "/" >> return (VBinOp BDiv )) AssocLeft
]
, [
Infix (reservedOp "+" >> return (VBinOp BAdd )) AssocLeft,
Infix (reservedOp "-" >> return (VBinOp BSub )) AssocLeft
Expand Down
12 changes: 9 additions & 3 deletions src/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ main = hspec $ do
evalProgram "return 2 + 3" `shouldBe` (Right (DNum 5))
evalProgram "return 2 * 3" `shouldBe` (Right (DNum 6))
evalProgram "return 5 - 2" `shouldBe` (Right (DNum 3))
evalProgram "return 5 / 2" `shouldBe` (Right (DNum 2))
evalProgram "return 6 / 2" `shouldBe` (Right (DNum 3))

it "Order of operations" $ do
evalProgram "return 2 + 3 * 2" `shouldBe` (Right (DNum 8))
Expand Down Expand Up @@ -117,12 +119,12 @@ main = hspec $ do
it "Identity effect handler" $ do
evalProgram "handle do Id 42 with {Id p r -> r p | return x -> return x }" `shouldBe` (Right (DNum 42))

it "Increment effect handler" $ do
evalProgram "handle do Inc 17 with {Id p r -> return p | Inc p r -> return p + 1 | return x -> return 1 }" `shouldBe` (Right (DNum 18))

it "Compose let with do" $ do
evalProgram "handle let x <- do Id 1 in return 3 with {Id p r -> r p | return x -> return x }" `shouldBe` (Right (DNum 3))

it "Increment effect handler" $ do
testFromFile "programs/increment.fk" (Right (DNum 18))

it "Simulate exceptions" $ do
testFromFile "programs/exceptions.fk" (Right (DNum 42))

Expand All @@ -144,6 +146,10 @@ main = hspec $ do
it "Max of possible choices" $ do
testFromFile "programs/choicesMin.fk" (Right (DNum 5))

it "List of possible choices" $ do
let result = DPair (DPair (DNum 10) (DNum 5)) (DPair (DNum 20) (DNum 15))
testFromFile "programs/choicesList.fk" (Right result)

-- it "State monad" $ do
-- testFromFile "programs/state.fk" (Right (DNum 1))

Expand Down
13 changes: 13 additions & 0 deletions src/programs/choicesList.fk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
handle
let c1 <- do Choice () in
let c2 <- do Choice () in
let x <- if c1 then return 10 else return 20 in
let y <- if c2 then return 0 else return 5 in
return x - y
with {
Choice p r ->
let t <- r 1 in
let f <- r 0 in
return (t, f) |
return x -> return x
}
7 changes: 7 additions & 0 deletions src/programs/increment.fk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
handle
do Inc 17
with {
Id p r -> return p |
Inc p r -> return p + 1 |
return x -> return x
}

0 comments on commit a01cb54

Please sign in to comment.