Added support for List types to the C and C++ backend, in genType at least

This commit is contained in:
Neil Brown 2007-10-27 11:24:24 +00:00
parent 25f2efb94c
commit 7433e49d49
6 changed files with 106 additions and 4 deletions

View File

@ -28,6 +28,7 @@ import Control.Monad.Error
import Control.Monad.State
import Control.Monad.Writer
import Text.Printf
import Text.Regex
import qualified AST as A
import BackendPasses
@ -409,6 +410,10 @@ cgenType _ (A.Chan _ _ t) = tell ["Channel*"]
-- Counted -- not used
-- Any -- not used
--cgenType ops (A.Port t) =
--TODO have a pass that declares these list types:
cgenType ops t@(A.List {}) = tell [subRegex (mkRegex "[^A-Za-z0-9]") (show t) ""]
cgenType ops t
= case call getScalarType ops t of
Just s -> tell [s]

View File

@ -912,7 +912,9 @@ cppgenType ops (A.Chan dir attr t)
tell [">/**/"]
cppTypeInsideChannel ops t = call genType ops t
cppgenType ops (A.Mobile t@(A.Array {})) = call genType ops t
cppgenType ops (A.Mobile t@(A.List {})) = call genType ops t
cppgenType ops (A.Mobile t) = call genType ops t >> tell ["*"]
cppgenType ops (A.List t) = tell ["tockList<"] >> call genType ops t >> tell [">/**/"]
cppgenType ops t
= case call getScalarType ops t of
Just s -> tell [s]

View File

@ -256,6 +256,12 @@ testGenType = TestList
(tcall genType $ A.Chan A.DirUnknown (A.ChanAttributes False False) $ A.Array [A.Dimension 6,A.Dimension 7,A.Dimension 8] A.Int)
-- List types:
,testBothS "GenType 2000" "ListInt16" "tockList<int16_t>" (tcall genType $ A.List A.Int16) markRainTest
,testBothS "GenType 2001" "ListListInt16" "tockList<tockList<int16_t>>" (tcall genType $ A.List $ A.List A.Int16) markRainTest
,testBothS "GenType 2010" "ListInt16*" "tockList<int16_t>" (tcall genType $ A.Mobile $ A.List A.Int16) markRainTest
,testBothS "GenType 2011" "ListListInt16*" "tockList<tockList<int16_t>>" (tcall genType $ A.Mobile $ A.List $ A.List A.Int16) markRainTest
,testBothS "GenType 2012" "ListMobileListInt16*" "tockList<tockList<int16_t>>" (tcall genType $ A.Mobile $ A.List $ A.Mobile $ A.List A.Int16) markRainTest
]
testStop :: Test

View File

@ -211,7 +211,7 @@ instance ShowOccam A.Type where
showOccamM (A.UserDataType n) = showName n +>> helper "{data type}"
showOccamM (A.Record n) = showName n +>> helper "{record}"
showOccamM (A.UserProtocol n) = showName n +>> helper "{protocol}"
showOccamM (A.List t) = return "LIST " +>> showOccamM t
instance ShowRain A.Type where
showRain A.Bool = "bool"
@ -235,6 +235,7 @@ instance ShowRain A.Type where
showRain A.Time = "time"
-- Mobility is not explicit in Rain:
showRain (A.Mobile t) = showRain t
showRain (A.List t) = "[" ++ showRain t ++ "]"
showRain x = "<invalid Rain type: " ++ show x ++ ">"
instance ShowOccam A.DyadicOp where

View File

@ -319,4 +319,18 @@ static inline double occam_DSQRT (double v, const char *pos) {
}
//}}}
//{{{ lists
#define DECLARE_LIST_TYPE(type, name) struct name { struct name * next; type data; }; \
name * array_to_list_##name (const type * _array, const int _size) { \
if (_size <= 0) return NULL; name * r = (name *)malloc(sizeof(name)); \
r->next = NULL; r->data = _array[0]; name * p = r; \
for (int i = 1;i < _size;i++) p = append_data_to_list_##name (p,&_array[i]); \
return r; } \
name * append_data_to_list_##name (const name * p, const type* data) { \
p->next = (name *)malloc(sizeof(name));p = p->next; \
p->next = NULL; p->data = *data; return p; }
//}}}
#endif

View File

@ -45,10 +45,8 @@ public:
#include <cppcsp/cppcsp.h>
#include <cppcsp/common/basic.h>
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/variant.hpp>
#include <boost/any.hpp>
#include <vector>
#include <list>
#include <boost/type_traits/remove_const.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
@ -473,3 +471,79 @@ public:
{
}
};
template <typename T>
class tockList
{
private:
mutable std::list<T> data;
public:
inline tockList()
{
}
inline explicit tockList(const T& t)
{
data.push_back(t);
}
inline tockList& operator()(const T& t)
{
data.push_back(t);
return *this;
}
typedef std::list<T>::iterator iterator;
inline iterator beginSeqEach()
{
return data.begin();
}
inline iterator limitIterator()
{
return data.end();
}
inline void endSeqEach()
{
}
inline void remove(const iterator& _it)
{
data.erase(_it);
}
//TODO add beginParEach
//TODO add functions for concatenation
//By default the class acts like a mobile thing:
inline void operator=(const tockList<T>& _rhs)
{
data.clear();
data.swap(_rhs.data);
}
class derefTockList
{
private:
tockList<T>* ref;
public:
inline explicit derefTockList(tockList<T>* _ref)
: ref(_ref)
{
}
//If you dereference both sides, you get a proper copy-assignment:
void operator=(const derefTockList& _rhs)
{
ref->data = _rhs.ref->data;
}
};
derefTockList operator*()
{
return derefTockList(this);
}
};