Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How GOTO statement in Groovy?

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.

like image 510
Julian Avatar asked Oct 11 '25 14:10

Julian


1 Answers

One approach is to use Groovy AST transformations. It's a big hammer and might be overengineering for the language you're trying to build. 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. https://github.com/spockframework/spock

Hamlet D'Arcy has given several presentations on the matter. Several posts can also be found on his blog. http://hamletdarcy.blogspot.com/

Useful starting points:

  • Groovy AST Transformations blog post.
  • Groovy Runtime and compile-time metaprogramming

Long story short, I'd say its quite possible.

like image 95
jpertino Avatar answered Oct 14 '25 09:10

jpertino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!