I am trying to create an own code to allow a client to post something on a pixel matrix.
Here an example of a code:
15e3n:3;geo(386299n283752w):4;delta(16e3n:5;line(20e0n;20e10n;0e10n;color:d);19w55n;rectangle(50w50n;height:100;width:50));88e2n:randomColor()
I want to interpret this using functions like in this case geo, delta, line, rectangle, geo, randomColor. The things that are written between (and )are the parameters, separated by a ;. (15e3n is in this case a position “15 east, 3 north”)
Example:
function(param1:value1;param2:value2;value3;value4);function2(param1:value1)
The functions should be executed and should replace the function with the result like here:
The text 16e13n:3;line(17e13n;19e13n;color:4);15e13n:3 should result in 16e13n:3;17e13n:4;18e13n:4;19e13n:4;15e13n:3. Because the function line with params 17e13n;19e13n;color:4 have the result 17e13n:4;18e13n:4;19e13n:4;that replaced the function in the input.
How can I achieve this? What is the best way? Regex?
Are there existing examples about how to do this?
Now, this looks a bit complicated 🙂
I thank you in advance for getting help on this!
Note: content are not necessary valid PHP script
My suggestion will be:
First implement a parser to separate statements into an array. You can loop every character, hold a counter of brackets and split by
;that is outside brackets, resulting into this array:Then extract the function parameters, you may want to use objects…
Do this recursively until all functions deep inside are extracted.
Then from the depth, process the functions by replacing a
(object)funcblock into the resulting string, by aswitchblock that do appropriate action according to the name, for example:will process this:
into this:
Then at the end you will end up a single array which the contents, for example (not directly matching above):
Then re-combine them into a resulting string.
I am not going to write a full script for you, but here’s an idea of how to deal with it. This may not be complete though, but by thinking a bit you should be able to complete it.