I am using Groovy to write a DSL handling BASIC and I would like some assistance with how to handle multi (more than 2) dimensional arrays.
I am dealing with BASIC code like this:
100 LET X = A(1, 2, 3)
It is easy to handle the 1 dimensional case - just create a closure (via MOP) that returns the elements of A, while for 2 dimensions I can do the same in the form
A(2, 3) == A.get(2)[3]
But how do I handle arrays of unlimited dimensions?
Update: To make this a bit clearer, the question is how can I return the array values dynamically in the DSL context? The script interpreter sees A(1, 2, 3) as a function call which I can intercept using the MOP. But how do I return the value of the array element in that context?
In the end I decided to parse the input and use that to build a closure via the MOP:
/* array references look like functions to Groovy so trap them */
BinsicInterpreter.metaClass."$varName" = {Object[] arg ->
def answer = "package binsic; $varName"
arg.each {
answer = answer + "[$it]"
}
def something = shell.evaluate(answer)
return something
}
So if we had:
100 LET X = A(10, 20, 3)
The MOP traps A(...) as a function call and the code above gives me A[10][20][3]