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

Parsing negative numbers a-la ruby, closes #383 and #382 #385

Merged
merged 1 commit into from
Jul 28, 2020

Conversation

odino
Copy link
Collaborator

@odino odino commented Jul 28, 2020

This bugfix introduces negative numbers into our parser:
before, -10 would be parsed as (minus, number) whereas
right now it's going to be parsed as (number) only.

This fixes a bug with precedences, where -1.str()
would be parsed as (-, (1, str)), leading to first
calling the str method on the positive number, then
applying the minus.

Before:

⧐  -1.234.str()
ERROR: unknown operator: -STRING
	[1:1]	-1.234.str()

After:

⧐  -1.234.str()
-1.234

If a space is found between the minus and the number,
the old parsing mechanism still applies.

I found inspiration in Ruby, where:

$ - 1.to_s()
-@: undefined method `-@' for "1":String

$-1.to_s()
-1

$ -10.3.floor()
-11

$ - 10.3.floor()
-10

@odino odino added the bug Something isn't working label Jul 28, 2020
@odino odino added this to the 2.2.x milestone Jul 28, 2020
@odino odino force-pushed the fix-negative-precedence branch from 751235a to 65a2c6d Compare July 28, 2020 14:18
This bugfix introduces negative numbers into our parser:
before, -10 would be parsed as (minus, number) whereas
right now it's going to be parsed as (number) only.

This fixes a bug with precedences, where `-1.str()`
would be parsed as (-, (1, str)), leading to first
calling the str method on the positive number, then
applying the minus.

Before:

```
⧐  -1.234.str()
ERROR: unknown operator: -STRING
	[1:1]	-1.234.str()

```

After:

```
⧐  -1.234.str()
-1.234
```

If a space is found between the minus and the number,
the old parsing mechanism still applies.

I found inspiration in Ruby, where:

```
$ - 1.to_s()
-@: undefined method `-@' for "1":String

$-1.to_s()
-1

$ -10.3.floor()
-11

$ - 10.3.floor()
-10
```
@odino odino changed the base branch from master to 2.2.x July 28, 2020 14:19
@odino odino force-pushed the fix-negative-precedence branch from 65a2c6d to be12045 Compare July 28, 2020 14:19
@odino odino merged commit 80a1495 into 2.2.x Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant