1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

nodes.coffee is continuing to roll along -- maybe a tenth implemented

This commit is contained in:
Jeremy Ashkenas 2010-02-08 19:49:39 -05:00
parent 135620b14a
commit aabfba9599
7 changed files with 730 additions and 699 deletions

View file

@ -1,5 +1,5 @@
(function(){
var compact, dup, flatten;
var Expressions, Node, TAB, TRAILING_WHITESPACE, __a, compact, del, dup, flatten, statement;
var __hasProp = Object.prototype.hasOwnProperty;
// The abstract base class for all CoffeeScript nodes.
// All nodes are implement a "compile_node" method, which performs the
@ -8,13 +8,6 @@
// generated code should be wrapped up in a closure. An options hash is passed
// and cloned throughout, containing messages from higher in the AST,
// information about the current scope, and indentation level.
exports.Node = function Node() {
var __a;
var arguments = Array.prototype.slice.call(arguments, 0);
this.values = arguments;
__a = this.name = this.constructor.name;
return Node === this.constructor ? this : __a;
};
exports.Expressions = function Expressions() {
var __a;
var arguments = Array.prototype.slice.call(arguments, 0);
@ -201,139 +194,124 @@
return this.values = values;
};
// Some helper functions
// TODO -- shallow (1 deep) flatten..
// need recursive version..
flatten = function flatten(aggList, newList) {
var __a, __b, item;
__a = newList;
for (__b = 0; __b < __a.length; __b++) {
item = __a[__b];
aggList.push(item);
}
return aggList;
};
compact = function compact(input) {
var __a, __b, __c, compected, item;
compected = [];
__a = []; __b = input;
// Tabs are two spaces for pretty printing.
TAB = ' ';
TRAILING_WHITESPACE = /\s+$/g;
// Flatten nested arrays recursively.
flatten = function flatten(list) {
var __a, __b, __c, item, memo;
memo = [];
__a = []; __b = list;
for (__c = 0; __c < __b.length; __c++) {
item = __b[__c];
__a.push((typeof item !== "undefined" && item !== null) ? compacted.push(item) : null);
if (item instanceof Array) {
return memo.concat(flatten(item));
}
memo.push(item);
memo;
}
return __a;
};
dup = function dup(input) {
var __a, __b, __c, key, output, val;
output = null;
if (input instanceof Array) {
output = [];
__a = input;
for (__b = 0; __b < __a.length; __b++) {
val = __a[__b];
output.push(val);
// Remove all null values from an array.
compact = function compact(input) {
var __a, __b, __c, item;
__a = []; __b = input;
for (__c = 0; __c < __b.length; __c++) {
item = __b[__c];
if ((typeof item !== "undefined" && item !== null)) {
__a.push(item);
}
}
return __a;
};
// Dup an array or object.
dup = function dup(input) {
var __a, __b, __c, __d, key, output, val;
if (input instanceof Array) {
__a = []; __b = input;
for (__c = 0; __c < __b.length; __c++) {
val = __b[__c];
__a.push(val);
}
return __a;
} else {
output = {
};
__c = input;
for (key in __c) {
val = __c[key];
if (__hasProp.call(__c, key)) {
output.key = val;
__d = input;
for (key in __d) {
val = __d[key];
if (__hasProp.call(__d, key)) {
((output[key] = val));
}
}
output;
return output;
}
return output;
};
exports.Node.prototype.TAB = ' ';
// Tag this node as a statement, meaning that it can't be used directly as
// the result of an expression.
exports.Node.prototype.mark_as_statement = function mark_as_statement() {
return this.is_statement = function is_statement() {
// Delete a key from an object, returning the value.
del = function del(obj, key) {
var val;
val = obj[key];
delete obj[key];
return val;
};
// # Provide a quick implementation of a children method.
// children: (klass, attrs...) ->
// klass::children: ->
// nodes: this[attr] for attr in attrs
// compact flatten nodes
// Mark a node as a statement, or a statement only.
statement = function statement(klass, only) {
klass.prototype.statement = function statement() {
return true;
};
};
// Tag this node as a statement that cannot be transformed into an expression.
// (break, continue, etc.) It doesn't make sense to try to transform it.
exports.Node.prototype.mark_as_statement_only = function mark_as_statement_only() {
this.mark_as_statement();
return this.is_statement_only = function is_statement_only() {
return true;
return klass.prototype.statement_only = function statement_only() {
if (only) {
return true;
}
};
};
// This node needs to know if it's being compiled as a top-level statement,
// in order to compile without special expression conversion.
exports.Node.prototype.mark_as_top_sensitive = function mark_as_top_sensitive() {
return this.is_top_sensitive = function is_top_sensitive() {
return true;
};
};
// Provide a quick implementation of a children method.
exports.Node.prototype.children = function children(attributes) {
var __a, __b, agg, compacted, item;
// TODO -- are these optimal impls of flatten and compact
// .. do better ones exist in a stdlib?
agg = [];
__a = attributes;
for (__b = 0; __b < __a.length; __b++) {
item = __a[__b];
agg = flatten(agg, item);
}
compacted = compact(agg);
return this.children = function children() {
return compacted;
};
};
exports.Node.prototype.write = function write(code) {
// hm..
// TODO -- should print to STDOUT in "VERBOSE" how to
// go about this.. ? jsonify 'this'?
// use node's puts ??
return code;
};
// The abstract base class for all CoffeeScript nodes.
// All nodes are implement a "compile_node" method, which performs the
// code generation for that node. To compile a node, call the "compile"
// method, which wraps "compile_node" in some extra smarts, to know when the
// generated code should be wrapped up in a closure. An options hash is passed
// and cloned throughout, containing messages from higher in the AST,
// information about the current scope, and indentation level.
Node = (exports.Node = function Node() { });
// This is extremely important -- we convert JS statements into expressions
// by wrapping them in a closure, only if it's possible, and we're not at
// the top level of a block (which would be unnecessary), and we haven't
// already been asked to return the result.
exports.Node.prototype.compile = function compile(o) {
var closure, opts, top;
// TODO -- need JS dup/clone
opts = (typeof !o !== "undefined" && !o !== null) ? {
} : o;
this.options = opts;
this.indent = opts.indent;
top = this.options.top;
!this.is_top_sentitive() ? (this.options.top = undefined) : null;
closure = this.is_statement() && !this.is_statement_only() && !top && typeof (this) === "CommentNode";
closure = closure && !this.do_i_contain(function(n) {
return n.is_statement_only();
Node.prototype.compile = function compile(o) {
var closure, top;
this.options = dup(o || {
});
return closure ? this.compile_closure(this.options) : compile_node(this.options);
this.indent = o.indent;
top = this.top_sensitive() ? o.top : del(obj('top'));
closure = this.statement() && !this.statement_only() && !top && !o.returns && !this instanceof CommentNode && !this.contains(function(node) {
return node.statement_only();
});
return closure ? this.compile_closure(this.options) : this.compile_node(this.options);
};
// Statements converted into expressions share scope with their parent
// closure, to preserve JavaScript-style lexical scope.
exports.Node.prototype.compile_closure = function compile_closure(o) {
var opts;
opts = (typeof !o !== "undefined" && !o !== null) ? {
} : o;
this.indent = opts.indent;
opts.shared_scope = o.scope;
return exports.ClosureNode.wrap(this).compile(opts);
Node.prototype.compile_closure = function compile_closure(o) {
this.indent = o.indent;
o.shared_scope = o.scope;
return ClosureNode.wrap(this).compile(o);
};
// Quick short method for the current indentation level, plus tabbing in.
exports.Node.prototype.idt = function idt(tLvl) {
var __a, __b, __c, __d, tabAmt, tabs, x;
tabs = (typeof tLvl !== "undefined" && tLvl !== null) ? tLvl : 0;
tabAmt = '';
__c = 0; __d = tabs;
for (__b=0, x=__c; (__c <= __d ? x < __d : x > __d); (__c <= __d ? x += 1 : x -= 1), __b++) {
tabAmt = tabAmt + this.TAB;
Node.prototype.idt = function idt(tabs) {
var __a, __b, __c, __d, i, idt;
idt = this.indent;
__c = 0; __d = (tabs || 0);
for (__b=0, i=__c; (__c <= __d ? i <= __d : i >= __d); (__c <= __d ? i += 1 : i -= 1), __b++) {
idt += TAB;
}
return this.indent + tabAmt;
return idt;
};
//Does this node, or any of it's children, contain a node of a certain kind?
exports.Node.prototype.do_i_contain = function do_i_contain(block) {
// Does this node, or any of its children, contain a node of a certain kind?
Node.prototype.contains = function contains(block) {
var __a, __b, node;
__a = this.children;
for (__b = 0; __b < __a.length; __b++) {
@ -341,103 +319,96 @@
if (block(node)) {
return true;
}
if (node instanceof exports.Node && node.do_i_contain(block)) {
if (node instanceof Node && node.contains(block)) {
return true;
}
}
return false;
};
// Default implementations of the common node methods.
exports.Node.prototype.unwrap = function unwrap() {
Node.prototype.unwrap = function unwrap() {
return this;
};
exports.Node.prototype.children = [];
exports.Node.prototype.is_a_statement = function is_a_statement() {
Node.prototype.children = [];
Node.prototype.statement = function statement() {
return false;
};
exports.Node.prototype.is_a_statement_only = function is_a_statement_only() {
Node.prototype.statement_only = function statement_only() {
return false;
};
exports.Node.prototype.is_top_sensitive = function is_top_sensitive() {
Node.prototype.top_sensitive = function top_sensitive() {
return false;
};
// A collection of nodes, each one representing an expression.
// exports.Expressions: (nodes) ->
// this.mark_as_statement()
// this.expressions: []
// this.children([this.expressions])
// for n in nodes
// this.expressions: flatten this.expressions, n
// exports.Expressions extends exports.Node
exports.Expressions.prototype.TRAILING_WHITESPACE = /\s+$/;
Expressions = (exports.Expressions = function Expressions() {
var __a, nodes;
nodes = Array.prototype.slice.call(arguments, 0);
this.expressions = flatten(nodes);
__a = this.children = this.expressions;
return Expressions === this.constructor ? this : __a;
});
__a = function(){};
__a.prototype = Node.prototype;
Expressions.__superClass__ = Node.prototype;
Expressions.prototype = new __a();
Expressions.prototype.constructor = Expressions;
statement(Expressions);
// Wrap up a node as an Expressions, unless it already is.
exports.Expressions.prototype.wrap = function wrap(nodes) {
if (nodes.length === 1 && nodes[0] instanceof exports.Expressions) {
Expressions.prototype.wrap = function wrap() {
var nodes;
nodes = Array.prototype.slice.call(arguments, 0);
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
return nodes[0];
}
return new Expressions(nodes);
return new Expressions.apply(this, nodes);
};
// Tack an expression on to the end of this expression list.
exports.Expressions.prototype.push = function push(node) {
Expressions.prototype.push = function push(node) {
this.expressions.push(node);
return this;
};
// Tack an expression on to the beginning of this expression list.
exports.Expressions.prototype.unshift = function unshift(node) {
Expressions.prototype.unshift = function unshift(node) {
this.expressions.unshift(node);
return this;
};
// If this Expressions consists of a single node, pull it back out.
exports.Expressions.prototype.unwrap = function unwrap() {
Expressions.prototype.unwrap = function unwrap() {
return this.expressions.length === 1 ? this.expressions[0] : this;
};
// Is this an empty block of code?
exports.Expressions.prototype.is_empty = function is_empty() {
Expressions.prototype.empty = function empty() {
return this.expressions.length === 0;
};
// Is the node last in this block of expressions.
exports.Expressions.prototype.is_last = function is_last(node) {
var arr_length;
arr_length = this.expressions.length;
this.last_index = this.last_index || this.expressions[arr_length - 1] instanceof exports.CommentNode ? -2 : -1;
return node === this.expressions[arr_length - this.last_index];
// Is the node last in this block of expressions?
Expressions.prototype.is_last = function is_last(node) {
var l;
l = this.expressions.length;
this.last_index = this.last_index || this.expressions[l - 1] instanceof CommentNode ? -2 : -1;
return node === this.expressions[l - this.last_index];
};
exports.Expressions.prototype.compile = function compile(o) {
var opts;
opts = (typeof o !== "undefined" && o !== null) ? o : {
};
return opts.scope ? exports.Expressions.__superClass__.compile.call(this, dup(opts)) : this.compile_root(o);
Expressions.prototype.compile = function compile(o) {
return o.scope ? Expressions.__superClass__.compile.call(this, o) : this.compile_root(o);
};
// Compile each expression in the Expressions body.
exports.Expressions.prototype.compile_node = function compile_node(options) {
var __a, __b, __c, __d, __e, code, compiled, e, line, opts;
opts = (typeof options !== "undefined" && options !== null) ? options : {
};
compiled = [];
__a = this.expressions;
for (__b = 0; __b < __a.length; __b++) {
e = __a[__b];
compiled.push(this.compile_expression(e, dup(options)));
}
code = '';
__c = []; __d = compiled;
for (__e = 0; __e < __d.length; __e++) {
line = __d[__e];
__c.push((code = code + line + '\n'));
}
return __c;
Expressions.prototype.compile_node = function compile_node(o) {
var __b, __c, __d, node;
return ((function() {
__b = []; __c = this.expressions;
for (__d = 0; __d < __c.length; __d++) {
node = __c[__d];
__b.push(this.compile_expression(node, dup(o)));
}
return __b;
}).call(this)).join("\n");
};
// If this is the top-level Expressions, wrap everything in a safety closure.
exports.Expressions.prototype.compile_root = function compile_root(o) {
var code, indent, opts;
opts = (typeof o !== "undefined" && o !== null) ? o : {
};
indent = opts.no_wrap ? '' : this.TAB;
this.indent = indent;
opts.indent = indent;
opts.scope = new Scope(null, this, null);
code = opts.globals ? compile_node(opts) : compile_with_declarations(opts);
code.replace(this.TRAILING_WHITESPACE, '');
return this.write(opts.no_wrap ? code : "(function(){\n" + code + "\n})();");
Expressions.prototype.compile_root = function compile_root(o) {
var code, indent;
o.indent = (this.indent = (indent = o.no_wrap ? '' : TAB));
o.scope = new Scope(null, this, null);
code = o.globals ? this.compile_node(o) : this.compile_with_declarations(o);
code = code.replace(TRAILING_WHITESPACE, '');
return o.no_wrap ? code : "(function(){\n" + code + "\n})();";
};
})();