From 8c4a48d9d08ae04f2671d4cd3ce83b2882865434 Mon Sep 17 00:00:00 2001 From: Timothy Jones Date: Wed, 20 Oct 2010 12:51:34 +1300 Subject: [PATCH] Adjusting refactor to better emulate map. --- lib/nodes.js | 186 +---------------------------------------------- lib/rewriter.js | 1 - lib/scope.js | 40 +++++----- src/scope.coffee | 17 ++++- 4 files changed, 35 insertions(+), 209 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 6b368f34..8329a825 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -6,168 +6,6 @@ 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; - }, __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; _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() { Literal = (function() { function Literal(_arg) { - var _arg; this.value = _arg; Literal.__super__.constructor.call(this); return this; @@ -450,7 +287,6 @@ exports.Return = (function() { Return = (function() { function Return(_arg) { - var _arg; this.expression = _arg; Return.__super__.constructor.call(this); return this; @@ -486,7 +322,6 @@ exports.Value = (function() { Value = (function() { function Value(_arg, _arg2, tag) { - var _arg, _arg2; this.properties = _arg2; this.base = _arg; Value.__super__.constructor.call(this); @@ -608,7 +443,6 @@ exports.Comment = (function() { Comment = (function() { function Comment(_arg) { - var _arg; this.comment = _arg; Comment.__super__.constructor.call(this); return this; @@ -626,7 +460,6 @@ exports.Call = (function() { Call = (function() { function Call(variable, _arg, _arg2) { - var _arg, _arg2; this.soakNode = _arg2; this.args = _arg; Call.__super__.constructor.call(this); @@ -765,7 +598,6 @@ exports.Extends = (function() { Extends = (function() { function Extends(_arg, _arg2) { - var _arg, _arg2; this.parent = _arg2; this.child = _arg; Extends.__super__.constructor.call(this); @@ -785,7 +617,6 @@ exports.Accessor = (function() { Accessor = (function() { function Accessor(_arg, tag) { - var _arg; this.name = _arg; Accessor.__super__.constructor.call(this); this.prototype = tag === 'prototype' ? '.prototype' : ''; @@ -808,7 +639,6 @@ exports.Index = (function() { Index = (function() { function Index(_arg) { - var _arg; this.index = _arg; Index.__super__.constructor.call(this); return this; @@ -831,7 +661,6 @@ exports.Range = (function() { Range = (function() { function Range(_arg, _arg2, tag) { - var _arg, _arg2; this.to = _arg2; this.from = _arg; Range.__super__.constructor.call(this); @@ -920,7 +749,6 @@ exports.Slice = (function() { Slice = (function() { function Slice(_arg) { - var _arg; this.range = _arg; Slice.__super__.constructor.call(this); return this; @@ -1003,7 +831,6 @@ exports.ArrayLiteral = (function() { ArrayLiteral = (function() { function ArrayLiteral(_arg) { - var _arg; this.objects = _arg; ArrayLiteral.__super__.constructor.call(this); this.objects || (this.objects = []); @@ -1049,7 +876,6 @@ exports.Class = (function() { Class = (function() { function Class(variable, _arg, _arg2) { - var _arg, _arg2; this.properties = _arg2; this.parent = _arg; Class.__super__.constructor.call(this); @@ -1148,7 +974,6 @@ exports.Assign = (function() { Assign = (function() { function Assign(_arg, _arg2, _arg3) { - var _arg, _arg2, _arg3; this.context = _arg3; this.value = _arg2; this.variable = _arg; @@ -1282,7 +1107,6 @@ exports.Code = (function() { Code = (function() { function Code(_arg, _arg2, tag) { - var _arg, _arg2; this.body = _arg2; this.params = _arg; Code.__super__.constructor.call(this); @@ -1373,7 +1197,6 @@ exports.Param = (function() { Param = (function() { function Param(_arg, _arg2, _arg3) { - var _arg, _arg2, _arg3; this.splat = _arg3; this.attach = _arg2; this.name = _arg; @@ -1642,7 +1465,6 @@ exports.In = (function() { In = (function() { function In(_arg, _arg2) { - var _arg, _arg2; this.array = _arg2; this.object = _arg; In.__super__.constructor.call(this); @@ -1681,7 +1503,6 @@ exports.Try = (function() { Try = (function() { function Try(_arg, _arg2, _arg3, _arg4) { - var _arg, _arg2, _arg3, _arg4; this.ensure = _arg4; this.recovery = _arg3; this.error = _arg2; @@ -1718,7 +1539,6 @@ exports.Throw = (function() { Throw = (function() { function Throw(_arg) { - var _arg; this.expression = _arg; Throw.__super__.constructor.call(this); return this; @@ -1737,7 +1557,6 @@ exports.Existence = (function() { Existence = (function() { function Existence(_arg) { - var _arg; this.expression = _arg; Existence.__super__.constructor.call(this); return this; @@ -1757,7 +1576,6 @@ exports.Parens = (function() { Parens = (function() { function Parens(_arg) { - var _arg; this.expression = _arg; Parens.__super__.constructor.call(this); return this; @@ -1794,7 +1612,7 @@ exports.For = (function() { For = (function() { function For(_arg, source, _arg2, _arg3) { - var _arg, _arg2, _arg3, _ref2; + var _ref2; this.index = _arg3; this.name = _arg2; this.body = _arg; @@ -1953,7 +1771,6 @@ exports.Switch = (function() { Switch = (function() { function Switch(_arg, _arg2, _arg3) { - var _arg, _arg2, _arg3; this.otherwise = _arg3; this.cases = _arg2; this.subject = _arg; @@ -2010,7 +1827,6 @@ exports.If = (function() { If = (function() { function If(condition, _arg, tags) { - var _arg; this.body = _arg; this.tags = tags || (tags = {}); this.condition = tags.invert ? condition.invert() : condition; diff --git a/lib/rewriter.js b/lib/rewriter.js index 924d58b5..6999fc88 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -8,7 +8,6 @@ return Rewriter; })(); exports.Rewriter.prototype.rewrite = function(_arg) { - var _arg; this.tokens = _arg; this.adjustComments(); this.removeLeadingNewlines(); diff --git a/lib/scope.js b/lib/scope.js index 5a89fbdb..3e72f828 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -4,7 +4,6 @@ exports.Scope = (function() { Scope = (function() { function Scope(_arg, _arg2, _arg3) { - var _arg, _arg2, _arg3; this.method = _arg3; this.expressions = _arg2; this.parent = _arg; @@ -14,6 +13,7 @@ type: 'arguments' } ]; + this.positions = {}; if (this.parent) { this.garbage = this.parent.garbage; } else { @@ -25,6 +25,20 @@ return Scope; })(); 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() { return this.garbage.push([]); }; @@ -53,10 +67,7 @@ if (this.check(name, options)) { return true; } - this.variables.push({ - name: name, - type: 'var' - }); + this.setVar(name, 'var'); return false; }; Scope.prototype.any = function(fn) { @@ -70,10 +81,7 @@ return false; }; Scope.prototype.parameter = function(name) { - return this.variables.push({ - name: name, - type: 'param' - }); + return this.setVar(name, 'param'); }; Scope.prototype.check = function(name, options) { var _ref2, immediate; @@ -102,22 +110,16 @@ while (this.check(temp = this.temporary(type, index)) && this.type(temp) !== 'reuse') { index++; } - this.variables.push({ - name: temp, - type: 'var' - }); + this.setVar(temp, 'var'); if (this.garbage.length) { last(this.garbage).push(temp); } return temp; }; Scope.prototype.assign = function(name, value) { - return this.variables.push({ - name: name, - type: { - value: value, - assigned: true - } + return this.setVar(name, { + value: value, + assigned: true }); }; Scope.prototype.hasDeclarations = function(body) { diff --git a/src/scope.coffee b/src/scope.coffee index 88aac430..b14c8e97 100644 --- a/src/scope.coffee +++ b/src/scope.coffee @@ -19,11 +19,20 @@ exports.Scope = class Scope # it wraps. constructor: (@parent, @expressions, @method) -> @variables = [{name: 'arguments', type: 'arguments'}] + @positions = {} if @parent @garbage = @parent.garbage else @garbage = [] 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 startLevel: -> @@ -42,7 +51,7 @@ exports.Scope = class Scope # already exist. find: (name, options) -> return true if @check name, options - @variables.push {name, type: 'var'} + @setVar name, 'var' false # 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 # scope. No `var` required for internal references. parameter: (name) -> - @variables.push {name, type: 'param'} + @setVar name, 'param' # Just check to see if a variable has already been declared, without reserving, # walks up to the root scope. @@ -80,14 +89,14 @@ exports.Scope = class Scope freeVariable: (type) -> index = 0 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 temp # Ensure that an assignment is made at the top of this scope # (or at the top-level scope, if requested). 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 # given function body?