Skip to content

Commit

Permalink
ENH Early check for in-block assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
luispedro committed Jul 24, 2020
1 parent a52c52f commit c8357b3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 1.2.0+
* Add early check that block assignments are always to block variables

Version 1.2.0 2020-07-12 by luispedro
* Add load_fastq_directory to builtin functions
* Better messages when using `lock1`
Expand Down
19 changes: 18 additions & 1 deletion NGLess/Validation.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{- Copyright 2013-2018 NGLess Authors
{- Copyright 2013-2020 NGLess Authors
- License: MIT
-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -51,6 +51,7 @@ validate mods expr = case errors of
,validateNGLessVersionUses
,validatePureFunctions
,validateWriteOName
,validateBlockAssignments
]

{- Each checking function has the type
Expand Down Expand Up @@ -341,3 +342,19 @@ validateNGLessVersionUses mods sc = case nglVersion <$> nglHeader sc of
guard $ not (T.null rest)
(minV, _) <- rightToMaybe $ T.decimal (T.tail rest)
return (majV, minV)


-- Check that only block variables are assigned inside a block
validateBlockAssignments :: [Module] -> Script -> Writer [T.Text] ()
validateBlockAssignments _ (Script _ es) = forM_ es validateBlockAssignments1
validateBlockAssignments1 :: (Int, Expression) -> Writer [T.Text] ()
validateBlockAssignments1 (lno, e) = case e of
Assignment _ e' -> validateBlockAssignments1 (lno, e')
FunctionCall (FuncName fname) _ _ (Just block) -> let [var] = blockVariable block
in recursiveAnalyse (checkAssignmentOnlyTo fname lno var) (blockBody block)
_ -> return ()
checkAssignmentOnlyTo fname lno v@(Variable n) e = case e of
Assignment v' _
| v /= v' -> tell1lno lno ["Inside blocks, only the block variable (in this case `", n, "`) can be assigned to",
" (when analysing function `", fname, "`)."]
_ -> return ()
9 changes: 9 additions & 0 deletions tests/error-block-assignment/reassign-in-block.ngl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ngless '1.2'
input = fastq('sample.fq')
trim = 3
input = preprocess(input) using |read|:
read = read[trim:]
trim = 5
if len(read) < 10:
discard
write(input, ofile='output.fq')
12 changes: 12 additions & 0 deletions tests/error-block-assignment/sample.fq
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@IRIS:7:1:17:394#0/1
GTCAGGACAAGAAAGACAANTCCAATTNACATT
+IRIS:7:1:17:394#0/1
aaabaa`]baaaaa_aab]D^^`b`aYDW]aba
@IRIS:7:1:17:800#0/1
GGAAACACTACTTAGGCTTATAAGATCNGGTTGCGG
+IRIS:7:1:17:800#0/1
ababbaaabaaaaa`]`ba`]`aaaaYD\\_a``XT
@IRIS:7:1:17:1479#0/1
TGAAAGAT
+IRIS:7:1:17:1479#0/1
__a_X]``

0 comments on commit c8357b3

Please sign in to comment.