How to write script input of Java program
I am writing a java program that requires its (technical) users to write scripts for input; It interprets these scripts as a series of actions and executes them I am currently looking for the simplest way to implement the scripting / configuration language I originally intended to follow the XML route, but the essence of the required input is actually a procedural linear action process 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);
wait. Please note that the above is just an example and does not constitute the exact syntax i want to implement I'm still in the "30000 foot view" design stage and don't know what my concrete scripting language will eventually look like I only provide this example to show that it is a flow / process script that users must write, and that the XML may not be the best candidate for its implementation
XML, which is very suitable for hierarchical data, feels not the best choice for such an implementation (although I can force it to work when needed)
I don't know about DSL licking. I've started reading groovy DSL. They think it's the perfect match I need
My understanding is that I can write a groovy (I'm stronger in groovy than Scala, jruby, etc.) DSL that allows users to write scripts (. Groovy files), and then my program can be executed as input at run time
Is this correct, or do I completely misunderstand the intention of DSL? If I make a mistake, does anyone have any suggestions for me? If I'm right, how will the Java program read and execute Groovy files (in other words, how will my program "consume" their scripts)?
Editor: I'm beginning to like ANTLR Although I'd 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 programs they want I want my own microlanguage. If the user walks out of it, I want the interpreter to invalidate the script It seems that groovy / DSL is not the right choice. Maybe ANTLR may be the solution I need?
Solution
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:
Binsic Is Not Sinclair Instruction Code
(finally, it has more explanations than DSL, but it is related to groovy's features and may not affect you – basic insists on using groovy's difficult to parse upper case keywords – so they must be converted to lowercase)
Groovy allows you to extend the scripting environment in various ways (for example, injecting variables into bindings and transferring execution from the current script to another dynamically loaded script), which makes this relatively simple