mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
more refactors to utilities ... removing dependencies and the namespacing
This commit is contained in:
parent
f3a60edc5d
commit
4a8c2e8a13
7 changed files with 46 additions and 60 deletions
|
|
@ -11,11 +11,11 @@
|
||||||
(from < 0 ? from + array.length : from || 0),
|
(from < 0 ? from + array.length : from || 0),
|
||||||
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
|
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
|
||||||
];
|
];
|
||||||
}, __bind = function(func, obj, args) {
|
}, __slice = Array.prototype.slice, __bind = function(func, obj, args) {
|
||||||
return function() {
|
return function() {
|
||||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||||
};
|
};
|
||||||
}, __slice = Array.prototype.slice;
|
};
|
||||||
// `nodes.coffee` contains all of the node classes for the syntax tree. Most
|
// `nodes.coffee` contains all of the node classes for the syntax tree. Most
|
||||||
// nodes are created as the result of actions in the [grammar](grammar.html),
|
// nodes are created as the result of actions in the [grammar](grammar.html),
|
||||||
// but some are created by other nodes as a method of code generation. To convert
|
// but some are created by other nodes as a method of code generation. To convert
|
||||||
|
|
@ -596,6 +596,7 @@
|
||||||
};
|
};
|
||||||
CurryNode.prototype.compile_node = function compile_node(o) {
|
CurryNode.prototype.compile_node = function compile_node(o) {
|
||||||
var ref;
|
var ref;
|
||||||
|
o.scope.utility('slice');
|
||||||
ref = new ValueNode(literal(o.scope.utility('bind')));
|
ref = new ValueNode(literal(o.scope.utility('bind')));
|
||||||
return (new CallNode(ref, [this.meth, this.context, literal(this.arguments(o))])).compile(o);
|
return (new CallNode(ref, [this.meth, this.context, literal(this.arguments(o))])).compile(o);
|
||||||
};
|
};
|
||||||
|
|
@ -1083,6 +1084,7 @@
|
||||||
if (!(this.bound)) {
|
if (!(this.bound)) {
|
||||||
return func;
|
return func;
|
||||||
}
|
}
|
||||||
|
o.scope.utility('slice');
|
||||||
ref = new ValueNode(literal(o.scope.utility('bind')));
|
ref = new ValueNode(literal(o.scope.utility('bind')));
|
||||||
return (new CallNode(ref, [literal(func), literal('this')])).compile(o);
|
return (new CallNode(ref, [literal(func), literal('this')])).compile(o);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
(function(){
|
(function(){
|
||||||
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, helpers, include, pair;
|
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, helpers, include, pair;
|
||||||
var __bind = function(func, obj, args) {
|
var __slice = Array.prototype.slice, __bind = function(func, obj, args) {
|
||||||
return function() {
|
return function() {
|
||||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||||
};
|
};
|
||||||
}, __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty;
|
}, __hasProp = Object.prototype.hasOwnProperty;
|
||||||
// The CoffeeScript language has a good deal of optional syntax, implicit syntax,
|
// The CoffeeScript language has a good deal of optional syntax, implicit syntax,
|
||||||
// and shorthand syntax. This can greatly complicate a grammar and bloat
|
// and shorthand syntax. This can greatly complicate a grammar and bloat
|
||||||
// the resulting parse table. Instead of making the parser handle it all, we take
|
// the resulting parse table. Instead of making the parser handle it all, we take
|
||||||
|
|
|
||||||
13
lib/scope.js
13
lib/scope.js
|
|
@ -94,18 +94,13 @@
|
||||||
// Ensure the CoffeeScript utility object is included in the top level
|
// Ensure the CoffeeScript utility object is included in the top level
|
||||||
// then return a CallNode curried constructor bound to the utility function
|
// then return a CallNode curried constructor bound to the utility function
|
||||||
Scope.prototype.utility = function utility(name) {
|
Scope.prototype.utility = function utility(name) {
|
||||||
var _a, _b, _c, _d, dep;
|
var _a;
|
||||||
if (this.parent) {
|
if (this.parent) {
|
||||||
return Scope.root.utility(name);
|
return Scope.root.utility(name);
|
||||||
}
|
}
|
||||||
if ((typeof (_d = utilities.functions[name]) !== "undefined" && _d !== null)) {
|
if ((typeof (_a = utilities[name]) !== "undefined" && _a !== null)) {
|
||||||
this.utilities = this.utilities || {};
|
this.utilities = this.utilities || {};
|
||||||
this.utilities[name] = utilities.functions[name];
|
this.utilities[name] = utilities[name];
|
||||||
_b = (utilities.dependencies[name] || []);
|
|
||||||
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
|
||||||
dep = _b[_a];
|
|
||||||
!this.utilities[dep] ? this.utility(dep) : null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return "__" + name;
|
return "__" + name;
|
||||||
};
|
};
|
||||||
|
|
@ -115,7 +110,7 @@
|
||||||
var _a, _b, key;
|
var _a, _b, key;
|
||||||
_a = []; _b = this.utilities;
|
_a = []; _b = this.utilities;
|
||||||
for (key in _b) { if (__hasProp.call(_b, key)) {
|
for (key in _b) { if (__hasProp.call(_b, key)) {
|
||||||
_a.push("__" + key + " = " + (utilities.functions[key]));
|
_a.push("__" + key + " = " + (utilities[key]));
|
||||||
}}
|
}}
|
||||||
return _a;
|
return _a;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,10 @@
|
||||||
this.exports = this;
|
this.exports = this;
|
||||||
}
|
}
|
||||||
exports.utilities = {
|
exports.utilities = {
|
||||||
dependencies: {
|
extend: "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n }",
|
||||||
bind: ['slice']
|
bind: "function(func, obj, args) {\n return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);\n };\n }",
|
||||||
},
|
range: "function(array, from, to, exclusive) {\n return [\n (from < 0 ? from + array.length : from || 0),\n (to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)\n ];\n }",
|
||||||
functions: {
|
hasProp: 'Object.prototype.hasOwnProperty',
|
||||||
extend: "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n }",
|
slice: 'Array.prototype.slice'
|
||||||
bind: "function(func, obj, args) {\n return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);\n };\n }",
|
|
||||||
range: "function(array, from, to, exclusive) {\n return [\n (from < 0 ? from + array.length : from || 0),\n (to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)\n ];\n }",
|
|
||||||
hasProp: 'Object.prototype.hasOwnProperty',
|
|
||||||
slice: 'Array.prototype.slice'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -430,6 +430,7 @@ exports.CurryNode: class CurryNode extends CallNode
|
||||||
(new ArrayNode(@args)).compile o
|
(new ArrayNode(@args)).compile o
|
||||||
|
|
||||||
compile_node: (o) ->
|
compile_node: (o) ->
|
||||||
|
o.scope.utility('slice')
|
||||||
ref: new ValueNode literal(o.scope.utility('bind'))
|
ref: new ValueNode literal(o.scope.utility('bind'))
|
||||||
(new CallNode(ref, [@meth, @context, literal(@arguments(o))])).compile o
|
(new CallNode(ref, [@meth, @context, literal(@arguments(o))])).compile o
|
||||||
|
|
||||||
|
|
@ -801,6 +802,7 @@ exports.CodeNode: class CodeNode extends BaseNode
|
||||||
func: "function${ if @bound then '' else name_part }(${ params.join(', ') }) {$code${@idt(if @bound then 1 else 0)}}"
|
func: "function${ if @bound then '' else name_part }(${ params.join(', ') }) {$code${@idt(if @bound then 1 else 0)}}"
|
||||||
func: "($func)" if top and not @bound
|
func: "($func)" if top and not @bound
|
||||||
return func unless @bound
|
return func unless @bound
|
||||||
|
o.scope.utility('slice')
|
||||||
ref: new ValueNode literal(o.scope.utility('bind'))
|
ref: new ValueNode literal(o.scope.utility('bind'))
|
||||||
(new CallNode ref, [literal(func), literal('this')]).compile o
|
(new CallNode ref, [literal(func), literal('this')]).compile o
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,16 +69,15 @@ exports.Scope: class Scope
|
||||||
# then return a CallNode curried constructor bound to the utility function
|
# then return a CallNode curried constructor bound to the utility function
|
||||||
utility: (name) ->
|
utility: (name) ->
|
||||||
return Scope.root.utility(name) if @parent
|
return Scope.root.utility(name) if @parent
|
||||||
if utilities.functions[name]?
|
if utilities[name]?
|
||||||
@utilities: or {}
|
@utilities: or {}
|
||||||
@utilities[name]: utilities.functions[name]
|
@utilities[name]: utilities[name]
|
||||||
@utility(dep) for dep in (utilities.dependencies[name] or []) when not @utilities[dep]
|
|
||||||
"__$name"
|
"__$name"
|
||||||
|
|
||||||
# Formats an javascript object containing the utility methods required
|
# Formats an javascript object containing the utility methods required
|
||||||
# in the scope
|
# in the scope
|
||||||
included_utilities: ->
|
included_utilities: ->
|
||||||
"__$key = ${utilities.functions[key]}" for key of @utilities
|
"__$key = ${utilities[key]}" for key of @utilities
|
||||||
|
|
||||||
# Does this scope reference any variables that need to be declared in the
|
# Does this scope reference any variables that need to be declared in the
|
||||||
# given function body?
|
# given function body?
|
||||||
|
|
|
||||||
|
|
@ -2,42 +2,35 @@ this.exports: this unless process?
|
||||||
|
|
||||||
exports.utilities: {
|
exports.utilities: {
|
||||||
|
|
||||||
dependencies: {
|
extend: """
|
||||||
bind: ['slice']
|
function(child, parent) {
|
||||||
}
|
var ctor = function(){ };
|
||||||
|
ctor.prototype = parent.prototype;
|
||||||
|
child.__superClass__ = parent.prototype;
|
||||||
|
child.prototype = new ctor();
|
||||||
|
child.prototype.constructor = child;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
functions: {
|
bind: """
|
||||||
|
function(func, obj, args) {
|
||||||
|
return function() {
|
||||||
|
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
extend: """
|
range: """
|
||||||
function(child, parent) {
|
function(array, from, to, exclusive) {
|
||||||
var ctor = function(){ };
|
return [
|
||||||
ctor.prototype = parent.prototype;
|
(from < 0 ? from + array.length : from || 0),
|
||||||
child.__superClass__ = parent.prototype;
|
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
|
||||||
child.prototype = new ctor();
|
];
|
||||||
child.prototype.constructor = child;
|
}
|
||||||
}
|
"""
|
||||||
"""
|
|
||||||
|
|
||||||
bind: """
|
hasProp: 'Object.prototype.hasOwnProperty'
|
||||||
function(func, obj, args) {
|
|
||||||
return function() {
|
|
||||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
range: """
|
slice: 'Array.prototype.slice'
|
||||||
function(array, from, to, exclusive) {
|
|
||||||
return [
|
|
||||||
(from < 0 ? from + array.length : from || 0),
|
|
||||||
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
|
|
||||||
];
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
hasProp: 'Object.prototype.hasOwnProperty'
|
|
||||||
|
|
||||||
slice: 'Array.prototype.slice'
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue