added the typeof operater as an OpNode

This commit is contained in:
Jeremy Ashkenas
2009-12-24 11:50:44 -08:00
parent f859eb6cec
commit 9eeac9b272
6 changed files with 958 additions and 929 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

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

File diff suppressed because it is too large Load Diff

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