Altered the backends to support lists and foreach loops over lists (at least in the C++ part)

This commit is contained in:
Neil Brown 2008-03-19 17:20:52 +00:00
parent 43b77ff1a0
commit 839d92546b
4 changed files with 57 additions and 10 deletions

View File

@ -17,7 +17,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
-} -}
-- | Generate C code from the mangled AST. -- | Generate C code from the mangled AST.
module GenerateC (cgenOps, cgenType, cintroduceSpec, cPreReq, genComma, genCPasses, generate, generateC, genLeftB, genMeta, genName, genRightB, seqComma, withIf ) where module GenerateC (cgenOps, cgenReplicatorLoop, cgenType, cintroduceSpec, cPreReq, genComma, genCPasses, generate, generateC, genLeftB, genMeta, genName, genRightB, seqComma, withIf ) where
import Data.Char import Data.Char
import Data.Generics import Data.Generics
@ -91,6 +91,8 @@ cgenOps = GenOps {
genInputItem = cgenInputItem, genInputItem = cgenInputItem,
genIntrinsicFunction = cgenIntrinsicFunction, genIntrinsicFunction = cgenIntrinsicFunction,
genIntrinsicProc = cgenIntrinsicProc, genIntrinsicProc = cgenIntrinsicProc,
genListLiteral = cgenListLiteral,
genListSize = cgenListSize,
genLiteral = cgenLiteral, genLiteral = cgenLiteral,
genLiteralRepr = cgenLiteralRepr, genLiteralRepr = cgenLiteralRepr,
genMissing = cgenMissing, genMissing = cgenMissing,
@ -511,6 +513,12 @@ genLitSuffix A.UInt64 = tell ["ULL"]
genLitSuffix A.Real32 = tell ["F"] genLitSuffix A.Real32 = tell ["F"]
genLitSuffix _ = return () genLitSuffix _ = return ()
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"
cgenLiteralRepr :: A.LiteralRepr -> A.Type -> CGen () cgenLiteralRepr :: A.LiteralRepr -> A.Type -> CGen ()
cgenLiteralRepr (A.RealLiteral m s) t = tell [s] >> genLitSuffix t cgenLiteralRepr (A.RealLiteral m s) t = tell [s] >> genLitSuffix t
cgenLiteralRepr (A.IntLiteral m s) t cgenLiteralRepr (A.IntLiteral m s) t
@ -533,7 +541,8 @@ cgenLiteralRepr (A.RecordLiteral _ es) _
= do genLeftB = do genLeftB
seqComma $ map (call genUnfoldedExpression) es seqComma $ map (call genUnfoldedExpression) es
genRightB genRightB
cgenLiteralRepr (A.ListLiteral _ es) t = call genListLiteral es t
-- | Generate an expression inside a record literal. -- | Generate an expression inside a record literal.
-- --
-- This is awkward: the sort of literal that this produces when there's a -- This is awkward: the sort of literal that this produces when there's a
@ -837,7 +846,7 @@ cgenExpression (A.SizeExpr m e)
cgenExpression (A.SizeVariable m v) cgenExpression (A.SizeVariable m v)
= do A.Array (d:_) _ <- typeOfVariable v = do A.Array (d:_) _ <- typeOfVariable v
case d of case d of
A.Dimension n -> call genExpression n A.Dimension n -> tell [show n]
A.UnknownDimension -> do call genVariable v A.UnknownDimension -> do call genVariable v
call genSizeSuffix "0" call genSizeSuffix "0"
cgenExpression (A.Conversion m cm t e) = call genConversion m cm t e cgenExpression (A.Conversion m cm t e) = call genConversion m cm t e
@ -1052,7 +1061,7 @@ cgenReplicatorLoop (A.For m index base count)
tell [";", counter, ">0;", counter, "--,"] tell [";", counter, ">0;", counter, "--,"]
genName index genName index
tell ["++"] tell ["++"]
cgenReplicatorLoop _ = cgenMissing "ForEach loops not yet supported in the C backend"
--}}} --}}}
--{{{ abbreviations --{{{ abbreviations

View File

@ -132,6 +132,8 @@ data GenOps = GenOps {
genInputItem :: A.Variable -> A.InputItem -> CGen (), genInputItem :: A.Variable -> A.InputItem -> CGen (),
genIntrinsicFunction :: Meta -> String -> [A.Expression] -> CGen (), genIntrinsicFunction :: Meta -> String -> [A.Expression] -> CGen (),
genIntrinsicProc :: Meta -> String -> [A.Actual] -> CGen (), genIntrinsicProc :: Meta -> String -> [A.Actual] -> CGen (),
genListLiteral :: [A.Expression] -> A.Type -> CGen (),
genListSize :: A.Variable -> CGen (),
genLiteral :: A.LiteralRepr -> A.Type -> CGen (), genLiteral :: A.LiteralRepr -> A.Type -> CGen (),
genLiteralRepr :: A.LiteralRepr -> A.Type -> CGen (), genLiteralRepr :: A.LiteralRepr -> A.Type -> CGen (),
genMissing :: String -> CGen (), genMissing :: String -> CGen (),

View File

@ -39,7 +39,7 @@ import System.IO
import qualified AST as A import qualified AST as A
import CompState import CompState
import GenerateC (cgenOps, cintroduceSpec, cgenType, generate, genComma, genLeftB, genMeta, genName, genRightB, seqComma, withIf) import GenerateC (cgenOps, cintroduceSpec, cgenReplicatorLoop, cgenType, generate, genComma, genLeftB, genMeta, genName, genRightB, seqComma, withIf)
import GenerateCBased import GenerateCBased
import Metadata import Metadata
import Pass import Pass
@ -65,10 +65,13 @@ cppgenOps = cgenOps {
genGetTime = cppgenGetTime, genGetTime = cppgenGetTime,
genIf = cppgenIf, genIf = cppgenIf,
genInputItem = cppgenInputItem, genInputItem = cppgenInputItem,
genListSize = cppgenListSize,
genListLiteral = cppgenListLiteral,
genOutputCase = cppgenOutputCase, genOutputCase = cppgenOutputCase,
genOutputItem = cppgenOutputItem, genOutputItem = cppgenOutputItem,
genPar = cppgenPar, genPar = cppgenPar,
genProcCall = cppgenProcCall, genProcCall = cppgenProcCall,
genReplicatorLoop = cppgenReplicatorLoop,
genStop = cppgenStop, genStop = cppgenStop,
genTimerRead = cppgenTimerRead, genTimerRead = cppgenTimerRead,
genTimerWait = cppgenTimerWait, genTimerWait = cppgenTimerWait,
@ -679,6 +682,34 @@ cppgenType t
Just s -> tell [s] Just s -> tell [s]
Nothing -> call genMissingC $ formatCode "genType %" t Nothing -> call genMissingC $ formatCode "genType %" t
cppgenListSize :: A.Variable -> CGen ()
cppgenListSize v
= do call genVariable v
tell [".size()"]
cppgenListLiteral :: [A.Expression] -> A.Type -> CGen ()
cppgenListLiteral es t
= do call genType t
tell ["()"]
mapM_ (\e -> tell ["("] >> call genExpression e >> tell [")"]) es
cppgenReplicatorLoop :: A.Replicator -> CGen ()
cppgenReplicatorLoop rep@(A.For {}) = cgenReplicatorLoop rep
cppgenReplicatorLoop (A.ForEach m n (A.ExprVariable _ v))
= do t <- typeOfVariable v
call genType t
tell ["::iterator "]
genName n
tell ["="]
call genVariable v
tell [".beginSeqEach();"] --TODO what if this is a pareach?
genName n
tell ["!="]
call genVariable v
tell [".limitIterator();"]
genName n
tell ["++"]
-- TODO call endSeqEach
-- | Helper function for prefixing an underscore to a name. -- | Helper function for prefixing an underscore to a name.
prefixUnderscore :: A.Name -> A.Name prefixUnderscore :: A.Name -> A.Name

View File

@ -224,17 +224,17 @@ public:
typedef typename std::list<T>::iterator iterator; typedef typename std::list<T>::iterator iterator;
inline iterator beginSeqEach() inline iterator beginSeqEach() const
{ {
return data.begin(); return data.begin();
} }
inline iterator limitIterator() inline iterator limitIterator() const
{ {
return data.end(); return data.end();
} }
inline void endSeqEach() inline void endSeqEach() const
{ {
} }
@ -242,6 +242,11 @@ public:
{ {
data.erase(_it); data.erase(_it);
} }
inline unsigned size()
{
return data.size();
}
//TODO add beginParEach //TODO add beginParEach
//TODO add functions for concatenation //TODO add functions for concatenation
@ -256,7 +261,7 @@ public:
class derefTockList class derefTockList
{ {
private: private:
tockList<T>* ref; tockList<T>* const ref;
public: public:
inline explicit derefTockList(tockList<T>* _ref) inline explicit derefTockList(tockList<T>* _ref)
: ref(_ref) : ref(_ref)
@ -264,7 +269,7 @@ public:
} }
//If you dereference both sides, you get a proper copy-assignment: //If you dereference both sides, you get a proper copy-assignment:
void operator=(const derefTockList& _rhs) inline void operator=(const derefTockList& _rhs)
{ {
ref->data = _rhs.ref->data; ref->data = _rhs.ref->data;
} }