Added ForEach to the replicator possibilities in the AST, and tested parsing Rain seqeach and pareach loops

This commit is contained in:
Neil Brown 2007-08-13 16:05:17 +00:00
parent 2902d085a4
commit d574ec8e3d
3 changed files with 21 additions and 6 deletions

5
AST.hs
View File

@ -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)

View File

@ -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)]))

View File

@ -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
]