Skip to content

Commit

Permalink
Fix incorrect if/elseif/else code generation
Browse files Browse the repository at this point in the history
Fix the incorrect tests that passed erroneously
Prevent a missing else from generating an empty else statement in code
Move the offset pointing to within else to the correct position
  • Loading branch information
adituv committed Dec 18, 2016
1 parent f93e5b2 commit 39f3efd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Compiler/QbScript/CodeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ putElseIfs = mapM_ putElseIf
modify $ updateHoles nh' lh'

putElse :: [Instruction] -> StateT BranchState Packing ()
putElse [] = pure ()
putElse body = do
BranchState nh lastHoles <- get
h' <- lift $ do
putWord8 0x01
fillOffsetHole nh
putWord8 0x48
nextHole <- putOffsetHole
fillOffsetHole nh
mapM_ putInstr body
return nextHole

Expand Down
12 changes: 11 additions & 1 deletion test/Compiler/QbScript/CodeGen/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ instrTests =
it "generates an assignment correctly" $
testPacking (putInstr (Assign (NonLocal . QbName $ "x") (ELit . SmallLit . LitN $ 2)))
`shouldBe` [0x01, 0x16, 0x7C, 0xE9, 0x23, 0x73, 0x07, 0x17, 0x02, 0x00, 0x00, 0x00]
it "generates an if with no else branches correctly" $
testPacking (putInstr (IfElse (ELit LitPassthrough, [BareExpr $ ELit LitPassthrough])
[] []))
`shouldBe` [ 0x01, 0x47, 0x07, 0x00, 0x2C, 0x01, 0x2C, 0x01, 0x28 ]
it "generates an if/else correctly" $
testPacking (putInstr (IfElse (ELit LitPassthrough, [BareExpr $ ELit LitPassthrough])
[]
[BareExpr $ ELit LitPassthrough]))
`shouldBe` [ 0x01, 0x47, 0x09, 0x00, 0x2C, 0x01, 0x2C
, 0x01, 0x48, 0x06, 0x00, 0x01, 0x2C, 0x01, 0x28]
it "generates an if/elseif/else correctly" $
testPacking (putInstr (IfElse (ELit LitPassthrough, [BareExpr $ ELit LitPassthrough])
[(ELit LitPassthrough, [BareExpr $ ELit LitPassthrough])]
[BareExpr $ ELit LitPassthrough]))
`shouldBe` [ 0x01, 0x47, 0x06, 0x00, 0x2C, 0x01, 0x2C
, 0x01, 0x27, 0x08, 0x00, 0x0D, 0x00, 0x2C, 0x01, 0x2C
, 0x01, 0x27, 0x0B, 0x00, 0x0D, 0x00, 0x2C, 0x01, 0x2C
, 0x01, 0x48, 0x06, 0x00, 0x01, 0x2C, 0x01, 0x28 ]
it "generates a repeat correctly" $
testPacking (putInstr (Repeat (ELit . SmallLit . LitN $ 4) [BareExpr $ ELit LitPassthrough]))
Expand Down

0 comments on commit 39f3efd

Please sign in to comment.