Groovy中的GOTO语句如何? [英] How GOTO statement in Groovy?

查看:337
本文介绍了Groovy中的GOTO语句如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到关于Scala延续版的好博客文章在Scala语言中'模拟'一个 GOTO 语句。 (详细了解继续



我希望在编程语言Groovy中具有相同的功能。我认为可以在 Groovy编译器阶段转换中完成。

我正在研究领域特定语言(DSL),并且首选嵌入到Groovy中。我想要 GOTO 语句,因为DSL是非结构化语言(并且从工作流程图生成)。我需要一个带标签的goto语句,而不是行号。



DSL是用于工作流定义的语言,并且由于节点之间的箭头没有限制,需要 goto 。 (或,而等不可读代码)



作为Groovy和Scala的初学者,我不知道如果我可以将Scala解决方案转换为Groovy,但我不认为Groovy中存在延续。



我正在寻找用于在Groovy中模拟标记的goto的算法/代码。我想到的一种算法是反复使用 eval ;当你在 goto 时执行 eval
DSL已经被 eval 评估过。



我不是在寻找'while 'loop或者其他东西,而是翻译这段代码以便它可以工作(其他一些语法是没有问题的)

  label1:
A();
b();
goto label1;

PS:
我不喜欢讨论如果我真的需要/想要GOTO声明。 DSL是一种规范语言,可能不会处理变量,效率等。

PS2:其他一些关键字,然后 GOTO

解决方案

您可能想更多地了解您正在尝试构建的语言,也许很简单,处理转换就是过度工程。

使用AST是一种常人已经做了很多年的事情,它真的很强大。

spock框架家伙改写测试您使用标签创建注释代码。 http://code.google.com/p/spock/

哈姆雷特·达西曾就此事发表过几次演讲。他的博客上也可以找到几篇文章。 http://hamletdarcy.blogspot.com/

Cedric Champeau描述了一个有趣的转变,他建设和它的演变 http://www.jroller.com/melix/



可能缺少很多其他人,但我记得那些人。

您可能已经知道的起点,但真的很有用。 http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST +转换

http://groovy.codehaus.org/建筑+ AST +指南



长话短说,我认为它很有可能


I saw this nice blog post about a Scala continuations that 'emulates' a GOTO statement in the Scala language. (read more about Continuations here)

I would like to have the same in the programming language Groovy. I think it's possible within a Groovy compiler phase transformation.

I'm working on an Domain-Specific Language (DSL), and preferred embedded in Groovy. I would like to have the GOTO statement, because the DSL is an unstructured language (and is generated from workflow diagrams). I need a 'labeled' goto statement, not to line numbers.

The DSL is a language for workflow definitions, and because there are no restrictions for the arrows between nodes, a goto is needed. (or unreadable code with while etc)

As a beginner of Groovy and Scala I don't know If I can translate the Scala solution to Groovy, but I don think there are continuations in Groovy.

I'm looking for an algorithm/code for emulating labeled goto's in Groovy. One algorithm I had in mind is using eval repeatedly; doing the eval when your are at a goto. The DSL is evaluated with an eval already.

I'm not looking for a 'while' loop or something, but rather translating this code so that it works (some other syntax is no problem)

label1: 
a();
b();
goto label1; 

PS: I don't prefer the discussion if I should really use/want the GOTO statement. The DSL is a specification-language and is probably not coping with variables, efficiency etc.

PS2: Some other keyword then GOTO can be used.

解决方案

You might want to tell a little bit more about the language you are trying to build, perhaps it's simple enough that dealing with transformations would be overengineering.
Playing with the AST is something groovy people have been doing for years and it's really powerful.
The spock framework guys rewrite the tests you create annotating the code with labels. http://code.google.com/p/spock/

Hamlet D'Arcy has given several presentations on the matter. Several posts can also be found on his blog. http://hamletdarcy.blogspot.com/
Cedric Champeau describes an interesting transformation he built and its evolution http://www.jroller.com/melix/

Probably missing lots of other guys but those I remember.
A possible starting points that you probably already know but are really useful. http://groovy.codehaus.org/Compile-time+Metaprogramming+-+AST+Transformations
http://groovy.codehaus.org/Building+AST+Guide

Long story short, I'd say its quite possible

这篇关于Groovy中的GOTO语句如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆