BigEd wrote:
You should do it if you want to do it. Is it organised in such a way that you could swap out one mechanism for another? You can put up with the slow mechanism, and fill our the rest of the project, until you have some idea about how to replace it with a faster one. (It's usually true that a better algorithm is a much preferable way forward than an optimised implementation of a slow algorithm.)
Yes, after all it is always a good practice to make your code interchangeable.
John West wrote:
A more flexible but slightly slower approach might be to allow handlers to register themselves for particular token types. The main loop looks at the next token and iterates over all of the handlers for it. Each handler can have a go at parsing, either succeeding, advancing the token pointer and doing whatever work is needed, or failing, leaving the token pointer where it was for the next handler. A handler can itself be a loop calling its own set of handlers - you'd need that for operands.
Thank you, that is a good idea, i will try to work from it
Quote:
I don't know how you'd generate decent error messages with that approach though. A handler can't emit an error when it fails, because a future handler might succeed. But the caller doesn't have information on what went wrong. If multiple handlers all produce their own candidate errors, which do you choose? Error messages are hard.
For exception handling, i could loop through all template handlers and let them a go at parsing, where each handler would return a boolean if it has suceeded parsing or not.
The handlers themselves would be responsible for displaying different error/warning messages in case the match was partial(for example the instruction mnemonic matched) and the error/warning handler-specific(ex. 16bit argument instead of 8bit, etc.)
The parsing loop itself can also keep track of current line, in order to display messages.
At the end of the loop, if none of the parsers have succeeded, an error message can be displayed stating that it is unable to parse a statement at whatever line the line pointer is set to