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 ])); }); });