mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -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:
parent
d8ceb3b4bb
commit
2d57ee693b
9 changed files with 177 additions and 106 deletions
16
Rakefile
16
Rakefile
|
@ -10,9 +10,19 @@ task :test do
|
|||
Dir['test/*/**/test_*.rb'].each {|test| require test }
|
||||
end
|
||||
|
||||
desc "Recompile the Racc parser (pass -v and -g for verbose debugging)"
|
||||
task :build, :extra_args do |t, args|
|
||||
sh "racc #{args[:extra_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y"
|
||||
namespace :build do
|
||||
|
||||
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
|
||||
|
||||
desc "Build the documentation page"
|
||||
|
|
|
@ -1,87 +1,64 @@
|
|||
var FILE = require("file");
|
||||
var OS = require("os");
|
||||
|
||||
exports.run = function(args) {
|
||||
// TODO: non-REPL
|
||||
|
||||
(function(){
|
||||
var File = require('file');
|
||||
var OS = require('os');
|
||||
exports.run = function(args) {
|
||||
args.shift();
|
||||
|
||||
if (args.length) {
|
||||
require(FILE.absolute(args[0]));
|
||||
return;
|
||||
return require(File.absolute(args[0]));
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
try {
|
||||
system.stdout.write("cs> ").flush();
|
||||
|
||||
var result = exports.cs_eval(require("readline").readline());
|
||||
|
||||
if (result !== undefined)
|
||||
print(result);
|
||||
|
||||
} catch (e) {
|
||||
print(e);
|
||||
while (true) {
|
||||
try {
|
||||
system.stdout.write('cs> ').flush();
|
||||
var result = exports.cs_eval(require('readline').readline());
|
||||
if (result !== undefined) {
|
||||
print(result);
|
||||
}
|
||||
} 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.
|
||||
var coffeePath = FILE.path(module.path).dirname().dirname().join("bin", "coffee-script");
|
||||
|
||||
exports.compileFile = function(path) {
|
||||
};
|
||||
// executes the coffee-script Ruby program to convert from CoffeeScript to Objective-J.
|
||||
// eventually this will hopefully be replaced by a JavaScript program.
|
||||
var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script');
|
||||
exports.compileFile = function(path) {
|
||||
var coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
|
||||
|
||||
if (coffee.wait() !== 0) {
|
||||
system.stderr.print(coffee.stderr.read());
|
||||
throw new Error("coffee-script compile error");
|
||||
system.stderr.print(coffee.stderr.read());
|
||||
throw new Error("coffee-script compile error");
|
||||
}
|
||||
|
||||
return coffee.stdout.read();
|
||||
}
|
||||
|
||||
exports.compile = function(source) {
|
||||
};
|
||||
exports.compile = function(source) {
|
||||
var coffee = OS.popen([coffeePath, "--eval", "--no-wrap"]);
|
||||
|
||||
coffee.stdin.write(source).flush().close();
|
||||
|
||||
if (coffee.wait() !== 0) {
|
||||
system.stderr.print(coffee.stderr.read());
|
||||
throw new Error("coffee-script compile error");
|
||||
system.stderr.print(coffee.stderr.read());
|
||||
throw new Error("coffee-script compile error");
|
||||
}
|
||||
|
||||
return 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 = function(source) {
|
||||
};
|
||||
// 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 = function(source) {
|
||||
init();
|
||||
|
||||
var code = exports.compile(source);
|
||||
|
||||
return eval(code);
|
||||
}
|
||||
|
||||
exports.make_narwhal_factory = function(path) {
|
||||
return eval(exports.compile(source));
|
||||
};
|
||||
exports.make_narwhal_factory = function(path) {
|
||||
init();
|
||||
|
||||
var code = exports.compileFile(path);
|
||||
|
||||
var factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
|
||||
|
||||
if (system.engine === "rhino")
|
||||
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
|
||||
|
||||
// eval requires parenthesis, but parenthesis break compileFunction.
|
||||
else
|
||||
return eval("(" + factoryText + ")");
|
||||
}
|
||||
|
||||
|
||||
var init = function() {
|
||||
if (system.engine === "rhino") {
|
||||
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
|
||||
} else {
|
||||
// eval requires parenthesis, but parenthesis break compileFunction.
|
||||
return eval("(" + factoryText + ")");
|
||||
}
|
||||
};
|
||||
var init = function() {
|
||||
// make sure it's only done once
|
||||
init = function(){}
|
||||
}
|
||||
init = function() {
|
||||
};
|
||||
return init;
|
||||
};
|
||||
})();
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
var coffeescript = null;
|
||||
|
||||
function CoffeeScriptLoader() {
|
||||
var loader = {};
|
||||
var factories = {};
|
||||
|
||||
(function(){
|
||||
var coffeescript = null;
|
||||
var CoffeeScriptLoader = function() {
|
||||
var loader = {
|
||||
};
|
||||
var factories = {
|
||||
};
|
||||
loader.reload = function(topId, path) {
|
||||
if (!coffeescript) coffeescript = require("coffee-script");
|
||||
|
||||
//print("loading objective-j: " + topId + " (" + path + ")");
|
||||
factories[topId] = coffeescript.make_narwhal_factory(path);
|
||||
}
|
||||
|
||||
coffeescript = coffeescript || require('coffee-script');
|
||||
// print("loading objective-j: " + topId + " (" + path + ")");
|
||||
factories[topId] = coffeescript.make_narwhal_factory(path);
|
||||
return factories[topId];
|
||||
};
|
||||
loader.load = function(topId, path) {
|
||||
if (!factories.hasOwnProperty(topId))
|
||||
loader.reload(topId, path);
|
||||
return factories[topId];
|
||||
}
|
||||
|
||||
if (!(factories.hasOwnProperty(topId))) {
|
||||
loader.reload(topId, path);
|
||||
}
|
||||
return factories[topId];
|
||||
};
|
||||
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={})
|
||||
o = super(o)
|
||||
o.delete(:return)
|
||||
indent = o[:indent] + TAB
|
||||
cond = @condition.compile(o.merge(:no_paren => true))
|
||||
write("while (#{cond}) {\n#{@body.compile(o.merge(:indent => indent))}\n#{o[:indent]}}")
|
||||
|
|
|
@ -11,7 +11,7 @@ module CoffeeScript
|
|||
def initialize(parent=nil)
|
||||
@parent = parent
|
||||
@variables = {}
|
||||
@temp_variable = @parent ? @parent.temp_variable : 'a'
|
||||
@temp_variable = @parent ? @parent.temp_variable : '__a'
|
||||
end
|
||||
|
||||
# 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) {
|
||||
obj.forEach(iterator, context);
|
||||
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||
var a = obj;
|
||||
for (var b=0, c=a.length; b<c; b++) {
|
||||
var item = a[b];
|
||||
var i = b;
|
||||
var __a = obj;
|
||||
for (var __b=0, __c=__a.length; __b<__c; __b++) {
|
||||
var item = __a[__b];
|
||||
var i = __b;
|
||||
iterator.call(context, item, i, obj);
|
||||
}
|
||||
} else {
|
||||
var d = _.keys(obj);
|
||||
for (var e=0, f=d.length; e<f; e++) {
|
||||
var key = d[e];
|
||||
var __d = _.keys(obj);
|
||||
for (var __e=0, __f=__d.length; __e<__f; __e++) {
|
||||
var key = __d[__e];
|
||||
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) {
|
||||
obj.forEach(iterator, context);
|
||||
} else if (_.isArray(obj) || _.isArguments(obj)) {
|
||||
var a = obj;
|
||||
for (var b=0, c=a.length; b<c; b++) {
|
||||
var item = a[b];
|
||||
var i = b;
|
||||
var __a = obj;
|
||||
for (var __b=0, __c=__a.length; __b<__c; __b++) {
|
||||
var item = __a[__b];
|
||||
var i = __b;
|
||||
iterator.call(context, item, i, obj);
|
||||
}
|
||||
} else {
|
||||
var d = _.keys(obj);
|
||||
for (var e=0, f=d.length; e<f; e++) {
|
||||
var key = d[e];
|
||||
var __d = _.keys(obj);
|
||||
for (var __e=0, __f=__d.length; __e<__f; __e++) {
|
||||
var key = __d[__e];
|
||||
iterator.call(context, obj[key], key, obj);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue