Support arithmetic sequences.

`from`      : [a .. ]
`fromThen`  : [a, b .. ]
`fromTo`    : [a .. b]
`fromThenTo`: [a, b .. c]

These are needed before supporting list comprehensions (#64).
This commit is contained in:
Jinwoo Lee 2020-05-28 10:45:45 -07:00
parent 3aa49fff6f
commit 6bc0965232
3 changed files with 48 additions and 0 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
dist/
# Generated automatically by hpack:
*.cabal
*.yaml.lock

View File

@ -21,6 +21,10 @@ module GHC.SourceGen.Expr
, tyApp
, recordConE
, recordUpd
, from
, fromThen
, fromTo
, fromThenTo
) where
import GHC.Hs.Expr
@ -187,3 +191,40 @@ recordUpd e fs =
}
withPlaceHolder4 = withPlaceHolder . withPlaceHolder . withPlaceHolder
. withPlaceHolder
-- | An arithmetic sequence expression with a start value.
--
-- > [a ..]
-- > =====
-- > from (var "a")
from :: HsExpr' -> HsExpr'
from from' =
noExt ArithSeq Nothing $ From (builtLoc from')
-- | An arithmetic sequence expression with a start and a step values.
--
-- > [a, b ..]
-- > =====
-- > fromThen (var "a") (var "b")
fromThen :: HsExpr' -> HsExpr' -> HsExpr'
fromThen from' then' =
noExt ArithSeq Nothing $ FromThen (builtLoc from') (builtLoc then')
-- | An arithmetic sequence expression with a start and an end values.
--
-- > [a .. b]
-- > =====
-- > fromTo (var "a") (var "b")
fromTo :: HsExpr' -> HsExpr' -> HsExpr'
fromTo from' to =
noExt ArithSeq Nothing $ FromTo (builtLoc from') (builtLoc to)
-- | An arithmetic sequence expression with a start, a step, and an end values.
--
-- > [a, b .. c]
-- > =====
-- > fromThenTo (var "a") (var "b") (var "c")
fromThenTo :: HsExpr' -> HsExpr' -> HsExpr' -> HsExpr'
fromThenTo from' then' to =
noExt ArithSeq Nothing $
FromThenTo (builtLoc from') (builtLoc then') (builtLoc to)

View File

@ -221,6 +221,12 @@ exprsTest dflags = testGroup "Expr"
-- TODO: add more tests.
[ "do (let x = 1 in x)" :~ do' [stmt $ let' [valBind "x" (int 1)] (var "x")]
]
, test "arithSeq"
[ "[a .. ]" :~ from (var "a")
, "[a, b .. ]" :~ fromThen (var "a") (var "b")
, "[a .. b]" :~ fromTo (var "a") (var "b")
, "[a, b .. c]" :~ fromThenTo (var "a") (var "b") (var "c")
]
]
where
test = testExprs dflags