Skip to content
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 docs/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ comprehension = "for" Ident { "," Ident } "in" expression
slice = "[" [ expression ] [ ":" expression ] "]";
lambda = "lambda" [ lambda_arg { "," lambda_arg } ] ":" expression;
lambda_arg = Ident [ "=" expression ];
operator = ("+" | "-" | "*" | "/" | "%" | "<" | ">" | "and" | "or" |
operator = ("+" | "-" | "*" | "/" | "//" | "%" | "<" | ">" | "and" | "or" |
"is" | "is" "not" | "in" | "not" "in" | "==" | "!=" | ">=" | "<=" | "|");
5 changes: 4 additions & 1 deletion src/parse/asp/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ const (
Multiply Operator = '×'
// Divide implements division, currently only between integers
Divide Operator = '÷'
// FloorDivide implements floor division, currently only between integers
FloorDivide Operator = '⑊'
// Modulo implements % (including string interpolation)
Modulo Operator = '%'
// Negate is the unary negation operator (not exactly the same as Subtract)
Expand Down Expand Up @@ -339,7 +341,7 @@ func (o Operator) Precedence() int {
switch o {
case Negate:
return 4
case Multiply, Divide, Modulo:
case Multiply, Divide, FloorDivide, Modulo:
return 3
case Add, Subtract:
return 2
Expand All @@ -366,6 +368,7 @@ var operators = map[string]Operator{
"-": Subtract,
"*": Multiply,
"/": Divide,
"//": FloorDivide,
"%": Modulo,
"<": LessThan,
">": GreaterThan,
Expand Down
12 changes: 12 additions & 0 deletions src/parse/asp/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ func TestDivide(t *testing.T) {
assert.EqualValues(t, -2, s.Lookup("k"))
}

func TestFloorDivide(t *testing.T) {
s, err := parseFile("src/parse/asp/test_data/interpreter/floor_divide.build")
assert.NoError(t, err)
assert.EqualValues(t, 2, s.Lookup("a"))
assert.EqualValues(t, -2, s.Lookup("b"))
assert.EqualValues(t, 0, s.Lookup("c"))
assert.EqualValues(t, -1, s.Lookup("d"))
assert.EqualValues(t, 0, s.Lookup("e"))
assert.EqualValues(t, 1, s.Lookup("f"))
assert.EqualValues(t, 5, s.Lookup("g"))
}

func TestFStringOptimisation(t *testing.T) {
s, stmts, err := parseFileToStatements("src/parse/asp/test_data/interpreter/fstring_optimisation.build")
require.NoError(t, err)
Expand Down
10 changes: 9 additions & 1 deletion src/parse/asp/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,15 @@ func (l *lex) nextToken() Token {
return Token{Type: LexOperator, Value: string([]byte{next, l.bytes[l.pos-1]}), Pos: pos}
}
fallthrough
case ',', '.', '%', '*', '|', '&', ':', '/':
case ',', '.', '%', '*', '|', '&', ':':
return Token{Type: rune(next), Value: string(next), Pos: pos}
case '/':
// Look ahead one byte to see if this is a floor division.
if l.bytes[l.pos] == '/' {
l.pos++
l.col++
return Token{Type: LexOperator, Value: string([]byte{next, l.bytes[l.pos-1]}), Pos: pos}
}
return Token{Type: rune(next), Value: string(next), Pos: pos}
case '#':
// Comment character, consume to end of line.
Expand Down
3 changes: 3 additions & 0 deletions src/parse/asp/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"iter"
"math"
"slices"
"sort"
"strconv"
Expand Down Expand Up @@ -221,6 +222,8 @@ func (i pyInt) Operator(operator Operator, operand pyObject) pyObject {
return i * o
case Divide:
return i / o
case FloorDivide:
return newPyInt(int(math.Floor(float64(i) / float64(o))))
case LessThan:
return newPyBool(i < o)
case GreaterThan:
Expand Down
9 changes: 9 additions & 0 deletions src/parse/asp/test_data/interpreter/floor_divide.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
a = 4 // 2
b = 4 // -2
c = 0 // 2
d = -1 // 2
e = -1 // -2

# Precedence tests
f = 8 / 4 // 2 # (8 / 4) // 2: / and // have the same precedence
g = 4 + 3 // 2 # 4 + (3 // 2): // has greater precedence than +
Loading