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

adding splice literals, with tests

This commit is contained in:
Jeremy Ashkenas 2010-01-02 00:20:24 -05:00
parent 38520bfece
commit 7ee5be674d
2 changed files with 19 additions and 2 deletions

View file

@ -294,8 +294,10 @@ module CoffeeScript
end
def compile(o={})
o = super(o)
parts = [@literal, @properties].flatten.map do |val|
o = super(o)
only = o.delete(:only_first)
props = only ? @properties[0...-1] : @properties
parts = [@literal, props].flatten.map do |val|
val.respond_to?(:compile) ? val.compile(o) : val.to_s
end
@last = parts.last
@ -413,6 +415,7 @@ module CoffeeScript
def compile(o={})
o = super(o)
return compile_splice(o) if @variable.properties.last.is_a?(SliceNode)
name = @variable.compile(o)
last = @variable.last.to_s.sub(LEADING_DOT, '')
proto = name[PROTO_ASSIGN, 1]
@ -424,6 +427,15 @@ module CoffeeScript
val = "#{name} = #{@value.compile(o)}"
write(o[:return] && !@value.custom_return? ? "#{o[:indent]}return (#{val})" : val)
end
def compile_splice(o)
var = @variable.compile(o.merge(:only_first => true))
range = @variable.properties.last.range
plus = range.exclusive? ? '' : ' + 1'
from = range.from.compile(o)
to = "#{range.to.compile(o)} - #{from}#{plus}"
write("#{var}.splice.apply(#{var}, [#{from}, #{to}].concat(#{@value.compile(o)}))")
end
end
# Simple Arithmetic and logical operations. Performs some conversion from

View file

@ -0,0 +1,5 @@
array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array[5..10]: [0, 0, 0]
print(array.join(' ') is '0 1 2 3 4 0 0 0')