Skip to content

Commit

Permalink
ES HPT: fixed codegen, added as-pattern tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anabra committed Oct 10, 2019
1 parent 0295798 commit ed01def
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ codeGenM = cata folder where
AsPat varName Lit{} -> do
addReg varName r
pure () -- TODO: is this ok? or error?
AsPat v1 (Var v2) -> do
addReg v1 r
addReg v2 r
AsPat varName (ConstTagNode tag args) -> do
addReg varName r
irTag <- getTag tag
Expand All @@ -202,8 +205,8 @@ codeGenM = cata folder where
addReg name argReg
pure [IR.Project {srcSelector = IR.NodeItem irTag idx, srcReg = r, dstReg = argReg}]
-- QUESTION: In HPTProgram the instructions are in reverse order, here they are in regular order, isn't this inconsistent?
-- A: each cpat argument has zero or one instruction
-- the order of cpat binding evaluation does not matter because they does not depend on each other
-- ANSWER: Each cpat argument has zero or one instruction.
-- The order of cpat binding evaluation does not matter because they don't depend on each other.
emit IR.If
{ condition = IR.NodeTypeExists irTag
, srcReg = r
Expand Down
30 changes: 29 additions & 1 deletion grin/test/AbstractInterpretation/ExtendedSyntax/HptSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,35 @@ spec = do
}
result `shouldBe` exptected

it "handles as-patterns correctly" $ do
it "handles as-patterns with variables correctly" $ do
let code = withPrimPrelude [prog|
grinMain =
k0 <- pure 0
x0 <- pure (CInt k0)
x1@x2 <- pure x0

pure ()
|]
let result = inferTypeEnv code
exptected = emptyTypeEnv
{ TypeEnv._variable = Map.fromList
[ ("k0", int64_t)
-- , ("k1", int64_t)
-- , ("k2", int64_t)
-- , ("_v", unit_t)
, ("x0", T_NodeSet $ cnode_t "Int" [TypeEnv.T_Int64])
, ("x1", T_NodeSet $ cnode_t "Int" [TypeEnv.T_Int64])
, ("x2", T_NodeSet $ cnode_t "Int" [TypeEnv.T_Int64])
]
, TypeEnv._function = mconcat
[ fun_t "grinMain" [] unit_t
-- , fun_t "_prim_int_add" [int64_t, int64_t] int64_t
-- , fun_t "_prim_int_print" [int64_t] unit_t
]
}
result `shouldBe` exptected

it "handles as-patterns with nodes correctly" $ do
let code = withPrimPrelude [prog|
grinMain =
k0 <- pure 0
Expand Down

0 comments on commit ed01def

Please sign in to comment.