4

I'm writing a Java program that requires its (technical) users to write scripts that it uses as input; it interprets these scripts into a series of actions and executes them. I am currently looking for the cleanest way to implement the script/configuration language. I was originally thinking of heading down the XML route, but the nature of the required input really is a procedural, linear flow of actions that need to be executed:

function move(Block b, Position p) {
    // user-defined algorithm for moving block "b" to position "p"
}

Block a = getBlockA();
Position p = getPositionP();

move(a, p);

Etc. Please note: the above is an example only and does not constitute the exact syntax I am looking to achieve. I am still in the "30,000 ft view"-design phase, and don't know what my concreted scripting language will ultimately look like. I only provide this example to show that it is a flow/procedural script that the users must write, and that XML is probably not the best candidate for its implementation.

XML, perfect for hierarchial data, just doesn't feel like the best choice for such an implementation (although I could force it to work if need-be).

Not knowing a lick about DSLs, I've begun to read up on Groovy DSLs and they feel like a perfect match for what I need.

My uderstanding is that I could write, say, a Groovy (I'm stronger in Groovy than Scala, JRuby, etc.) DSL that would allow users to write scripts (.groovy files) that my program could then execute as input at runtime.

Is this correct, or am I misunderstanding the intent of DSLs altogether? If I am mistaken, does anybody have any suggestions for me? And if I am correct then how would a Java program read and execute a .groovy file (in other words, how would my program "consume" their script)?

Edit: I'm beginning to like ANTLR. Although I would love to roll up my sleeves and write a Groovy DSL, I don't want my users to be able to write any old Groovy program they want. I want my own "micro-language" and if users step outside of it I want the interpreter to invalidate the script. It's beginning to seem like Groovy/DSLs aren't the right choice, and maybe ANTLR could be the solution I need...?

11
  • You may also want to have a look at Beanshell. Commented Jul 14, 2012 at 13:16
  • 1
    @HovercraftFullOfEels, it's rather old-school, and (AFAIK) not actively being developed on. But it may serve Zac's purpose: the example code he posted can pretty much be copy-pasted and used as a Beanshell script and used in a Java application. Commented Jul 14, 2012 at 13:45
  • 1
    Have a look at Xtext, it is very pleasant to write DSLs with it (and it is actively developed!) Commented Jul 14, 2012 at 14:04
  • 1
    @Zac You seem to be (faintly) mistaken about the purpose of DSLs. They're mostly used to implement a declarative-ish specialised language (e.g. for configuration) on top of a mainly procedural language. That's not to say you're on the wrong path. If your users are going to write mostly procedural code and you just need to have a sane way to load / evaluate it at runtime, you can use straight Groovy and a binding to your Java code. Commented Jul 14, 2012 at 14:17
  • 1
    @ZacHarvey They'd be regular Groovy. However, so would a Groovy-based DSL. If you want restricted execution, you need a language / environment designed for it. Lua is generally a great choice for that, but I don't know how easy it is to integrate with Java. (Another option might be using the Java code security stuff, but I have no idea how that works or if it can implement the kind of restrictions you need.) Commented Jul 15, 2012 at 11:48

2 Answers 2

1

I think you are on a really good path. Your users can write their files using your simple DSL and them you can run them by Evaling them at runtime. Your biggest challenge will be helping them to use the API of your DSL correctly. Unless they use an IDE this will be pretty tough.

Equivalent of eval() in Groovy

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can write a Groovy program that will accept a script as input and execute it. I recently wrote a BASIC DSL/interpreter in this way using groovy :

http://cartesianproduct.wordpress.com/binsic-is-not-sinclair-instruction-code/

(In the end it was more interpreter than DSL but that was to do with a peculiarity of Groovy that likely won't affect you - BASIC insists on UPPER CASE keywords which Groovy finds hard to parse - hence they have to be converted to lower case).

Groovy allows you to extend the script environment in various ways (eg injecting variables into the binding and transferring execution from the current script to a different, dynamically loaded script) which make this relatively simple.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.