gorillascript
Version:
GorillaScript is a compile-to-JavaScript language designed to empower the user while attempting to prevent some common errors.
1,655 lines • 169 kB
JavaScript
(function () {
"use strict";
var __cmp, __create, __import, __in, __isArray, __name, __owns, __slice,
__toArray, __typeof, _ref, Arguments, Arr, AstTypeToClass, Binary, Block,
BlockExpression, BlockStatement, Break, Call, Comment, Const, Continue,
Debugger, DoWhile, Eval, Expression, For, ForIn, fromJSON, Func,
getIndent, Ident, If, IfExpression, IfStatement, inspect,
isAcceptableIdent, NEWLINE_REGEXP, Node, Noop, Obj, padLeft, Regex,
Return, Root, Statement, Switch, This, Throw, toJSIdent, toJSSource,
TryCatch, TryFinally, Unary, util, While;
__cmp = function (left, right) {
var type;
if (left === right) {
return 0;
} else {
type = typeof left;
if (type !== "number" && type !== "string") {
throw new TypeError("Cannot compare a non-number/string: " + type);
} else if (type !== typeof right) {
throw new TypeError("Cannot compare elements of different types: " + type + " vs " + typeof right);
} else if (left < right) {
return -1;
} else {
return 1;
}
}
};
__create = typeof Object.create === "function" ? Object.create
: function (x) {
function F() {}
F.prototype = x;
return new F();
};
__import = function (dest, source) {
var k;
for (k in source) {
if (__owns.call(source, k)) {
dest[k] = source[k];
}
}
return dest;
};
__in = typeof Array.prototype.indexOf === "function"
? (function (indexOf) {
return function (child, parent) {
return indexOf.call(parent, child) !== -1;
};
}(Array.prototype.indexOf))
: function (child, parent) {
var i, len;
len = +parent.length;
i = -1;
while (++i < len) {
if (child === parent[i] && i in parent) {
return true;
}
}
return false;
};
__isArray = typeof Array.isArray === "function" ? Array.isArray
: (function (_toString) {
return function (x) {
return _toString.call(x) === "[object Array]";
};
}(Object.prototype.toString));
__name = function (func) {
if (typeof func !== "function") {
throw new TypeError("Expected func to be a Function, got " + __typeof(func));
}
return func.displayName || func.name || "";
};
__owns = Object.prototype.hasOwnProperty;
__slice = Array.prototype.slice;
__toArray = function (x) {
if (x == null) {
throw new TypeError("Expected an object, got " + __typeof(x));
} else if (__isArray(x)) {
return x;
} else if (typeof x === "string") {
return x.split("");
} else if (typeof x.length === "number") {
return __slice.call(x);
} else {
throw new TypeError("Expected an object with a length property, got " + __typeof(x));
}
};
__typeof = (function () {
var _toString;
_toString = Object.prototype.toString;
return function (o) {
if (o === void 0) {
return "Undefined";
} else if (o === null) {
return "Null";
} else {
return o.constructor && o.constructor.name || _toString.call(o).slice(8, -1);
}
};
}());
util = require("util");
if (util != null) {
inspect = util.inspect;
}
padLeft = require("./utils").padLeft;
_ref = require("./jsutils");
isAcceptableIdent = _ref.isAcceptableIdent;
toJSSource = _ref.toJSSource;
_ref = null;
function incIndent(options) {
var clone;
clone = __create(options);
++clone.indent;
return clone;
}
getIndent = (function () {
var cache;
cache = [""];
return function (indent) {
var i, result;
if (indent >= cache.length) {
result = cache[cache.length - 1];
for (i = cache.length; i <= indent; ++i) {
result += " ";
cache.push(result);
}
}
return cache[indent];
};
}());
NEWLINE_REGEXP = /(?:\r\n?|[\n\u2028\u2029])/g;
function wrapStringHandler(callback) {
function cb(item) {
var len, parts, s;
s = String(item);
parts = s.split(NEWLINE_REGEXP);
switch (parts.length) {
case 0:
break;
case 1:
cb.column -= -parts[0].length;
break;
default:
len = parts.length;
cb.line -= -len + 1;
cb.column = +parts[len - 1].length + 1;
}
callback(s);
}
cb.line = 1;
cb.column = 1;
cb.indent = function (count) {
this(getIndent(count));
};
return cb;
}
function StringWriter(callback) {
return wrapStringHandler(callback);
}
function StringBuilder() {
var data, sb;
data = [];
sb = wrapStringHandler(function (item) {
data.push(item);
});
sb.toString = function () {
if (typeof data === "string") {
return data;
} else {
return data = data.join("");
}
};
return sb;
}
exports.Node = Node = (function () {
var _Node_prototype;
function Node() {
var _this;
_this = this instanceof Node ? this : __create(_Node_prototype);
throw new Error("Node cannot be instantiated directly");
}
_Node_prototype = Node.prototype;
Node.displayName = "Node";
_Node_prototype.toString = function (options) {
var sb;
if (options == null) {
options = {};
}
sb = StringBuilder();
this.compile(
__import(
{ indent: 0, bare: true },
options
),
2,
false,
sb
);
return sb.toString();
};
_Node_prototype.compile = function () {
throw new Error("Not implemented: " + __name(this.constructor) + ".compile()");
};
_Node_prototype.maybeToStatement = function () {
if (typeof this.toStatement === "function") {
return this.toStatement();
} else {
return this;
}
};
_Node_prototype.isConst = function () {
return false;
};
_Node_prototype.isNoop = function () {
return false;
};
_Node_prototype.constValue = function () {
throw new Error(this.constructor.name + " has no const value");
};
_Node_prototype.isLarge = function () {
return true;
};
_Node_prototype.isSmall = function () {
return !this.isLarge();
};
_Node_prototype.mutateLast = function () {
return this;
};
_Node_prototype.exitType = function () {
return null;
};
_Node_prototype.last = function () {
return this;
};
_Node_prototype.removeTrailingReturnVoids = function () {
return this;
};
_Node_prototype.walkWithThis = function (walker) {
var _ref;
if ((_ref = walker(this)) != null) {
return _ref;
} else {
return this.walk(walker);
}
};
function inspectArray(depth, array) {
var _arr, _i, _len, item, sb;
if (array.length === 0) {
return "[]";
} else {
sb = "";
sb += "[";
for (_arr = __toArray(array), _i = 0, _len = _arr.length; _i < _len; ++_i) {
item = _arr[_i];
sb += "\n ";
sb += inspect(item, null, decDepth(depth)).split("\n").join("\n ");
}
sb += "\n]";
return sb;
}
}
_Node_prototype.inspect = function (depth, includeEmpty) {
var k, sb, v;
sb = this.constructor.displayName;
sb += ' "';
if (this.pos.file) {
sb += this.pos.file;
sb += ":";
}
sb += this.pos.line;
sb += ":";
sb += this.pos.column;
sb += '"';
for (k in this) {
if (__owns.call(this, k)) {
v = this[k];
if (k !== "pos" && (includeEmpty || v && (!__isArray(v) || v.length !== 0) && !(v instanceof Noop))) {
sb += "\n ";
sb += k;
sb += ": ";
if (__isArray(v)) {
sb += inspectArray(depth, v).split("\n").join("\n ");
} else {
sb += inspect(v, null, decDepth(depth)).split("\n").join("\n ");
}
}
}
}
return sb;
};
_Node_prototype.toAst = function (pos, ident) {
return Call(pos, ident, [
Const(pos, this.typeId),
Const(pos, this.pos.line),
Const(pos, this.pos.column),
Const(pos, this.pos.file || 0)
].concat(__toArray(this._toAst(pos, ident))));
};
_Node_prototype._toAst = function () {
return [];
};
_Node_prototype.toJSON = function () {
return [this.typeId, this.pos.line, this.pos.column, this.pos.file || 0].concat(__toArray(this._toJSON()));
};
_Node_prototype._toJSON = function () {
return [];
};
return Node;
}());
exports.Expression = Expression = (function (Node) {
var _Expression_prototype, _Node_prototype;
function Expression() {
var _this;
_this = this instanceof Expression ? this : __create(_Expression_prototype);
throw new Error("Expression cannot be instantiated directly");
}
_Node_prototype = Node.prototype;
_Expression_prototype = Expression.prototype = __create(_Node_prototype);
_Expression_prototype.constructor = Expression;
Expression.displayName = "Expression";
if (typeof Node.extended === "function") {
Node.extended(Expression);
}
_Expression_prototype.compileAsBlock = function (options, level, lineStart, sb) {
this.compile(options, level, lineStart, sb);
};
_Expression_prototype.compileAsStatement = function (options, lineStart, sb) {
if (typeof this.toStatement === "function") {
this.toStatement().compileAsStatement(options, lineStart, sb);
} else {
this.compile(options, 1, lineStart, sb);
sb(";");
}
};
_Expression_prototype.isLarge = function () {
return false;
};
_Expression_prototype.invert = function () {
return Unary(this.pos, "!", this);
};
_Expression_prototype.mutateLast = function (func) {
return func(this);
};
_Expression_prototype.withLabel = function (label) {
if (label == null) {
label = null;
}
if (label) {
return BlockStatement(this.pos, [this], label);
} else {
return this;
}
};
return Expression;
}(Node));
exports.Statement = Statement = (function (Node) {
var _Node_prototype, _Statement_prototype;
function Statement() {
var _this;
_this = this instanceof Statement ? this : __create(_Statement_prototype);
throw new Error("Expression cannot be instantiated directly");
}
_Node_prototype = Node.prototype;
_Statement_prototype = Statement.prototype = __create(_Node_prototype);
_Statement_prototype.constructor = Statement;
Statement.displayName = "Statement";
if (typeof Node.extended === "function") {
Node.extended(Statement);
}
_Statement_prototype.compileAsStatement = function (options, lineStart, sb) {
return this.compile(options, 1, lineStart, sb);
};
return Statement;
}(Node));
exports.Access = function (pos, parent) {
var _i, _len, child, children, current;
children = __slice.call(arguments, 2);
current = parent;
for (_i = 0, _len = children.length; _i < _len; ++_i) {
child = children[_i];
current = Binary(pos, current, ".", child);
}
return current;
};
function makePos(line, column, file) {
var pos;
if (file == null) {
file = void 0;
}
pos = { line: line, column: column };
if (file) {
if (typeof file !== "string") {
throw new TypeError("Must provide a valid string for file");
}
pos.file = file;
}
return pos;
}
function decDepth(depth) {
if (depth != null) {
return depth - 1;
} else {
return null;
}
}
exports.Arguments = Arguments = (function (Expression) {
var _Arguments_prototype, _Expression_prototype;
function Arguments(pos) {
var _this;
_this = this instanceof Arguments ? this : __create(_Arguments_prototype);
_this.pos = pos;
return _this;
}
_Expression_prototype = Expression.prototype;
_Arguments_prototype = Arguments.prototype = __create(_Expression_prototype);
_Arguments_prototype.constructor = Arguments;
Arguments.displayName = "Arguments";
if (typeof Expression.extended === "function") {
Expression.extended(Arguments);
}
_Arguments_prototype.compile = function (options, level, lineStart, sb) {
var _ref;
if ((_ref = options.sourceMap) != null) {
_ref.add(
sb.line,
sb.column,
this.pos.line,
this.pos.column,
this.pos.file
);
}
sb("arguments");
};
_Arguments_prototype.compileAsBlock = function (options, level, lineStart, sb) {
Noop(this.pos).compileAsBlock(options, level, lineStart, sb);
};
_Arguments_prototype.walk = function () {
return this;
};
_Arguments_prototype.isNoop = function () {
return true;
};
_Arguments_prototype.typeId = 1;
Arguments._fromAst = function (pos) {
return Arguments(pos);
};
Arguments.fromJSON = function (line, column, file) {
return Arguments(makePos(line, column, file));
};
return Arguments;
}(Expression));
function walkArray(array, parent, message, walker) {
var _arr, _i, _len, _ref, changed, item, newItem, result;
changed = false;
_arr = [];
for (_i = 0, _len = array.length; _i < _len; ++_i) {
item = array[_i];
if ((_ref = walker(item, parent, message)) != null) {
newItem = _ref;
} else {
newItem = item.walk(walker);
}
if (item !== newItem) {
changed = true;
}
_arr.push(newItem);
}
result = _arr;
if (changed) {
return result;
} else {
return array;
}
}
function simplifyArray(array, childDefaultValue, keepTrailing) {
var _len, i, item, lastNoop, result;
if (keepTrailing == null) {
keepTrailing = false;
}
if (array.length === 0) {
return array;
} else {
result = [];
lastNoop = -1;
for (i = 0, _len = array.length; i < _len; ++i) {
item = array[i];
if (item instanceof Noop) {
lastNoop = i;
} else {
lastNoop = -1;
}
result.push(simplify(item, childDefaultValue));
}
if (!keepTrailing && lastNoop !== -1) {
result.splice(lastNoop, 1/0);
}
return result;
}
}
function simplify(obj, defaultValue) {
if (__isArray(obj)) {
return simplifyArray(obj);
} else if (obj instanceof Noop) {
return defaultValue;
} else {
return obj;
}
}
exports.Arr = Arr = (function (Expression) {
var _Arr_prototype, _Expression_prototype;
function Arr(pos, elements) {
var _this;
_this = this instanceof Arr ? this : __create(_Arr_prototype);
_this.pos = pos;
if (elements == null) {
elements = [];
}
_this.elements = elements;
return _this;
}
_Expression_prototype = Expression.prototype;
_Arr_prototype = Arr.prototype = __create(_Expression_prototype);
_Arr_prototype.constructor = Arr;
Arr.displayName = "Arr";
if (typeof Expression.extended === "function") {
Expression.extended(Arr);
}
function compileLarge(elements, options, level, lineStart, sb) {
var _arr, childOptions, i, item, len;
childOptions = incIndent(options);
for (_arr = __toArray(elements), i = 0, len = _arr.length; i < len; ++i) {
item = _arr[i];
sb(options.linefeed || "\n");
sb.indent(childOptions.indent);
item.compile(childOptions, 3, false, sb);
if (i < len - 1) {
sb(",");
}
}
sb(options.linefeed || "\n");
sb.indent(options.indent);
}
function compileSmall(elements, options, level, lineStart, sb) {
var _arr, _len, i, item;
for (_arr = __toArray(elements), i = 0, _len = _arr.length; i < _len; ++i) {
item = _arr[i];
if (i > 0) {
sb(",");
if (!options.minify) {
sb(" ");
}
}
item.compile(options, 3, false, sb);
}
}
_Arr_prototype.compile = function (options, level, lineStart, sb) {
var _ref, f;
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.pushFile(this.pos.file);
}
if ((_ref = options.sourceMap) != null) {
_ref.add(sb.line, sb.column, this.pos.line, this.pos.column);
}
sb("[");
if (!options.minify && this.shouldCompileLarge()) {
f = compileLarge;
} else {
f = compileSmall;
}
f(
this.elements,
options,
level,
lineStart,
sb
);
sb("]");
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.popFile();
}
};
_Arr_prototype.compileAsBlock = function (options, level, lineStart, sb) {
return BlockExpression(this.pos, this.elements).compileAsBlock(options, level, lineStart, sb);
};
_Arr_prototype.compileAsStatement = function (options, lineStart, sb) {
BlockStatement(this.pos, this.elements).compile(options, 1, lineStart, sb);
};
_Arr_prototype.shouldCompileLarge = function () {
switch (this.elements.length) {
case 0: return false;
case 1: return this.elements[0].isLarge();
default: return this.isLarge();
}
};
_Arr_prototype.isSmall = function () {
switch (this.elements.length) {
case 0: return true;
case 1: return this.elements[0].isSmall();
default: return false;
}
};
_Arr_prototype.isLarge = function () {
var _ref, _this;
_this = this;
if ((_ref = this._isLarge) == null) {
return this._isLarge = this.elements.length > 4 || (function () {
var _arr, _i, _some, element;
_some = false;
for (_arr = __toArray(_this.elements), _i = _arr.length; _i--; ) {
element = _arr[_i];
if (!element.isSmall()) {
_some = true;
break;
}
}
return _some;
}());
} else {
return _ref;
}
};
_Arr_prototype.isNoop = function () {
var _arr, _every, _i, _ref, element;
if ((_ref = this._isNoop) == null) {
_every = true;
for (_arr = __toArray(this.elements), _i = _arr.length; _i--; ) {
element = _arr[_i];
if (!element.isNoop()) {
_every = false;
break;
}
}
return this._isNoop = _every;
} else {
return _ref;
}
};
_Arr_prototype.walk = function (walker) {
var elements;
elements = walkArray(this.elements, this, "element", walker);
if (this.elements !== elements) {
return Arr(this.pos, elements);
} else {
return this;
}
};
_Arr_prototype.typeId = 2;
_Arr_prototype._toAst = function (pos, ident) {
var _arr, _arr2, _i, _len, element;
_arr = [];
for (_arr2 = __toArray(this.elements), _i = 0, _len = _arr2.length; _i < _len; ++_i) {
element = _arr2[_i];
_arr.push(element.toAst(pos, ident));
}
return _arr;
};
Arr._fromAst = function (pos) {
var elements;
elements = __slice.call(arguments, 1);
return Arr(pos, elements);
};
_Arr_prototype._toJSON = function () {
return simplifyArray(this.elements, 0);
};
Arr.fromJSON = function (line, column, file) {
var elements;
elements = __slice.call(arguments, 3);
return Arr(
makePos(line, column, file),
arrayFromJSON(elements)
);
};
return Arr;
}(Expression));
exports.Assign = function (pos) {
var _i, _i2, end, left, right, start;
_i = arguments.length - 1;
if (_i > 1) {
start = __slice.call(arguments, 1, _i);
} else {
_i = 1;
start = [];
}
end = arguments[_i];
right = end;
for (_i2 = start.length; _i2--; ) {
left = start[_i2];
right = Binary(pos, left, "=", right);
}
return right;
};
exports.BinaryChain = function (pos, op) {
var _i, _len, arg, args, current, i, left, right;
args = __slice.call(arguments, 2);
if (op === "+") {
for (i = args.length - 2; i >= 0; --i) {
left = args[i];
right = args[i + 1];
if ((typeof left === "string" || left instanceof Const && typeof left.value === "string") && (typeof right === "string" || right instanceof Const && typeof right.value === "string")) {
args.splice(i, 2, "" + (typeof left === "string" ? left : left.value) + (typeof right === "string" ? right : right.value));
}
}
}
current = args[0];
for (_i = 1, _len = args.length; _i < _len; ++_i) {
arg = args[_i];
current = Binary(pos, current, op, arg);
}
return current;
};
exports.And = function (pos) {
var _end, args, current, i;
args = __slice.call(arguments, 1);
if (args.length === 0) {
return Const(pos, true);
} else {
current = args[0];
for (i = 1, _end = args.length; i < _end; ++i) {
current = Binary(pos, current, "&&", args[i]);
}
return current;
}
};
exports.Or = function (pos) {
var _end, args, current, i;
args = __slice.call(arguments, 1);
if (args.length === 0) {
return Const(pos, false);
} else {
current = args[0];
for (i = 1, _end = args.length; i < _end; ++i) {
current = Binary(pos, current, "||", args[i]);
}
return current;
}
};
function toConst(pos, value) {
if (value instanceof Node) {
throw new Error("Cannot convert " + __typeof(value) + " to a Const");
} else if (value instanceof RegExp) {
return Regex(pos, value.source, value.flags);
} else {
return Const(pos, value);
}
}
function isNegative(value) {
return value < 0 || value === 0 && 1 / value < 0;
}
exports.Binary = Binary = (function (Expression) {
var _Binary_prototype, _Expression_prototype, ASSIGNMENT_OPS, inversions,
LEVEL_TO_ASSOCIATIVITY, OPERATOR_PRECEDENCE;
function Binary(pos, left, op, right) {
var _ref, _this;
_this = this instanceof Binary ? this : __create(_Binary_prototype);
_this.pos = pos;
if (left == null) {
left = Noop(pos);
}
_this.left = left;
_this.op = op;
if (right == null) {
right = Noop(pos);
}
_this.right = right;
if (!__owns.call(OPERATOR_PRECEDENCE, op)) {
throw new Error("Unknown binary operator: " + toJSSource(op));
}
if (!(left instanceof Expression)) {
left = toConst(pos, left);
}
if (!(right instanceof Expression)) {
right = toConst(pos, right);
}
if (__owns.call(ASSIGNMENT_OPS, op)) {
if (!(left instanceof Ident) && (!(left instanceof Binary) || left.op !== ".")) {
throw new Error("Cannot assign with " + op + " to non-Ident or Access: " + __typeof(left));
}
if (left instanceof Binary && left.left instanceof BlockExpression) {
return BlockExpression(pos, __toArray(__slice.call(left.left.body, 0, -1)).concat([
Binary.call(
_this,
pos,
Binary(left.pos, (_ref = left.left.body)[_ref.length - 1], ".", left.right),
op,
right
)
]));
}
if (right instanceof BlockExpression && (left instanceof Ident || left.left.isNoop() && left.right.isNoop())) {
return BlockExpression(pos, __toArray(__slice.call(right.body, 0, -1)).concat([
Binary.call(
_this,
pos,
left,
op,
(_ref = right.body)[_ref.length - 1]
)
]));
}
} else if (left instanceof BlockExpression && op !== ".") {
return BlockExpression(pos, __toArray(__slice.call(left.body, 0, -1)).concat([
Binary.call(
_this,
pos,
(_ref = left.body)[_ref.length - 1],
op,
right
)
]));
}
_this.left = left;
_this.right = right;
return _this;
}
_Expression_prototype = Expression.prototype;
_Binary_prototype = Binary.prototype = __create(_Expression_prototype);
_Binary_prototype.constructor = Binary;
Binary.displayName = "Binary";
if (typeof Expression.extended === "function") {
Expression.extended(Binary);
}
function compileAccess(op, left, right, options, level, lineStart, sb) {
var dotAccess, stringLeft, wrap;
dotAccess = right instanceof Const && typeof right.value === "string" && isAcceptableIdent(right.value);
wrap = level > 18;
if (wrap) {
sb("(");
}
if (left instanceof Const && typeof left.value === "number") {
stringLeft = toJSSource(left.value);
if (isNegative(left.value) || !isFinite(left.value)) {
sb("(");
sb(stringLeft);
sb(")");
} else {
sb(stringLeft);
if (dotAccess && stringLeft.indexOf("e") === -1 && stringLeft.indexOf(".") === -1) {
sb(".");
}
}
} else if (left.isConst() && left.constValue() === void 0) {
sb("(");
(left instanceof Const ? left : Const(left.pos, void 0)).compile(options, 2, false, sb);
sb(")");
} else {
left.compile(options, 18, lineStart, sb);
}
if (dotAccess) {
sb(".");
sb(right.value);
} else {
sb("[");
right.compile(options, 2, false, sb);
sb("]");
}
if (wrap) {
sb(")");
}
}
function compileOther(op, left, right, options, level, lineStart, sb) {
var associativity, opLevel, spaced, wrap;
opLevel = OPERATOR_PRECEDENCE[op];
associativity = LEVEL_TO_ASSOCIATIVITY[opLevel];
if (associativity === "paren") {
wrap = level >= opLevel;
} else {
wrap = level > opLevel;
}
if (wrap) {
sb("(");
}
left.compile(
options,
associativity === "right" && left instanceof Binary && OPERATOR_PRECEDENCE[left.op] === opLevel ? +opLevel + 1 : opLevel,
lineStart && !wrap,
sb
);
spaced = !options.minify || /^\w/.test(op);
if (spaced) {
sb(" ");
}
sb(op);
if (spaced) {
sb(" ");
}
right.compile(
options,
associativity === "left" && right instanceof Binary && OPERATOR_PRECEDENCE[right.op] === opLevel ? +opLevel + 1 : opLevel,
false,
sb
);
if (wrap) {
sb(")");
}
}
_Binary_prototype.compile = function (options, level, lineStart, sb) {
var _ref, f;
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.pushFile(this.pos.file);
}
if ((_ref = options.sourceMap) != null) {
_ref.add(sb.line, sb.column, this.pos.line, this.pos.column);
}
if (this.op === ".") {
f = compileAccess;
} else {
f = compileOther;
}
f(
this.op,
this.left,
this.right,
options,
level,
lineStart,
sb
);
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.popFile();
}
};
_Binary_prototype.compileAsBlock = function (options, level, lineStart, sb) {
var _ref;
if (__owns.call(ASSIGNMENT_OPS, this.op) || (_ref = this.op) === "." || _ref === "&&" || _ref === "||") {
_Expression_prototype.compileAsBlock.call(
this,
options,
level,
lineStart,
sb
);
} else {
BlockExpression(this.pos, [this.left, this.right]).compileAsBlock(options, level, lineStart, sb);
}
};
_Binary_prototype.compileAsStatement = function (options, lineStart, sb) {
var _this, left, op;
_this = this;
left = this.left;
op = this.op;
if (__owns.call(ASSIGNMENT_OPS, op)) {
if (left instanceof Ident && typeof this.right.toStatement === "function" && false) {
this.right.toStatement().mutateLast(
function (node) {
return Binary(_this.pos, left, op, node);
},
{ noop: true }
).compileAsStatement(options, lineStart, sb);
} else {
_Expression_prototype.compileAsStatement.call(this, options, lineStart, sb);
}
} else if (this.op === "&&") {
IfStatement(this.pos, this.left, this.right).compileAsStatement(options, lineStart, sb);
} else if (this.op === "||") {
IfStatement(this.pos, this.left.invert(), this.right).compileAsStatement(options, lineStart, sb);
} else if (op === ".") {
_Expression_prototype.compileAsStatement.call(this, options, lineStart, sb);
} else {
BlockStatement(this.pos, [this.left, this.right]).compileAsStatement(options, lineStart, sb);
}
};
ASSIGNMENT_OPS = {
"=": true,
"+=": true,
"-=": true,
"*=": true,
"/=": true,
"%=": true,
"<<=": true,
">>=": true,
">>>=": true,
"&=": true,
"^=": true,
"|=": true
};
_Binary_prototype.isAssign = function () {
return __owns.call(ASSIGNMENT_OPS, this.op);
};
OPERATOR_PRECEDENCE = {
".": 18,
"*": 15,
"/": 15,
"%": 15,
"+": 14,
"-": 14,
"<<": 13,
">>": 13,
">>>": 13,
"<": 12,
"<=": 12,
">": 12,
">=": 12,
"in": 12,
"instanceof": 12,
"==": 11,
"!=": 11,
"===": 11,
"!==": 11,
"&": 9,
"^": 10,
"|": 8,
"&&": 7,
"||": 6,
"=": 4,
"+=": 4,
"-=": 4,
"*=": 4,
"/=": 4,
"%=": 4,
"<<=": 4,
">>=": 4,
">>>=": 4,
"&=": 4,
"^=": 4,
"|=": 4
};
inversions = {
"<": ">=",
"<=": ">",
">": "<=",
">=": ">",
"==": "!=",
"!=": "==",
"===": "!==",
"!==": "==="
};
_Binary_prototype.invert = function () {
if (__owns.call(inversions, this.op)) {
return Binary(this.pos, this.left, inversions[this.op], this.right);
} else {
return _Expression_prototype.invert.call(this);
}
};
LEVEL_TO_ASSOCIATIVITY = {
11: "paren",
12: "paren",
14: "left",
15: "left",
9: "none",
8: "none",
10: "none",
13: "left",
4: "right"
};
_Binary_prototype.isLarge = function () {
var _ref;
if ((_ref = this._isLarge) == null) {
return this._isLarge = !this.left.isSmall() || !this.right.isSmall();
} else {
return _ref;
}
};
_Binary_prototype.isSmall = function () {
var _ref;
if ((_ref = this._isSmall) == null) {
return this._isSmall = this.left.isSmall() && this.right.isSmall();
} else {
return _ref;
}
};
_Binary_prototype.isNoop = function () {
var _ref;
if ((_ref = this._isNoop) == null) {
return this._isNoop = !__owns.call(ASSIGNMENT_OPS, this.op) && this.op !== "." && this.left.isNoop() && this.right.isNoop();
} else {
return _ref;
}
};
_Binary_prototype.walk = function (walker) {
var _ref, changed, left, right;
changed = false;
if ((_ref = walker(this.left, this, "left")) != null) {
left = _ref;
} else {
left = this.left.walk(walker);
}
if ((_ref = walker(this.right, this, "right")) != null) {
right = _ref;
} else {
right = this.right.walk(walker);
}
if (this.left !== left || this.right !== right) {
return Binary(this.pos, left, this.op, right);
} else {
return this;
}
};
_Binary_prototype.typeId = 3;
_Binary_prototype._toAst = function (pos, ident) {
return [
this.left.toAst(pos, ident),
Const(pos, this.op),
this.right.toAst(pos, ident)
];
};
Binary._fromAst = function (pos, left, op, right) {
return Binary(pos, left, op, right);
};
_Binary_prototype._toJSON = function () {
var result;
result = [
simplify(this.left, 0),
this.op
];
if (simplify(this.right)) {
result.push.apply(result, __toArray(this.right.toJSON()));
}
return result;
};
Binary.fromJSON = function (line, column, file, left, op) {
var right;
right = __slice.call(arguments, 5);
return Binary(
makePos(line, column, file),
fromJSON(left),
op,
fromJSON(right)
);
};
return Binary;
}(Expression));
exports.BlockStatement = BlockStatement = (function (Statement) {
var _BlockStatement_prototype, _Statement_prototype;
function BlockStatement(pos, body, label) {
var _i, _len, _this, item, result, statement;
_this = this instanceof BlockStatement ? this : __create(_BlockStatement_prototype);
_this.pos = pos;
if (body == null) {
body = [];
}
if (label == null) {
label = null;
}
_this.label = label;
result = [];
for (_i = 0, _len = body.length; _i < _len; ++_i) {
item = body[_i];
statement = item.maybeToStatement();
if (statement instanceof BlockStatement && !statement.label && (statement.pos.file === pos.file || !statement.pos.file)) {
result.push.apply(result, __toArray(statement.body));
} else if (!(statement instanceof Noop)) {
result.push(statement);
}
if (statement.exitType() != null) {
break;
}
}
switch (result.length) {
case 0: return Noop(pos);
case 1:
if (pos.file && !result[0].pos.file) {
result[0].pos.file = pos.file;
}
return result[0];
default: _this.body = result;
}
return _this;
}
_Statement_prototype = Statement.prototype;
_BlockStatement_prototype = BlockStatement.prototype = __create(_Statement_prototype);
_BlockStatement_prototype.constructor = BlockStatement;
BlockStatement.displayName = "BlockStatement";
if (typeof Statement.extended === "function") {
Statement.extended(BlockStatement);
}
_BlockStatement_prototype.compile = function (options, level, lineStart, sb) {
var _arr, _arr2, _i, _len, _ref, childOptions, i, item, minify, node,
nodes;
if (level !== 1) {
throw new Error("Cannot compile a statement except on the Block level");
}
_arr = [];
for (_arr2 = __toArray(this.body), _i = 0, _len = _arr2.length; _i < _len; ++_i) {
node = _arr2[_i];
if (!node.isNoop()) {
_arr.push(node);
}
}
nodes = _arr;
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.pushFile(this.pos.file);
}
if ((_ref = options.sourceMap) != null) {
_ref.add(sb.line, sb.column, this.pos.line, this.pos.column);
}
if (this.label != null) {
childOptions = incIndent(options);
} else {
childOptions = options;
}
minify = options.minify;
if (this.label != null) {
this.label.compile(options, level, lineStart, sb);
lineStart = false;
sb(":");
if (!minify) {
sb(" ");
}
sb("{");
if (!minify) {
sb(options.linefeed || "\n");
sb.indent(childOptions.indent);
lineStart = true;
}
}
for (i = 0, _len = nodes.length; i < _len; ++i) {
item = nodes[i];
if (i > 0 && !minify) {
sb(options.linefeed || "\n");
sb.indent(childOptions.indent);
lineStart = true;
}
item.compileAsStatement(childOptions, lineStart, sb);
lineStart = false;
}
if (this.label != null) {
if (!minify) {
sb(options.linefeed || "\n");
sb.indent(options.indent);
}
sb("}");
}
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.popFile();
}
};
_BlockStatement_prototype.walk = function (walker) {
var _ref, body, label;
body = walkArray(this.body, this, "node", walker);
if (this.label != null) {
if ((_ref = walker(this.label, this, "label")) != null) {
label = _ref;
} else {
label = this.label.walk(walker, this);
}
} else {
label = this.label;
}
if (this.body !== body || this.label !== label) {
return Block(this.pos, body, label);
} else {
return this;
}
};
_BlockStatement_prototype.mutateLast = function (func, options) {
var body, last, newLast;
last = this.last();
newLast = last.mutateLast(func, options);
if (last !== newLast) {
body = __slice.call(this.body, 0, -1);
body.push(newLast);
return Block(this.pos, body, this.label);
} else {
return this;
}
};
_BlockStatement_prototype.exitType = function () {
return this.last().exitType();
};
_BlockStatement_prototype.last = function () {
var _ref;
return (_ref = this.body)[_ref.length - 1];
};
_BlockStatement_prototype.removeTrailingReturnVoids = function () {
var body, last, newLast;
last = this.last();
newLast = last.removeTrailingReturnVoids();
if (last !== newLast) {
body = __slice.call(this.body, 0, -1);
body.push(newLast);
return Block(this.pos, body, this.label);
} else {
return this;
}
};
_BlockStatement_prototype.isNoop = function () {
var _arr, _every, _i, _ref, node;
if ((_ref = this._isNoop) == null) {
_every = true;
for (_arr = __toArray(this.body), _i = _arr.length; _i--; ) {
node = _arr[_i];
if (!node.isNoop()) {
_every = false;
break;
}
}
return this._isNoop = _every;
} else {
return _ref;
}
};
_BlockStatement_prototype.withLabel = function (label) {
if (label == null) {
label = null;
}
return BlockStatement(this.pos, this.body, label);
};
_BlockStatement_prototype.typeId = 4;
_BlockStatement_prototype._toAst = function (pos, ident) {
var _this;
_this = this;
return [
this.label != null ? this.label.toAst(pos, ident) : Const(pos, 0)
].concat((function () {
var _arr, _arr2, _i, _len, node;
_arr = [];
for (_arr2 = __toArray(_this.body), _i = 0, _len = _arr2.length; _i < _len; ++_i) {
node = _arr2[_i];
_arr.push(node.toAst(pos, ident));
}
return _arr;
}()));
};
BlockStatement._fromAst = function (pos, label) {
var nodes;
nodes = __slice.call(arguments, 2);
return BlockStatement(pos, nodes, label || null);
};
_BlockStatement_prototype._toJSON = function () {
return [this.label || 0].concat(__toArray(this.body));
};
BlockStatement.fromJSON = function (line, column, file, label) {
var body;
body = __slice.call(arguments, 4);
return BlockStatement(
makePos(line, column, file),
arrayFromJSON(body),
label ? fromJSON(label) : null
);
};
return BlockStatement;
}(Statement));
exports.BlockExpression = BlockExpression = (function (Expression) {
var _BlockExpression_prototype, _Expression_prototype;
function BlockExpression(pos, body) {
var _this, i, item, len, result;
_this = this instanceof BlockExpression ? this : __create(_BlockExpression_prototype);
_this.pos = pos;
if (body == null) {
body = [];
}
result = [];
for (i = 0, len = body.length; i < len; ++i) {
item = body[i];
if (i === len - 1 || !(!item instanceof Noop)) {
if (item instanceof BlockExpression && (item.pos.file === pos.file || !item.pos.file)) {
result.push.apply(result, __toArray(item.body));
if (i < len - 1 && result[result.length - 1] instanceof Noop) {
result.pop();
}
} else if (!(item instanceof Noop)) {
result.push(item);
}
}
}
switch (result.length) {
case 0: return Noop(pos);
case 1:
if (pos.file && !result[0].pos.file) {
result[0].pos.file = pos.file;
}
return result[0];
default: _this.body = result;
}
return _this;
}
_Expression_prototype = Expression.prototype;
_BlockExpression_prototype = BlockExpression.prototype = __create(_Expression_prototype);
_BlockExpression_prototype.constructor = BlockExpression;
BlockExpression.displayName = "BlockExpression";
if (typeof Expression.extended === "function") {
Expression.extended(BlockExpression);
}
_BlockExpression_prototype.toStatement = function () {
return BlockStatement(this.pos, this.body);
};
_BlockExpression_prototype.compile = function (options, level, lineStart, sb) {
var _arr, _arr2, _ref, i, innerLevel, item, len, node, nodes, wrap;
if (level === 1) {
this.toStatement().compile(options, level, lineStart, sb);
} else {
_arr = [];
for (_arr2 = __toArray(this.body), i = 0, len = _arr2.length; i < len; ++i) {
node = _arr2[i];
if (!node.isNoop() || i === len - 1) {
_arr.push(node);
}
}
nodes = _arr;
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.pushFile(this.pos.file);
}
if ((_ref = options.sourceMap) != null) {
_ref.add(sb.line, sb.column, this.pos.line, this.pos.column);
}
wrap = level > 2 && nodes.length > 1;
if (wrap) {
sb("(");
}
if (wrap) {
innerLevel = 3;
} else {
innerLevel = level;
}
for (i = 0, len = nodes.length; i < len; ++i) {
item = nodes[i];
if (i > 0) {
sb(",");
if (!options.minify) {
sb(" ");
}
}
if (i < len - 1) {
item.compileAsBlock(options, innerLevel, !wrap && i === 0, sb);
} else {
item.compile(options, innerLevel, !wrap && i === 0, sb);
}
}
if (wrap) {
sb(")");
}
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.popFile();
}
}
};
_BlockExpression_prototype.compileAsBlock = function (options, level, lineStart, sb) {
var _arr, _arr2, _len, _ref, i, item, len, node, nodes, wrap;
if (level === 1) {
this.compile(options, level, lineStart, sb);
} else {
_arr = [];
for (_arr2 = __toArray(this.body), i = 0, len = _arr2.length; i < len; ++i) {
node = _arr2[i];
if (!node.isNoop()) {
_arr.push(node);
}
}
nodes = _arr;
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.pushFile(this.pos.file);
}
if ((_ref = options.sourceMap) != null) {
_ref.add(sb.line, sb.column, this.pos.line, this.pos.column);
}
wrap = level > 2 && nodes.length > 1;
if (wrap) {
sb("(");
}
for (i = 0, _len = nodes.length; i < _len; ++i) {
item = nodes[i];
if (i > 0) {
sb(", ");
}
item.compileAsBlock(
options,
wrap ? 3 : level,
false,
sb
);
}
if (wrap) {
sb(")");
}
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.popFile();
}
}
};
_BlockExpression_prototype.isLarge = function () {
var _ref, _this;
_this = this;
if ((_ref = this._isLarge) == null) {
return this._isLarge = this.body.length > 4 || (function () {
var _arr, _i, _some, part;
_some = false;
for (_arr = __toArray(_this.body), _i = _arr.length; _i--; ) {
part = _arr[_i];
if (part.isLarge()) {
_some = true;
break;
}
}
return _some;
}());
} else {
return _ref;
}
};
_BlockExpression_prototype.isSmall = function () {
return false;
};
_BlockExpression_prototype.isNoop = function () {
var _arr, _every, _i, _ref, node;
if ((_ref = this._isNoop) == null) {
_every = true;
for (_arr = __toArray(this.body), _i = _arr.length; _i--; ) {
node = _arr[_i];
if (!node.isNoop()) {
_every = false;
break;
}
}
return this._isNoop = _every;
} else {
return _ref;
}
};
_BlockExpression_prototype.walk = BlockStatement.prototype.walk;
_BlockExpression_prototype.last = function () {
var _ref;
return (_ref = this.body)[_ref.length - 1];
};
_BlockExpression_prototype.withLabel = BlockStatement.prototype.withLabel;
_BlockExpression_prototype.typeId = 5;
_BlockExpression_prototype._toAst = function (pos, ident) {
var _arr, _arr2, _i, _len, node;
_arr = [];
for (_arr2 = __toArray(this.body), _i = 0, _len = _arr2.length; _i < _len; ++_i) {
node = _arr2[_i];
_arr.push(node.toAst(pos, ident));
}
return _arr;
};
BlockExpression._fromAst = function (pos) {
var nodes;
nodes = __slice.call(arguments, 1);
return BlockExpression(pos, nodes);
};
_BlockExpression_prototype._toJSON = function () {
return this.body;
};
BlockExpression.fromJSON = function (line, column, file) {
var body;
body = __slice.call(arguments, 3);
return BlockExpression(
makePos(line, column, file),
arrayFromJSON(body)
);
};
return BlockExpression;
}(Expression));
Block = exports.Block = function (pos, body, label) {
if (body == null) {
body = [];
}
if (label == null) {
label = null;
}
if (body.length === 0) {
return Noop(pos);
} else if (label == null && (function () {
var _every, _i, item;
_every = true;
for (_i = body.length; _i--; ) {
item = body[_i];
if (!(item instanceof Expression)) {
_every = false;
break;
}
}
return _every;
}())) {
return BlockExpression(pos, body);
} else {
return BlockStatement(pos, body, label);
}
};
exports.Break = Break = (function (Statement) {
var _Break_prototype, _Statement_prototype;
function Break(pos, label) {
var _this;
_this = this instanceof Break ? this : __create(_Break_prototype);
_this.pos = pos;
if (label == null) {
label = null;
}
_this.label = label;
return _this;
}
_Statement_prototype = Statement.prototype;
_Break_prototype = Break.prototype = __create(_Statement_prototype);
_Break_prototype.constructor = Break;
Break.displayName = "Break";
if (typeof Statement.extended === "function") {
Statement.extended(Break);
}
_Break_prototype.compile = function (options, level, lineStart, sb) {
var _ref;
if (level !== 1) {
throw new Error("Cannot compile a statement except on the Block level");
}
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.pushFile(this.pos.file);
}
if ((_ref = options.sourceMap) != null) {
_ref.add(sb.line, sb.column, this.pos.line, this.pos.column);
}
sb("break");
if (this.label != null) {
sb(" ");
this.label.compile(options, 2, false, sb);
}
sb(";");
if (options.sourceMap != null && this.pos.file) {
options.sourceMap.popFile(this.pos.file);
}
};
_Break_prototype.walk = function () {
return this;
};
_Break_prototype.exitType = function () {
return "break";
};
_Break_prototype.walk = function (walker) {
var _ref, label;
if (this.label != null) {
if ((_ref = walker(this.label, this, "label")) != null) {
label = _ref;
} else {
label = this.label.walk(walker);
}
} else {
label = this.label;
}
if (label !== this.label) {
return Break(this.pos, label);
} else {
return this;
}
};
_Break_prototype.isLarge = function () {
return false;
};
_Break_prototype.typeId = 6;
_Break_prototype._toAst = function (pos, ident) {
if (this.label != null) {
return [this.label.toAst(pos, ident)];
} else {
return [];
}
};
Break._fromAst = function (pos, label) {
return Break(pos, label);
};
_Break_prototype._toJSON = function () {
if (this.label != null) {
return [this.label];
} else {
return [];
}
};
Break.fromJSON = function (line, column, file, label) {
return Break(
makePos(line, column, file),
label ? from