How to use script for Java program

I am writing a Java program that requires its (technical) users to write scripts that it uses as input; he interprets these scenarios in a sequence of actions and executes them. I'm currently looking for the cleanest way to implement the script / language. Initially, I was thinking of moving along an XML route, but the nature of the required input is actually a procedural, linear flow of actions that needs to be performed:

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 example does not constitute the exact syntax I'm looking for. I am still in the “30,000 feet” phase, and I don’t know what my specific scripting language will look like. I only provide this example to show that this is a thread / procedural script that users should write, and that XML is probably not the best candidate for its implementation.

Perfect for hierarchical data, XML just doesn't seem to be the best choice for such an implementation (although I can get it working if necessary).

Unaware of DSL licking, I started reading Groovy DSL discs and they feel perfect for what I need.

I'm sure I can write, say, Groovy (I'm stronger in Groovy than Scala, JRuby, etc.) DSL, which allows users to write scripts ( .groovy files) that my program could run as input at runtime.

Is this correct, or am I misunderstanding the purpose of DSL? If I'm wrong, does anyone have any suggestions for me? And if I'm right, how will the Java program read and execute the .groovy file (in other words, how .groovy my program “consume” their script)?

Change I like ANTLR. Although I would like to roll up my sleeves and write DSL Groovy, I do not want my users to be able to write any old Groovy program that they want. I want my own “micro language”, and if users go beyond it, I want the interpreter to invalidate the script. It starts to seem that Groovy / DSL is not the right choice, and perhaps ANTLR might be the solution I need ...?

+4
source share
2 answers

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

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

(In the end, it was more of an interpreter than DSL, but this is due to a Groovy feature that probably won't affect you - BASIC insists on UPPER CASE keywords that Groovy finds are hard to parse - hence they need to be converted to lower case).

Groovy allows you to expand your script environment in various ways (for example, insert variables into a binding and pass execution from the current script to another dynamically loaded script), which makes this relatively simple.

0
source

I think you're really a good way. Your users can write their files using your simple DSL, and you can run them by evaluating them at runtime. Your biggest problem will help them use your DSL APIs correctly. If they do not use the IDE, it will be quite difficult.

Equivalent to eval () in Groovy

+1
source

All Articles