From 7f502543d2f6d1e070206d6b6bdafe004aa7fa99 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 24 Dec 2009 11:50:44 -0800 Subject: [PATCH] added the typeof operater as an OpNode --- lib/coffee_script/grammar.y | 5 +++-- lib/coffee_script/lexer.rb | 2 +- lib/coffee_script/nodes.rb | 5 +++-- test/fixtures/execution/keyword_operators.cs | 6 +++--- test/fixtures/execution/keyword_operators.js | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/coffee_script/grammar.y b/lib/coffee_script/grammar.y index 3e283126..c31d5196 100644 --- a/lib/coffee_script/grammar.y +++ b/lib/coffee_script/grammar.y @@ -10,7 +10,7 @@ token TRY CATCH FINALLY THROW token BREAK CONTINUE token FOR IN WHILE token SWITCH WHEN -token DELETE INSTANCEOF +token DELETE INSTANCEOF TYPEOF token SUPER token NEWLINE token COMMENT @@ -27,7 +27,7 @@ prechigh right '==' '!=' IS AINT left '&&' '||' AND OR right '-=' '+=' '/=' '*=' - right DELETE INSTANCEOF + right DELETE INSTANCEOF TYPEOF left "." right THROW FOR IN WHILE NEW left UNLESS IF ELSE @@ -185,6 +185,7 @@ rule | Expression '&&:' Expression { result = OpNode.new(val[1], val[0], val[2]) } | DELETE Expression { result = OpNode.new(val[0], val[1]) } + | TYPEOF Expression { result = OpNode.new(val[0], val[1]) } | Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) } ; diff --git a/lib/coffee_script/lexer.rb b/lib/coffee_script/lexer.rb index 3d074d80..683de472 100644 --- a/lib/coffee_script/lexer.rb +++ b/lib/coffee_script/lexer.rb @@ -15,7 +15,7 @@ module CoffeeScript "for", "in", "while", "switch", "when", "super", - "delete", "instanceof"] + "delete", "instanceof", "typeof"] # Token matching regexes. IDENTIFIER = /\A([a-zA-Z$_]\w*)/ diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index e1b228d0..41d94f63 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -342,7 +342,8 @@ module CoffeeScript "aint" => "!==", 'not' => '!', } - CONDITIONALS = ['||:', '&&:'] + CONDITIONALS = ['||:', '&&:'] + PREFIX_OPERATORS = ['typeof', 'delete'] attr_reader :operator, :first, :second @@ -369,7 +370,7 @@ module CoffeeScript end def compile_unary(o) - space = @operator.to_s == 'delete' ? ' ' : '' + space = PREFIX_OPERATORS.include?(@operator.to_s) ? ' ' : '' parts = [@operator.to_s, space, @first.compile(o)] parts.reverse! if @flip parts.join('') diff --git a/test/fixtures/execution/keyword_operators.cs b/test/fixtures/execution/keyword_operators.cs index 66ceb4b7..573de104 100644 --- a/test/fixtures/execution/keyword_operators.cs +++ b/test/fixtures/execution/keyword_operators.cs @@ -1,10 +1,10 @@ a: 5 -atype: typeof(a) +atype: typeof a b: "hello" -btype: typeof(b) +btype: typeof b Klass: => . k: new Klass() -print(atype is 'number' and btype is 'string' and k instanceof(Klass)) \ No newline at end of file +print(atype is 'number' and btype is 'string' and k instanceof Klass) \ No newline at end of file diff --git a/test/fixtures/execution/keyword_operators.js b/test/fixtures/execution/keyword_operators.js index 21e06cad..ffccad4c 100644 --- a/test/fixtures/execution/keyword_operators.js +++ b/test/fixtures/execution/keyword_operators.js @@ -1,8 +1,8 @@ (function(){ var a = 5; - var atype = typeof(a); + var atype = typeof a; var b = "hello"; - var btype = typeof(b); + var btype = typeof b; var Klass = function() { }; var k = new Klass();