Skip to content

Conversation

Danil-Zaripov
Copy link
Contributor

Compiler was failing in cases let f x=x-1, where it would compile it as let f x = x (-1) treating x as a function, when it should certainly be treated as a number. This was due to a lexer bug.

The fix is that lexer only matches negative values if there could not be a variable name(letters, numbers, _, ') or a number behind it.

Change lexer so it does not match negative numbers if there is a
variable or a number(letter or digit) behind it
Forgot the "_" and "'" which may be in the variable/digit names
@gsvgit gsvgit requested a review from WoWaster October 2, 2025 11:02
Copy link
Member

@WoWaster WoWaster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use such complicated lexer construction? I'd better simplify lexer with this change rather than complicating it more.

I propose a way similar to OCaml.
At first we generate separate tokens for minus and a number (as Text).

https://github.com/ocaml/ocaml/blob/f024962042c57a257f4367fbadbcb8d3428c3895/parsing/lexer.mll#L552

Then in the parser prepend number with - if we see TokMinus before TokInt.

https://github.com/ocaml/ocaml/blob/f024962042c57a257f4367fbadbcb8d3428c3895/parsing/parser.mly#L4004

OCaml then converts string to int during typechecking phase, but we can just read inside parser rule for integer constant.

Now negative values are handled inside the parser. Lexer is changed not
to scan negative values.
@Danil-Zaripov Danil-Zaripov requested a review from WoWaster October 6, 2025 17:18
@gsvgit gsvgit merged commit e43edb3 into Lamagraph:main Oct 10, 2025
6 checks passed
@Danil-Zaripov Danil-Zaripov deleted the minus branch October 10, 2025 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants