I've looked through several documents explaining the process of scanning, lexing, parsing, and executing hierarchal commands from a string of characters. They typically break each of these steps up into separate components, and pass data structures from one component to the next, but for my first implementation, I'm trying to come up with a combined solution, for simplicity.
So, after working through a few ideas on paper, I think I've found one that may work for what I need in the short term. Here is a summary of what I'm trying to accomplish:
Take a string, such as:
Code: Select all
Pci.GetDevices().Skip(Integer.Parse("1")).First()
So, my current idea is a single-pass, dual-stack setup.
The first stack is for "commands" and the second stack is for "arguments". As the string is scanned, character by character, special characters will trigger certain events. For example, the period "." character will take the previous string, and dynamically find classes or methods by name, and push them on the command stack. The open parentheses "(" and comma "," characters will start pushing strings and integers onto the arguments stack. The close parenthesis ")" character will pull the command off of the command stack, and pass it the argument stack, and any return value will be pushed onto the argument stack. Any commands after that will use the top of the argument stack to find the correct class, and push it to the command stack, and then push the next command onto the command stack, and so on, and so on.
I will also need a similar procedure that I could use for things like autocomplete, which will walk through the classes and methods, but not execute them.
If this works, I'll eventually move this code to its own (reusable) function that will simply return a list of commands to execute in the correct order, but for now, I'll probably hard-code this into the console application.
Let me know if anyone sees any reason why this would not work, or if my description above is too hard to follow, I'll try to elaborate.
EDIT: Slight correction/clarification...
Numbers and quoted "" strings (separated by commas) will be pushed directly to the argument stack.