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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.2.2
2 changes: 2 additions & 0 deletions evaluator/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ func TestBetween(t *testing.T) {
{`1.between(0, 0.9)`, false},
{`1.between(1, 0)`, "arguments to between(min, max) must satisfy min < max (1 < 0 given)"},
{`1.between(1, 2)`, true},
{`-1.between(-10, 0)`, true},
{`-1.between(-10, -2)`, false},
}

testBuiltinFunction(tests, t)
Expand Down
7 changes: 7 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func (l *Lexer) NextToken() token.Token {
tok.Position = l.position
tok.Literal = "-="
l.readChar()
} else if isDigit(l.peekChar()) {
tok.Position = l.position
l.readChar()
literal, kind := l.readNumber()
tok.Type = kind
tok.Literal = "-" + literal
return tok
} else {
tok = l.newToken(token.MINUS)
}
Expand Down
12 changes: 12 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
func TestNextToken(t *testing.T) {
input := `five = 5;
ten = 10;
-10;
- 10;
1 - 2;
add = f(x, y) {
x + y;
};
Expand Down Expand Up @@ -127,6 +130,15 @@ f hello(x, y) {
{token.ASSIGN, "="},
{token.NUMBER, "10"},
{token.SEMICOLON, ";"},
{token.NUMBER, "-10"},
{token.SEMICOLON, ";"},
{token.MINUS, "-"},
{token.NUMBER, "10"},
{token.SEMICOLON, ";"},
{token.NUMBER, "1"},
{token.MINUS, "-"},
{token.NUMBER, "2"},
{token.SEMICOLON, ";"},
{token.IDENT, "add"},
{token.ASSIGN, "="},
{token.FUNCTION, "f"},
Expand Down
1 change: 0 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ func (p *Parser) parsePrefixExpression() ast.Expression {
}

p.nextToken()

expression.Right = p.parseExpression(PREFIX)

return expression
Expand Down
6 changes: 5 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestParsingPrefixExpressions(t *testing.T) {
value interface{}
}{
{"!5;", "!", 5},
{"-15;", "-", 15},
{"- 15;", "-", 15},
{"!foobar;", "!", "foobar"},
{"-foobar;", "-", "foobar"},
{"!true;", "!", true},
Expand Down Expand Up @@ -310,6 +310,10 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
},
{
"3 + 4; -5 * 5",
"(3 + 4)(-5 * 5)",
},
{
"3 + 4; - 5 * 5",
"(3 + 4)((-5) * 5)",
},
{
Expand Down