Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ code generator

Reply
Thread Tools

C++ code generator

 
 
christopher diggins
Guest
Posts: n/a
 
      12-10-2004
I am considering writing a code generator which takes interfaces
specifications in the following style:

interface IStack<class Elem_T>
invariant: // number of items pushed >= number of items popped
{
Bool IsEmpty();
precondition: result = number of items pushed == number of items popped
void Push(const Elem_T& x);
void Pop();
precondition: IsEmpty() == false
postcondition: // result = most recent pushed item which hasn't been
popped
const Elem_T& Top();
precondition: IsEmpty() == false
}

The code generator would output a C++ class to represent the interface (
using the BIL, see http://www.codeproject.com/cpp/retrofitpolymorphism.asp )
It would also output a contract class which verifies the preconditions /
postconditions / etc. It would also ouput a stub implementation class. That
is a class which implements the interface but does nothing.

This is actually only scratching the surface of what I would do with it: I
would also support pseudo-code class definitions, which implement Heron
style classes.

The feature list for the classes would be considerably more far reaching:
- aspect oriented programming support (
http://www.heron-language.com/aspect...ogramming.html )
- self / inherited keywords
- delegation
- implicit result variables
- properties
- _ctor and _dtor named constructors and destructors

I am trying to gauge the level of interest in such a tool. Thanks in
advance, and feel free to share your two cents.

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com


 
Reply With Quote
 
 
 
 
Puppet_Sock
Guest
Posts: n/a
 
      12-10-2004
christopher diggins wrote:
> I am considering writing a code generator which takes interfaces
> specifications in the following style:
>
> interface IStack<class Elem_T>
> invariant: // number of items pushed >= number of items popped
> {
> Bool IsEmpty();
> precondition: result = number of items pushed == number of items

popped
> void Push(const Elem_T& x);
> void Pop();
> precondition: IsEmpty() == false
> postcondition: // result = most recent pushed item which hasn't

been
> popped
> const Elem_T& Top();
> precondition: IsEmpty() == false
> }


Code generation tools are good things in most cases. However,
there's something, um, skewed about requiring psuedo code as
input to such a tool. The level of effort required to create
the input you've typed here is too large compared to the result.

Instead, how about a menu driven something-or-other that lets
you select certain items from a list, then blasts the appropriate
text into a file for you. Put a nice GUI on there and you've got
something.

Of course, with the example you've got, what you've got is a
fairly straightforward template class. So, the question that
arises fairly quickly is, what's wrong with just providing that
fairly straightforward template class? Template classes are
pretty cool.
Socks

 
Reply With Quote
 
 
 
 
christopher diggins
Guest
Posts: n/a
 
      12-10-2004
"Puppet_Sock" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> christopher diggins wrote:
>> I am considering writing a code generator which takes interfaces
>> specifications in the following style:
>>
>> interface IStack<class Elem_T>
>> invariant: // number of items pushed >= number of items popped
>> {
>> Bool IsEmpty();
>> precondition: result = number of items pushed == number of items

> popped
>> void Push(const Elem_T& x);
>> void Pop();
>> precondition: IsEmpty() == false
>> postcondition: // result = most recent pushed item which hasn't

> been
>> popped
>> const Elem_T& Top();
>> precondition: IsEmpty() == false
>> }

>
> Code generation tools are good things in most cases. However,
> there's something, um, skewed about requiring psuedo code as
> input to such a tool. The level of effort required to create
> the input you've typed here is too large compared to the result.


I don't see how I could reduce it down further.

Take a look at the code needed to replicate the interface:

template<typename Elem_T>
BOOST_IDL_BEGIN1(IStack)
BOOST_IDL_FN1(Push, void, (const Elem_T&, x))
BOOST_IDL_FN0(Pop, const Elem_T&)
BOOST_IDL_FN0(Peek, const Elem_T&)
BOOST_IDL_FN0(IsEmpty, Bool)
BOOST_IDL_END1(IStack)

There is the contract:

template<typename Stack_T, typename Elem_T>
struct Stack_contract : public Stack_T
{
// type identities
typedef Stack_T inherited;
// constructor
Stack_contract() { }
template<typename Arg_T> Stack_contract(const Arg_T& x) : inherited(X)
{ };
// contract verification
const Elem_T& Pop() { PRE(!IsEmpty()); return inherited:op(); }
const Elem_T& Peek() { PRE(!IsEmpty()); return inherited:eek(); }
};

The wrapper :

template<typename Stack_T, typename Elem_T>
struct Stack_wrapper
{
#ifdef APPLY_CONTRACTS
typedef Stack_ext<Stack_contract<Stack_T, Elem_T>, Elem_T> type;
#else
typedef Stack_ext<Stack_T, Elem_T> type;
#endif
};

And a stub:

template<typename Elem_T>
struct Stack_strub
{
Stack_stub() { }
// IStack implementation
const Elem_T& Peek() { }
const Elem_T& Pop() { }
void Push(const Elem_T& x) { }
Bool IsEmpty() { }
};

> Instead, how about a menu driven something-or-other that lets
> you select certain items from a list, then blasts the appropriate
> text into a file for you. Put a nice GUI on there and you've got
> something.


This is an interesting idea.

> Of course, with the example you've got, what you've got is a
> fairly straightforward template class. So, the question that
> arises fairly quickly is, what's wrong with just providing that
> fairly straightforward template class? Template classes are
> pretty cool.


All of this is not as easy to implement as it might seem on first glance
check out http://www.ootl.org/src/collections.hpp

> Socks


Thanks for your feedback
CD


 
Reply With Quote
 
fuzzylollipop
Guest
Posts: n/a
 
      12-13-2004
what you have to ask yourself is why create yet another templating
language.

C++ has one built in already.
There is already a standard IDL, for doing just this.
There is XML/UML formats for this also ->
http://www.yy.ics.keio.ac.jp/~suzuki/project/uxf/
Tools like SWIG do this already for other languages to C/C++ mappings.
There are tonnes of template based parsers and frameworks, some like
EMF that are already integrated into an IDE ( Eclipse )

second you have to answer, is your idea and implementation so much
better than everyone elses that people are going to drop what they are
doing, learn your NEW language/syntax/api or whatever and adopt it
because it saves them so much time and effort ( including the learning
curve ).
First pass at your suggestion my answer to the second question is a NO.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generator functions subclass generator? Terry Reedy Python 0 06-18-2009 09:51 PM
Problems with email.Generator.Generator Chris Withers Python 20 09-12-2006 11:13 AM
UART with fractional baudrate generator ? Or fractional baudrate generator alone Martin Maurer VHDL 3 04-19-2006 01:26 PM
subtle side effect of generator/generator expression bonono@gmail.com Python 9 10-16-2005 06:42 PM
generator function within a generator function doesn't execute? TheDustbustr Python 1 07-25-2003 10:45 AM



Advertisments