mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
the narwhal integration written in JavaScript has been replaced with CoffeeScript, and compiler-generated variable names now start with '__'
This commit is contained in:
16
Rakefile
16
Rakefile
@@ -10,9 +10,19 @@ task :test do
|
|||||||
Dir['test/*/**/test_*.rb'].each {|test| require test }
|
Dir['test/*/**/test_*.rb'].each {|test| require test }
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Recompile the Racc parser (pass -v and -g for verbose debugging)"
|
namespace :build do
|
||||||
task :build, :extra_args do |t, args|
|
|
||||||
sh "racc #{args[:extra_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y"
|
desc "Recompile the Racc parser (pass -v and -g for verbose debugging)"
|
||||||
|
task :parser, :extra_args do |t, args|
|
||||||
|
sh "racc #{args[:extra_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y"
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Compile the Narwhal interface for bin/cs"
|
||||||
|
task :narwhal do
|
||||||
|
sh "bin/coffee-script lib/coffee_script/narwhal/coffee-script.cs --print > lib-js/coffee-script.js"
|
||||||
|
sh "bin/coffee-script lib/coffee_script/narwhal/loader.cs --print > lib-js/coffee-script/loader.js"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Build the documentation page"
|
desc "Build the documentation page"
|
||||||
|
|||||||
@@ -1,87 +1,64 @@
|
|||||||
var FILE = require("file");
|
(function(){
|
||||||
var OS = require("os");
|
var File = require('file');
|
||||||
|
var OS = require('os');
|
||||||
exports.run = function(args) {
|
exports.run = function(args) {
|
||||||
// TODO: non-REPL
|
|
||||||
|
|
||||||
args.shift();
|
args.shift();
|
||||||
|
|
||||||
if (args.length) {
|
if (args.length) {
|
||||||
require(FILE.absolute(args[0]));
|
return require(File.absolute(args[0]));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
while (true) {
|
||||||
while (true)
|
try {
|
||||||
{
|
system.stdout.write('cs> ').flush();
|
||||||
try {
|
var result = exports.cs_eval(require('readline').readline());
|
||||||
system.stdout.write("cs> ").flush();
|
if (result !== undefined) {
|
||||||
|
print(result);
|
||||||
var result = exports.cs_eval(require("readline").readline());
|
|
||||||
|
|
||||||
if (result !== undefined)
|
|
||||||
print(result);
|
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
print(e);
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
// executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
|
||||||
// executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
|
// eventually this will hopefully be replaced by a JavaScript program.
|
||||||
// eventually this will hopefully be replaced by a JavaScript program.
|
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
|
||||||
var coffeePath = FILE.path(module.path).dirname().dirname().join("bin", "coffee-script");
|
exports.compileFile = function(path) {
|
||||||
|
|
||||||
exports.compileFile = function(path) {
|
|
||||||
var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
|
var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
|
||||||
|
|
||||||
if (coffee.wait() !== 0) {
|
if (coffee.wait() !== 0) {
|
||||||
system.stderr.print(coffee.stderr.read());
|
system.stderr.print(coffee.stderr.read());
|
||||||
throw new Error("coffee-script compile error");
|
throw new Error("coffee-script compile error");
|
||||||
}
|
}
|
||||||
|
|
||||||
return coffee.stdout.read();
|
return coffee.stdout.read();
|
||||||
}
|
};
|
||||||
|
exports.compile = function(source) {
|
||||||
exports.compile = function(source) {
|
|
||||||
var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]);
|
var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]);
|
||||||
|
|
||||||
coffee.stdin.write(source).flush().close();
|
coffee.stdin.write(source).flush().close();
|
||||||
|
|
||||||
if (coffee.wait() !== 0) {
|
if (coffee.wait() !== 0) {
|
||||||
system.stderr.print(coffee.stderr.read());
|
system.stderr.print(coffee.stderr.read());
|
||||||
throw new Error("coffee-script compile error");
|
throw new Error("coffee-script compile error");
|
||||||
}
|
}
|
||||||
|
|
||||||
return coffee.stdout.read();
|
return coffee.stdout.read();
|
||||||
}
|
};
|
||||||
|
// these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory.
|
||||||
// these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory.
|
// implemented as a call to coffee and objj_eval/make_narwhal_factory
|
||||||
// implemented as a call to coffee and objj_eval/make_narwhal_factory
|
exports.cs_eval = function(source) {
|
||||||
exports.cs_eval = function(source) {
|
|
||||||
init();
|
init();
|
||||||
|
return eval(exports.compile(source));
|
||||||
var code = exports.compile(source);
|
};
|
||||||
|
exports.make_narwhal_factory = function(path) {
|
||||||
return eval(code);
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.make_narwhal_factory = function(path) {
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
var code = exports.compileFile(path);
|
var code = exports.compileFile(path);
|
||||||
|
|
||||||
var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
|
var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
|
||||||
|
if (system.engine === "rhino") {
|
||||||
if (system.engine === "rhino")
|
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
|
||||||
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
|
} else {
|
||||||
|
// eval requires parenthesis, but parenthesis break compileFunction.
|
||||||
// eval requires parenthesis, but parenthesis break compileFunction.
|
return eval("(" + factoryText + ")");
|
||||||
else
|
}
|
||||||
return eval("(" + factoryText + ")");
|
};
|
||||||
}
|
var init = function() {
|
||||||
|
|
||||||
|
|
||||||
var init = function() {
|
|
||||||
// make sure it's only done once
|
// make sure it's only done once
|
||||||
init = function(){}
|
init = function() {
|
||||||
}
|
};
|
||||||
|
return init;
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
var coffeescript = null;
|
(function(){
|
||||||
|
var coffeescript = null;
|
||||||
function CoffeeScriptLoader() {
|
var CoffeeScriptLoader = function() {
|
||||||
var loader = {};
|
var loader = {
|
||||||
var factories = {};
|
};
|
||||||
|
var factories = {
|
||||||
|
};
|
||||||
loader.reload = function(topId, path) {
|
loader.reload = function(topId, path) {
|
||||||
if (!coffeescript) coffeescript = require("coffee-script");
|
coffeescript = coffeescript || require('coffee-script');
|
||||||
|
// print("loading objective-j: " + topId + " (" + path + ")");
|
||||||
//print("loading objective-j: " + topId + " (" + path + ")");
|
factories[topId] = coffeescript.make_narwhal_factory(path);
|
||||||
factories[topId] = coffeescript.make_narwhal_factory(path);
|
return factories[topId];
|
||||||
}
|
};
|
||||||
|
|
||||||
loader.load = function(topId, path) {
|
loader.load = function(topId, path) {
|
||||||
if (!factories.hasOwnProperty(topId))
|
if (!(factories.hasOwnProperty(topId))) {
|
||||||
loader.reload(topId, path);
|
loader.reload(topId, path);
|
||||||
return factories[topId];
|
}
|
||||||
}
|
return factories[topId];
|
||||||
|
};
|
||||||
return loader;
|
return loader;
|
||||||
};
|
};
|
||||||
|
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()]);
|
||||||
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()]);
|
})();
|
||||||
|
|||||||
63
lib/coffee_script/narwhal/coffee-script.cs
Normal file
63
lib/coffee_script/narwhal/coffee-script.cs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.cs
|
||||||
|
|
||||||
|
File: require('file')
|
||||||
|
OS: require('os')
|
||||||
|
|
||||||
|
exports.run: args =>
|
||||||
|
args.shift()
|
||||||
|
return require(File.absolute(args[0])) if args.length
|
||||||
|
|
||||||
|
while true
|
||||||
|
try
|
||||||
|
system.stdout.write('cs> ').flush()
|
||||||
|
result: exports.cs_eval(require('readline').readline())
|
||||||
|
print(result) if result isnt undefined
|
||||||
|
catch e
|
||||||
|
print(e)...
|
||||||
|
|
||||||
|
# executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
|
||||||
|
# eventually this will hopefully be replaced by a JavaScript program.
|
||||||
|
coffeePath: File.path(module.path).dirname().dirname().join('bin', 'coffee-script')
|
||||||
|
|
||||||
|
exports.compileFile: path =>
|
||||||
|
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
|
||||||
|
|
||||||
|
if coffee.wait() isnt 0
|
||||||
|
system.stderr.print(coffee.stderr.read())
|
||||||
|
throw new Error("coffee-script compile error").
|
||||||
|
|
||||||
|
coffee.stdout.read().
|
||||||
|
|
||||||
|
exports.compile: source =>
|
||||||
|
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"])
|
||||||
|
|
||||||
|
coffee.stdin.write(source).flush().close()
|
||||||
|
|
||||||
|
if coffee.wait() isnt 0
|
||||||
|
system.stderr.print(coffee.stderr.read())
|
||||||
|
throw new Error("coffee-script compile error").
|
||||||
|
|
||||||
|
coffee.stdout.read().
|
||||||
|
|
||||||
|
# these two functions are equivalent to objective-j's objj_eval/make_narwhal_factory.
|
||||||
|
# implemented as a call to coffee and objj_eval/make_narwhal_factory
|
||||||
|
exports.cs_eval: source =>
|
||||||
|
init()
|
||||||
|
eval(exports.compile(source)).
|
||||||
|
|
||||||
|
exports.make_narwhal_factory: path =>
|
||||||
|
init()
|
||||||
|
code: exports.compileFile(path)
|
||||||
|
|
||||||
|
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
|
||||||
|
|
||||||
|
if system.engine is "rhino"
|
||||||
|
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
|
||||||
|
else
|
||||||
|
# eval requires parenthesis, but parenthesis break compileFunction.
|
||||||
|
eval("(" + factoryText + ")")..
|
||||||
|
|
||||||
|
|
||||||
|
init: =>
|
||||||
|
# make sure it's only done once
|
||||||
|
init: => ..
|
||||||
20
lib/coffee_script/narwhal/loader.cs
Normal file
20
lib/coffee_script/narwhal/loader.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# This (javascript) file is generated from lib/coffee_script/narwhal/loader.cs
|
||||||
|
|
||||||
|
coffeescript: null
|
||||||
|
|
||||||
|
CoffeeScriptLoader: =>
|
||||||
|
loader: {}
|
||||||
|
factories: {}
|
||||||
|
|
||||||
|
loader.reload: topId, path =>
|
||||||
|
coffeescript ||: require('coffee-script')
|
||||||
|
# print("loading objective-j: " + topId + " (" + path + ")");
|
||||||
|
factories[topId]: coffeescript.make_narwhal_factory(path).
|
||||||
|
|
||||||
|
loader.load: topId, path =>
|
||||||
|
loader.reload(topId, path) unless factories.hasOwnProperty(topId)
|
||||||
|
factories[topId].
|
||||||
|
|
||||||
|
loader.
|
||||||
|
|
||||||
|
require.loader.loaders.unshift([".cs", CoffeeScriptLoader()])
|
||||||
@@ -479,6 +479,7 @@ module CoffeeScript
|
|||||||
|
|
||||||
def compile(o={})
|
def compile(o={})
|
||||||
o = super(o)
|
o = super(o)
|
||||||
|
o.delete(:return)
|
||||||
indent = o[:indent] + TAB
|
indent = o[:indent] + TAB
|
||||||
cond = @condition.compile(o.merge(:no_paren => true))
|
cond = @condition.compile(o.merge(:no_paren => true))
|
||||||
write("while (#{cond}) {\n#{@body.compile(o.merge(:indent => indent))}\n#{o[:indent]}}")
|
write("while (#{cond}) {\n#{@body.compile(o.merge(:indent => indent))}\n#{o[:indent]}}")
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ module CoffeeScript
|
|||||||
def initialize(parent=nil)
|
def initialize(parent=nil)
|
||||||
@parent = parent
|
@parent = parent
|
||||||
@variables = {}
|
@variables = {}
|
||||||
@temp_variable = @parent ? @parent.temp_variable : 'a'
|
@temp_variable = @parent ? @parent.temp_variable : '__a'
|
||||||
end
|
end
|
||||||
|
|
||||||
# Look up a variable in lexical scope, or declare it if not found.
|
# Look up a variable in lexical scope, or declare it if not found.
|
||||||
|
|||||||
14
test/fixtures/each.js
vendored
14
test/fixtures/each.js
vendored
@@ -8,16 +8,16 @@
|
|||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||||
var a = obj;
|
var __a = obj;
|
||||||
for (var b=0, c=a.length; b<c; b++) {
|
for (var __b=0, __c=__a.length; __b<__c; __b++) {
|
||||||
var item = a[b];
|
var item = __a[__b];
|
||||||
var i = b;
|
var i = __b;
|
||||||
iterator.call(context, item, i, obj);
|
iterator.call(context, item, i, obj);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var d = _.keys(obj);
|
var __d = _.keys(obj);
|
||||||
for (var e=0, f=d.length; e<f; e++) {
|
for (var __e=0, __f=__d.length; __e<__f; __e++) {
|
||||||
var key = d[e];
|
var key = __d[__e];
|
||||||
iterator.call(context, obj[key], key, obj);
|
iterator.call(context, obj[key], key, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
test/fixtures/each_no_wrap.js
vendored
14
test/fixtures/each_no_wrap.js
vendored
@@ -7,16 +7,16 @@ _.each = function(obj, iterator, context) {
|
|||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||||
var a = obj;
|
var __a = obj;
|
||||||
for (var b=0, c=a.length; b<c; b++) {
|
for (var __b=0, __c=__a.length; __b<__c; __b++) {
|
||||||
var item = a[b];
|
var item = __a[__b];
|
||||||
var i = b;
|
var i = __b;
|
||||||
iterator.call(context, item, i, obj);
|
iterator.call(context, item, i, obj);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var d = _.keys(obj);
|
var __d = _.keys(obj);
|
||||||
for (var e=0, f=d.length; e<f; e++) {
|
for (var __e=0, __f=__d.length; __e<__f; __e++) {
|
||||||
var key = d[e];
|
var key = __d[__e];
|
||||||
iterator.call(context, obj[key], key, obj);
|
iterator.call(context, obj[key], key, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user