Skip to content

Commit

Permalink
Add a Tree transform to check that anoma-get is not used
Browse files Browse the repository at this point in the history
Apply this transformation to the Tree->Asm translation.
  • Loading branch information
paulcadman committed Mar 20, 2024
1 parent 9cd176f commit 40be37d
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Juvix/Compiler/Tree/Data/TransformationId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data TransformationId
| TempHeight
| FilterUnreachable
| Validate
| CheckNoAnoma
deriving stock (Data, Bounded, Enum, Show)

data PipelineId
Expand All @@ -26,7 +27,7 @@ toNockmaTransformations :: [TransformationId]
toNockmaTransformations = [Validate, Apply, FilterUnreachable, TempHeight]

toAsmTransformations :: [TransformationId]
toAsmTransformations = [Validate]
toAsmTransformations = [Validate, CheckNoAnoma]

toCairoAsmTransformations :: [TransformationId]
toCairoAsmTransformations = [Validate, Apply, FilterUnreachable]
Expand All @@ -41,6 +42,7 @@ instance TransformationId' TransformationId where
TempHeight -> strTempHeight
FilterUnreachable -> strFilterUnreachable
Validate -> strValidate
CheckNoAnoma -> strCheckNoAnoma

instance PipelineId' TransformationId PipelineId where
pipelineText :: PipelineId -> Text
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Tree/Data/TransformationId/Strings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ strFilterUnreachable = "filter-unreachable"

strValidate :: Text
strValidate = "validate"

strCheckNoAnoma :: Text
strCheckNoAnoma = "check-no-anoma"
4 changes: 3 additions & 1 deletion src/Juvix/Compiler/Tree/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Juvix.Compiler.Tree.Keywords.Base
import Juvix.Data.Keyword.All
( kwAdd_,
kwAlloc,
kwAnomaGet,
kwArgsNum,
kwAtoi,
kwBr,
Expand Down Expand Up @@ -68,5 +69,6 @@ allKeywords =
kwCCall,
kwBr,
kwCase,
kwSave
kwSave,
kwAnomaGet
]
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Tree/Transformation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Juvix.Compiler.Tree.Data.TransformationId
import Juvix.Compiler.Tree.Error
import Juvix.Compiler.Tree.Transformation.Apply
import Juvix.Compiler.Tree.Transformation.Base
import Juvix.Compiler.Tree.Transformation.CheckNoAnoma
import Juvix.Compiler.Tree.Transformation.FilterUnreachable
import Juvix.Compiler.Tree.Transformation.Identity
import Juvix.Compiler.Tree.Transformation.TempHeight
Expand All @@ -27,3 +28,4 @@ applyTransformations ts tbl = foldM (flip appTrans) tbl ts
TempHeight -> return . computeTempHeight
FilterUnreachable -> return . filterUnreachable
Validate -> mapError (JuvixError @TreeError) . validate
CheckNoAnoma -> \tbl' -> mapError (JuvixError @TreeError) (checkNoAnoma tbl') $> tbl'
23 changes: 23 additions & 0 deletions src/Juvix/Compiler/Tree/Transformation/CheckNoAnoma.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Juvix.Compiler.Tree.Transformation.CheckNoAnoma where

import Juvix.Compiler.Tree.Data.InfoTable
import Juvix.Compiler.Tree.Error
import Juvix.Compiler.Tree.Extra.Recursors
import Juvix.Compiler.Tree.Transformation.Base

checkNoAnoma :: forall r. (Member (Error TreeError) r) => InfoTable -> Sem r ()
checkNoAnoma = walkT checkNode
where
checkNode :: Symbol -> Node -> Sem r ()
checkNode _ = \case
Unop NodeUnop {..} -> case _nodeUnopOpcode of
OpAnomaGet ->
throw
TreeError
{ _treeErrorMsg = "OpAnomaGet is unsupported",
_treeErrorLoc = _nodeUnopInfo ^. nodeInfoLocation
}
OpFail -> return ()
OpTrace -> return ()
PrimUnop {} -> return ()
_ -> return ()
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Tree/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ parseUnop =
<|> parseUnaryOp kwTrace OpTrace
<|> parseUnaryOp kwFail OpFail
<|> parseUnaryOp kwArgsNum (PrimUnop OpArgsNum)
<|> parseUnaryOp kwAnomaGet (OpAnomaGet)

parseUnaryOp ::
(Members '[Reader ParserSig, InfoTableBuilder, State LocalParams] r) =>
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Data/Keyword/All.hs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ kwRet = asciiKw Str.ret
kwLive :: Keyword
kwLive = asciiKw Str.live

kwAnomaGet :: Keyword
kwAnomaGet = asciiKw Str.anomaGet

delimBraceL :: Keyword
delimBraceL = mkDelim Str.braceL

Expand Down
5 changes: 4 additions & 1 deletion test/Compilation/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ compileErrorAssertion root' mainFile step = do
step "Translate to JuvixCore"
entryPoint <- testDefaultEntryPointIO root' mainFile
PipelineResult {..} <- snd <$> testRunIO entryPoint upToCore
case run $ runReader Core.defaultCoreOptions $ runError @JuvixError $ Core.toStored' (_pipelineResult ^. Core.coreResultModule) >>= Core.toStripped' Core.CheckExec of
case run
. runReader Core.defaultCoreOptions
. runError @JuvixError
$ Core.toStored' (_pipelineResult ^. Core.coreResultModule) >>= Core.toStripped' Core.CheckExec of
Left _ -> assertBool "" True
Right _ -> assertFailure "no error"
4 changes: 3 additions & 1 deletion test/Tree/Transformation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Tree.Transformation where

import Base
import Tree.Transformation.Apply qualified as Apply
import Tree.Transformation.CheckNoAnoma qualified as CheckNoAnoma
import Tree.Transformation.Identity qualified as Identity
import Tree.Transformation.Reachability qualified as Reachability

Expand All @@ -11,5 +12,6 @@ allTests =
"JuvixTree transformations"
[ Identity.allTests,
Apply.allTests,
Reachability.allTests
Reachability.allTests,
CheckNoAnoma.allTests
]
67 changes: 67 additions & 0 deletions test/Tree/Transformation/CheckNoAnoma.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Tree.Transformation.CheckNoAnoma where

import Base
import Juvix.Compiler.Tree.Error
import Juvix.Compiler.Tree.Transformation as Tree
import Juvix.Compiler.Tree.Translation.FromSource
import Juvix.Data.PPOutput
import Tree.Eval.Negative qualified as Eval

data CheckNoAnomaTest = CheckNoAnomaTest
{ _testEval :: Eval.NegTest
}

fromTest :: CheckNoAnomaTest -> TestTree
fromTest = mkTest . toTestDescr

root :: Path Abs Dir
root = relToProject $(mkRelDir "tests/Tree/negative/")

treeEvalTransformationErrorAssertion ::
Path Abs File ->
[TransformationId] ->
(JuvixError -> IO ()) ->
(String -> IO ()) ->
Assertion
treeEvalTransformationErrorAssertion mainFile trans checkError step = do
step "Parse"
s <- readFile mainFile
case runParser mainFile s of
Left err -> assertFailure (show (pretty err))
Right tab0 -> do
step "Validate"
case run $ runError @JuvixError $ applyTransformations [Validate] tab0 of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Right tab1 -> do
unless (null trans) $
step "Transform"
case run $ runError @JuvixError $ applyTransformations trans tab1 of
Left e -> checkError e
Right {} -> assertFailure "Expected error"

toTestDescr :: CheckNoAnomaTest -> TestDescr
toTestDescr CheckNoAnomaTest {..} =
let Eval.NegTest {..} = _testEval
tRoot = root <//> _relDir
file' = tRoot <//> _file
checkError :: JuvixError -> IO ()
checkError e =
unless
(isJust (fromJuvixError @TreeError e))
(assertFailure (unpack ("Expected TreeError. got: " <> renderTextDefault e)))
in TestDescr
{ _testName = _name,
_testRoot = tRoot,
_testAssertion = Steps $ treeEvalTransformationErrorAssertion file' [CheckNoAnoma] checkError
}

allTests :: TestTree
allTests = testGroup "CheckNoAnoma" (map (fromTest . CheckNoAnomaTest) tests)

tests :: [Eval.NegTest]
tests =
[ Eval.NegTest
"anomaGet"
$(mkRelDir ".")
$(mkRelFile "test009.jvt")
]
5 changes: 5 additions & 0 deletions tests/Tree/negative/test009.jvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- calling unsupported anoma-get

function main() : * {
anoma-get(1)
}

0 comments on commit 40be37d

Please sign in to comment.