1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

added the typeof operater as an OpNode

This commit is contained in:
Jeremy Ashkenas 2009-12-24 11:50:44 -08:00
parent 5d1ec9d2a9
commit 7f502543d2
5 changed files with 12 additions and 10 deletions

View file

@ -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]) }
;

View file

@ -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*)/

View file

@ -343,6 +343,7 @@ module CoffeeScript
'not' => '!',
}
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('')

View file

@ -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))
print(atype is 'number' and btype is 'string' and k instanceof Klass)

View file

@ -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();