From a4249fd5738cc37187f177be490d92171527864d Mon Sep 17 00:00:00 2001 From: charliesome Date: Wed, 11 Jan 2012 17:14:23 +1100 Subject: [PATCH 1/2] power operator + tests --- src/grammar.coffee | 2 ++ src/lexer.coffee | 1 + src/nodes.coffee | 6 ++++++ test/operators.coffee | 9 +++++++++ 4 files changed, 18 insertions(+) diff --git a/src/grammar.coffee b/src/grammar.coffee index 91ab43c5..7ad4fdd6 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -526,6 +526,7 @@ grammar = o 'Expression - Expression', -> new Op '-' , $1, $3 o 'Expression MATH Expression', -> new Op $2, $1, $3 + o 'Expression ** Expression', -> new Op $2, $1, $3 o 'Expression SHIFT Expression', -> new Op $2, $1, $3 o 'Expression COMPARE Expression', -> new Op $2, $1, $3 o 'Expression LOGIC Expression', -> new Op $2, $1, $3 @@ -560,6 +561,7 @@ operators = [ ['nonassoc', '++', '--'] ['left', '?'] ['right', 'UNARY'] + ['right', '**'] ['left', 'MATH'] ['left', '+', '-'] ['left', 'SHIFT'] diff --git a/src/lexer.coffee b/src/lexer.coffee index 050b3ce1..670edaee 100644 --- a/src/lexer.coffee +++ b/src/lexer.coffee @@ -599,6 +599,7 @@ OPERATOR = /// ^ ( | ([&|<>])\2=? # logic / shift | \?\. # soak access | \.{2,3} # range or splat + | \*\* # power ) /// WHITESPACE = /^[^\n\S]+/ diff --git a/src/nodes.coffee b/src/nodes.coffee index 414f9130..0d3b23a5 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1406,6 +1406,7 @@ exports.Op = class Op extends Base return @compileUnary o if @isUnary() return @compileChain o if isChain return @compileExistence o if @operator is '?' + return @compilePower o if @operator is '**' code = @first.compile(o, LEVEL_OP) + ' ' + @operator + ' ' + @second.compile(o, LEVEL_OP) if o.level <= LEVEL_OP then code else "(#{code})" @@ -1441,6 +1442,11 @@ exports.Op = class Op extends Base parts.push @first.compile o, LEVEL_OP parts.reverse() if @flip parts.join '' + + compilePower: (o) -> + left = @first.compile o, LEVEL_OP + right = @second.compile o, LEVEL_OP + "Math.pow(#{left}, #{right})" toString: (idt) -> super idt, @constructor.name + ' ' + @operator diff --git a/test/operators.coffee b/test/operators.coffee index 2ae0c6a9..c377410a 100644 --- a/test/operators.coffee +++ b/test/operators.coffee @@ -269,3 +269,12 @@ test "Regression with implicit calls against an indented assignment", -> 1 eq a, 1 + +test "power operator", -> + eq 27, 3 ** 3 + +test "power operator has higher precedence than other maths operators", -> + eq 55, 1 + 3 ** 3 * 2 + +test "power operator is right associative", -> + eq 1, 1 ** 2 ** 3 \ No newline at end of file From 3bd4dea3054ceb23476a6a743c1f4ef2206caea3 Mon Sep 17 00:00:00 2001 From: charliesome Date: Wed, 11 Jan 2012 21:37:06 +1100 Subject: [PATCH 2/2] fix the precedence test so it's actually meaningful --- test/operators.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/operators.coffee b/test/operators.coffee index c377410a..716b104a 100644 --- a/test/operators.coffee +++ b/test/operators.coffee @@ -277,4 +277,4 @@ test "power operator has higher precedence than other maths operators", -> eq 55, 1 + 3 ** 3 * 2 test "power operator is right associative", -> - eq 1, 1 ** 2 ** 3 \ No newline at end of file + eq 2, 2 ** 1 ** 3 \ No newline at end of file