mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
made array comprehensions into expressions
This commit is contained in:
parent
b093a437f4
commit
8773b43e9a
2 changed files with 29 additions and 12 deletions
7
code.jaa
7
code.jaa
|
@ -1,5 +1,4 @@
|
|||
# TODO: switch/case statements
|
||||
# Make array comprehensions expressions. (return the computed array).
|
||||
# Make arbitrary-length if else if else if chains possible.
|
||||
# Think of a name for this crazy thing.
|
||||
|
||||
|
@ -37,7 +36,6 @@ exponents : [x => x., x => x * x., x => x * x * x.]
|
|||
# Conditionals and ternaries.
|
||||
if submarine.shields_up
|
||||
full_speed_ahead()
|
||||
# else if submarine.fully_loaded
|
||||
fire_torpedos()
|
||||
else
|
||||
run_away().
|
||||
|
@ -100,7 +98,6 @@ change_a_and_set_b: =>
|
|||
b: 20
|
||||
|
||||
# Array comprehensions.
|
||||
print(food.capitalize()) for food in ['toast', 'wine', 'cheese'].
|
||||
supper: food.capitalize() for food in ['toast', 'cheese', 'wine'].
|
||||
|
||||
cooler: ['soda', 'wine', 'lemonade']
|
||||
drink(bottle) for bottle, i in cooler if even(i).
|
||||
drink(bottle) for bottle, i in ['soda', 'wine', 'lemonade'] if even(i).
|
||||
|
|
34
nodes.rb
34
nodes.rb
|
@ -36,10 +36,9 @@ class Node
|
|||
TAB = ' '
|
||||
|
||||
def line_ending; ';'; end
|
||||
|
||||
def statement?; false; end
|
||||
|
||||
def custom_return?; false; end
|
||||
def custom_return?; false; end
|
||||
def custom_assign?; false; end
|
||||
|
||||
def compile(indent='', scope=nil, opts={}); end
|
||||
end
|
||||
|
@ -196,8 +195,10 @@ class AssignNode < Node
|
|||
return "#{name} = #{value}" if @variable.properties?
|
||||
defined = scope.find(name)
|
||||
postfix = !defined && opts[:return] ? ";\n#{indent}return #{name}" : ''
|
||||
name = "var #{name}" if !defined
|
||||
"#{name} = #{@value.compile(indent, scope)}#{postfix}"
|
||||
def_part = defined ? "" : "var #{name};\n#{indent}"
|
||||
return def_part + @value.compile(indent, scope, opts.merge(:assign => name)) if @value.custom_assign?
|
||||
def_part = defined ? name : "var #{name}"
|
||||
"#{def_part} = #{@value.compile(indent, scope)}#{postfix}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -346,7 +347,11 @@ class ForNode < Node
|
|||
''
|
||||
end
|
||||
|
||||
def statement?
|
||||
def custom_return?
|
||||
true
|
||||
end
|
||||
|
||||
def custom_assign?
|
||||
true
|
||||
end
|
||||
|
||||
|
@ -360,7 +365,22 @@ class ForNode < Node
|
|||
for_part = "var #{ivar}=0, #{lvar}=#{svar}.length; #{ivar}<#{lvar}; #{ivar}++"
|
||||
var_part = "\n#{indent + TAB}#{name_part} = #{svar}[#{ivar}];\n"
|
||||
index_part = @index ? "#{indent + TAB}#{index_name} = #{ivar};\n" : ''
|
||||
"#{source_part}\n#{indent}for (#{for_part}) {#{var_part}#{index_part}#{indent + TAB}#{@body.compile(indent + TAB, scope)};\n#{indent}}"
|
||||
|
||||
set_result = ''
|
||||
save_result = ''
|
||||
return_result = ''
|
||||
if opts[:return] || opts[:assign]
|
||||
rvar = scope.free_variable
|
||||
set_result = "var #{rvar} = [];\n#{indent}"
|
||||
save_result = "#{rvar}[#{ivar}] = "
|
||||
return_result = rvar
|
||||
return_result = "#{opts[:assign]} = #{return_result}" if opts[:assign]
|
||||
return_result = "return #{return_result}" if opts[:return]
|
||||
return_result = "\n#{indent}#{return_result}"
|
||||
end
|
||||
|
||||
body = @body.compile(indent + TAB, scope)
|
||||
"#{source_part}\n#{indent}#{set_result}for (#{for_part}) {#{var_part}#{index_part}#{indent + TAB}#{save_result}#{body};\n#{indent}}#{return_result}"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue