Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove block comments #33

Closed
brendanzab opened this issue Apr 8, 2018 · 7 comments
Closed

Remove block comments #33

brendanzab opened this issue Apr 8, 2018 · 7 comments
Assignees

Comments

@brendanzab
Copy link
Member

brendanzab commented Apr 8, 2018

PR #32 introduced block comments to the lexer. It would be nice if these nested too! This would then admit the following string:

{- 
    {- ... -} 
    {- 
       {- ... -} 
    -}
-}

We have decided instead to remove the block comments entirely!

@brendanzab
Copy link
Member Author

Not sure the best approach to this. Perhaps we need a 'seen bracket' counter that we increment and decrement in the lexer?

Or perhaps (as I mentioned in gitter) we could just remove block comments! This is what Futhark has done.

@adrianwong
Copy link
Member

I'm currently writing up a Brainfuck interpreter in Haskell, and this was the exact same issue I ran into.

If we take the approach of incrementing a counter on an open brace, and decrementing it on a close brace (and expect the end value to be 0), we don't account for the fact that this case {- -} -} {- will be considered valid.

What I eventually ended up doing is make use of a stack to keep track of the braces:

  • Push a token onto the stack on an open brace
  • Pop the topmost token on a close brace
  • If we pop an empty stack, raise an error
  • If the stack is non-empty at the end of the lexing process, raise an error

This takes care of the cases that the counter doesn't.

I'll try to put forward a PR and you can decide if it's really too messy to be worth it. But I think I prefer the idea of removing block comments (or at the very least, not nesting them).

@brendanzab
Copy link
Member Author

Let's just remove them then! Sorry for the wasted effort - I guess it helped to get an idea of what the lexer looks like though, hopefully.

@adrianwong
Copy link
Member

Nah, all good! Yeah it was quite informative. Also, by attempting to hack in an implementation for this, I got to learn a bit about Option and Vec and pattern matching in Rust.

If you'd like, I can submit a PR to remove support for block comments.

@brendanzab
Copy link
Member Author

Sounds great! Yeah, it's not too dissimilar to Maybe in Haskell. Vec is like std::vector in C++.

@brendanzab brendanzab changed the title Nested block comments Remove block comments Jun 13, 2018
@adrianwong
Copy link
Member

I just realised that my post about using a stack in favour of a counter makes no sense... We could just raise an error if we're decrementing when the counter is 0.

I was fairly certain that there was an edge case which I was accounting for, but now I feel a bit foolish...

@brendanzab
Copy link
Member Author

Easy mistake to make if your nose is right next to the problem! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants