Simplified the Rain parser by reintroducing a RangeLiteral item that will get replaced by an early pass
This commit is contained in:
parent
bb6937433f
commit
41cca68e4e
|
@ -227,6 +227,9 @@ data LiteralRepr =
|
|||
| ByteLiteral Meta String
|
||||
-- | This can be for arrays and for lists
|
||||
| ArrayListLiteral Meta (Structured Expression)
|
||||
-- | This is transformed out very early on. The first item is the start of the
|
||||
-- range, the second is the end of the range (both inclusive)
|
||||
| RangeLiteral Meta Expression Expression
|
||||
| RecordLiteral Meta [Expression]
|
||||
deriving (Show, Eq, Typeable, Data)
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@ import qualified LexRain as L
|
|||
import Metadata
|
||||
import ParseUtils
|
||||
import Pass
|
||||
import Types
|
||||
import Utils
|
||||
|
||||
type RainState = CompState
|
||||
|
@ -266,20 +265,16 @@ range = try $ do m <- sLeftQ
|
|||
sDots
|
||||
end <- literal
|
||||
sRightQ
|
||||
(t, rep) <- case optTy of
|
||||
(t, be) <- case optTy of
|
||||
Just (t, mc) ->
|
||||
let begin' = A.Conversion mc A.DefaultConversion t begin
|
||||
end' = A.Conversion mc A.DefaultConversion t end
|
||||
in return (t, A.For m begin' (subOne $ addExprs begin' end') (makeConstant m 1))
|
||||
in return (A.List t, (begin', end'))
|
||||
Nothing -> do u <- getUniqueIdentifer
|
||||
let t = A.List $ A.UnknownVarType (A.TypeRequirements
|
||||
False) (Right (m,u))
|
||||
return (t, A.For m begin
|
||||
(subOne $ addExprs begin end) (makeConstant m 1))
|
||||
repSpec@(A.Specification _ repN _) <- defineNonce m "range_rep" (A.Rep m rep) A.ValAbbrev
|
||||
return $ A.Literal m t $ A.ArrayListLiteral m $
|
||||
A.Spec m repSpec $ A.Only m $
|
||||
A.ExprVariable m $ A.Variable m repN
|
||||
False) (Right (m,u))
|
||||
return (t, (begin, end))
|
||||
return $ A.Literal m t $ A.RangeLiteral m (fst be) (snd be)
|
||||
|
||||
expression :: RainParser A.Expression
|
||||
expression
|
||||
|
|
|
@ -82,7 +82,8 @@ intLiteralPattern :: Integer -> Pattern
|
|||
intLiteralPattern = pat . intLiteral
|
||||
|
||||
makeListLiteralPattern :: [Pattern] -> Pattern
|
||||
makeListLiteralPattern items = mLiteral (A.List inferExpType) (mListLiteral items)
|
||||
makeListLiteralPattern items = mLiteral (A.List inferExpType) (mArrayListLiteral $
|
||||
mSeveralE items)
|
||||
|
||||
|
||||
-- | Runs a parse test, given a tuple of: (source text, parser function, assertion)
|
||||
|
@ -286,13 +287,13 @@ testLiteral =
|
|||
|
||||
-- Lists:
|
||||
,pass ("[0]", RP.literal, assertPatternMatch "testLiteral 400" $ pat $
|
||||
makeListLiteralPattern [intLiteralPattern 0])
|
||||
makeListLiteralPattern [mOnlyE $ intLiteralPattern 0])
|
||||
,pass ("[]", RP.literal, assertPatternMatch "testLiteral 401" $ pat $
|
||||
makeListLiteralPattern [])
|
||||
,pass ("[0,1,2]", RP.literal, assertPatternMatch "testLiteral 402" $ pat $
|
||||
makeListLiteralPattern $ map intLiteralPattern [0,1,2])
|
||||
makeListLiteralPattern $ map (mOnlyE . intLiteralPattern) [0,1,2])
|
||||
,pass ("['0']", RP.literal, assertPatternMatch "testLiteral 403" $ pat $
|
||||
makeListLiteralPattern [makeLiteralCharPattern '0'])
|
||||
makeListLiteralPattern [mOnlyE $ makeLiteralCharPattern '0'])
|
||||
|
||||
,fail ("[", RP.literal)
|
||||
,fail ("]", RP.literal)
|
||||
|
@ -306,11 +307,11 @@ testRange :: [ParseTest A.Expression]
|
|||
testRange =
|
||||
[
|
||||
pass("[0..1]", RP.expression, assertPatternMatch "testRange 0" $ pat $
|
||||
A.ExprConstr m $ A.RangeConstr m (A.List inferExpType) (intLiteral 0) (intLiteral 1))
|
||||
A.Literal m (A.List inferExpType) $ A.RangeLiteral m (intLiteral 0) (intLiteral 1))
|
||||
,pass("[0..10000]", RP.expression, assertPatternMatch "testRange 1" $ pat $
|
||||
A.ExprConstr m $ A.RangeConstr m (A.List inferExpType) (intLiteral 0) (intLiteral 10000))
|
||||
A.Literal m (A.List inferExpType) $ A.RangeLiteral m (intLiteral 0) (intLiteral 10000))
|
||||
,pass("[-3..-1]", RP.expression, assertPatternMatch "testRange 2" $ pat $
|
||||
A.ExprConstr m $ A.RangeConstr m (A.List inferExpType) (intLiteral $ -3) (intLiteral $ -1))
|
||||
A.Literal m (A.List inferExpType) $ A.RangeLiteral m (intLiteral $ -3) (intLiteral $ -1))
|
||||
,pass("[sint16: 0..1]", RP.expression, rangePattern 4 (A.List A.Int16)
|
||||
(buildExprPattern $ Cast A.Int16 (Lit $ intLiteral 0))
|
||||
(buildExprPattern $ Cast A.Int16 (Lit $ intLiteral 1)))
|
||||
|
@ -321,7 +322,8 @@ testRange =
|
|||
where
|
||||
rangePattern :: Int -> A.Type -> Pattern -> Pattern -> (A.Expression -> Assertion)
|
||||
rangePattern n t start end = assertPatternMatch ("testRange " ++ show n) $
|
||||
pat $ mExprConstr $ mRangeConstr t start end
|
||||
pat $ mLiteral t $ mRangeLiteral start end
|
||||
|
||||
|
||||
--Helper function for ifs:
|
||||
makeIf :: [(A.Expression,A.Process)] -> A.Process
|
||||
|
|
Loading…
Reference in New Issue
Block a user