Adjusting refactor to better emulate map.

This commit is contained in:
Timothy Jones
2010-10-20 12:51:34 +13:00
parent 8fff6e9baf
commit 8c4a48d9d0
4 changed files with 35 additions and 209 deletions

View File

@@ -6,168 +6,6 @@
child.prototype = new ctor; child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child); if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype; child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __extends = function(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}; };
Scope = require('./scope').Scope; Scope = require('./scope').Scope;
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, include = _ref.include, starts = _ref.starts, ends = _ref.ends, last = _ref.last; _ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, include = _ref.include, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
@@ -412,7 +250,6 @@
exports.Literal = (function() { exports.Literal = (function() {
Literal = (function() { Literal = (function() {
function Literal(_arg) { function Literal(_arg) {
var _arg;
this.value = _arg; this.value = _arg;
Literal.__super__.constructor.call(this); Literal.__super__.constructor.call(this);
return this; return this;
@@ -450,7 +287,6 @@
exports.Return = (function() { exports.Return = (function() {
Return = (function() { Return = (function() {
function Return(_arg) { function Return(_arg) {
var _arg;
this.expression = _arg; this.expression = _arg;
Return.__super__.constructor.call(this); Return.__super__.constructor.call(this);
return this; return this;
@@ -486,7 +322,6 @@
exports.Value = (function() { exports.Value = (function() {
Value = (function() { Value = (function() {
function Value(_arg, _arg2, tag) { function Value(_arg, _arg2, tag) {
var _arg, _arg2;
this.properties = _arg2; this.properties = _arg2;
this.base = _arg; this.base = _arg;
Value.__super__.constructor.call(this); Value.__super__.constructor.call(this);
@@ -608,7 +443,6 @@
exports.Comment = (function() { exports.Comment = (function() {
Comment = (function() { Comment = (function() {
function Comment(_arg) { function Comment(_arg) {
var _arg;
this.comment = _arg; this.comment = _arg;
Comment.__super__.constructor.call(this); Comment.__super__.constructor.call(this);
return this; return this;
@@ -626,7 +460,6 @@
exports.Call = (function() { exports.Call = (function() {
Call = (function() { Call = (function() {
function Call(variable, _arg, _arg2) { function Call(variable, _arg, _arg2) {
var _arg, _arg2;
this.soakNode = _arg2; this.soakNode = _arg2;
this.args = _arg; this.args = _arg;
Call.__super__.constructor.call(this); Call.__super__.constructor.call(this);
@@ -765,7 +598,6 @@
exports.Extends = (function() { exports.Extends = (function() {
Extends = (function() { Extends = (function() {
function Extends(_arg, _arg2) { function Extends(_arg, _arg2) {
var _arg, _arg2;
this.parent = _arg2; this.parent = _arg2;
this.child = _arg; this.child = _arg;
Extends.__super__.constructor.call(this); Extends.__super__.constructor.call(this);
@@ -785,7 +617,6 @@
exports.Accessor = (function() { exports.Accessor = (function() {
Accessor = (function() { Accessor = (function() {
function Accessor(_arg, tag) { function Accessor(_arg, tag) {
var _arg;
this.name = _arg; this.name = _arg;
Accessor.__super__.constructor.call(this); Accessor.__super__.constructor.call(this);
this.prototype = tag === 'prototype' ? '.prototype' : ''; this.prototype = tag === 'prototype' ? '.prototype' : '';
@@ -808,7 +639,6 @@
exports.Index = (function() { exports.Index = (function() {
Index = (function() { Index = (function() {
function Index(_arg) { function Index(_arg) {
var _arg;
this.index = _arg; this.index = _arg;
Index.__super__.constructor.call(this); Index.__super__.constructor.call(this);
return this; return this;
@@ -831,7 +661,6 @@
exports.Range = (function() { exports.Range = (function() {
Range = (function() { Range = (function() {
function Range(_arg, _arg2, tag) { function Range(_arg, _arg2, tag) {
var _arg, _arg2;
this.to = _arg2; this.to = _arg2;
this.from = _arg; this.from = _arg;
Range.__super__.constructor.call(this); Range.__super__.constructor.call(this);
@@ -920,7 +749,6 @@
exports.Slice = (function() { exports.Slice = (function() {
Slice = (function() { Slice = (function() {
function Slice(_arg) { function Slice(_arg) {
var _arg;
this.range = _arg; this.range = _arg;
Slice.__super__.constructor.call(this); Slice.__super__.constructor.call(this);
return this; return this;
@@ -1003,7 +831,6 @@
exports.ArrayLiteral = (function() { exports.ArrayLiteral = (function() {
ArrayLiteral = (function() { ArrayLiteral = (function() {
function ArrayLiteral(_arg) { function ArrayLiteral(_arg) {
var _arg;
this.objects = _arg; this.objects = _arg;
ArrayLiteral.__super__.constructor.call(this); ArrayLiteral.__super__.constructor.call(this);
this.objects || (this.objects = []); this.objects || (this.objects = []);
@@ -1049,7 +876,6 @@
exports.Class = (function() { exports.Class = (function() {
Class = (function() { Class = (function() {
function Class(variable, _arg, _arg2) { function Class(variable, _arg, _arg2) {
var _arg, _arg2;
this.properties = _arg2; this.properties = _arg2;
this.parent = _arg; this.parent = _arg;
Class.__super__.constructor.call(this); Class.__super__.constructor.call(this);
@@ -1148,7 +974,6 @@
exports.Assign = (function() { exports.Assign = (function() {
Assign = (function() { Assign = (function() {
function Assign(_arg, _arg2, _arg3) { function Assign(_arg, _arg2, _arg3) {
var _arg, _arg2, _arg3;
this.context = _arg3; this.context = _arg3;
this.value = _arg2; this.value = _arg2;
this.variable = _arg; this.variable = _arg;
@@ -1282,7 +1107,6 @@
exports.Code = (function() { exports.Code = (function() {
Code = (function() { Code = (function() {
function Code(_arg, _arg2, tag) { function Code(_arg, _arg2, tag) {
var _arg, _arg2;
this.body = _arg2; this.body = _arg2;
this.params = _arg; this.params = _arg;
Code.__super__.constructor.call(this); Code.__super__.constructor.call(this);
@@ -1373,7 +1197,6 @@
exports.Param = (function() { exports.Param = (function() {
Param = (function() { Param = (function() {
function Param(_arg, _arg2, _arg3) { function Param(_arg, _arg2, _arg3) {
var _arg, _arg2, _arg3;
this.splat = _arg3; this.splat = _arg3;
this.attach = _arg2; this.attach = _arg2;
this.name = _arg; this.name = _arg;
@@ -1642,7 +1465,6 @@
exports.In = (function() { exports.In = (function() {
In = (function() { In = (function() {
function In(_arg, _arg2) { function In(_arg, _arg2) {
var _arg, _arg2;
this.array = _arg2; this.array = _arg2;
this.object = _arg; this.object = _arg;
In.__super__.constructor.call(this); In.__super__.constructor.call(this);
@@ -1681,7 +1503,6 @@
exports.Try = (function() { exports.Try = (function() {
Try = (function() { Try = (function() {
function Try(_arg, _arg2, _arg3, _arg4) { function Try(_arg, _arg2, _arg3, _arg4) {
var _arg, _arg2, _arg3, _arg4;
this.ensure = _arg4; this.ensure = _arg4;
this.recovery = _arg3; this.recovery = _arg3;
this.error = _arg2; this.error = _arg2;
@@ -1718,7 +1539,6 @@
exports.Throw = (function() { exports.Throw = (function() {
Throw = (function() { Throw = (function() {
function Throw(_arg) { function Throw(_arg) {
var _arg;
this.expression = _arg; this.expression = _arg;
Throw.__super__.constructor.call(this); Throw.__super__.constructor.call(this);
return this; return this;
@@ -1737,7 +1557,6 @@
exports.Existence = (function() { exports.Existence = (function() {
Existence = (function() { Existence = (function() {
function Existence(_arg) { function Existence(_arg) {
var _arg;
this.expression = _arg; this.expression = _arg;
Existence.__super__.constructor.call(this); Existence.__super__.constructor.call(this);
return this; return this;
@@ -1757,7 +1576,6 @@
exports.Parens = (function() { exports.Parens = (function() {
Parens = (function() { Parens = (function() {
function Parens(_arg) { function Parens(_arg) {
var _arg;
this.expression = _arg; this.expression = _arg;
Parens.__super__.constructor.call(this); Parens.__super__.constructor.call(this);
return this; return this;
@@ -1794,7 +1612,7 @@
exports.For = (function() { exports.For = (function() {
For = (function() { For = (function() {
function For(_arg, source, _arg2, _arg3) { function For(_arg, source, _arg2, _arg3) {
var _arg, _arg2, _arg3, _ref2; var _ref2;
this.index = _arg3; this.index = _arg3;
this.name = _arg2; this.name = _arg2;
this.body = _arg; this.body = _arg;
@@ -1953,7 +1771,6 @@
exports.Switch = (function() { exports.Switch = (function() {
Switch = (function() { Switch = (function() {
function Switch(_arg, _arg2, _arg3) { function Switch(_arg, _arg2, _arg3) {
var _arg, _arg2, _arg3;
this.otherwise = _arg3; this.otherwise = _arg3;
this.cases = _arg2; this.cases = _arg2;
this.subject = _arg; this.subject = _arg;
@@ -2010,7 +1827,6 @@
exports.If = (function() { exports.If = (function() {
If = (function() { If = (function() {
function If(condition, _arg, tags) { function If(condition, _arg, tags) {
var _arg;
this.body = _arg; this.body = _arg;
this.tags = tags || (tags = {}); this.tags = tags || (tags = {});
this.condition = tags.invert ? condition.invert() : condition; this.condition = tags.invert ? condition.invert() : condition;

View File

@@ -8,7 +8,6 @@
return Rewriter; return Rewriter;
})(); })();
exports.Rewriter.prototype.rewrite = function(_arg) { exports.Rewriter.prototype.rewrite = function(_arg) {
var _arg;
this.tokens = _arg; this.tokens = _arg;
this.adjustComments(); this.adjustComments();
this.removeLeadingNewlines(); this.removeLeadingNewlines();

View File

@@ -4,7 +4,6 @@
exports.Scope = (function() { exports.Scope = (function() {
Scope = (function() { Scope = (function() {
function Scope(_arg, _arg2, _arg3) { function Scope(_arg, _arg2, _arg3) {
var _arg, _arg2, _arg3;
this.method = _arg3; this.method = _arg3;
this.expressions = _arg2; this.expressions = _arg2;
this.parent = _arg; this.parent = _arg;
@@ -14,6 +13,7 @@
type: 'arguments' type: 'arguments'
} }
]; ];
this.positions = {};
if (this.parent) { if (this.parent) {
this.garbage = this.parent.garbage; this.garbage = this.parent.garbage;
} else { } else {
@@ -25,6 +25,20 @@
return Scope; return Scope;
})(); })();
Scope.root = null; Scope.root = null;
Scope.prototype.setVar = function(name, type) {
if (this.positions.hasOwnProperty(name)) {
return this.variables.splice(this.positions[name], 1, {
name: name,
type: type
});
} else {
this.positions[name] = this.variables.length;
return this.variables.push({
name: name,
type: type
});
}
};
Scope.prototype.startLevel = function() { Scope.prototype.startLevel = function() {
return this.garbage.push([]); return this.garbage.push([]);
}; };
@@ -53,10 +67,7 @@
if (this.check(name, options)) { if (this.check(name, options)) {
return true; return true;
} }
this.variables.push({ this.setVar(name, 'var');
name: name,
type: 'var'
});
return false; return false;
}; };
Scope.prototype.any = function(fn) { Scope.prototype.any = function(fn) {
@@ -70,10 +81,7 @@
return false; return false;
}; };
Scope.prototype.parameter = function(name) { Scope.prototype.parameter = function(name) {
return this.variables.push({ return this.setVar(name, 'param');
name: name,
type: 'param'
});
}; };
Scope.prototype.check = function(name, options) { Scope.prototype.check = function(name, options) {
var _ref2, immediate; var _ref2, immediate;
@@ -102,22 +110,16 @@
while (this.check(temp = this.temporary(type, index)) && this.type(temp) !== 'reuse') { while (this.check(temp = this.temporary(type, index)) && this.type(temp) !== 'reuse') {
index++; index++;
} }
this.variables.push({ this.setVar(temp, 'var');
name: temp,
type: 'var'
});
if (this.garbage.length) { if (this.garbage.length) {
last(this.garbage).push(temp); last(this.garbage).push(temp);
} }
return temp; return temp;
}; };
Scope.prototype.assign = function(name, value) { Scope.prototype.assign = function(name, value) {
return this.variables.push({ return this.setVar(name, {
name: name, value: value,
type: { assigned: true
value: value,
assigned: true
}
}); });
}; };
Scope.prototype.hasDeclarations = function(body) { Scope.prototype.hasDeclarations = function(body) {

View File

@@ -19,11 +19,20 @@ exports.Scope = class Scope
# it wraps. # it wraps.
constructor: (@parent, @expressions, @method) -> constructor: (@parent, @expressions, @method) ->
@variables = [{name: 'arguments', type: 'arguments'}] @variables = [{name: 'arguments', type: 'arguments'}]
@positions = {}
if @parent if @parent
@garbage = @parent.garbage @garbage = @parent.garbage
else else
@garbage = [] @garbage = []
Scope.root = this Scope.root = this
# Adds a new variable or overrides an existing one.
setVar: (name, type) ->
if @positions.hasOwnProperty name
@variables.splice @positions[name], 1, {name, type}
else
@positions[name] = @variables.length
@variables.push {name, type}
# Create a new garbage level # Create a new garbage level
startLevel: -> startLevel: ->
@@ -42,7 +51,7 @@ exports.Scope = class Scope
# already exist. # already exist.
find: (name, options) -> find: (name, options) ->
return true if @check name, options return true if @check name, options
@variables.push {name, type: 'var'} @setVar name, 'var'
false false
# Test variables and return true the first time fn(v, k) returns true # Test variables and return true the first time fn(v, k) returns true
@@ -54,7 +63,7 @@ exports.Scope = class Scope
# Reserve a variable name as originating from a function parameter for this # Reserve a variable name as originating from a function parameter for this
# scope. No `var` required for internal references. # scope. No `var` required for internal references.
parameter: (name) -> parameter: (name) ->
@variables.push {name, type: 'param'} @setVar name, 'param'
# Just check to see if a variable has already been declared, without reserving, # Just check to see if a variable has already been declared, without reserving,
# walks up to the root scope. # walks up to the root scope.
@@ -80,14 +89,14 @@ exports.Scope = class Scope
freeVariable: (type) -> freeVariable: (type) ->
index = 0 index = 0
index++ while @check(temp = @temporary type, index) and @type(temp) isnt 'reuse' index++ while @check(temp = @temporary type, index) and @type(temp) isnt 'reuse'
@variables.push {name: temp, type: 'var'} @setVar temp, 'var'
last(@garbage).push temp if @garbage.length last(@garbage).push temp if @garbage.length
temp temp
# Ensure that an assignment is made at the top of this scope # Ensure that an assignment is made at the top of this scope
# (or at the top-level scope, if requested). # (or at the top-level scope, if requested).
assign: (name, value) -> assign: (name, value) ->
@variables.push {name, type: { value: value, assigned: true }} @setVar name, value: value, assigned: true
# 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?