Skip to content

Commit 971d241

Browse files
committed
Add ^ as exponent operator
1 parent 2aec311 commit 971d241

File tree

9 files changed

+14
-8
lines changed

9 files changed

+14
-8
lines changed

checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func (v *visitor) BinaryNode(node *ast.BinaryNode) (reflect.Type, info) {
282282
return anyType, info{}
283283
}
284284

285-
case "**":
285+
case "**", "^":
286286
if isNumber(l) && isNumber(r) {
287287
return floatType, info{}
288288
}

checker/checker_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var successTests = []string{
4343
"-1 + +1 == 0",
4444
"1 / 2 == 0",
4545
"2**3 + 1 != 0",
46+
"2^3 + 1 != 0",
4647
"Float == 1",
4748
"Float < 1.0",
4849
"Float <= 1.0",

compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func (c *compiler) BinaryNode(node *ast.BinaryNode) {
385385
c.compile(node.Right)
386386
c.emit(OpModulo)
387387

388-
case "**":
388+
case "**", "^":
389389
c.compile(node.Left)
390390
c.compile(node.Right)
391391
c.emit(OpExponent)

docs/Language-Definition.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ foo.Method()
4949
* `*` (multiplication)
5050
* `/` (division)
5151
* `%` (modulus)
52-
* `**` (pow)
52+
* `^` or `**` (exponent)
5353

5454
Example:
5555

5656
```
57-
x**2 + y
57+
x^2 + y^2
5858
```
5959

6060
### Comparison Operators

expr_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ func TestExpr(t *testing.T) {
619619
`2 ** 8`,
620620
float64(256),
621621
},
622+
{
623+
`2 ^ 8`,
624+
float64(256),
625+
},
622626
{
623627
`-(2-5)**3-2/(+4-3)+-2`,
624628
float64(23),

optimizer/fold.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (fold *fold) Visit(node *Node) {
8989
patch(&IntegerNode{Value: a.Value % b.Value})
9090
}
9191
}
92-
case "**":
92+
case "**", "^":
9393
if a, ok := n.Left.(*IntegerNode); ok {
9494
if b, ok := n.Right.(*IntegerNode); ok {
9595
patch(&FloatNode{Value: math.Pow(float64(a.Value), float64(b.Value))})

parser/lexer/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func root(l *lexer) stateFn {
3030
l.emit(Bracket)
3131
case strings.ContainsRune(")]}", r):
3232
l.emit(Bracket)
33-
case strings.ContainsRune("#,?:%+-/", r): // single rune operator
33+
case strings.ContainsRune("#,?:%+-/^", r): // single rune operator
3434
l.emit(Operator)
3535
case strings.ContainsRune("&|!=*<>", r): // possible double rune operator
3636
l.accept("&|=*")

parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var binaryOperators = map[string]operator{
5959
"/": {60, left},
6060
"%": {60, left},
6161
"**": {100, right},
62+
"^": {100, right},
6263
}
6364

6465
var builtins = map[string]builtin{

parser/parser_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ func TestParse(t *testing.T) {
6060
&ast.UnaryNode{Operator: "-", Node: &ast.IntegerNode{Value: 3}},
6161
},
6262
{
63-
"-2**2",
63+
"-2^2",
6464
&ast.UnaryNode{
6565
Operator: "-",
6666
Node: &ast.BinaryNode{
67-
Operator: "**",
67+
Operator: "^",
6868
Left: &ast.IntegerNode{Value: 2},
6969
Right: &ast.IntegerNode{Value: 2},
7070
},

0 commit comments

Comments
 (0)