chiroptical / snail

A programming language for gastropods
MIT License
12 stars 1 forks source link

Chars before SExprs are only parsed if the SExpr is at the top level #9

Closed 0xmycf closed 4 months ago

0xmycf commented 4 months ago

Hello! Given this piece of code:

'(test)

(test '(inside))

#(different-char)

(different-char/2 #(inside))

I'll show the third and fourth expr as examples:

SExpression (Just '#') Round [Lexeme (SourcePos {...},"different-char")]

SExpression Nothing Round [
                 Lexeme (SourcePos {...},"different-char/2"),Lexeme (SourcePos {...},"#") <--------------- here

                       Shouldnt it be (Just '#')?                             <--------------- and here
                             vvvvvvv
               , SExpression Nothing Round [Lexeme (SourcePos {...},"inside")]]

I think this is because leaves tries lexeme before it tries sExpression

leaves :: Parser SnailAst
leaves = lexeme <|> textLiteral <|> sExpression

I switched this around in a local clone:

leaves :: Parser SnailAst
leaves = try sExpression <|> lexeme <|> textLiteral 

This needs a try though (imagine something like @,(foo ...).

But with this change I get:

SExpression (Just '#') Round [Lexeme (SourcePos {...},"different-char")]
                                                                                       vvvvvv
SExpression Nothing Round [Lexeme (SourcePos  {...},"different-char/2"),SExpression (Just '#') Round [Lexeme (SourcePos {...},"inside")]]

Which is what I had expected.

I am not super familiar with the grammar of LISP and its dialects, so maybe this is intended.

Thank you for creating this though, its gonna be super handy for me in the future!

chiroptical commented 4 months ago

Nice find! Let me get a test in place and try your suggestion.

chiroptical commented 4 months ago

Yeah, I think you are on to something for sure. My biggest concern is that this will break other stuff but let's see how the test turns out. It is possible that I should just not allow # to lead lexemes.

As for @,(foo ...) I am also kind of curious how this parses. I can't recall if @, is a valid lexeme in snail. It only supports a single leading character for s-expressions for sure.

chiroptical commented 4 months ago

What do you think about #10? I think it will be tricky to make (@,(foo)) parse as ["@", "foo"] because @, is technically a valid lexeme right now.

0xmycf commented 4 months ago

Yes I can see the issue with this. I think this is fine.

The leading double char thing is a bit weird, but probably ok. It would require a bit of a rewrite here and there and probably overcomplicate things. How it works now is how I would have expected it to work!

This is not really related to this issue, but when setting up a project for testing I saw that snail is marked as broken on nixpkgs. Not sure if thats something you can do anything about.

Thank you for the fast response!

chiroptical commented 4 months ago

This is not really related to this issue, but when setting up a project for testing I saw that snail is marked as broken on nixpkgs. Not sure if thats something you can do anything about.

Do you mind opening up a ticket for this actually? I am unsure how easy it is to fix that.