pikelet-lang / pikelet

A friendly little systems language with first-class types. Very WIP! 🚧 🚧 🚧
https://pikelet-lang.github.io/pikelet/
Apache License 2.0
610 stars 26 forks source link

Remove block comments #33

Closed brendanzab closed 6 years ago

brendanzab commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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:

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

adrianwong commented 6 years ago

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 commented 6 years ago

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