From d574ec8e3d3552189543f9252e625daace6aa115 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Mon, 13 Aug 2007 16:05:17 +0000 Subject: [PATCH] Added ForEach to the replicator possibilities in the AST, and tested parsing Rain seqeach and pareach loops --- AST.hs | 5 +---- RainParse.hs | 4 ++++ RainParseTest.hs | 18 ++++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/AST.hs b/AST.hs index a8b6379..1cfd02c 100644 --- a/AST.hs +++ b/AST.hs @@ -259,10 +259,7 @@ data OutputItem = | OutExpression Meta Expression deriving (Show, Eq, Typeable, Data) --- | A replicator. --- The 'Name' names the replicator index, the first expression is the base and --- the second expression is the count. --- (In the future this will have additional constructors for stepped replicators.) +-- | The Name names the replicator index, the first expression is the base and the second expression is the FOR data Replicator = For Meta Name Expression Expression deriving (Show, Eq, Typeable, Data) diff --git a/RainParse.hs b/RainParse.hs index 2ae9c37..12b5262 100644 --- a/RainParse.hs +++ b/RainParse.hs @@ -241,6 +241,10 @@ statement } <|> do { m <- md ; optionalSeq ; b <- block ; return $ A.Seq m b} <|> do { m <- md ; sPar ; b <- block ; return $ A.Par m A.PlainPar b} + <|> do { m <- md ; sPareach ; sLeftR ; n <- name ; sColon ; exp <- expression ; sRightR ; st <- statement ; + return $ A.Par m A.PlainPar $ A.Rep m (A.ForEach m n exp) $ A.OnlyP m st } + <|> do { m <- md ; sSeqeach ; sLeftR ; n <- name ; sColon ; exp <- expression ; sRightR ; st <- statement ; + return $ A.Seq m $ A.Rep m (A.ForEach m n exp) $ A.OnlyP m st } <|> do { m <- md ; lv <- lvalue ; op <- assignOp ; exp <- expression ; sSemiColon ; case op of (m', Just dyOp) -> return (A.Assign m' [lv] (A.ExpressionList m' [(A.Dyadic m' dyOp (A.ExprVariable m lv) exp)])) diff --git a/RainParseTest.hs b/RainParseTest.hs index 0cf0a58..95ee079 100644 --- a/RainParseTest.hs +++ b/RainParseTest.hs @@ -56,10 +56,10 @@ makeAssign v e = A.Assign m [v] $ A.ExpressionList m [e] makeLiteralString :: String -> A.Expression -makeLiteralString str = A.Literal m (A.Array [A.Dimension 1] A.Byte) (A.ArrayLiteral m (map makeLiteralChar str)) +makeLiteralString str = A.Literal m (A.Array [A.Dimension (length str)] A.Byte) (A.ArrayLiteral m (map makeLiteralChar str)) where makeLiteralChar :: Char -> A.ArrayElem - makeLiteralChar c = A.ArrayElemExpr $ A.Literal m A.Byte (A.ByteLiteral m (show (fromEnum c))) + makeLiteralChar c = A.ArrayElemExpr $ A.Literal m A.Byte (A.ByteLiteral m [c] {-(show (fromEnum c))-}) data EachType = Seq | Par @@ -185,6 +185,19 @@ testPar = assertEqual "Par Skip Test" $ A.Par m A.PlainPar $ A.Several m [(A.OnlyP m (A.Skip m)),(A.OnlyP m (A.Skip m))] ) ] +testEach :: [ParseTest A.Process] +testEach = + [ + pass ("seqeach (c : \"1\") c = 7;", RP.statement, + assertEqual "Each Test 0" $ A.Seq m $ A.Rep m (A.ForEach m (simpleName "c") (makeLiteralString "1")) $ + A.OnlyP m $ (makeAssign (variable "c") (A.Literal m A.Int (A.IntLiteral m "7"))) ) + ,pass ("pareach (c : \"345\") {c = 1; c = 2;}", RP.statement, + assertEqual "Each Test 1" $ A.Par m A.PlainPar $ A.Rep m (A.ForEach m (simpleName "c") (makeLiteralString "345")) $ + A.OnlyP m $ makeSeq[(makeAssign (variable "c") (A.Literal m A.Int (A.IntLiteral m "1"))),(makeAssign (variable "c") (A.Literal m A.Int (A.IntLiteral m "2")))] ) + ] + + + --Returns the list of tests: tests :: Test tests = TestList @@ -193,6 +206,7 @@ tests = TestList parseTests testWhile, parseTests testSeq, parseTests testPar, + parseTests testEach, parseTests testIf, parseTests testAssign ]