Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some caching to op-overloading example.

  • Loading branch information...
commit 4ba02fd44d03c6a56ea1b6da08b8c8d2a32afd05 1 parent bba37a7
@bebraw authored
Showing with 23 additions and 7 deletions.
  1. +23 −7 src/examples/op-overloading/op-overloading.js
View
30 src/examples/op-overloading/op-overloading.js
@@ -10,15 +10,31 @@ Shaper("op-overloading", function(root) {
'/': 'div',
'*': 'mul'
};
+ // cache expressions here
+ // Note that "call" may not be cached since it is mutable.
+ var exprs = function() {
+ var ret = [];
+
+ for(var op in ops) {
+ var func = ops[op];
+
+ ret.push({
+ from: Shaper.parseExpression('$ ' + op + ' $'),
+ func: func
+ });
+ }
+
+ return ret;
+ }();
return Shaper.traverseTree(root, {
pre: function(node, ref) {
- for(var op in ops) {
- var template = Shaper.parseExpression('$ ' + op + ' $');
- var func = ops[op];
-
- if(Shaper.match(template, node)) {
- var call = Shaper.parseExpression(prefix + func + '($, $)');
+ for(var i = 0; i < exprs.length; i++) {
+ var expr = exprs[i];
+
+ if(Shaper.match(expr.from, node)) {
+ var callExpr = prefix + expr.func + '($, $)';
+ var call = Shaper.parseExpression(callExpr);
Shaper.replace(call, node.children[0], node.children[1]);
Shaper.cloneComments(call, node);
@@ -28,4 +44,4 @@ Shaper("op-overloading", function(root) {
}
}
});
-});
+});
Please sign in to comment.
Something went wrong with that request. Please try again.