fixed up the comment/assignment interleaving in nodes.rb

This commit is contained in:
Jeremy Ashkenas 2009-12-30 20:24:24 -05:00
parent 5c7b77aa4d
commit c8711b419e
2 changed files with 31 additions and 24 deletions

View File

@ -2,7 +2,7 @@
# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize } # ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
['toast', 'wine', 'cheese'].each( food => print(food.capitalize()). ) ['toast', 'wine', 'cheese'].each(food => print(food.capitalize()))
@ -14,10 +14,10 @@
# end # end
LotteryTicket: { LotteryTicket: {
get_picks: => this.picks. get_picks: => this.picks
set_picks: nums => this.picks: nums. set_picks: nums => this.picks: nums
get_purchase: => this.purchase. get_purchase: => this.purchase
set_purchase: amount => this.purchase: amount. set_purchase: amount => this.purchase: amount
} }
@ -33,8 +33,8 @@ LotteryTicket: {
WishScanner: { WishScanner: {
scan_for_a_wish: => scan_for_a_wish: =>
wish: this.read().detect( thought => thought.index('wish: ') is 0. ) wish: this.read().detect(thought => thought.index('wish: ') is 0)
wish.replace('wish: ', ''). wish.replace('wish: ', '')
} }
@ -79,28 +79,28 @@ Creature : {
# This method applies a hit taken during a fight. # This method applies a hit taken during a fight.
hit: damage => hit: damage =>
p_up: Math.rand( this.charisma ) p_up: Math.rand(this.charisma)
if p_up % 9 is 7 if p_up % 9 is 7
this.life += p_up / 4 this.life += p_up / 4
puts( "[" + this.name + " magick powers up " + p_up + "!]" ). puts("[" + this.name + " magick powers up " + p_up + "!]")
this.life -= damage this.life -= damage
if this.life <= 0 then puts( "[" + this.name + " has died.]" ).. if this.life <= 0 then puts("[" + this.name + " has died.]")
# This method takes one turn in a fight. # This method takes one turn in a fight.
fight: enemy, weapon => fight: enemy, weapon =>
if this.life <= 0 then return puts( "[" + this.name + "is too dead to fight!]" ). if this.life <= 0 then return puts("[" + this.name + "is too dead to fight!]")
# Attack the opponent. # Attack the opponent.
your_hit: Math.rand( this.strength + weapon ) your_hit: Math.rand(this.strength + weapon)
puts( "[You hit with " + your_hit + "points of damage!]" ) puts("[You hit with " + your_hit + "points of damage!]")
enemy.hit( your_hit ) enemy.hit(your_hit)
# Retaliation. # Retaliation.
puts( enemy ) puts(enemy)
if enemy.life > 0 if enemy.life > 0
enemy_hit: Math.rand( enemy.strength + enemy.weapon ) enemy_hit: Math.rand(enemy.strength + enemy.weapon)
puts( "[Your enemy hit with " + enemy_hit + "points of damage!]" ) puts("[Your enemy hit with " + enemy_hit + "points of damage!]")
this.hit( enemy_hit ).. this.hit(enemy_hit)
} }
@ -123,12 +123,12 @@ Creature : {
# Get evil idea and swap in code words # Get evil idea and swap in code words
print("Enter your new idea: ") print("Enter your new idea: ")
idea: gets() idea: gets()
code_words.each( real, code => idea.replace(real, code). ) code_words.each(real, code => idea.replace(real, code))
# Save the jibberish to a new file # Save the jibberish to a new file
print("File encoded. Please enter a name for this idea: ") print("File encoded. Please enter a name for this idea: ")
idea_name: gets().strip() idea_name: gets().strip()
File.open("idea-" + idea_name + '.txt', 'w', file => file.write(idea). ) File.open("idea-" + idea_name + '.txt', 'w', file => file.write(idea))
@ -149,5 +149,5 @@ wipe_mutterings_from: sentence =>
while sentence.indexOf('(') >= 0 while sentence.indexOf('(') >= 0
open: sentence.indexOf('(') - 1 open: sentence.indexOf('(') - 1
close: sentence.indexOf(')') + 1 close: sentence.indexOf(')') + 1
sentence: sentence[0..open] + sentence[close..sentence.length]. sentence: sentence[0..open] + sentence[close..sentence.length]
sentence. sentence

View File

@ -464,13 +464,20 @@ module CoffeeScript
@properties = properties @properties = properties
end end
# All the mucking about with commas is to make sure that CommentNodes and
# AssignNodes get interleaved correctly, with no trailing commas or
# commas affixed to comments. TODO: Extract this and add it to ArrayNode.
def compile(o={}) def compile(o={})
o = super(o) o = super(o)
indent = o[:indent] indent = o[:indent]
o[:indent] += TAB o[:indent] += TAB
joins = Hash.new("\n")
non_comments = @properties.select {|p| !p.is_a?(CommentNode) }
non_comments.each {|p| joins[p] = p == non_comments.last ? "\n" : ",\n" }
props = @properties.map { |prop| props = @properties.map { |prop|
joiner = prop == @properties.first ? '' : prop.is_a?(CommentNode) ? "\n" : ",\n" join = joins[prop]
joiner + o[:indent] + prop.compile(o) join = '' if prop == @properties.last
o[:indent] + prop.compile(o) + join
}.join('') }.join('')
write("{\n#{props}\n#{indent}}") write("{\n#{props}\n#{indent}}")
end end