merging in master

This commit is contained in:
Jeremy Ashkenas
2009-12-28 20:06:23 -05:00
21 changed files with 295 additions and 299 deletions

View File

@@ -19,6 +19,10 @@ Usage:
# Seconds to pause between checks for changed source files.
WATCH_INTERVAL = 0.5
# Command to execute in Narwhal
PACKAGE = File.expand_path(File.dirname(__FILE__) + '/../..')
LAUNCHER = "narwhal -p #{PACKAGE} -e 'require(\"coffee-script\").run(system.args);'"
# Run the CommandLine off the contents of ARGV.
def initialize
@mtimes = {}
@@ -104,7 +108,7 @@ Usage:
# Use Narwhal to run an interactive CoffeeScript session.
def launch_repl
exec "narwhal lib/coffee_script/narwhal/js/launcher.js"
exec "#{LAUNCHER}"
rescue Errno::ENOENT
puts "Error: Narwhal must be installed to use the interactive REPL."
exit(1)
@@ -113,7 +117,7 @@ Usage:
# Use Narwhal to compile and execute CoffeeScripts.
def run_scripts
sources = @sources.join(' ')
exec "narwhal lib/coffee_script/narwhal/js/launcher.js #{sources}"
exec "#{LAUNCHER} #{sources}"
rescue Errno::ENOENT
puts "Error: Narwhal must be installed in order to execute CoffeeScripts."
exit(1)

View File

@@ -327,7 +327,9 @@ rule
# Looks a little confusing, check nodes.rb for the arguments to ForNode.
For:
Expression FOR
ForVariables ForSource { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) }
ForVariables ForSource "." { result = ForNode.new(val[0], val[3][0], val[2][0], val[3][1], val[2][1]) }
| FOR ForVariables ForSource
Terminator Expressions "." { result = ForNode.new(val[4], val[2][0], val[1][0], val[2][1], val[1][1]) }
;
# An array comprehension has variables for the current element and index.
@@ -338,9 +340,9 @@ rule
# The source of the array comprehension can optionally be filtered.
ForSource:
IN PureExpression "." { result = [val[1]] }
IN PureExpression { result = [val[1]] }
| IN PureExpression
IF Expression "." { result = [val[1], val[3]] }
IF Expression { result = [val[1], val[3]] }
;
# Switch/When blocks.

View File

@@ -19,7 +19,7 @@ module CoffeeScript
# Token matching regexes.
IDENTIFIER = /\A([a-zA-Z$_]\w*)/
NUMBER = /\A\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?))\b/i
NUMBER = /\A((\b|-)((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
STRING = /\A(""|''|"(.*?)[^\\]"|'(.*?)[^\\]')/m
JS = /\A(``|`(.*?)[^\\]`)/m
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/

View File

@@ -20,8 +20,9 @@ checkForErrors: coffeeProcess =>
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
# command.
exports.run: args =>
args.shift()
return require(File.absolute(args[0])) if args.length
if args.length
exports.evalCS(File.read(path)) for path in args.
return true.
while true
try

View File

@@ -1,3 +0,0 @@
(function(){
require("coffee-script").run(system.args);
})();

View File

@@ -1 +0,0 @@
require("coffee-script").run(system.args)

View File

@@ -18,10 +18,16 @@
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
// command.
exports.run = function(args) {
var result;
args.shift();
var __a, __b, __c, __d, path, result;
if (args.length) {
return require(File.absolute(args[0]));
__a = args;
__d = [];
for (__b=0, __c=__a.length; __b<__c; __b++) {
path = __a[__b];
__d[__b] = exports.evalCS(File.read(path));
}
__d;
return true;
}
while (true) {
try {

View File

@@ -8,8 +8,7 @@
// Reload the coffee-script environment from source.
reload: function(topId, path) {
coffeescript = coffeescript || require('coffee-script');
factories[topId] = coffeescript.makeNarwhalFactory(path);
return factories[topId];
return (factories[topId] = coffeescript.makeNarwhalFactory(path));
},
// Ensure that the coffee-script environment is loaded.
load: function(topId, path) {

View File

@@ -95,6 +95,7 @@ module CoffeeScript
if node.statement? || node.custom_return?
"#{o[:indent]}#{node.compile(o)}#{node.line_ending}"
else
o.delete(:return)
"#{o[:indent]}return #{node.compile(o)}#{node.line_ending}"
end
elsif o[:assign]
@@ -307,8 +308,6 @@ module CoffeeScript
# A range literal. Ranges can be used to extract portions (slices) of arrays,
# or to specify a range for array comprehensions.
# RangeNodes get expanded into the equivalent array, if not used to index
# a slice or define an array comprehension.
class RangeNode
attr_reader :from, :to
@@ -320,15 +319,20 @@ module CoffeeScript
@exclusive
end
# TODO -- figure out if we can detect if a range runs negative.
def downward?
def less_operator
@exclusive ? '<' : '<='
end
# TODO -- figure out if we can expand ranges into the corresponding array,
# as an expression, reliably.
def compile(o={})
write()
def greater_operator
@exclusive ? '>' : '>='
end
def compile(o, fv, tv)
fvv, tvv = @from.compile(o), @to.compile(o)
vars = "#{fv}=#{fvv}, #{tv}=#{tvv}"
compare = "(#{fvv} <= #{tvv} ? #{fv} #{less_operator} #{tv} : #{fv} #{greater_operator} #{tv})"
incr = "(#{fvv} <= #{tvv} ? #{fv} += 1 : #{fv} -= 1)"
"#{vars}; #{compare}; #{incr}"
end
end
@@ -374,12 +378,11 @@ module CoffeeScript
last = @variable.last.to_s
proto = name[PROTO_ASSIGN, 1]
o = o.merge(:assign => @variable, :last_assign => last, :proto_assign => proto)
postfix = o[:return] ? ";\n#{o[:indent]}return #{name}" : ''
return write("#{name}: #{@value.compile(o)}") if @context == :object
return write("#{name} = #{@value.compile(o)}#{postfix}") if @variable.properties? && !@value.custom_assign?
o[:scope].find(name) unless @variable.properties?
return write(@value.compile(o)) if @value.custom_assign?
write("#{name} = #{@value.compile(o)}#{postfix}")
val = "#{name} = #{@value.compile(o)}"
write(o[:return] && !@value.custom_return? ? "return (#{val})" : val)
end
end
@@ -548,11 +551,10 @@ module CoffeeScript
index_name = @index ? @index : nil
if range
source_part = ''
operator = @source.exclusive? ? '<' : '<='
index_var = scope.free_variable
for_part = "#{index_var}=0, #{ivar}=#{@source.from.compile(o)}, #{lvar}=#{@source.to.compile(o)}; #{ivar}#{operator}#{lvar}; #{ivar}++, #{index_var}++"
var_part = ''
index_part = ''
index_var = scope.free_variable
for_part = "#{index_var}=0, #{@source.compile(o, ivar, lvar)}, #{index_var}++"
else
index_var = nil
source_part = "#{svar} = #{@source.compile(o)};\n#{o[:indent]}"
@@ -569,6 +571,8 @@ module CoffeeScript
if o[:return] || o[:assign]
return_result = "#{o[:assign].compile(o)} = #{return_result}" if o[:assign]
return_result = "return #{return_result}" if o[:return]
o.delete(:assign)
o.delete(:return)
if @filter
body = CallNode.new(ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [@body])
body = IfNode.new(@filter, body, nil, :statement => true)
@@ -726,8 +730,11 @@ module CoffeeScript
# force sub-else bodies into statement form.
def compile_statement(o)
indent = o[:indent]
cond_o = o.dup
cond_o.delete(:assign)
cond_o.delete(:return)
o[:indent] += TAB
if_part = "if (#{@condition.compile(o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}"
if_part = "if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{indent}}"
return if_part unless @else_body
else_part = chain? ?
" else #{@else_body.compile(o.merge(:indent => indent))}" :