From 41cca68e4eb5e27ba534bc7f47a1b0f3dda1c7db Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Tue, 3 Feb 2009 12:24:10 +0000 Subject: [PATCH] Simplified the Rain parser by reintroducing a RangeLiteral item that will get replaced by an early pass --- data/AST.hs | 3 +++ frontends/ParseRain.hs | 15 +++++---------- frontends/ParseRainTest.hs | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/data/AST.hs b/data/AST.hs index 4927ec5..3443407 100644 --- a/data/AST.hs +++ b/data/AST.hs @@ -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) diff --git a/frontends/ParseRain.hs b/frontends/ParseRain.hs index 37d496e..626ca4c 100644 --- a/frontends/ParseRain.hs +++ b/frontends/ParseRain.hs @@ -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 diff --git a/frontends/ParseRainTest.hs b/frontends/ParseRainTest.hs index 8cae4e5..de05751 100644 --- a/frontends/ParseRainTest.hs +++ b/frontends/ParseRainTest.hs @@ -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