arith fixes
This commit is contained in:
12
src/GM.hs
12
src/GM.hs
@@ -701,12 +701,12 @@ buildInitialHeap (Program ss) = mapAccumL allocateSc mempty compiledScs
|
||||
compileBinder (_ := v, a) = compileC g' v <> [Update a]
|
||||
|
||||
-- special cases for prim functions; essentially inlining
|
||||
compileE g ("negate#" :$ a) = compileE g a <> [Neg]
|
||||
compileE g ("+#" :$ a :$ b) = compileE g a <> compileE g b <> [Add]
|
||||
compileE g ("-#" :$ a :$ b) = compileE g a <> compileE g b <> [Sub]
|
||||
compileE g ("*#" :$ a :$ b) = compileE g a <> compileE g b <> [Mul]
|
||||
compileE g ("/#" :$ a :$ b) = compileE g a <> compileE g b <> [Div]
|
||||
compileE g ("==#" :$ a :$ b) = compileE g a <> compileE g b <> [Equals]
|
||||
compileE g ("negate#" :$ a) = compileE g a <> [Neg]
|
||||
compileE g ("+#" :$ a :$ b) = compileE g b <> compileE g a <> [Add]
|
||||
compileE g ("-#" :$ a :$ b) = compileE g b <> compileE g a <> [Sub]
|
||||
compileE g ("*#" :$ a :$ b) = compileE g b <> compileE g a <> [Mul]
|
||||
compileE g ("/#" :$ a :$ b) = compileE g b <> compileE g a <> [Div]
|
||||
compileE g ("==#" :$ a :$ b) = compileE g b <> compileE g a <> [Equals]
|
||||
|
||||
compileE g (Case e as) = compileE g e <> [CaseJump (compileD g as)]
|
||||
|
||||
|
||||
19
tst/Arith.hs
19
tst/Arith.hs
@@ -35,7 +35,7 @@ instance Arbitrary ArithExpr where
|
||||
gen :: Int -> Gen ArithExpr
|
||||
gen n
|
||||
| n > 0 = oneof
|
||||
-- i don't feel like dealing with zero at the moment
|
||||
-- i don't feel like dealing with division at the moment
|
||||
[ IntA <$> int
|
||||
, NegateA <$> arbitrary
|
||||
-- , IdA <$> arbitrary
|
||||
@@ -47,7 +47,8 @@ instance Arbitrary ArithExpr where
|
||||
where
|
||||
b f = liftA2 f s s
|
||||
s = gen (n `div` 2)
|
||||
int = chooseInt (minBound,maxBound)
|
||||
-- int = chooseInt (minBound,maxBound)
|
||||
int = chooseInt (-500,500)
|
||||
|
||||
prop_ArithExprEqCoreExpr :: ArithExpr -> Bool
|
||||
prop_ArithExprEqCoreExpr e = arithResult `eq1` coreResult
|
||||
@@ -55,23 +56,23 @@ prop_ArithExprEqCoreExpr e = arithResult `eq1` coreResult
|
||||
arithResult = Just (evalA e)
|
||||
coreResult = evalCore (toCore e)
|
||||
|
||||
toCore :: ArithExpr -> Program
|
||||
toCore :: ArithExpr -> Program'
|
||||
toCore expr = Program
|
||||
[ ScDef "id" ["x"] $ Var "x"
|
||||
, ScDef "main" [] $ go expr
|
||||
]
|
||||
where
|
||||
go :: ArithExpr -> Expr
|
||||
go (IntA n) = IntE n
|
||||
go :: ArithExpr -> Expr'
|
||||
go (IntA n) = LitE (IntL n)
|
||||
go (NegateA e) = "negate#" :$ go e
|
||||
go (IdA e) = "id" :$ go e
|
||||
go (a :+ b) = f "+#" a b
|
||||
go (a :- b) = f "-#" a b
|
||||
go (a :* b) = f "*#" a b
|
||||
go (a :+ b) = f "+#" a b
|
||||
go (a :- b) = f "-#" a b
|
||||
go (a :* b) = f "*#" a b
|
||||
|
||||
f n a b = n :$ go a :$ go b
|
||||
|
||||
evalCore :: Program -> Maybe Int
|
||||
evalCore :: Program' -> Maybe Int
|
||||
evalCore p = do
|
||||
a <- fst <$> evalProg p
|
||||
case a of
|
||||
|
||||
Reference in New Issue
Block a user