Skip to content

Commit 2aec311

Browse files
committed
Fix exponent operator precedence
Now -2^2 == -4
1 parent feedcc8 commit 2aec311

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

parser/parser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ type builtin struct {
3131
var unaryOperators = map[string]operator{
3232
"not": {50, left},
3333
"!": {50, left},
34-
"-": {500, left},
35-
"+": {500, left},
34+
"-": {90, left},
35+
"+": {90, left},
3636
}
3737

3838
var binaryOperators = map[string]operator{
@@ -58,7 +58,7 @@ var binaryOperators = map[string]operator{
5858
"*": {60, left},
5959
"/": {60, left},
6060
"%": {60, left},
61-
"**": {70, right},
61+
"**": {100, right},
6262
}
6363

6464
var builtins = map[string]builtin{

parser/parser_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ func TestParse(t *testing.T) {
5959
"-3",
6060
&ast.UnaryNode{Operator: "-", Node: &ast.IntegerNode{Value: 3}},
6161
},
62+
{
63+
"-2**2",
64+
&ast.UnaryNode{
65+
Operator: "-",
66+
Node: &ast.BinaryNode{
67+
Operator: "**",
68+
Left: &ast.IntegerNode{Value: 2},
69+
Right: &ast.IntegerNode{Value: 2},
70+
},
71+
},
72+
},
6273
{
6374
"1 - 2",
6475
&ast.BinaryNode{Operator: "-", Left: &ast.IntegerNode{Value: 1}, Right: &ast.IntegerNode{Value: 2}},

0 commit comments

Comments
 (0)