Julian
Julian

Reputation: 36800

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.

Upvotes: 4

Views: 10235

Answers (4)

jpertino
jpertino

Reputation: 2219

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:

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

Upvotes: 5

corsiKa
corsiKa

Reputation: 82579

Just throwing this out there, perhaps you could have a scoped switch case

So if your DSL says this:

def foo() {
   def x = x()
   def y
   def z
   label a:
     y = y(x)
   if(y < someConst) goto a
   label b: 
    z = y(z)
    if(z > someConst) goto c
    x = y(y(z+x))
    z = y(x)
   label c:
    return z; 
}

Your "compiler" can turn it into this:

def foo() {
    String currentLABEL = "NO_LABEL"
    while(SCOPED_INTO_BLOCK_0143) {
       def x
       def y
       def z
       def retval
       switch(currentLABEL) {
       case "NO_LABEL":
          x = x()
       case "LABEL_A"
          y = y(x)

          if(y < someConst) {
            currentLABEL = "LABEL_A"
           break
          }
       case "LABEL_B"
          z = y(z)

          if(z > someConst) {
            currentLabel = "LABEL_C"
            break
          }
          x = y(y(z+x))
          z = y(x)
       case "LABEL_C"
          SCOPED_INTO_BLOCK_0143 = false
          retval = z
       }
    }
    return retval
}

Upvotes: 1

CodesInChaos
CodesInChaos

Reputation: 108840

You can emulate if and goto with while loops. It won't be pretty, it will introduce lots of unnecessary code-blocks, but it should work for any function. There is some proof that this is always possible to rewrite code like that, but of course being possible does not mean it's nice or easy.

Basically you move all local variables to the beginning of the function and add a bool takeJump local variable. Then add a while(takeJump){+} pair for any goto+label pair and set the flag before the while and before the end of the while to the value you want.

But to be honest I don't recommend that approach. I'd rather use a library that allows me to build an AST with labels and gotos and then translates that directly to byte-code.

Or use some other language built on the java vm that does support goto. I'm sure there is such a language.

Upvotes: 1

tim_yates
tim_yates

Reputation: 171154

You won't get anywhere trying this, as goto is a reserved word in Groovy (as it is in Java), so using it in your DSL will be problematic.

It's not a reserved word in Scala, so this isn't an issue

Upvotes: 1

Related Questions