Permalink
Browse files

coffee#1638: helper functions are now declared last

  • Loading branch information...
satyr committed Aug 31, 2011
1 parent f470b45 commit 63d66d7095a382fe9708f4b1b338cbeec473b293
Showing with 124 additions and 88 deletions.
  1. +1 −1 extras/coco.js
  2. +1 −1 extras/mode-coco.js
  3. +52 −36 lib/ast.js
  4. +10 −5 lib/coco.js
  5. +5 −4 lib/coke.js
  6. +15 −11 lib/command.js
  7. +6 −5 lib/lexer.js
  8. +5 −2 lib/node.js
  9. +5 −4 lib/optparse.js
  10. +16 −13 src/ast.co
  11. +8 −6 test/compilation.co
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,19 +1,4 @@
-var Node, Negatable, Block, Atom, Literal, Var, Key, Index, Chain, Call, List, Obj, Prop, Arr, Op, Assign, Import, Of, Existence, Fun, Class, Super, Parens, Splat, Jump, Throw, Return, While, For, Try, Switch, Case, If, Label, JS, Util, UTILITIES, LEVEL_TOP, LEVEL_PAREN, LEVEL_LIST, LEVEL_COND, LEVEL_OP, LEVEL_CALL, PREC, TAB, ID, SIMPLENUM, _ref, __import = function(obj, src){
- var own = {}.hasOwnProperty;
- for (var key in src) if (own.call(src, key)) obj[key] = src[key];
- return obj;
-}, __clone = function(it){
- function fn(){ if (this.__proto__ !== it) this.__proto__ = it }
- return fn.prototype = it, new fn;
-}, __extends = function(sub, sup){
- function ctor(){} ctor.prototype = (sub.superclass = sup).prototype;
- (sub.prototype = new ctor).constructor = sub;
- if (typeof sup.extended == 'function') sup.extended(sub);
- return sub;
-}, __repeatString = function(str, n){
- for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
- return r;
-}, __importAll = function(obj, src){ for (var key in src) obj[key] = src[key]; return obj };
+var Node, Negatable, Block, Atom, Literal, Var, Key, Index, Chain, Call, List, Obj, Prop, Arr, Op, Assign, Import, Of, Existence, Fun, Class, Super, Parens, Splat, Jump, Throw, Return, While, For, Try, Switch, Case, If, Label, JS, Util, UTILITIES, LEVEL_TOP, LEVEL_PAREN, LEVEL_LIST, LEVEL_COND, LEVEL_OP, LEVEL_CALL, PREC, TAB, ID, SIMPLENUM, _ref;
(Node = function(){
throw Error('unimplemented');
}).prototype = {
@@ -159,15 +144,16 @@ var Node, Negatable, Block, Atom, Literal, Var, Key, Index, Chain, Call, List, O
})));
},
delegate: function(names, fn){
- var name, _i, _len, _fn = function(name){
- this[name] = function(it){
- return fn.call(this, name, it);
- };
- };
+ var name, _i, _len;
for (_i = 0, _len = names.length; _i < _len; ++_i) {
name = names[_i];
(_fn.call(this, name));
}
+ function _fn(name){
+ this[name] = function(it){
+ return fn.call(this, name, it);
+ };
+ }
},
children: [],
terminator: ';',
@@ -410,9 +396,9 @@ exports.Block = Block = (function(_super){
return [prefix] + code;
};
prototype.compileWithDeclarations = function(o){
- var code, i, node, rest, post, that, _ref, _len;
+ var pre, i, node, rest, post, that, _ref, _len;
o.level = LEVEL_TOP;
- code = '';
+ pre = '';
for (i = 0, _len = (_ref = this.lines).length; i < _len; ++i) {
node = _ref[i];
if (!(node.comment || node instanceof Literal)) {
@@ -421,16 +407,13 @@ exports.Block = Block = (function(_super){
}
if (i) {
rest = this.lines.splice(i, 9e9);
- code = this.compile(o);
+ pre = this.compile(o);
this.lines = rest;
}
- if (post = this.compile(o)) {
- code && (code += '\n');
+ if (!(post = this.compile(o))) {
+ return pre;
}
- if (that = (_ref = this.scope) != null ? _ref.vars(o) : void 8) {
- code += o.indent + that + ';\n';
- }
- return code + post;
+ return (pre && pre + "\n") + ((that = this.scope) ? that.emit(post, o.indent) : post);
};
return Block;
}(Node));
@@ -3006,21 +2989,31 @@ _ref.type = function(it){
var _ref;
return (_ref = this.variables[this.positions[it]]) != null ? _ref.type : void 8;
};
-_ref.vars = function(o){
- var usr, tmp, asn, name, type, that, _i, _ref, _len, _ref2;
+_ref.emit = function(code, tab){
+ var usr, tmp, asn, fun, name, type, that, val, _i, _ref, _len, _ref2;
usr = [];
tmp = [];
asn = [];
+ fun = [];
for (_i = 0, _len = (_ref = this.variables).length; _i < _len; ++_i) {
_ref2 = _ref[_i], name = _ref2.name, type = _ref2.type;
if (type === 'var' || type === 'reuse') {
('_' === name.charAt(0) ? tmp : usr).push(name);
- } else if (type.value) {
- asn.push(entab(name + ' = ' + type.value, o.indent));
+ } else if (that = type.value) {
+ if (~(val = entab(that, tab)).lastIndexOf('function(', 0)) {
+ fun.push("function " + name + val.slice(8));
+ } else {
+ asn.push(name + " = " + val);
+ }
}
}
if (that = usr.concat(tmp, asn).join(', ')) {
- return 'var ' + that;
+ code = tab + "var " + that + ";\n" + code;
+ }
+ if (that = fun.join("\n" + tab)) {
+ return code + "\n" + tab + that;
+ } else {
+ return code;
}
};
function YES(){
@@ -3038,7 +3031,7 @@ UTILITIES = {
'extends': 'function(sub, sup){\n function ctor(){} ctor.prototype = (sub.superclass = sup).prototype;\n (sub.prototype = new ctor).constructor = sub;\n if (typeof sup.extended == \'function\') sup.extended(sub);\n return sub;\n}',
bind: 'function(me, fn){ return function(){ return fn.apply(me, arguments) } }',
'import': 'function(obj, src){\n var own = {}.hasOwnProperty;\n for (var key in src) if (own.call(src, key)) obj[key] = src[key];\n return obj;\n}',
- importAll: 'function(obj, src){ for (var key in src) obj[key] = src[key]; return obj }',
+ importAll: 'function(obj, src){\n for (var key in src) obj[key] = src[key];\n return obj;\n}',
repeatString: 'function(str, n){\n for (var r = \'\'; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;\n return r;\n}',
repeatArray: 'function(arr, n){\n for (var r = []; n > 0; (n >>= 1) && (arr = arr.concat(arr)))\n if (n & 1) r.push.apply(r, arr);\n return r;\n}',
split: "''.split",
@@ -3073,4 +3066,27 @@ function utility(it){
}
function entab(code, tab){
return code.replace(/\n/g, '\n' + tab);
+}
+function __import(obj, src){
+ var own = {}.hasOwnProperty;
+ for (var key in src) if (own.call(src, key)) obj[key] = src[key];
+ return obj;
+}
+function __clone(it){
+ function fn(){ if (this.__proto__ !== it) this.__proto__ = it }
+ return fn.prototype = it, new fn;
+}
+function __extends(sub, sup){
+ function ctor(){} ctor.prototype = (sub.superclass = sup).prototype;
+ (sub.prototype = new ctor).constructor = sub;
+ if (typeof sup.extended == 'function') sup.extended(sub);
+ return sub;
+}
+function __repeatString(str, n){
+ for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
+ return r;
+}
+function __importAll(obj, src){
+ for (var key in src) obj[key] = src[key];
+ return obj;
}
View
@@ -1,8 +1,4 @@
-var lexer, parser, __import = function(obj, src){
- var own = {}.hasOwnProperty;
- for (var key in src) if (own.call(src, key)) obj[key] = src[key];
- return obj;
-}, __importAll = function(obj, src){ for (var key in src) obj[key] = src[key]; return obj };
+var lexer, parser;
lexer = require('./lexer');
parser = require('./parser').parser;
parser.yy = require('./ast');
@@ -54,4 +50,13 @@ if (require.extensions) {
if ('' + this === '[object BackstagePass]') {
this.EXPORTED_SYMBOLS = ['Coco'];
}
+}
+function __import(obj, src){
+ var own = {}.hasOwnProperty;
+ for (var key in src) if (own.call(src, key)) obj[key] = src[key];
+ return obj;
+}
+function __importAll(obj, src){
+ for (var key in src) obj[key] = src[key];
+ return obj;
}
View
@@ -1,8 +1,5 @@
#!/usr/bin/env node
-var Tasks, Aliases, Flags, Options, args, filename, _ref, __slice = [].slice, __repeatString = function(str, n){
- for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
- return r;
-};
+var Tasks, Aliases, Flags, Options, args, filename, _ref, __slice = [].slice;
Tasks = {
__proto__: null
};
@@ -85,4 +82,8 @@ function printTasks(){
say('\nTask options:\n' + that);
}
return say('\nCoke options:\n -f, --cokefile FILE use FILE as the Cokefile');
+}
+function __repeatString(str, n){
+ for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
+ return r;
}
View
@@ -1,15 +1,5 @@
#!/usr/bin/env node
-var o, that, $args, argv, filename, __join = [].join, __import = function(obj, src){
- var own = {}.hasOwnProperty;
- for (var key in src) if (own.call(src, key)) obj[key] = src[key];
- return obj;
-}, __bind = function(me, fn){ return function(){ return fn.apply(me, arguments) } }, __clone = function(it){
- function fn(){ if (this.__proto__ !== it) this.__proto__ = it }
- return fn.prototype = it, new fn;
-}, __repeatString = function(str, n){
- for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
- return r;
-};
+var o, that, $args, argv, filename, __join = [].join;
global.Coco = require('./coco');
global.fs = require('fs');
global.path = require('path');
@@ -372,4 +362,18 @@ function help(){
}
function version(){
return "Coco " + Coco.VERSION;
+}
+function __import(obj, src){
+ var own = {}.hasOwnProperty;
+ for (var key in src) if (own.call(src, key)) obj[key] = src[key];
+ return obj;
+}
+function __bind(me, fn){ return function(){ return fn.apply(me, arguments) } }
+function __clone(it){
+ function fn(){ if (this.__proto__ !== it) this.__proto__ = it }
+ return fn.prototype = it, new fn;
+}
+function __repeatString(str, n){
+ for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
+ return r;
}
View
@@ -1,7 +1,4 @@
-var string, TABS, INDENTS, LINES, BACKSLASHES, reslash, character, KEYWORDS_SHARED, KEYWORDS_UNUSED, KEYWORDS, COCO_ALIASES, ID, SYMBOL, SPACE, MULTIDENT, SIMPLESTR, JSTOKEN, BSTOKEN, NUMBER, NUMBER_OMIT, REGEX, HEREGEX_OMIT, LASTDENT, LINE_CONTINUER, OPENERS, CLOSERS, INVERSES, CHAIN, ARG, __clone = function(it){
- function fn(){ if (this.__proto__ !== it) this.__proto__ = it }
- return fn.prototype = it, new fn;
-}, __indexOf = [].indexOf || function(x){
+var string, TABS, INDENTS, LINES, BACKSLASHES, reslash, character, KEYWORDS_SHARED, KEYWORDS_UNUSED, KEYWORDS, COCO_ALIASES, ID, SYMBOL, SPACE, MULTIDENT, SIMPLESTR, JSTOKEN, BSTOKEN, NUMBER, NUMBER_OMIT, REGEX, HEREGEX_OMIT, LASTDENT, LINE_CONTINUER, OPENERS, CLOSERS, INVERSES, CHAIN, ARG, __indexOf = [].indexOf || function(x){
var i = -1, l = this.length;
while (++i < l) if (this.hasOwnProperty(i) && this[i] === x) return i;
return -1;
@@ -1336,4 +1333,8 @@ INVERSES = new function(){
}
};
CHAIN = ['(', '{', '[', 'ID', 'STRNUM', 'LITERAL', 'LET', 'WITH', 'WORDS'];
-ARG = CHAIN.concat(['...', 'UNARY', 'CREMENT', 'PARAM(', 'FUNCTION', 'IF', 'SWITCH', 'TRY', 'CLASS', 'SUPER', 'RANGE', 'LABEL', 'DO']);
+ARG = CHAIN.concat(['...', 'UNARY', 'CREMENT', 'PARAM(', 'FUNCTION', 'IF', 'SWITCH', 'TRY', 'CLASS', 'SUPER', 'RANGE', 'LABEL', 'DO']);
+function __clone(it){
+ function fn(){ if (this.__proto__ !== it) this.__proto__ = it }
+ return fn.prototype = it, new fn;
+}
View
@@ -1,4 +1,3 @@
-var __importAll = function(obj, src){ for (var key in src) obj[key] = src[key]; return obj };
module.exports = function(Coco){
var fs, path;
fs = require('fs');
@@ -24,4 +23,8 @@ module.exports = function(Coco){
bare: true
}), filename);
};
-};
+};
+function __importAll(obj, src){
+ for (var key in src) obj[key] = src[key];
+ return obj;
+}
Oops, something went wrong.

0 comments on commit 63d66d7

Please sign in to comment.