1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-18 20:31:51 +03:00
juvix/test/Nockma/Parse/Positive.hs
Jan Mas Rovira 39d176e643
Fast nockma eval (#2580)
Adds annotations to cells to indicate that it is a call to the stdlib
and might be evaluated faster in the Haskell evaluator.

The syntax for stdlib calls is as follows:
```
[stdlib@add args@<args-term> <left-term> <right-term>]
```
where `add` is the name of the function being called, `<args-term>` is a
nockma term that points to the position of the arguments, and
`<left-term>` and `<right-term>` are the actual components of the cell.
2024-01-19 12:01:58 +01:00

58 lines
1.8 KiB
Haskell

module Nockma.Parse.Positive where
import Base
import Data.ByteString qualified as BS
import Juvix.Compiler.Nockma.Language hiding (Path)
import Juvix.Compiler.Nockma.Pretty (ppPrint)
import Juvix.Compiler.Nockma.Translation.FromSource (parseText)
import Juvix.Parser.Error
import Juvix.Prelude.Pretty
import Text.Megaparsec
data PosTest = PosTest
{ _name :: String,
_relDir :: Path Rel Dir,
_file :: Path Rel File
}
makeLenses ''PosTest
root :: Path Abs Dir
root = relToProject $(mkRelDir "tests/nockma/positive")
testDescr :: PosTest -> TestDescr
testDescr PosTest {..} =
let tRoot = root <//> _relDir
file' = tRoot <//> _file
in TestDescr
{ _testName = _name,
_testRoot = tRoot,
_testAssertion = Steps $ \step -> do
step "Parsing"
txt <- decodeUtf8 <$> BS.readFile (toFilePath file')
nockmaTerm <- assertParse txt
step "Pretty printing"
let ppTerm = ppPrint nockmaTerm
step "parse . pretty . parse == parse"
prettyNockmaTerm <- assertParse ppTerm
assertEqual "expected equal" nockmaTerm prettyNockmaTerm
}
assertParse :: Text -> IO (Term Natural)
assertParse txt = case parseText txt of
Left (MegaparsecError b) -> assertFailure ("Nockma parsing failed " <> unpack (prettyText (errorBundlePretty b)))
Right t -> return t
allTests :: TestTree
allTests = testGroup "Nockma parse positive" (map (mkTest . testDescr) tests)
tests :: [PosTest]
tests =
[ PosTest "Identity" $(mkRelDir ".") $(mkRelFile "Identity.nock"),
PosTest "Identity Pretty" $(mkRelDir ".") $(mkRelFile "IdentityPretty.pnock"),
PosTest "StdlibCall" $(mkRelDir ".") $(mkRelFile "StdlibCall.pnock"),
PosTest "Stdlib" $(mkRelDir ".") $(mkRelFile "Stdlib.nock")
]