1

I am trying to write a BASIC-like DSL using Groovy and I am at a very early stage. I have a short script (ignore the package bit, I will refactor that away in due course):

package Binsic
PRINT "Hello World"

and this class:

package Binsic

abstract class BinsicInterpreter extends Script {

static def textArea

static def setTextArea(def window)
{
    textArea = window
}

def PRINT(def param) {
    textArea.append param
}

}

called in this way:

def engine = new BinsicEngine()
BinsicInterpreter.setTextArea(engine.binsicWindow.screenZX)
def conf = new CompilerConfiguration()
conf.setScriptBaseClass("BinsicInterpreter")
def shell = new GroovyShell(conf)
shell.evaluate(new File("./src/Binsic/test.bas"))

(BinsicEngine just sets the TextArea up at the moment)

This code fails...

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: /Users/adrian/Documents/workspace-sts-2.9.1.RELEASE/BINSIC/src/Binsic/test.bas: 3: unexpected token: Hello World @ line 3, column 7. PRINT "Hello World" ^

1 error

But if I change the statement to PRINT ("Hello World") it works...

Similarly, I can get PRINT this to work (i.e. it prints the memory reference for this) if I adjust the PRINT code to handle non-strings. But no brackets are required.

Why won't the unbracketed version work? And how can I fix this?

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
  • Incidentally, for now (and perhaps for ever) I am going to get round this by writing a preprocessor class that will convert the input script into something Groovy can process comfortably. See https://github.com/mcmenaminadrian/BINSIC – adrianmcmenamin Jun 04 '12 at 13:52

2 Answers2

1

The problem is the upper-case PRINT - or upper-case first letter anything (such as Print).

In Groovy, omitting parentheses is syntactical sugar to provide better DSL support. The compiler will have a set of rules on when it is allowed and when not.

In my tests

def Print(String arg) {
    println arg
}
def a = Print "Hello World"

works, while

def Print(String arg) {
    println arg
}
Print "Hello World"

fails as you discovered. I suggest raising it as an issue on http://groovy.codehaus.org/.

Paul Marrington
  • 557
  • 2
  • 7
1

It seems to have something to do with the PRINT method being upper-cased. Change it from 'PRINT' to 'foo', and it works. Change it to 'FOO', and it doesn't work.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67