nixpulvis / brainfuck

A simple brainfuck interpreter in Rust.
http://nixpulvis.com/brainfuck
23 stars 3 forks source link

Refactor Parse Algorithm to Use One Pass #19

Closed offlinemark closed 8 years ago

offlinemark commented 8 years ago

Figured there might be feedback, best to submit for review

nixpulvis commented 8 years ago

@mossberg nice cleanup. I'm not sure why I made a hash table for this, this is clearly better.

nixpulvis commented 8 years ago

@mossberg I can add a few tests to this branch if it would help make clear the interface and expectations of the parse function. There are a few cases to consider with the matching brackets.

offlinemark commented 8 years ago

@nixpulvis i could give a shot at that

the parse cases I can think of:

if I add an Error:InvalidProgram, seems like parse should return a Result<Program, InvalidProgram>?

nixpulvis commented 8 years ago

Yea exactly.

nixpulvis commented 8 years ago

Actually Result<Program, Error> but the right idea, since Error::InvalidProgram itself isn't s type.

offlinemark commented 8 years ago

actually this will break in regards to comment blocks. it will count comment brackets as actual code

nixpulvis commented 8 years ago

There is no such thing as a comment bracket, they are just brackets that will always skip over. See #20 for more about skipping irrelevant code.

offlinemark commented 8 years ago

right, but as of now, the "syntax analysis" in parse treats everything as code, even if it doesn't matter during runtime.

i guess that's an implementation decision. does incidental brainfuck code in a "comment" block need to be syntactically accurate? i feel like the "spirit" of brainfuck would say that the interpreter should not care.

example

[this is my comment block. this single bracket -> ] will cause an imbalance in brackets and cause an invalid program error, even though during execution, it's fine
]
+++[-,.]
nixpulvis commented 8 years ago

I see what you are saying now. Well, I'd guess most brainfuck interpreters don't preproces. You could try to find one that died and see what it does. For now until we have work done for #20 I'd say it's fine to require matching brackets in comments.

offlinemark commented 8 years ago

@nixpulvis if there are no other comments, i'll go ahead and merge on your ok

nixpulvis commented 8 years ago

LGTM