ensure that functions are only called once, when chaining comparators

This commit is contained in:
Jeremy Ashkenas
2010-01-16 22:04:08 -05:00
parent 9679fc0b52
commit 0bc4da2b51
2 changed files with 18 additions and 2 deletions

View File

@@ -544,6 +544,7 @@ module CoffeeScript
class OpNode < Node
children :first, :second
attr_reader :operator
attr_accessor :second
CONVERSIONS = {
:== => "===",
@@ -581,7 +582,13 @@ module CoffeeScript
# Mimic Python's chained comparisons. See:
# http://docs.python.org/reference/expressions.html#notin
def compile_chain(o)
write("(#{@first.compile(o)}) && (#{@first.unwrap.second.compile(o)} #{@operator} #{@second.compile(o)})")
shared = @first.unwrap.second
if shared.is_a?(CallNode)
temp = o[:scope].free_variable
@first.second = ParentheticalNode.new(AssignNode.new(temp, shared))
shared = temp
end
write("(#{@first.compile(o)}) && (#{shared.compile(o)} #{@operator} #{@second.compile(o)})")
end
def compile_conditional(o)