Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

how did you handle the ambiguous syntax, i'm writing a logo interpreter right now? having a lot of problems with the inline command syntax.


It's been a while but, from memory, the parts that caused me the most grief were infix operators, unary minus and statements like IF with a variable number of parameters.

I parsed the text into nested lists of Nodes where each node could be a literal or "word" value, a [ ] list or a ( ) grouping. The interpreter treats the nodes as a stream to be consumed one at a time.

When the interpreter is expecting a call to a procedure the next node in the stream should be a word. It can then look up the procedure in a dictionary of built in and user defined procedures. The list of parameters to the procedure indicates how many additional nodes to evaluate when building the values to pass to it.

In the case of variable parameters like a call to SUM you can express it as "SUM 10 20" for the minimum parameters or "(SUM 10 20 30)" for a variable number. The grouping indicates the first element, SUM, is the proc and all the other values are parameters. You could also express it as "10 + 20 + 30" which is evaluated as either "(SUM 10 (SUM 20 30))" or "(SUM (SUM 10 20) 30)" - I can't remember off the top of my head!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: