Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join assignment #226

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,17 @@ function ast_walker() {
return [ this[0], walk(cond), walk(t), walk(e) ];
},
"assign": function(op, lvalue, rvalue) {
return [ this[0], op, walk(lvalue), walk(rvalue) ];
rvalue = walk(rvalue)
lvalue = walk(lvalue)
return [ this[0], op, lvalue, rvalue ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this change to me? Why do you have to walk the rvalue before the lvalue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelficarra consider this example:

a.b = 3;
c.d = 3;

If I'd walk lvalue first, c.d would prevent me from joining those two because my code would (incorrectly) assume that c.d gets evaluated before the second 3 and therefore could trigger a setter that alters state or so. My code depends on the walkers going through the code in the order the code would be evaluated in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, alright. Thanks.

},
"dot": function(expr) {
return [ this[0], walk(expr) ].concat(slice(arguments, 1));
},
"call": function(expr, args) {
return [ this[0], walk(expr), MAP(args, walk) ];
args = MAP(args, walk)
expr = walk(expr)
return [ this[0], expr, args ];
},
"function": function(name, args, body) {
return [ this[0], name, args.slice(), MAP(body, walk) ];
Expand Down Expand Up @@ -249,6 +253,7 @@ function ast_walker() {
};

return {
default_walkers: walkers,
walk: walk,
dive: dive,
with_walkers: with_walkers,
Expand Down Expand Up @@ -983,6 +988,81 @@ function ast_lift_variables(ast) {
});
};

function mergeAssignSimple(ast, options) {
var w = ast_walker()
, walk = w.walk
, mergeable = null
, obsolete = [];

function arrayCopy(arr) {
var result = [];
for (var i=0; i<arr.length; i++) {
result.push(arr[i]);
}
return result;
}

function deepExterminateAll(arr, targets) {
for (var i=0; i<arr.length; i++) {
if (~targets.indexOf(arr[i])) {
arr.splice(i, 1);
i--;
continue;
}
if (Array.isArray(arr[i])) {
deepExterminateAll(arr[i], targets);
}
}
}

function handleSimpleLiteral(value) {
var type = this[0];
if (mergeable && mergeable.type === type && mergeable.value === value) {
var stack = w.stack();
obsolete.push(mergeable.statement);
var result = mergeable.assign;
mergeable = null;
return arrayCopy(result);
} else {
return [type, value];
}
}

function handleStatement(expr) {
var result = [this[0], walk(expr)]
mergeable = null
if (expr[0] === "assign" && expr[1] === true && expr[3][0] in {string:1,num:1,atom:1}) {
// this is a simple-value assigning statement
mergeable = {statement: result, type: expr[3][0], value: expr[3][1], assign: expr}
}
return result;
}

var walkers =
{ string: handleSimpleLiteral
, num: handleSimpleLiteral
, atom: handleSimpleLiteral /*false,true,null,undefined*/
, stat: handleStatement
};

Object.keys(w.default_walkers).forEach(function(type) {
if (!walkers[type]) {
walkers[type] = function() {
var result = w.default_walkers[type].apply(this, arguments);
mergeable = null;
return result;
};
}
});

w.with_walkers(walkers, function() {
ast = walk(ast);
});

deepExterminateAll(ast, obsolete);
return ast;
}

function ast_squeeze(ast, options) {
options = defaults(options, {
make_seqs : true,
Expand Down Expand Up @@ -1322,6 +1402,7 @@ function ast_squeeze(ast, options) {
}, function() {
for (var i = 0; i < 2; ++i) {
ast = prepare_ifs(ast);
ast = mergeAssignSimple(ast);
ast = ast_add_scope(ast);
ast = walk(ast);
}
Expand Down