Groovy を使用して BASIC のような DSL を作成しようとしていますが、まだ非常に初期の段階です。私は短いスクリプトを持っています (パッケージのビットは無視してください。そのうちリファクタリングします):
package Binsic
PRINT "Hello World"
そしてこのクラス:
package Binsic
abstract class BinsicInterpreter extends Script {
static def textArea
static def setTextArea(def window)
{
textArea = window
}
def PRINT(def param) {
textArea.append param
}
}
次のように呼び出されます。
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 は現時点で TextArea を設定するだけです)
このコードは失敗します...
org.codehaus.groovy.control.MultipleCompilationErrorsException: 起動に失敗しました: /Users/adrian/Documents/workspace-sts-2.9.1.RELEASE/BINSIC/src/Binsic/test.bas: 3: 予期しないトークン: Hello World @ 行 3 、列 7. PRINT "Hello World" ^
1 エラー
しかし、ステートメントを PRINT ("Hello World") に変更すると機能します...
同様に、非文字列を処理するように PRINT コードを調整すると、PRINT this を機能させることができます (つまり、this のメモリ参照を出力します)。ただし、ブラケットは必要ありません。
ブラケットなしのバージョンが機能しないのはなぜですか? どうすればこれを修正できますか?