2023-01-12 13:22:32 +03:00
|
|
|
module Compilation.Base where
|
|
|
|
|
|
|
|
import Base
|
2023-02-15 13:30:12 +03:00
|
|
|
import Core.Eval.Base
|
2023-01-12 13:22:32 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2023-02-15 13:30:12 +03:00
|
|
|
import Data.Text.IO qualified as TIO
|
2023-01-12 13:22:32 +03:00
|
|
|
import Juvix.Compiler.Builtins (iniState)
|
|
|
|
import Juvix.Compiler.Core.Data.InfoTable qualified as Core
|
2023-02-15 13:30:12 +03:00
|
|
|
import Juvix.Compiler.Core.Extra
|
|
|
|
import Juvix.Compiler.Core.Info qualified as Info
|
|
|
|
import Juvix.Compiler.Core.Info.NoDisplayInfo
|
|
|
|
import Juvix.Compiler.Core.Pipeline qualified as Core
|
|
|
|
import Juvix.Compiler.Core.Pretty
|
2023-01-12 13:22:32 +03:00
|
|
|
import Juvix.Compiler.Core.Translation.FromInternal.Data qualified as Core
|
|
|
|
import Juvix.Compiler.Pipeline
|
|
|
|
|
|
|
|
compileAssertion ::
|
|
|
|
Path Abs File ->
|
|
|
|
Path Abs File ->
|
|
|
|
(String -> IO ()) ->
|
|
|
|
Assertion
|
2023-02-15 13:30:12 +03:00
|
|
|
compileAssertion mainFile expectedFile step = do
|
2023-01-12 13:22:32 +03:00
|
|
|
step "Translate to JuvixCore"
|
|
|
|
cwd <- getCurrentDir
|
|
|
|
let entryPoint = defaultEntryPoint cwd mainFile
|
2023-02-15 13:30:12 +03:00
|
|
|
tab' <- (^. Core.coreResultTable) . snd <$> runIO' iniState entryPoint upToCore
|
|
|
|
let tab = Core.toEval tab'
|
2023-01-12 13:22:32 +03:00
|
|
|
case (tab ^. Core.infoMain) >>= ((tab ^. Core.identContext) HashMap.!?) of
|
2023-02-15 13:30:12 +03:00
|
|
|
Just node -> do
|
|
|
|
withTempDir'
|
|
|
|
( \dirPath -> do
|
|
|
|
let outputFile = dirPath <//> $(mkRelFile "out.out")
|
|
|
|
hout <- openFile (toFilePath outputFile) WriteMode
|
|
|
|
step "Evaluate"
|
|
|
|
r' <- doEval mainFile hout tab node
|
|
|
|
case r' of
|
|
|
|
Left err -> do
|
|
|
|
hClose hout
|
|
|
|
assertFailure (show (pretty err))
|
|
|
|
Right value -> do
|
|
|
|
unless
|
|
|
|
(Info.member kNoDisplayInfo (getInfo value))
|
|
|
|
(hPutStrLn hout (ppPrint value))
|
|
|
|
hClose hout
|
|
|
|
actualOutput <- TIO.readFile (toFilePath outputFile)
|
|
|
|
step "Compare expected and actual program output"
|
|
|
|
expected <- TIO.readFile (toFilePath expectedFile)
|
|
|
|
assertEqDiffText ("Check: EVAL output = " <> toFilePath expectedFile) actualOutput expected
|
|
|
|
)
|
2023-01-12 13:22:32 +03:00
|
|
|
Nothing -> assertFailure ("No main function registered in: " <> toFilePath mainFile)
|