I'm trying to write a 'wizard' for users to make it easier for them generate Python logic, which in this app is basically a bunch of if statements to implement business logic.
The users aren't developers and asking them to write Python won't go down well. One option that sprung to mind was to let them use a tiny subset of English to generate the business logic, and then write a tool to parse and convert it to Python.
The business logic is contains filters (if statements) and modifiers (modifying data, obviously!).
Here's a simple example:
The English for the filter would be:
person.name is "fred" and person.age is 25 and person.address contains "canada"
The tool would convert this to Python:
if person.get_value("name") == "fred" and person.get_value("age") == 25 and person.get_status().find("canada") > -1:
The English for the modifier would be:
person.salary to 20000 and person.status to "hired"
Which would be converted to:
person.set_value("salary", 20000)
person.set_value("status", "hired")
Hopefully that makes sense.
I started writing my tool to perform the filter conversion. I parse the string andI break it down into tokens for each possible English word, so:
TOKEN_AND
TOKEN_STRING
TOKEN_VARIABLE (e.g. person)
etc.
I've also added logic to determine which tokens can precede or follow other tokens and that works nicely.
But what's next? How do I take the list (stream I guess) of tokens and convert it into Python? Do I turn the stream into a tree, where, for example, TOKEN_AND is a branch, and TOKEN_STRING is a leaf? Then traverse the tree to generate the Python? I tried this, but I couldn't get it working.
Any suggestions?
Many thanks