From dad1207c46fa252e80f44e566cbaec3ad0fa83ba Mon Sep 17 00:00:00 2001 From: David Majda Date: Thu, 15 May 2014 19:12:38 +0200 Subject: [PATCH] Improve semantics of the TEXT bytecode instruction The TEXT instruction now replaces position at the top of the stack with the input from that position until the current position. This is simpler and cleaner semantics than the previous one, where TEXT also popped an additional value from the stack and kept the position there. --- lib/compiler/passes/generate-bytecode.js | 10 ++++++---- lib/compiler/passes/generate-javascript.js | 6 ++---- spec/compiler/passes/generate-bytecode.spec.js | 7 ++++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/compiler/passes/generate-bytecode.js b/lib/compiler/passes/generate-bytecode.js index 23c3139..960811a 100644 --- a/lib/compiler/passes/generate-bytecode.js +++ b/lib/compiler/passes/generate-bytecode.js @@ -68,8 +68,7 @@ var arrays = require("../../utils/arrays"), * * [8] TEXT * - * stack.pop(); - * stack.push(input.substring(stack.top(), currPos)); + * stack.push(input.substring(stack.pop(), currPos)); * * Conditions and Loops * -------------------- @@ -443,8 +442,11 @@ function generateBytecode(ast) { env: { }, action: null }), - buildCondition([op.IF_NOT_ERROR], [op.TEXT], []), - [op.NIP] + buildCondition( + [op.IF_NOT_ERROR], + buildSequence([op.POP], [op.TEXT]), + [op.NIP] + ) ); }, diff --git a/lib/compiler/passes/generate-javascript.js b/lib/compiler/passes/generate-javascript.js index 342b166..6ec7c0e 100644 --- a/lib/compiler/passes/generate-javascript.js +++ b/lib/compiler/passes/generate-javascript.js @@ -219,8 +219,7 @@ function generateJavascript(ast, options) { ' break;', '', ' case ' + op.TEXT + ':', // TEXT - ' stack.pop();', - ' stack.push(input.substring(stack[stack.length - 1], peg$currPos));', + ' stack.push(input.substring(stack.pop(), peg$currPos));', ' ip++;', ' break;', '', @@ -522,9 +521,8 @@ function generateJavascript(ast, options) { break; case op.TEXT: // TEXT - stack.pop(); parts.push( - stack.push('input.substring(' + stack.top() + ', peg$currPos)') + stack.push('input.substring(' + stack.pop() + ', peg$currPos)') ); ip++; break; diff --git a/spec/compiler/passes/generate-bytecode.spec.js b/spec/compiler/passes/generate-bytecode.spec.js index 2c9d7d5..4f51585 100644 --- a/spec/compiler/passes/generate-bytecode.spec.js +++ b/spec/compiler/passes/generate-bytecode.spec.js @@ -221,9 +221,10 @@ describe("compiler pass |generateBytecode|", function() { expect(pass).toChangeAST('start = $"a"', bytecodeDetails([ 1, // PUSH_CURR_POS 14, 0, 2, 2, 18, 0, 19, 1, // - 11, 1, 0, // IF_NOT_ERROR - 8, // * TEXT - 5 // NIP + 11, 2, 1, // IF_NOT_ERROR + 2, // * POP + 8, // TEXT + 5 // * NIP ])); }); });