Skip to content

Commit

Permalink
in expression
Browse files Browse the repository at this point in the history
  • Loading branch information
alongubkin committed Nov 15, 2014
1 parent 879e39d commit 6530cec
Show file tree
Hide file tree
Showing 5 changed files with 808 additions and 616 deletions.
1 change: 1 addition & 0 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
SuperExpression: require('./ast/expressions/SuperExpression').SuperExpression,
SplatExpression: require('./ast/expressions/SplatExpression').SplatExpression,
ConditionalExpression: require('./ast/expressions/ConditionalExpression').ConditionalExpression,
InExpression: require('./ast/expressions/InExpression').InExpression,
BlockStatement: require('./ast/statements/BlockStatement').BlockStatement,
ExpressionStatement: require('./ast/statements/ExpressionStatement').ExpressionStatement,
IfStatement: require('./ast/statements/IfStatement').IfStatement,
Expand Down
114 changes: 114 additions & 0 deletions lib/ast/expressions/InExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
var Node = require('../Node').Node;

exports.InExpression = function (left, right) {
Node.call(this);

this.type = 'InExpression';

this.left = left;
this.left.parent = this;

this.right = right;
this.right.parent = this;
};

exports.InExpression.prototype = Object.create(Node);

exports.InExpression.prototype.codegen = function () {
if (!Node.prototype.codegen.call(this)) {
return;
}

this.left = this.left.codegen();
this.right = this.right.codegen();

if (this.right.hasCallExpression && this.right.hasCallExpression()) {
var context = this.getContext();

var id = {
"type": "Identifier",
"name": exports.InExpression.getNextVariableName(),
"codeGenerated": true
};

context.node.body.splice(context.position +
(exports.InExpression.inExpressionIndex - 2), 0, {
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": id,
"init": this.right
}
],
"kind": "var",
"codeGenerated": true
});

this.right = id;
}

this.type = "ConditionalExpression";
this.test = {
"type": "BinaryExpression",
"operator": "instanceof",
"left": this.right,
"right": {
"type": "Identifier",
"name": "Array"
}
};

this.consequent = {
"type": "BinaryExpression",
"operator": "!==",
"left": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"computed": false,
"object": this.right,
"property": {
"type": "Identifier",
"name": "indexOf"
}
},
"arguments": [this.left]
},
"right": {
"type": "UnaryExpression",
"operator": "-",
"argument": {
"type": "Literal",
"value": 1,
"raw": "1"
},
"prefix": true
}
};

this.alternate = {
"type": "BinaryExpression",
"operator": "in",
"left": this.left,
"right": this.right
};

return this;
};

exports.InExpression.prototype.hasCallExpression = function () {
return true;
};

exports.InExpression.getNextVariableName = function () {
if (!this.inExpressionIndex) {
this.inExpressionIndex = 0;
}

return "inExpression" + this.inExpressionIndex++;
};

exports.InExpression.resetVariableNames = function () {
this.inExpressionIndex = 0;
};
Loading

0 comments on commit 6530cec

Please sign in to comment.