Minimize variable scope

Where possible, move "let" statements into inner blocks, loop headers,
etc.

See #442.
redux
David Majda 8 years ago
parent bdf91b5941
commit d00e9526c3

@ -68,19 +68,17 @@ let Runner = {
function testRunner(benchmark, test) {
return function() {
let input, parseTime, averageParseTime, i, t;
callbacks.testStart(benchmark, test);
input = callbacks.readFile(benchmark.id + "/" + test.file);
let input = callbacks.readFile(benchmark.id + "/" + test.file);
parseTime = 0;
for (i = 0; i < runCount; i++) {
t = (new Date()).getTime();
let parseTime = 0;
for (let i = 0; i < runCount; i++) {
let t = (new Date()).getTime();
state.parser.parse(input);
parseTime += (new Date()).getTime() - t;
}
averageParseTime = parseTime / runCount;
let averageParseTime = parseTime / runCount;
callbacks.testFinish(benchmark, test, input.length, averageParseTime);

@ -42,8 +42,6 @@ let compiler = {
compile: function(ast, passes, options) {
options = options !== undefined ? options : {};
let stage;
options = objects.clone(options);
objects.defaults(options, {
allowedStartRules: [ast.rules[0].name],
@ -56,7 +54,7 @@ let compiler = {
trace: false
});
for (stage in passes) {
for (let stage in passes) {
if (passes.hasOwnProperty(stage)) {
passes[stage].forEach(function(p) { p(ast, options); });
}

@ -368,10 +368,8 @@ function generateBytecode(ast) {
sequence: function(node, context) {
function buildElementsCode(elements, context) {
let processedCount, functionIndex;
if (elements.length > 0) {
processedCount = node.elements.length - elements.slice(1).length;
let processedCount = node.elements.length - elements.slice(1).length;
return buildSequence(
generate(elements[0], {
@ -395,7 +393,7 @@ function generateBytecode(ast) {
);
} else {
if (context.action) {
functionIndex = addFunctionConst(
let functionIndex = addFunctionConst(
Object.keys(context.env),
context.action.code
);
@ -531,16 +529,14 @@ function generateBytecode(ast) {
},
literal: function(node) {
let stringIndex, expectedIndex;
if (node.value.length > 0) {
stringIndex = addConst('"'
let stringIndex = addConst('"'
+ js.stringEscape(
node.ignoreCase ? node.value.toLowerCase() : node.value
)
+ '"'
);
expectedIndex = addConst(
let expectedIndex = addConst(
'peg$literalExpectation('
+ '"' + js.stringEscape(node.value) + '", '
+ node.ignoreCase
@ -562,7 +558,7 @@ function generateBytecode(ast) {
[op.FAIL, expectedIndex]
);
} else {
stringIndex = addConst('""');
let stringIndex = addConst('""');
return [op.PUSH, stringIndex];
}

@ -428,12 +428,10 @@ function generateJS(ast, options) {
},
pop: function(n) {
let values;
if (n === undefined) {
return s(this.sp--);
} else {
values = arrays.range(this.sp - n + 1, this.sp + 1).map(s);
let values = arrays.range(this.sp - n + 1, this.sp + 1).map(s);
this.sp -= n;
return values;
@ -745,10 +743,7 @@ function generateJS(ast, options) {
}
function generateToplevel() {
let parts = [],
startRuleIndices, startRuleIndex,
startRuleFunctions, startRuleFunction,
ruleNames;
let parts = [];
parts.push([
'function peg$subclass(child, parent) {',
@ -940,24 +935,24 @@ function generateJS(ast, options) {
].join('\n'));
if (options.optimize === "size") {
startRuleIndices = '{ '
let startRuleIndices = '{ '
+ options.allowedStartRules.map(
function(r) { return r + ': ' + asts.indexOfRule(ast, r); }
).join(', ')
+ ' }';
startRuleIndex = asts.indexOfRule(ast, options.allowedStartRules[0]);
let startRuleIndex = asts.indexOfRule(ast, options.allowedStartRules[0]);
parts.push([
' peg$startRuleIndices = ' + startRuleIndices + ',',
' peg$startRuleIndex = ' + startRuleIndex + ','
].join('\n'));
} else {
startRuleFunctions = '{ '
let startRuleFunctions = '{ '
+ options.allowedStartRules.map(
function(r) { return r + ': peg$parse' + r; }
).join(', ')
+ ' }';
startRuleFunction = 'peg$parse' + options.allowedStartRules[0];
let startRuleFunction = 'peg$parse' + options.allowedStartRules[0];
parts.push([
' peg$startRuleFunctions = ' + startRuleFunctions + ',',
@ -989,7 +984,7 @@ function generateJS(ast, options) {
if (options.trace) {
if (options.optimize === "size") {
ruleNames = '['
let ruleNames = '['
+ ast.rules.map(
function(r) { return '"' + js.stringEscape(r.name) + '"'; }
).join(', ')

@ -25,9 +25,9 @@ let peg = {
options = options !== undefined ? options : {};
function convertPasses(passes) {
let converted = {}, stage;
let converted = {};
for (stage in passes) {
for (let stage in passes) {
if (passes.hasOwnProperty(stage)) {
converted[stage] = objects.values(passes[stage]);
}

@ -4,10 +4,9 @@
let arrays = {
range: function(start, stop) {
let length = stop - start,
result = new Array(length),
i, j;
result = new Array(length);
for (i = 0, j = start; i < length; i++, j++) {
for (let i = 0, j = start; i < length; i++, j++) {
result[i] = j;
}
@ -15,16 +14,16 @@ let arrays = {
},
find: function(array, valueOrPredicate) {
let length = array.length, i;
let length = array.length;
if (typeof valueOrPredicate === "function") {
for (i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
if (valueOrPredicate(array[i])) {
return array[i];
}
}
} else {
for (i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
if (array[i] === valueOrPredicate) {
return array[i];
}
@ -33,16 +32,16 @@ let arrays = {
},
indexOf: function(array, valueOrPredicate) {
let length = array.length, i;
let length = array.length;
if (typeof valueOrPredicate === "function") {
for (i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
if (valueOrPredicate(array[i])) {
return i;
}
}
} else {
for (i = 0; i < length; i++) {
for (let i = 0; i < length; i++) {
if (array[i] === valueOrPredicate) {
return i;
}

@ -3,9 +3,9 @@
/* Object utilities. */
let objects = {
values: function(object) {
let result = [], key;
let result = [];
for (key in object) {
for (let key in object) {
if (object.hasOwnProperty(key)) {
result.push(object[key]);
}
@ -15,9 +15,9 @@ let objects = {
},
clone: function(object) {
let result = {}, key;
let result = {};
for (key in object) {
for (let key in object) {
if (object.hasOwnProperty(key)) {
result[key] = object[key];
}
@ -27,9 +27,7 @@ let objects = {
},
defaults: function(object, defaults) {
let key;
for (key in defaults) {
for (let key in defaults) {
if (defaults.hasOwnProperty(key)) {
if (!(key in object)) {
object[key] = defaults[key];

@ -8,9 +8,9 @@ let peg = require("../../lib/peg");
describe("generated parser behavior", function() {
function varyOptimizationOptions(block) {
function clone(object) {
let result = {}, key;
let result = {};
for (key in object) {
for (let key in object) {
if (object.hasOwnProperty(key)) {
result[key] = object[key];
}
@ -77,7 +77,7 @@ describe("generated parser behavior", function() {
toFailToParse: function(input, details, options) {
options = options !== undefined ? options : {};
let result, key;
let result;
try {
result = this.actual.parse(input, options);
@ -92,7 +92,7 @@ describe("generated parser behavior", function() {
};
} else {
if (details) {
for (key in details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = function() {

@ -16,13 +16,11 @@ beforeEach(function() {
return value !== null && typeof value === "object";
}
let i, key;
if (isArray(details)) {
if (!isArray(value)) { return false; }
if (value.length !== details.length) { return false; }
for (i = 0; i < details.length; i++) {
for (let i = 0; i < details.length; i++) {
if (!matchDetails(value[i], details[i])) { return false; }
}
@ -30,7 +28,7 @@ beforeEach(function() {
} else if (isObject(details)) {
if (!isObject(value)) { return false; }
for (key in details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!(key in value)) { return false; }
@ -61,8 +59,7 @@ beforeEach(function() {
},
toReportError: function(grammar, details) {
let ast = peg.parser.parse(grammar),
key;
let ast = peg.parser.parse(grammar);
try {
this.actual(ast);
@ -75,7 +72,7 @@ beforeEach(function() {
};
} else {
if (details) {
for (key in details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = function() {

@ -194,7 +194,7 @@ describe("PEG.js grammar parser", function() {
},
toFailToParse: function(details) {
let result, key;
let result;
try {
result = peg.parser.parse(this.actual);
@ -207,7 +207,7 @@ describe("PEG.js grammar parser", function() {
};
} else {
if (details) {
for (key in details) {
for (let key in details) {
if (details.hasOwnProperty(key)) {
if (!this.env.equals_(e[key], details[key])) {
this.message = function() {

Loading…
Cancel
Save