From b1ad2a1f61b44c897b37975e023b8883e155aab0 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 3 Apr 2015 13:54:54 +0200 Subject: [PATCH] Rename |reportedPos| to |savedPos| Preform the following renames: * |reportedPos| -> |savedPos| (abstract machine variable) * |peg$reportedPos| -> |peg$savedPos| (variable in generated code) * |REPORT_SAVED_POS| -> |LOAD_SAVED_POS| (instruction) * |REPORT_CURR_POS| -> |UPDATE_SAVED_POS| (instruction) The idea is that the name |reportedPos| is no longer accurate after the |location| change (seea the previous commit) because now both |reportedPos| and |currPos| are reported to user code. Renaming to |savedPos| resolves this inaccuracy. There is probably some better name for the concept than quite generic |savedPos|, but it doesn't come to me. --- lib/compiler/opcodes.js | 4 +- lib/compiler/passes/generate-bytecode.js | 14 +-- lib/compiler/passes/generate-javascript.js | 34 +++---- lib/parser.js | 96 +++++++++---------- .../compiler/passes/generate-bytecode.spec.js | 14 +-- 5 files changed, 81 insertions(+), 81 deletions(-) diff --git a/lib/compiler/opcodes.js b/lib/compiler/opcodes.js index 63d6c58..86ff811 100644 --- a/lib/compiler/opcodes.js +++ b/lib/compiler/opcodes.js @@ -35,8 +35,8 @@ var opcodes = { /* Calls */ - REPORT_SAVED_POS: 20, // REPORT_SAVED_POS p - REPORT_CURR_POS: 21, // REPORT_CURR_POS + LOAD_SAVED_POS: 20, // LOAD_SAVED_POS p + UPDATE_SAVED_POS: 21, // UPDATE_SAVED_POS CALL: 22, // CALL f, n, pc, p1, p2, ..., pN /* Rules */ diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 23c31bd..dcdc8ad 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -156,13 +156,13 @@ var arrays = require("../../utils/arrays"), * Calls * ----- * - * [20] REPORT_SAVED_POS p + * [20] LOAD_SAVED_POS p * - * reportedPos = stack[p]; + * savedPos = stack[p]; * - * [21] REPORT_CURR_POS + * [21] UPDATE_SAVED_POS * - * reportedPos = currPos; + * savedPos = currPos; * * [22] CALL f, n, pc, p1, p2, ..., pN * @@ -255,7 +255,7 @@ function generateBytecode(ast) { var functionIndex = addFunctionConst(objects.keys(context.env), code); return buildSequence( - [op.REPORT_CURR_POS], + [op.UPDATE_SAVED_POS], buildCall(functionIndex, 0, context.env, context.sp), buildCondition( [op.IF], @@ -354,7 +354,7 @@ function generateBytecode(ast) { buildCondition( [op.IF_NOT_ERROR], buildSequence( - [op.REPORT_SAVED_POS, 1], + [op.LOAD_SAVED_POS, 1], buildCall(functionIndex, 1, env, context.sp + 2) ), [] @@ -399,7 +399,7 @@ function generateBytecode(ast) { ); return buildSequence( - [op.REPORT_SAVED_POS, node.elements.length], + [op.LOAD_SAVED_POS, node.elements.length], buildCall( functionIndex, node.elements.length, diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index 9135abc..5877d7c 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -348,13 +348,13 @@ function generateJavascript(ast, options) { ' ip += 2;', ' break;', '', - ' case ' + op.REPORT_SAVED_POS + ':', // REPORT_SAVED_POS p - ' peg$reportedPos = stack[stack.length - 1 - bc[ip + 1]];', + ' case ' + op.LOAD_SAVED_POS + ':', // LOAD_SAVED_POS p + ' peg$savedPos = stack[stack.length - 1 - bc[ip + 1]];', ' ip += 2;', ' break;', '', - ' case ' + op.REPORT_CURR_POS + ':', // REPORT_CURR_POS - ' peg$reportedPos = peg$currPos;', + ' case ' + op.UPDATE_SAVED_POS + ':', // UPDATE_SAVED_POS + ' peg$savedPos = peg$currPos;', ' ip++;', ' break;', '', @@ -666,13 +666,13 @@ function generateJavascript(ast, options) { ip += 2; break; - case op.REPORT_SAVED_POS: // REPORT_SAVED_POS p - parts.push('peg$reportedPos = ' + stack.index(bc[ip + 1]) + ';'); + case op.LOAD_SAVED_POS: // LOAD_SAVED_POS p + parts.push('peg$savedPos = ' + stack.index(bc[ip + 1]) + ';'); ip += 2; break; - case op.REPORT_CURR_POS: // REPORT_CURR_POS - parts.push('peg$reportedPos = peg$currPos;'); + case op.UPDATE_SAVED_POS: // UPDATE_SAVED_POS + parts.push('peg$savedPos = peg$currPos;'); ip++; break; @@ -858,7 +858,7 @@ function generateJavascript(ast, options) { parts.push([ '', ' peg$currPos = 0,', - ' peg$reportedPos = 0,', + ' peg$savedPos = 0,', ' peg$cachedPos = 0,', ' peg$cachedPosDetails = { line: 1, column: 1, seenCR: false },', ' peg$maxFailPos = 0,', @@ -925,18 +925,18 @@ function generateJavascript(ast, options) { parts.push([ '', ' function text() {', - ' return input.substring(peg$reportedPos, peg$currPos);', + ' return input.substring(peg$savedPos, peg$currPos);', ' }', '', ' function location() {', - ' var reportedPosDetails = peg$computePosDetails(peg$reportedPos),', - ' currPosDetails = peg$computePosDetails(peg$currPos);', + ' var savedPosDetails = peg$computePosDetails(peg$savedPos),', + ' currPosDetails = peg$computePosDetails(peg$currPos);', '', ' return {', ' start: {', - ' offset: peg$reportedPos,', - ' line: reportedPosDetails.line,', - ' column: reportedPosDetails.column', + ' offset: peg$savedPos,', + ' line: savedPosDetails.line,', + ' column: savedPosDetails.column', ' },', ' end: {', ' offset: peg$currPos,', @@ -950,12 +950,12 @@ function generateJavascript(ast, options) { ' throw peg$buildException(', ' null,', ' [{ type: "other", description: description }],', - ' peg$reportedPos', + ' peg$savedPos', ' );', ' }', '', ' function error(message) {', - ' throw peg$buildException(message, null, peg$reportedPos);', + ' throw peg$buildException(message, null, peg$savedPos);', ' }', '', ' function peg$computePosDetails(pos) {', diff --git a/lib/parser.js b/lib/parser.js index 46d2fe8..fcf59a5 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -335,7 +335,7 @@ module.exports = (function() { peg$c239 = { type: "literal", value: ";", description: "\";\"" }, peg$currPos = 0, - peg$reportedPos = 0, + peg$savedPos = 0, peg$cachedPos = 0, peg$cachedPosDetails = { line: 1, column: 1, seenCR: false }, peg$maxFailPos = 0, @@ -353,18 +353,18 @@ module.exports = (function() { } function text() { - return input.substring(peg$reportedPos, peg$currPos); + return input.substring(peg$savedPos, peg$currPos); } function location() { - var reportedPosDetails = peg$computePosDetails(peg$reportedPos), - currPosDetails = peg$computePosDetails(peg$currPos); + var savedPosDetails = peg$computePosDetails(peg$savedPos), + currPosDetails = peg$computePosDetails(peg$currPos); return { start: { - offset: peg$reportedPos, - line: reportedPosDetails.line, - column: reportedPosDetails.column + offset: peg$savedPos, + line: savedPosDetails.line, + column: savedPosDetails.column }, end: { offset: peg$currPos, @@ -378,12 +378,12 @@ module.exports = (function() { throw peg$buildException( null, [{ type: "other", description: description }], - peg$reportedPos + peg$savedPos ); } function error(message) { - throw peg$buildException(message, null, peg$reportedPos); + throw peg$buildException(message, null, peg$savedPos); } function peg$computePosDetails(pos) { @@ -574,7 +574,7 @@ module.exports = (function() { s3 = peg$FAILED; } if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c0(s2, s3); s0 = s1; } else { @@ -601,7 +601,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseEOS(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c1(s1); s0 = s1; } else { @@ -657,7 +657,7 @@ module.exports = (function() { if (s6 !== peg$FAILED) { s7 = peg$parseEOS(); if (s7 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c4(s1, s3, s6); s0 = s1; } else { @@ -769,7 +769,7 @@ module.exports = (function() { } } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c7(s1, s2); s0 = s1; } else { @@ -809,7 +809,7 @@ module.exports = (function() { s2 = null; } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c8(s1, s2); s0 = s1; } else { @@ -865,7 +865,7 @@ module.exports = (function() { } } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c9(s1, s2); s0 = s1; } else { @@ -900,7 +900,7 @@ module.exports = (function() { if (s4 !== peg$FAILED) { s5 = peg$parsePrefixedExpression(); if (s5 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c12(s1, s5); s0 = s1; } else { @@ -940,7 +940,7 @@ module.exports = (function() { if (s2 !== peg$FAILED) { s3 = peg$parseSuffixedExpression(); if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c13(s1, s3); s0 = s1; } else { @@ -1004,7 +1004,7 @@ module.exports = (function() { if (s2 !== peg$FAILED) { s3 = peg$parseSuffixedOperator(); if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c20(s1, s3); s0 = s1; } else { @@ -1094,7 +1094,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c30); } } if (s5 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c31(s3); s0 = s1; } else { @@ -1186,7 +1186,7 @@ module.exports = (function() { s2 = peg$FAILED; } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c32(s1); s0 = s1; } else { @@ -1211,7 +1211,7 @@ module.exports = (function() { if (s2 !== peg$FAILED) { s3 = peg$parseCodeBlock(); if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c33(s1, s3); s0 = s1; } else { @@ -1730,7 +1730,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseIdentifierName(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c68(s2); s0 = s1; } else { @@ -1759,7 +1759,7 @@ module.exports = (function() { s3 = peg$parseIdentifierPart(); } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c70(s1, s2); s0 = s1; } else { @@ -1811,7 +1811,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseUnicodeEscapeSequence(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c75(s2); s0 = s1; } else { @@ -2053,7 +2053,7 @@ module.exports = (function() { s2 = null; } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c83(s1, s2); s0 = s1; } else { @@ -2101,7 +2101,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c86); } } if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c87(s2); s0 = s1; } else { @@ -2141,7 +2141,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c89); } } if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c87(s2); s0 = s1; } else { @@ -2201,7 +2201,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseSourceCharacter(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c90(); s0 = s1; } else { @@ -2224,7 +2224,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseEscapeSequence(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c75(s2); s0 = s1; } else { @@ -2278,7 +2278,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseSourceCharacter(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c90(); s0 = s1; } else { @@ -2301,7 +2301,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseEscapeSequence(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c75(s2); s0 = s1; } else { @@ -2376,7 +2376,7 @@ module.exports = (function() { s5 = null; } if (s5 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c98(s2, s3, s5); s0 = s1; } else { @@ -2424,7 +2424,7 @@ module.exports = (function() { if (s2 !== peg$FAILED) { s3 = peg$parseClassCharacter(); if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c101(s1, s3); s0 = s1; } else { @@ -2478,7 +2478,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseSourceCharacter(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c90(); s0 = s1; } else { @@ -2501,7 +2501,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseEscapeSequence(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c75(s2); s0 = s1; } else { @@ -2534,7 +2534,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseLineTerminatorSequence(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c102(); s0 = s1; } else { @@ -2574,7 +2574,7 @@ module.exports = (function() { s2 = peg$FAILED; } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c105(); s0 = s1; } else { @@ -2643,7 +2643,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c107); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c108(); } s0 = s1; @@ -2657,7 +2657,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c110); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c111(); } s0 = s1; @@ -2671,7 +2671,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c113); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c114(); } s0 = s1; @@ -2685,7 +2685,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c116); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c117(); } s0 = s1; @@ -2699,7 +2699,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c119); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c120(); } s0 = s1; @@ -2713,7 +2713,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c122); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c123(); } s0 = s1; @@ -2749,7 +2749,7 @@ module.exports = (function() { if (s1 !== peg$FAILED) { s2 = peg$parseSourceCharacter(); if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c90(); s0 = s1; } else { @@ -2827,7 +2827,7 @@ module.exports = (function() { s2 = s3; } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c128(s2); s0 = s1; } else { @@ -2888,7 +2888,7 @@ module.exports = (function() { s2 = s3; } if (s2 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c128(s2); s0 = s1; } else { @@ -2943,7 +2943,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c134); } } if (s1 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c135(); } s0 = s1; @@ -2974,7 +2974,7 @@ module.exports = (function() { if (peg$silentFails === 0) { peg$fail(peg$c140); } } if (s3 !== peg$FAILED) { - peg$reportedPos = s0; + peg$savedPos = s0; s1 = peg$c141(s2); s0 = s1; } else { diff --git a/spec/unit/compiler/passes/generate-bytecode.spec.js b/spec/unit/compiler/passes/generate-bytecode.spec.js index ca5cc73..60d6584 100644 --- a/spec/unit/compiler/passes/generate-bytecode.spec.js +++ b/spec/unit/compiler/passes/generate-bytecode.spec.js @@ -93,7 +93,7 @@ describe("compiler pass |generateBytecode|", function() { 1, // PUSH_CURR_POS 14, 0, 2, 2, 18, 0, 19, 1, // 11, 6, 0, // IF_NOT_ERROR - 20, 1, // * REPORT_SAVED_POS + 20, 1, // * LOAD_SAVED_POS 22, 2, 1, 0, // CALL 5 // NIP ])); @@ -116,7 +116,7 @@ describe("compiler pass |generateBytecode|", function() { 1, // PUSH_CURR_POS 14, 0, 2, 2, 18, 0, 19, 1, // 11, 7, 0, // IF_NOT_ERROR - 20, 1, // * REPORT_SAVED_POS + 20, 1, // * LOAD_SAVED_POS 22, 2, 1, 1, 0, // CALL 5 // NIP ])); @@ -143,7 +143,7 @@ describe("compiler pass |generateBytecode|", function() { 11, 25, 4, // IF_NOT_ERROR 14, 4, 2, 2, 18, 4, 19, 5, // * 11, 10, 4, // IF_NOT_ERROR - 20, 3, // * REPORT_SAVED_POS + 20, 3, // * LOAD_SAVED_POS 22, 6, 3, 3, 2, 1, 0, // CALL 5, // NIP 4, 3, // * POP_N @@ -359,7 +359,7 @@ describe("compiler pass |generateBytecode|", function() { it("generates correct bytecode", function() { expect(pass).toChangeAST(grammar, bytecodeDetails([ - 21, // REPORT_CURR_POS + 21, // UPDATE_SAVED_POS 22, 0, 0, 0, // CALL 9, 2, 2, // IF 2, // * POP @@ -389,7 +389,7 @@ describe("compiler pass |generateBytecode|", function() { 11, 40, 4, // IF_NOT_ERROR 14, 4, 2, 2, 18, 4, 19, 5, // * 11, 25, 4, // IF_NOT_ERROR - 21, // * REPORT_CURR_POS + 21, // * UPDATE_SAVED_POS 22, 6, 0, 3, 2, 1, 0, // CALL 9, 2, 2, // IF 2, // * POP @@ -434,7 +434,7 @@ describe("compiler pass |generateBytecode|", function() { it("generates correct bytecode", function() { expect(pass).toChangeAST(grammar, bytecodeDetails([ - 21, // REPORT_CURR_POS + 21, // UPDATE_SAVED_POS 22, 0, 0, 0, // CALL 9, 2, 2, // IF 2, // * POP @@ -464,7 +464,7 @@ describe("compiler pass |generateBytecode|", function() { 11, 40, 4, // IF_NOT_ERROR 14, 4, 2, 2, 18, 4, 19, 5, // * 11, 25, 4, // IF_NOT_ERROR - 21, // * REPORT_CURR_POS + 21, // * UPDATE_SAVED_POS 22, 6, 0, 3, 2, 1, 0, // CALL 9, 2, 2, // IF 2, // * POP