Permalink
Browse files

added the conditional existence operator

  • Loading branch information...
1 parent 0bc4da2 commit 95b362499fb6a17a71422e874b39f8d6637b9863 @jashkenas jashkenas committed Jan 17, 2010
@@ -38,7 +38,7 @@ prechigh
right WHEN LEADING_WHEN IN OF BY
right THROW FOR NEW SUPER
left EXTENDS
- left ASSIGN '||=' '&&='
+ left ASSIGN '||=' '&&=' '?='
right RETURN
right '=>' '==>' UNLESS IF ELSE WHILE
preclow
@@ -187,6 +187,7 @@ rule
| Expression '%=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '||=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&&=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
+ | Expression '?=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression IN Expression { result = OpNode.new(val[1], val[0], val[2]) }
@@ -25,7 +25,7 @@ class Lexer
STRING = /\A(""|''|"(.*?)([^\\]|\\\\)"|'(.*?)([^\\]|\\\\)')/m
HEREDOC = /\A("{6}|'{6}|"{3}\n?(.*?)\n?(\s*)"{3}|'{3}\n?(.*?)\n?(\s*)'{3})/m
JS = /\A(``|`(.*?)([^\\]|\\\\)`)/m
- OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
+ OPERATOR = /\A([+\*&|\/\-%=<>:!?]+)/
WHITESPACE = /\A([ \t]+)/
COMMENT = /\A(((\n?[ \t]*)?#.*$)+)/
CODE = /\A(=?=>)/
@@ -556,7 +556,7 @@ class OpNode < Node
:not => '!'
}
CHAINABLE = [:<, :>, :>=, :<=, :===, :'!===']
- CONDITIONALS = [:'||=', :'&&=']
+ CONDITIONALS = [:'||=', :'&&=', :'?=']
PREFIX_OPERATORS = [:typeof, :delete]
def initialize(operator, first, second=nil, flip=false)
@@ -593,7 +593,9 @@ def compile_chain(o)
def compile_conditional(o)
first, second = @first.compile(o), @second.compile(o)
+ o[:scope].find(first) if @first.unwrap.is_a?(Value)
sym = @operator[0..1]
+ return "#{first} = (typeof #{first} !== \"undefined\" && #{first} !== null) ? #{first} : #{second}" if @operator == '?='
"#{first} = #{first} #{sym} #{second}"
end
@@ -16,3 +16,13 @@ i: 0
func: => i++
print(1 > func() < 1)
+
+
+# The conditional assignment based on existence.
+
+a: 5
+a: null
+a ?= 10
+b ?= 10
+
+print(a is 10 and b is 10)

0 comments on commit 95b3624

Please sign in to comment.