1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-14 17:32:00 +03:00
juvix/test/Compilation/Base.hs
Łukasz Czajka 272b93e595
Constant folding (#2450)
* Closes #2154 
* Evaluates closed applications with value arguments when the result
type is zero-order. For example, `3 + 4` is evaluated to `7`, and `id 3`
is evaluated to `3`, but `id id` is not evaluated because the target
type is not zero-order (it's a function type).
2023-10-20 12:03:56 +02:00

47 lines
1.6 KiB
Haskell

module Compilation.Base where
import Base
import Core.Compile.Base
import Core.Eval.Base
import Juvix.Compiler.Core qualified as Core
import Juvix.Data.PPOutput
data CompileAssertionMode
= EvalOnly
| -- | Specify text to be sent to stdin of the process under test
CompileOnly Text
| EvalAndCompile
compileAssertion ::
Int ->
CompileAssertionMode ->
Path Abs File ->
Path Abs File ->
(String -> IO ()) ->
Assertion
compileAssertion optLevel mode mainFile expectedFile step = do
step "Translate to JuvixCore"
entryPoint <- defaultEntryPointCwdIO mainFile
tab <- (^. Core.coreResultTable) . snd <$> runIO' entryPoint upToCore
case run $ runReader Core.defaultCoreOptions $ runError $ Core.toEval' tab of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Right tab' -> do
let evalAssertion = coreEvalAssertion' EvalModePlain tab' mainFile expectedFile step
compileAssertion' stdinText = coreCompileAssertion' optLevel tab' mainFile expectedFile stdinText step
case mode of
EvalOnly -> evalAssertion
CompileOnly stdinText -> compileAssertion' stdinText
EvalAndCompile -> evalAssertion >> compileAssertion' ""
compileErrorAssertion ::
Path Abs File ->
(String -> IO ()) ->
Assertion
compileErrorAssertion mainFile step = do
step "Translate to JuvixCore"
entryPoint <- defaultEntryPointCwdIO mainFile
tab <- (^. Core.coreResultTable) . snd <$> runIO' entryPoint upToCore
case run $ runReader Core.defaultCoreOptions $ runError @JuvixError $ Core.toStripped' tab of
Left _ -> assertBool "" True
Right _ -> assertFailure "no error"