Search code examples
c++compiler-constructionumllanguage-designstate-machine

A proposal to add statemachine support to C++-like language


Lately as part of my day job I've been learning IBM Rhapsody and using it to generate code in C++ from the UML.

Yesterday it struck me that it might be cool to think about adding state machine support to my C++ compiler, so I jotted a few notes here: http://ellcc.org/wiki/index.php/State_machines_and_Active_Classes

My motivations for doing this are:

  1. It seems like a cool idea.
  2. The compiler could do much better semantic checking (with better error checking) than the current Rhapsody/normal C++ compiler.
  3. There are many optimization possibilities available when the compiler itself understands the state machine structure.

I may try to extend my grammar to except something like the proposal to see how well it works.

What is your opinion of the proposal? Does it seem readable? Does it seem worthwhile?


Edit:

Thanks for the answers recommending specific libraries to do state machines, but that wasn't my question. I've implemented many state machines using both libraries and code that I've written.

I was really looking for ideas, criticism, etc. about the design of a state machine extension to a C++-like language, not whether this change would be appropriate for addition to standard C++. Think of it as a domain specific extension, where my my domain is real-time control applications.

I've started implementation of the extension in my compiler as described here: http://ellcc.org/wiki/index.php/State%5Fmachines%5Fand%5FActive%5FClasses

So far the concept hasn't had to change much going from proposal to implementation but there have been a few changes in details and I'm refining my understanding of the semantics of the problem.

Time will tell whether the whole concept has any value, however. ;-)


Solution

  • With a few exceptions, C++ has traditionally been extended using class libraries, not new keywords. State machines can easily be implemented using such libraries, so I don't think your proposal has much of a chance.

    One problem I see in your proposal is the use of 'goto' to go to another state. What happens if I want to use goto in my own code within a state transition?