From aae2405de4b81b01d6ebc48c21cab92539e28681 Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas compile() on the root.
Set up for both Node.js and the browser, by -including the Scope class.
if process?
Scope: require('./scope').Scope
helpers: require('./helpers').helpers
else
this.exports: this
helpers: this.helpers
- Scope: this.ScopeImport the helpers we need.
compact: helpers.compact
+ Scope: this.ScopeImport the helpers we plan to use.
compact: helpers.compact
flatten: helpers.flatten
merge: helpers.merge
del: helpers.delHelper function that marks a node as a JavaScript statement, or as a @@ -926,7 +926,7 @@ in which case, no dice.
UTILITIES: {Correctly set up a prototype chain for inheritance, including a reference
to the superclass for super() calls. See:
-goog.inherits
__extends: """
+goog.inherits. __extends: """
function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
@@ -935,21 +935,18 @@ to the superclass for super() calls. See:
child.prototype.constructor = child;
}
"""Bind a function to a calling context, optionally including curried arguments. -See Underscore's implementation
__bind: """
+See Underscore's implementation. __bind: """
function(func, obj, args) {
return function() {
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
};
}
- """
+ """Shortcuts to speed up the lookup time for native functions.
__hasProp: 'Object.prototype.hasOwnProperty'
+ __slice: 'Array.prototype.slice'
- __hasProp:'Object.prototype.hasOwnProperty'
-
- __slice: 'Array.prototype.slice'
-
-}Tabs are two spaces for pretty printing.
TAB: ' 'Trim out all trailing whitespace, so that the generated code plays nice -with Git.
TRAILING_WHITESPACE: /\s+$/gmKeep this identifier regex in sync with the Lexer.
IDENTIFIER: /^[a-zA-Z\$_](\w|\$)*$/Handy helper for a generating LiteralNode.
literal: (name) ->
- new LiteralNode(name)Helper for ensuring that utility functions are assigned at the top level.
utility: (name) ->
+}Tabs are two spaces for pretty printing.
TAB: ' 'Trim out all trailing whitespace, so that the generated code plays nice +with Git.
TRAILING_WHITESPACE: /\s+$/gmKeep this identifier regex in sync with the Lexer.
IDENTIFIER: /^[a-zA-Z\$_](\w|\$)*$/Handy helper for a generating LiteralNode.
literal: (name) ->
+ new LiteralNode(name)Helper for ensuring that utility functions are assigned at the top level.
utility: (name) ->
ref: "__$name"
Scope.root.assign ref, UTILITIES[ref]
ref
diff --git a/lib/nodes.js b/lib/nodes.js
index f5af457a..60028d9e 100644
--- a/lib/nodes.js
+++ b/lib/nodes.js
@@ -16,7 +16,7 @@
// but some are created by other nodes as a method of code generation. To convert
// the syntax tree into a string of JavaScript code, call `compile()` on the root.
// Set up for both **Node.js** and the browser, by
- // including the [Scope](scope.html) class.
+ // including the [Scope](scope.html) class and the [helper](helpers.html) functions.
if ((typeof process !== "undefined" && process !== null)) {
Scope = require('./scope').Scope;
helpers = require('./helpers').helpers;
@@ -25,7 +25,7 @@
helpers = this.helpers;
Scope = this.Scope;
}
- // Import the helpers we need.
+ // Import the helpers we plan to use.
compact = helpers.compact;
flatten = helpers.flatten;
merge = helpers.merge;
@@ -158,7 +158,7 @@
BaseNode.prototype.toString = function toString(idt) {
var _a, _b, _c, _d, child;
idt = idt || '';
- return '\n' + idt + this.type + (function() {
+ return '\n' + idt + this.constructor.name + (function() {
_a = []; _c = this.children;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
child = _c[_b];
@@ -194,7 +194,6 @@
return this;
};
__extends(Expressions, BaseNode);
- Expressions.prototype.type = 'Expressions';
// Tack an expression on to the end of this expression list.
Expressions.prototype.push = function push(node) {
this.expressions.push(node);
@@ -322,7 +321,6 @@
return this;
};
__extends(LiteralNode, BaseNode);
- LiteralNode.prototype.type = 'Literal';
// Break and continue must be treated as pure statements -- they lose their
// meaning when wrapped in a closure.
LiteralNode.prototype.is_statement = function is_statement() {
@@ -349,7 +347,6 @@
return this;
};
__extends(ReturnNode, BaseNode);
- ReturnNode.prototype.type = 'Return';
ReturnNode.prototype.top_sensitive = function top_sensitive() {
return true;
};
@@ -377,7 +374,6 @@
return this;
};
__extends(ValueNode, BaseNode);
- ValueNode.prototype.type = 'Value';
ValueNode.prototype.SOAK = " == undefined ? undefined : ";
// A **ValueNode** has a base and a list of property accesses.
// Add a property access to the list.
@@ -471,7 +467,6 @@
return this;
};
__extends(CommentNode, BaseNode);
- CommentNode.prototype.type = 'Comment';
CommentNode.prototype.make_return = function make_return() {
return this;
};
@@ -494,7 +489,6 @@
return this;
};
__extends(CallNode, BaseNode);
- CallNode.prototype.type = 'Call';
// Tag this invocation as creating a new instance.
CallNode.prototype.new_instance = function new_instance() {
this.is_new = true;
@@ -564,7 +558,6 @@
return this;
};
__extends(CurryNode, CallNode);
- CurryNode.prototype.type = 'Curry';
CurryNode.prototype.arguments = function arguments(o) {
var _a, _b, _c, arg;
_b = this.args;
@@ -594,7 +587,6 @@
return this;
};
__extends(ExtendsNode, BaseNode);
- ExtendsNode.prototype.type = 'Extends';
// Hooks one constructor into another's prototype chain.
ExtendsNode.prototype.compile_node = function compile_node(o) {
var ref;
@@ -615,7 +607,6 @@
return this;
};
__extends(AccessorNode, BaseNode);
- AccessorNode.prototype.type = 'Accessor';
AccessorNode.prototype.compile_node = function compile_node(o) {
var proto_part;
proto_part = this.prototype ? 'prototype.' : '';
@@ -632,7 +623,6 @@
return this;
};
__extends(IndexNode, BaseNode);
- IndexNode.prototype.type = 'Index';
IndexNode.prototype.compile_node = function compile_node(o) {
var idx;
idx = this.index.compile(o);
@@ -651,7 +641,6 @@
return this;
};
__extends(RangeNode, BaseNode);
- RangeNode.prototype.type = 'Range';
// Compiles the range's source variables -- where it starts and where it ends.
RangeNode.prototype.compile_variables = function compile_variables(o) {
var _a, _b, from, to;
@@ -707,7 +696,6 @@
return this;
};
__extends(SliceNode, BaseNode);
- SliceNode.prototype.type = 'Slice';
SliceNode.prototype.compile_node = function compile_node(o) {
var from, plus_part, to;
from = this.range.from.compile(o);
@@ -725,7 +713,6 @@
return this;
};
__extends(ObjectNode, BaseNode);
- ObjectNode.prototype.type = 'Object';
// 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.
@@ -775,7 +762,6 @@
return this;
};
__extends(ArrayNode, BaseNode);
- ArrayNode.prototype.type = 'Array';
ArrayNode.prototype.compile_node = function compile_node(o) {
var _a, _b, code, ending, i, obj, objects;
o.indent = this.idt(1);
@@ -809,7 +795,6 @@
return this;
};
__extends(ClassNode, BaseNode);
- ClassNode.prototype.type = 'Class';
// Initialize a **ClassNode** with its name, an optional superclass, and a
// list of prototype property assignments.
ClassNode.prototype.make_return = function make_return() {
@@ -869,7 +854,6 @@
return this;
};
__extends(AssignNode, BaseNode);
- AssignNode.prototype.type = 'Assign';
// Matchers for detecting prototype assignments.
AssignNode.prototype.PROTO_ASSIGN = /^(\S+)\.prototype/;
AssignNode.prototype.LEADING_DOT = /^\.(prototype\.)?/;
@@ -993,7 +977,6 @@
return this;
};
__extends(CodeNode, BaseNode);
- CodeNode.prototype.type = 'Code';
// Compilation creates a new scope unless explicitly asked to share with the
// outer scope. Handles splat parameters in the parameter list by peeking at
// the JavaScript `arguments` objects. If the function is bound with the `=>`
@@ -1099,7 +1082,6 @@
return this;
};
__extends(SplatNode, BaseNode);
- SplatNode.prototype.type = 'Splat';
SplatNode.prototype.compile_node = function compile_node(o) {
var _a;
if ((typeof (_a = this.index) !== "undefined" && _a !== null)) {
@@ -1170,7 +1152,6 @@
return this;
};
__extends(WhileNode, BaseNode);
- WhileNode.prototype.type = 'While';
WhileNode.prototype.add_body = function add_body(body) {
this.children.push((this.body = body));
return this;
@@ -1219,14 +1200,13 @@
// CoffeeScript operations into their JavaScript equivalents.
exports.OpNode = (function() {
OpNode = function OpNode(operator, first, second, flip) {
- this.type += ' ' + operator;
+ this.constructor.name += ' ' + operator;
this.children = compact([(this.first = first), (this.second = second)]);
this.operator = this.CONVERSIONS[operator] || operator;
this.flip = !!flip;
return this;
};
__extends(OpNode, BaseNode);
- OpNode.prototype.type = 'Op';
// The map of conversions from CoffeeScript to JavaScript symbols.
OpNode.prototype.CONVERSIONS = {
'==': '===',
@@ -1327,7 +1307,6 @@
return this;
};
__extends(TryNode, BaseNode);
- TryNode.prototype.type = 'Try';
TryNode.prototype.make_return = function make_return() {
if (this.attempt) {
this.attempt = this.attempt.make_return();
@@ -1360,7 +1339,6 @@
return this;
};
__extends(ThrowNode, BaseNode);
- ThrowNode.prototype.type = 'Throw';
// A **ThrowNode** is already a return, of sorts...
ThrowNode.prototype.make_return = function make_return() {
return this;
@@ -1381,7 +1359,6 @@
return this;
};
__extends(ExistenceNode, BaseNode);
- ExistenceNode.prototype.type = 'Existence';
ExistenceNode.prototype.compile_node = function compile_node(o) {
return ExistenceNode.compile_test(o, this.expression);
};
@@ -1416,7 +1393,6 @@
return this;
};
__extends(ParentheticalNode, BaseNode);
- ParentheticalNode.prototype.type = 'Paren';
ParentheticalNode.prototype.is_statement = function is_statement() {
return this.expression.is_statement();
};
@@ -1468,7 +1444,6 @@
return this;
};
__extends(ForNode, BaseNode);
- ForNode.prototype.type = 'For';
ForNode.prototype.top_sensitive = function top_sensitive() {
return true;
};
@@ -1572,7 +1547,6 @@
return this;
};
__extends(IfNode, BaseNode);
- IfNode.prototype.type = 'If';
// Add a new *else* clause to this **IfNode**, or push it down to the bottom
// of the chain recursively.
IfNode.prototype.push = function push(else_body) {
@@ -1737,11 +1711,12 @@
UTILITIES = {
// Correctly set up a prototype chain for inheritance, including a reference
// to the superclass for `super()` calls. See:
- // [goog.inherits](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line1206)
+ // [goog.inherits](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line1206).
__extends: "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 a function to a calling context, optionally including curried arguments.
- // See [Underscore's implementation](http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html#section-47)
+ // See [Underscore's implementation](http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html#section-47).
__bind: "function(func, obj, args) {\n return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);\n };\n }",
+ // Shortcuts to speed up the lookup time for native functions.
__hasProp: 'Object.prototype.hasOwnProperty',
__slice: 'Array.prototype.slice'
};
diff --git a/src/nodes.coffee b/src/nodes.coffee
index f35bd587..0a9a1618 100644
--- a/src/nodes.coffee
+++ b/src/nodes.coffee
@@ -4,7 +4,7 @@
# the syntax tree into a string of JavaScript code, call `compile()` on the root.
# Set up for both **Node.js** and the browser, by
-# including the [Scope](scope.html) class.
+# including the [Scope](scope.html) class and the [helper](helpers.html) functions.
if process?
Scope: require('./scope').Scope
helpers: require('./helpers').helpers
@@ -13,7 +13,7 @@ else
helpers: this.helpers
Scope: this.Scope
-# Import the helpers we need.
+# Import the helpers we plan to use.
compact: helpers.compact
flatten: helpers.flatten
merge: helpers.merge
@@ -113,7 +113,7 @@ exports.BaseNode: class BaseNode
# This is what `coffee --nodes` prints out.
toString: (idt) ->
idt: or ''
- '\n' + idt + @type + (child.toString(idt + TAB) for child in @children).join('')
+ '\n' + idt + @constructor.name + (child.toString(idt + TAB) for child in @children).join('')
# Default implementations of the common node identification methods. Nodes
# will override these with custom logic, if needed.
@@ -129,7 +129,6 @@ exports.BaseNode: class BaseNode
# indented block of code -- the implementation of a function, a clause in an
# `if`, `switch`, or `try`, and so on...
exports.Expressions: class Expressions extends BaseNode
- type: 'Expressions'
constructor: (nodes) ->
@children: @expressions: compact flatten nodes or []
@@ -214,7 +213,6 @@ statement Expressions
# JavaScript without translation, such as: strings, numbers,
# `true`, `false`, `null`...
exports.LiteralNode: class LiteralNode extends BaseNode
- type: 'Literal'
constructor: (value) ->
@value: value
@@ -238,7 +236,6 @@ exports.LiteralNode: class LiteralNode extends BaseNode
# A `return` is a *pure_statement* -- wrapping it in a closure wouldn't
# make sense.
exports.ReturnNode: class ReturnNode extends BaseNode
- type: 'Return'
constructor: (expression) ->
@children: [@expression: expression]
@@ -260,7 +257,6 @@ statement ReturnNode, true
# A value, variable or literal or parenthesized, indexed or dotted into,
# or vanilla.
exports.ValueNode: class ValueNode extends BaseNode
- type: 'Value'
SOAK: " == undefined ? undefined : "
@@ -335,7 +331,6 @@ exports.ValueNode: class ValueNode extends BaseNode
# CoffeeScript passes through comments as JavaScript comments at the
# same position.
exports.CommentNode: class CommentNode extends BaseNode
- type: 'Comment'
constructor: (lines) ->
@lines: lines
@@ -354,7 +349,6 @@ statement CommentNode
# Node for a function invocation. Takes care of converting `super()` calls into
# calls against the prototype's function of the same name.
exports.CallNode: class CallNode extends BaseNode
- type: 'Call'
constructor: (variable, args) ->
@is_new: false
@@ -406,7 +400,6 @@ exports.CallNode: class CallNode extends BaseNode
# returning the bound function. After ECMAScript 5, Prototype.js, and
# Underscore's `bind` functions.
exports.CurryNode: class CurryNode extends CallNode
- type: 'Curry'
constructor: (meth, args) ->
@children: flatten [@meth: meth, @context: args[0], @args: (args.slice(1) or [])]
@@ -429,7 +422,6 @@ exports.CurryNode: class CurryNode extends CallNode
# After `goog.inherits` from the
# [Closure Library](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html).
exports.ExtendsNode: class ExtendsNode extends BaseNode
- type: 'Extends'
constructor: (child, parent) ->
@children: [@child: child, @parent: parent]
@@ -444,11 +436,10 @@ exports.ExtendsNode: class ExtendsNode extends BaseNode
# A `.` accessor into a property of a value, or the `::` shorthand for
# an accessor into the object's prototype.
exports.AccessorNode: class AccessorNode extends BaseNode
- type: 'Accessor'
constructor: (name, tag) ->
@children: [@name: name]
- @prototype: tag is 'prototype'
+ @prototype:tag is 'prototype'
@soak_node: tag is 'soak'
this
@@ -460,7 +451,6 @@ exports.AccessorNode: class AccessorNode extends BaseNode
# A `[ ... ]` indexed accessor into an array or object.
exports.IndexNode: class IndexNode extends BaseNode
- type: 'Index'
constructor: (index, tag) ->
@children: [@index: index]
@@ -476,7 +466,6 @@ exports.IndexNode: class IndexNode extends BaseNode
# to specify a range for comprehensions, or as a value, to be expanded into the
# corresponding array of integers at runtime.
exports.RangeNode: class RangeNode extends BaseNode
- type: 'Range'
constructor: (from, to, exclusive) ->
@children: [@from: from, @to: to]
@@ -518,7 +507,6 @@ exports.RangeNode: class RangeNode extends BaseNode
# specifies the index of the end of the slice, just as the first parameter
# is the index of the beginning.
exports.SliceNode: class SliceNode extends BaseNode
- type: 'Slice'
constructor: (range) ->
@children: [@range: range]
@@ -534,7 +522,6 @@ exports.SliceNode: class SliceNode extends BaseNode
# An object literal, nothing fancy.
exports.ObjectNode: class ObjectNode extends BaseNode
- type: 'Object'
constructor: (props) ->
@children: @objects: @properties: props or []
@@ -562,7 +549,6 @@ exports.ObjectNode: class ObjectNode extends BaseNode
# An array literal.
exports.ArrayNode: class ArrayNode extends BaseNode
- type: 'Array'
constructor: (objects) ->
@children: @objects: objects or []
@@ -589,7 +575,6 @@ exports.ArrayNode: class ArrayNode extends BaseNode
# The CoffeeScript class definition.
exports.ClassNode: class ClassNode extends BaseNode
- type: 'Class'
# Initialize a **ClassNode** with its name, an optional superclass, and a
# list of prototype property assignments.
@@ -645,7 +630,6 @@ statement ClassNode
# The **AssignNode** is used to assign a local variable to value, or to set the
# property of an object -- including within object literals.
exports.AssignNode: class AssignNode extends BaseNode
- type: 'Assign'
# Matchers for detecting prototype assignments.
PROTO_ASSIGN: /^(\S+)\.prototype/
@@ -735,7 +719,6 @@ exports.AssignNode: class AssignNode extends BaseNode
# When for the purposes of walking the contents of a function body, the CodeNode
# has no *children* -- they're within the inner scope.
exports.CodeNode: class CodeNode extends BaseNode
- type: 'Code'
constructor: (params, body, tag) ->
@params: params or []
@@ -804,7 +787,6 @@ exports.CodeNode: class CodeNode extends BaseNode
# A splat, either as a parameter to a function, an argument to a call,
# or as part of a destructuring assignment.
exports.SplatNode: class SplatNode extends BaseNode
- type: 'Splat'
constructor: (name) ->
name: literal(name) unless name.compile
@@ -857,7 +839,6 @@ exports.SplatNode: class SplatNode extends BaseNode
# it, all other loops can be manufactured. Useful in cases where you need more
# flexibility or more speed than a comprehension can provide.
exports.WhileNode: class WhileNode extends BaseNode
- type: 'While'
constructor: (condition, opts) ->
@children:[@condition: condition]
@@ -903,7 +884,6 @@ statement WhileNode
# Simple Arithmetic and logical operations. Performs some conversion from
# CoffeeScript operations into their JavaScript equivalents.
exports.OpNode: class OpNode extends BaseNode
- type: 'Op'
# The map of conversions from CoffeeScript to JavaScript symbols.
CONVERSIONS: {
@@ -922,7 +902,7 @@ exports.OpNode: class OpNode extends BaseNode
PREFIX_OPERATORS: ['typeof', 'delete']
constructor: (operator, first, second, flip) ->
- @type: + ' ' + operator
+ @constructor.name: + ' ' + operator
@children: compact [@first: first, @second: second]
@operator: @CONVERSIONS[operator] or operator
@flip: !!flip
@@ -979,7 +959,6 @@ exports.OpNode: class OpNode extends BaseNode
# A classic *try/catch/finally* block.
exports.TryNode: class TryNode extends BaseNode
- type: 'Try'
constructor: (attempt, error, recovery, ensure) ->
@children: compact [@attempt: attempt, @recovery: recovery, @ensure: ensure]
@@ -1008,7 +987,6 @@ statement TryNode
# Simple node to throw an exception.
exports.ThrowNode: class ThrowNode extends BaseNode
- type: 'Throw'
constructor: (expression) ->
@children: [@expression: expression]
@@ -1028,7 +1006,6 @@ statement ThrowNode
# similar to `.nil?` in Ruby, and avoids having to consult a JavaScript truth
# table.
exports.ExistenceNode: class ExistenceNode extends BaseNode
- type: 'Existence'
constructor: (expression) ->
@children: [@expression: expression]
@@ -1054,7 +1031,6 @@ exports.ExistenceNode: class ExistenceNode extends BaseNode
#
# Parentheses are a good way to force any statement to become an expression.
exports.ParentheticalNode: class ParentheticalNode extends BaseNode
- type: 'Paren'
constructor: (expression) ->
@children: [@expression: expression]
@@ -1082,7 +1058,6 @@ exports.ParentheticalNode: class ParentheticalNode extends BaseNode
# the current index of the loop as a second parameter. Unlike Ruby blocks,
# you can map and filter in a single pass.
exports.ForNode: class ForNode extends BaseNode
- type: 'For'
constructor: (body, source, name, index) ->
@body: body
@@ -1163,7 +1138,6 @@ statement ForNode
# Single-expression **IfNodes** are compiled into ternary operators if possible,
# because ternaries are already proper expressions, and don't need conversion.
exports.IfNode: class IfNode extends BaseNode
- type: 'If'
constructor: (condition, body, else_body, tags) ->
@condition: condition
@@ -1304,7 +1278,7 @@ UTILITIES: {
# Correctly set up a prototype chain for inheritance, including a reference
# to the superclass for `super()` calls. See:
- # [goog.inherits](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line1206)
+ # [goog.inherits](http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line1206).
__extends: """
function(child, parent) {
var ctor = function(){ };
@@ -1316,7 +1290,7 @@ UTILITIES: {
"""
# Bind a function to a calling context, optionally including curried arguments.
- # See [Underscore's implementation](http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html#section-47)
+ # See [Underscore's implementation](http://jashkenas.github.com/coffee-script/documentation/docs/underscore.html#section-47).
__bind: """
function(func, obj, args) {
return function() {
@@ -1325,9 +1299,9 @@ UTILITIES: {
}
"""
- __hasProp:'Object.prototype.hasOwnProperty'
-
- __slice: 'Array.prototype.slice'
+ # Shortcuts to speed up the lookup time for native functions.
+ __hasProp: 'Object.prototype.hasOwnProperty'
+ __slice: 'Array.prototype.slice'
}