diff --git a/backends/GenerateC.hs b/backends/GenerateC.hs index f37a1d0..a8d660f 100644 --- a/backends/GenerateC.hs +++ b/backends/GenerateC.hs @@ -122,7 +122,9 @@ data GenOps = GenOps { genPar :: GenOps -> A.ParMode -> A.Structured -> CGen (), genProcCall :: GenOps -> A.Name -> [A.Actual] -> CGen (), genProcess :: GenOps -> A.Process -> CGen (), + -- | Generates a replicator loop, given the replicator and body genReplicator :: GenOps -> A.Replicator -> CGen () -> CGen (), + -- | Generates the three bits of a for loop (e.g. "int i=0;i<10;i++" for the given replicator genReplicatorLoop :: GenOps -> A.Replicator -> CGen (), genRetypeSizes :: GenOps -> Meta -> A.AbbrevMode -> A.Type -> A.Name -> A.Type -> A.Variable -> CGen (), genSeq :: GenOps -> A.Structured -> CGen (), @@ -983,11 +985,11 @@ cgenOutputItem ops c (A.OutExpression m e) --{{{ replicators cgenReplicator :: GenOps -> A.Replicator -> CGen () -> CGen () cgenReplicator ops rep body - = do tell ["for ("] + = do tell ["for("] call genReplicatorLoop ops rep - tell [") {\n"] + tell ["){"] body - tell ["}\n"] + tell ["}"] isZero :: A.Expression -> Bool isZero (A.Literal _ A.Int (A.IntLiteral _ "0")) = True @@ -1003,24 +1005,24 @@ cgenReplicatorLoop ops (A.For m index base count) simple = do tell ["int "] genName index - tell [" = 0; "] + tell ["=0;"] genName index - tell [" < "] + tell ["<"] call genExpression ops count - tell ["; "] + tell [";"] genName index tell ["++"] general :: CGen () general = do counter <- makeNonce "replicator_count" - tell ["int ", counter, " = "] + tell ["int ", counter, "="] call genExpression ops count - tell [", "] + tell [","] genName index - tell [" = "] + tell ["="] call genExpression ops base - tell ["; ", counter, " > 0; ", counter, "--, "] + tell [";", counter, ">0;", counter, "--,"] genName index tell ["++"] diff --git a/backends/GenerateCTest.hs b/backends/GenerateCTest.hs index 44fa426..2b14176 100644 --- a/backends/GenerateCTest.hs +++ b/backends/GenerateCTest.hs @@ -65,7 +65,7 @@ assertGenR n exp act Right ss -> case matchRegex (mkRegex exp) (subRegex (mkRegex "/\\*\\*/") (concat ss) "") of Just matches -> return matches - Nothing -> (assertFailure $ n ++ " regex match failed, regex: \"" ++ show exp ++ "\" text: " ++ (concat ss)) >> return [] + Nothing -> (assertFailure $ n ++ " regex match failed, regex: \"" ++ exp ++ "\" text: " ++ (concat ss)) >> return [] -- | Asserts that the given output of a CGen pass is a failure @@ -129,6 +129,13 @@ testBothSameS :: -> Test testBothSameS n e a s = testBothS n e e a s +testBothSameR :: + String -- ^ Test Name + -> String -- ^ C and C++ expected + -> (GenOps -> CGen ()) -- ^ Actual + -> Test +testBothSameR n e a = TestCase $ (testRS n e (a cgenOps) (return ())) >> (testRS n e (a cppgenOps) (return ())) >> (return ()) + testBothFail :: String -> (GenOps -> CGen ()) -> Test testBothFail a b = testBothFailS a b (return ()) @@ -291,6 +298,13 @@ testOverArray = TestList $ map testOverArray' state1 = defineName (simpleName "foo") $ simpleDefDecl "foo" (A.Array [A.Dimension 7] A.Int) state3 = defineName (simpleName "foo") $ simpleDefDecl "foo" (A.Array [A.Dimension 7, A.Dimension 8, A.Dimension 9] A.Int) +testReplicator :: Test +testReplicator = TestList + [ + testBothSame "testReplicator 0" "for(int foo=0;foo<10;foo++){@}" (tcall2 genReplicator (A.For emptyMeta foo (intLiteral 0) (intLiteral 10)) at) + ,testBothSameR "testReplicator 1" "for\\(int ([[:alnum:]_]+)=10,foo=1;\\1>0;\\1--,foo\\+\\+\\)\\{@\\}" (tcall2 genReplicator (A.For emptyMeta foo (intLiteral 1) (intLiteral 10)) at) + ] + testDeclaration :: Test testDeclaration = TestList [ @@ -343,5 +357,6 @@ tests = TestList ,testDeclaration ,testGenType ,testOverArray + ,testReplicator ,testStop ]