diff options
-rw-r--r-- | lib/scope.js | 28 | ||||
-rw-r--r-- | lib/utils.js | 96 |
2 files changed, 50 insertions, 74 deletions
diff --git a/lib/scope.js b/lib/scope.js index c3a77746..17d87643 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -55,7 +55,7 @@ function SymbolDef(scope, orig, init) { this.mangled_name = null; this.undeclared = false; this.id = SymbolDef.next_id++; -}; +} SymbolDef.next_id = 1; @@ -556,17 +556,21 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){ }); var base54 = (function() { - var leading = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".split(""); - var digits = "0123456789".split(""); + var freq = Object.create(null); + function init(chars) { + var array = []; + for (var i = 0, len = chars.length; i < len; i++) { + var ch = chars[i]; + array.push(ch); + freq[ch] = -1e-2 * i; + } + return array; + } + var digits = init("0123456789"); + var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"); var chars, frequency; function reset() { - frequency = Object.create(null); - leading.forEach(function(ch) { - frequency[ch] = 0; - }); - digits.forEach(function(ch) { - frequency[ch] = 0; - }); + frequency = Object.create(freq); } base54.consider = function(str, delta) { for (var i = str.length; --i >= 0;) { @@ -577,7 +581,7 @@ var base54 = (function() { return frequency[b] - frequency[a]; } base54.sort = function() { - chars = mergeSort(leading, compare).concat(mergeSort(digits, compare)); + chars = leading.sort(compare).concat(digits.sort(compare)); }; base54.reset = reset; reset(); @@ -591,6 +595,6 @@ var base54 = (function() { base = 64; } while (num > 0); return ret; - }; + } return base54; })(); diff --git a/lib/utils.js b/lib/utils.js index 7e99f0d9..d4d28348 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -45,27 +45,25 @@ function characters(str) { return str.split(""); -}; +} function member(name, array) { return array.indexOf(name) >= 0; -}; +} function find_if(func, array) { for (var i = 0, n = array.length; i < n; ++i) { - if (func(array[i])) - return array[i]; + if (func(array[i])) return array[i]; } -}; +} function repeat_string(str, i) { if (i <= 0) return ""; if (i == 1) return str; var d = repeat_string(str, i >> 1); d += d; - if (i & 1) d += str; - return d; -}; + return i & 1 ? d + str : d; +} function configure_error_stack(fn) { Object.defineProperty(fn.prototype, "stack", { @@ -84,27 +82,23 @@ function configure_error_stack(fn) { function DefaultsError(msg, defs) { this.message = msg; this.defs = defs; -}; +} DefaultsError.prototype = Object.create(Error.prototype); DefaultsError.prototype.constructor = DefaultsError; DefaultsError.prototype.name = "DefaultsError"; configure_error_stack(DefaultsError); -DefaultsError.croak = function(msg, defs) { - throw new DefaultsError(msg, defs); -}; - function defaults(args, defs, croak) { - if (args === true) - args = {}; + if (args === true) args = {}; var ret = args || {}; - if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) - DefaultsError.croak("`" + i + "` is not a supported option", defs); + if (croak) for (var i in ret) if (HOP(ret, i) && !HOP(defs, i)) { + throw new DefaultsError("`" + i + "` is not a supported option", defs); + } for (var i in defs) if (HOP(defs, i)) { ret[i] = (args && HOP(args, i)) ? args[i] : defs[i]; } return ret; -}; +} function merge(obj, ext) { var count = 0; @@ -113,7 +107,7 @@ function merge(obj, ext) { count++; } return count; -}; +} function noop() {} function return_false() { return false; } @@ -172,43 +166,19 @@ var MAP = (function(){ function push_uniq(array, el) { if (array.indexOf(el) < 0) array.push(el); -}; +} function string_template(text, props) { return text.replace(/\{(.+?)\}/g, function(str, p){ return props && props[p]; }); -}; +} function remove(array, el) { for (var i = array.length; --i >= 0;) { if (array[i] === el) array.splice(i, 1); } -}; - -function mergeSort(array, cmp) { - if (array.length < 2) return array.slice(); - function merge(a, b) { - var r = [], ai = 0, bi = 0, i = 0; - while (ai < a.length && bi < b.length) { - cmp(a[ai], b[bi]) <= 0 - ? r[i++] = a[ai++] - : r[i++] = b[bi++]; - } - if (ai < a.length) r.push.apply(r, a.slice(ai)); - if (bi < b.length) r.push.apply(r, b.slice(bi)); - return r; - }; - function _ms(a) { - if (a.length <= 1) - return a; - var m = Math.floor(a.length / 2), left = a.slice(0, m), right = a.slice(m); - left = _ms(left); - right = _ms(right); - return merge(left, right); - }; - return _ms(array); -}; +} function makePredicate(words) { if (!Array.isArray(words)) words = words.split(" "); @@ -224,12 +194,12 @@ function all(array, predicate) { if (!predicate(array[i])) return false; return true; -}; +} function Dictionary() { this._values = Object.create(null); this._size = 0; -}; +} Dictionary.prototype = { set: function(key, val) { if (!this.has(key)) ++this._size; @@ -290,20 +260,22 @@ function HOP(obj, prop) { // a statement. function first_in_statement(stack) { var node = stack.parent(-1); - for (var i = 0, p; p = stack.parent(i); i++) { - if (p instanceof AST_Statement && p.body === node) - return true; - if ((p instanceof AST_Sequence && p.expressions[0] === node) || - (p.TYPE == "Call" && p.expression === node ) || - (p instanceof AST_Dot && p.expression === node ) || - (p instanceof AST_Sub && p.expression === node ) || - (p instanceof AST_Conditional && p.condition === node ) || - (p instanceof AST_Binary && p.left === node ) || - (p instanceof AST_UnaryPostfix && p.expression === node )) - { - node = p; - } else { - return false; + for (var i = 0, p; p = stack.parent(i++); node = p) { + if (p.TYPE == "Call") { + if (p.expression === node) continue; + } else if (p instanceof AST_Binary) { + if (p.left === node) continue; + } else if (p instanceof AST_Conditional) { + if (p.condition === node) continue; + } else if (p instanceof AST_PropAccess) { + if (p.expression === node) continue; + } else if (p instanceof AST_Sequence) { + if (p.expressions[0] === node) continue; + } else if (p instanceof AST_Statement) { + return p.body === node; + } else if (p instanceof AST_UnaryPostfix) { + if (p.expression === node) continue; } + return false; } } |