From bc7e682119140267ce05d82ff24fb459fb9f60c2 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Fri, 21 Mar 2008 19:20:15 +0000 Subject: [PATCH] Added a Concat operator for lists, and added support for it (and list assignment) to the backends --- backends/GenerateC.hs | 12 +++++++++++- backends/GenerateCBased.hs | 2 ++ backends/GenerateCPPCSP.hs | 17 +++++++++++++++++ data/AST.hs | 1 + support/tock_support_cppcsp.h | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/backends/GenerateC.hs b/backends/GenerateC.hs index 4a17b7c..2e2f92a 100644 --- a/backends/GenerateC.hs +++ b/backends/GenerateC.hs @@ -91,6 +91,8 @@ cgenOps = GenOps { genInputItem = cgenInputItem, genIntrinsicFunction = cgenIntrinsicFunction, genIntrinsicProc = cgenIntrinsicProc, + genListAssign = cgenListAssign, + genListConcat = cgenListConcat, genListLiteral = cgenListLiteral, genListSize = cgenListSize, genLiteral = cgenLiteral, @@ -513,12 +515,15 @@ genLitSuffix A.UInt64 = tell ["ULL"] genLitSuffix A.Real32 = tell ["F"] genLitSuffix _ = return () -cgenListLiteral :: [A.Expression] -> A.Type -> CGen() +cgenListLiteral :: [A.Expression] -> A.Type -> CGen () cgenListLiteral _ _ = call genMissing "C backend does not yet support lists" cgenListSize :: A.Variable -> CGen () cgenListSize _ = call genMissing "C backend does not yet support lists" +cgenListAssign :: A.Variable -> A.Expression -> CGen () +cgenListAssign _ _ = call genMissing "C backend does not yet support lists" + cgenLiteralRepr :: A.LiteralRepr -> A.Type -> CGen () cgenLiteralRepr (A.RealLiteral m s) t = tell [s] >> genLitSuffix t cgenLiteralRepr (A.IntLiteral m s) t @@ -951,8 +956,12 @@ cgenDyadic _ A.Less e f = call genSimpleDyadic "<" e f cgenDyadic _ A.More e f = call genSimpleDyadic ">" e f cgenDyadic _ A.LessEq e f = call genSimpleDyadic "<=" e f cgenDyadic _ A.MoreEq e f = call genSimpleDyadic ">=" e f +cgenDyadic _ A.Concat e f = call genListConcat e f --}}} +cgenListConcat :: A.Expression -> A.Expression -> CGen () +cgenListConcat _ _ = call genMissing "C backend does not yet support lists" + --{{{ input/output items cgenInputItem :: A.Variable -> A.InputItem -> CGen () cgenInputItem c (A.InCounted m cv av) @@ -1454,6 +1463,7 @@ cgenAssign m [v] (A.ExpressionList _ [e]) -- Assignment of channel-ends, but not channels, is possible (at least in Rain): A.Chan A.DirInput _ _ -> doAssign v e A.Chan A.DirOutput _ _ -> doAssign v e + A.List _ -> call genListAssign v e _ -> call genMissingC $ formatCode "assignment of type %" t where doAssign :: A.Variable -> A.Expression -> CGen () diff --git a/backends/GenerateCBased.hs b/backends/GenerateCBased.hs index a2fc67c..9abd679 100644 --- a/backends/GenerateCBased.hs +++ b/backends/GenerateCBased.hs @@ -132,6 +132,8 @@ data GenOps = GenOps { genInputItem :: A.Variable -> A.InputItem -> CGen (), genIntrinsicFunction :: Meta -> String -> [A.Expression] -> CGen (), genIntrinsicProc :: Meta -> String -> [A.Actual] -> CGen (), + genListAssign :: A.Variable -> A.Expression -> CGen (), + genListConcat :: A.Expression -> A.Expression -> CGen (), genListLiteral :: [A.Expression] -> A.Type -> CGen (), genListSize :: A.Variable -> CGen (), genLiteral :: A.LiteralRepr -> A.Type -> CGen (), diff --git a/backends/GenerateCPPCSP.hs b/backends/GenerateCPPCSP.hs index 2089a62..cdb9917 100644 --- a/backends/GenerateCPPCSP.hs +++ b/backends/GenerateCPPCSP.hs @@ -65,6 +65,8 @@ cppgenOps = cgenOps { genGetTime = cppgenGetTime, genIf = cppgenIf, genInputItem = cppgenInputItem, + genListAssign = cppgenListAssign, + genListConcat = cppgenListConcat, genListSize = cppgenListSize, genListLiteral = cppgenListLiteral, genOutputCase = cppgenOutputCase, @@ -684,6 +686,13 @@ cppgenType t Just s -> tell [s] Nothing -> call genMissingC $ formatCode "genType %" t +cppgenListAssign :: A.Variable -> A.Expression -> CGen () +cppgenListAssign v e + = do call genVariable v + tell ["="] + call genExpression e + tell [";"] + cppgenListSize :: A.Variable -> CGen () cppgenListSize v = do call genVariable v @@ -695,6 +704,14 @@ cppgenListLiteral es t tell ["()"] mapM_ (\e -> tell ["("] >> call genExpression e >> tell [")"]) es +cppgenListConcat :: A.Expression -> A.Expression -> CGen () +cppgenListConcat a b + = do tell ["("] + call genExpression a + tell ["+"] + call genExpression b + tell [")"] + cppgenReplicatorLoop :: A.Replicator -> CGen () cppgenReplicatorLoop rep@(A.For {}) = cgenReplicatorLoop rep cppgenReplicatorLoop (A.ForEach m n (A.ExprVariable _ v)) diff --git a/data/AST.hs b/data/AST.hs index dde44e9..ff94ebc 100644 --- a/data/AST.hs +++ b/data/AST.hs @@ -307,6 +307,7 @@ data DyadicOp = | And | Or | Eq | NotEq | Less | More | LessEq | MoreEq | After + | Concat deriving (Show, Eq, Ord, Typeable, Data) -- | An item in an input. diff --git a/support/tock_support_cppcsp.h b/support/tock_support_cppcsp.h index e2f9809..abbdd91 100644 --- a/support/tock_support_cppcsp.h +++ b/support/tock_support_cppcsp.h @@ -215,6 +215,12 @@ public: { data.push_back(t); } + + //Copy construction has the same behaviour as assignment: + inline tockList(const tockList& _rhs) + { + *this = _rhs; + } inline tockList& operator()(const T& t) { @@ -250,6 +256,14 @@ public: //TODO add beginParEach //TODO add functions for concatenation + + //By default, the class is mobile, so operator+ adds to this list + //and effectively blanks the other + inline tockList operator+(const tockList& _rhs) const + { + data.splice(data.end(), _rhs.data); + return *this; + } //By default the class acts like a mobile thing: inline void operator=(const tockList& _rhs)