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?
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
works, while
fails as you discovered. I suggest raising it as an issue on http://groovy.codehaus.org/.