向类似 C++ 的语言添加状态机支持的提案

发布于 2024-08-14 02:12:28 字数 994 浏览 4 评论 0原文

最近,作为我日常工作的一部分,我一直在学习 IBM Rhapsody 并使用它从 UML 生成 C++ 代码。

昨天,我突然想到,为我的 C++ 编译器添加状态机支持可能会很酷,所以我在这里记下了一些注释:http://ellcc.org/wiki/index.php/State_machines_and_Active_Classes

我这样做的动机是:

  1. 这似乎是一个很酷的主意。
  2. 与当前的 Rhapsody/普通 C++ 编译器相比,该编译器可以进行更好的语义检查(具有更好的错误检查)。
  3. 当编译器本身理解状态机结构时,有许多优化的可能性。

我可能会尝试将我的语法扩展到除了提案之类的内容,看看它的效果如何。

您对该提案有何看法?看起来可读吗?看起来值得吗?


Edit:

感谢您推荐特定库来执行状态机的答案,但这不是我的问题。我已经使用我编写的库和代码实现了许多状态机。

我实际上是在寻找关于类似 C++ 语言的状态机扩展设计的想法、批评等,而不是这种更改是否适合添加到标准 C++。将其视为特定于域的扩展,其中我的域是实时控制应用程序。

我已经开始在编译器中实现扩展,如下所述: http://ellcc.org/wiki/index.php/State%5Fmachines%5Fand%5FActive%5FClasses

到目前为止,这个概念从提案到实施还没有发生太大变化,但有一些变化细节上的变化,我正在完善对问题语义的理解。

然而,时间会证明整个概念是否有价值。 ;-)

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. ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

且行且努力 2024-08-21 02:12:28

除了少数例外,C++ 传统上是使用类库而不是新关键字进行扩展的。使用此类库可以轻松实现状态机,因此我认为您的建议没有太大机会。

我在您的提案中看到的一个问题是使用“goto”转到另一个状态。如果我想在状态转换中在自己的代码中使用 goto 会发生什么?

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?

勿忘心安 2024-08-21 02:12:28

出色的工作发展了你所做的事情。像你所做的那样的事情可能是可能的,但我怀疑它是否会进入 C++。大多数对语言本身的更改只是为了让人们能够编写更有用、更强大的库。

这里有一个库为状态机提供支持。我还没有尝试过,但它可能会让您感兴趣,并且您也许能够将您的想法与这样的库结合起来,以允许其他人使用它。

http://www.boost.org/doc/ libs/1_34_1/libs/statechart/doc/index.html

或者,您可以按照您的建议开发自己的扩展,它至少对您有用。 Microsoft 实现了一些扩展关键字,因此您没有理由不能创建自己的 C++ 扩展版本。

不断涌现新想法。

Excellent work developing what you've done. Something like you've done probably is possible, but I'm doubtful it would ever get into the C++. Most changes that make it into the language itself are included only to allow people to write more useful and powerful libraries.

There's a library here that provides support for state machines. I haven't tried it, but it might interest you, and you may be able to combine your ideas with a library like this to allow other people to make use of it.

http://www.boost.org/doc/libs/1_34_1/libs/statechart/doc/index.html

Or, you could develop your own extension as you suggest, and it would at least be useful for you. Microsoft implement some extension keywords, so there is no reason why you couldn't create your own extended version of C++.

Keep the new ideas coming.

ぃ双果 2024-08-21 02:12:28

您应该看看另一位聪明的开发人员如何向类 C 语言添加状态机支持:UnrealScript 语言参考。请参阅名为“状态”的部分。

UnrealScript 支持以下状态
语言水平。在 UnrealScript 中,每个
世界上的演员永远是一体的
并且只有一个状态。它的状态反映了
它想要执行的操作。为了
例如,移动画笔有几个
诸如“StandOpenTimed”之类的状态和
“碰撞打开定时”。棋子有几个
诸如“死亡”、“攻击”等状态,
和“流浪”。在 UnrealScript 中,您
可以编写函数和代码
存在于特定的状态。这些
函数仅在以下情况下被调用
演员就处于那种状态

You should take a look at how another smart developer added State machine support to a C-like language: UnrealScript Language Reference. See the section named "States".

UnrealScript supports states at the
language level. In UnrealScript, each
actor in the world is always in one
and only one state. Its state reflects
the action it wants to perform. For
example, moving brushes have several
states like "StandOpenTimed" and
"BumpOpenTimed". Pawns have several
states such as "Dying", "Attacking",
and "Wandering". In UnrealScript, you
can write functions and code which
exist in a particular state. These
functions are only called when the
actor is in that state

零崎曲识 2024-08-21 02:12:28

这是一个有趣的想法,但我认为为状态机创建自己的领域特定语言实际上比正式扩展 C++ 更幸运。 C++ 被设计为一种非常通用的编程语言。我认为 Boost 已经证明了 C++ 足够灵活,大多数功能都可以使用库很好地实现。它的发展也非常缓慢,以至于到 2009 年标准 C++ 仍然没有内置的线程支持(计划在 0x 中)。因此,委员会在一段时间内不太可能考虑这一补充。

It's an interesting idea, but I think you'd actually have better luck creating your own domain-specific language for state machines than officially extending C++. C++ is designed as a very general-purpose programming language. I think Boost has proven that C++ is flexible enough that most features can be implemented nicely using libraries. It also evolves very slowly, to the extent that standard C++ still doesn't even have built-in threading support as of 2009, (it is planned in 0x). So it's unlikely the committee would consider this addition for some time.

此岸叶落 2024-08-21 02:12:28

您的解决方案看起来与基于模板或预处理器宏的解决方案没有任何优势。

我也不确定如何提供更好的语义检查。我怀疑您是否可以应用许多有用的代码优化。

然而,为了更好的优化和语义检查,您还应该用新的关键字替换“goto”(例如__change__ newState),并不允许使用goto进行状态更改!像往常一样允许 goto 进行本地跳转。

然后编译器可以提取可能的转换列表。

Your solution does not look like it has any advantages to a template- or preprocessor-macro-based solution.

I am also not sure, how you can provide better semantic checking. And I doubt that you can apply many useful code optimizations.

However, to allow better optimizations and semantic checking, you should also replace "goto" with a new keyword (e.g. __change__ newState), and disallow goto for state changes! Allow goto for local jumps as usual.

Then the compiler can extract a list of possible transitions.

乙白 2024-08-21 02:12:28

阅读您的提案,并发表以下评论:

  1. 实际上没有关键字来声明和定义实际的状态机!您是否假设有一个全局状态机(因此也是一个全局状态)?这与 __active__ 有何关系?

  2. C++ 中最接近的可比较结构实际上是枚举。为什么不扩展它?

  3. 定义的事件和状态之间似乎存在某种联系,但我看不出它是如何实现的。

  4. 为什么需要线程和计时器?状态机的一些用例可能会从中受益,但一个好的建议应该将它们分开。最重要的是,这应该允许使用标准 C++0x 线程。

就我个人而言,我会扩展枚举语法:

enum Foo {
  red, blue, green; /* Standard C++ so far - defines states. State list ends with a ; not a , */ 
  Foo() { *this = red; } // Reuse ctor syntax, instead of __initial__
  ~Foo() { } // reuse dtor syntax, instead of __onexit__

  void Bar() {/**/} // Defines an event, no return value. Doesn't need keyword __event__
};

很自然地,您现在可以在标头中声明事件,并在 .cpp 文件中定义它们。我什至不需要在这里建议语法,任何 C++ 程序员此时都可以猜到。为组合状态添加一些继承语法:

enum DrawingObject : public Shape, public Color { /** } // allows (red && circle)

并且您几乎已经达到了您的建议,没有任何新关键字,所有这些都是通过重用已经熟悉的语法来实现的。

Read your proposal, have the following comments:

  1. There's actually no keyword to declare and define an actual state machine! Do you assume a single global state machine (and thus a single global state)? How does that relate to __active__ ?

  2. The closest comparable construct in C++ is actually the enum. Why not extend it?

  3. There seems to be some connection between defined events and states, but I fail to see how it's implemented.

  4. Why are threads and timers needed at all? Some use cases of state machines may benefit from them, but a good proposal should keep these seperate. Most importantly this should allow the use of standard C++0x threads.

Personally, I would extend the enum syntax:

enum Foo {
  red, blue, green; /* Standard C++ so far - defines states. State list ends with a ; not a , */ 
  Foo() { *this = red; } // Reuse ctor syntax, instead of __initial__
  ~Foo() { } // reuse dtor syntax, instead of __onexit__

  void Bar() {/**/} // Defines an event, no return value. Doesn't need keyword __event__
};

It follows naturally that you can now declare your events in a header, and define them in a .cpp file. I don't even need to suggest the syntax here, any C++ programmer can guess that at this point. Add a bit of inheritance syntax for combined states:

enum DrawingObject : public Shape, public Color { /** } // allows (red && circle)

and you're pretty much at the point where your proposal is, without any new keywords, all by reusing an already familiar syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文