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.
redux
David Majda 10 years ago
parent a815a8b902
commit dad1207c46

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

@ -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;

@ -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, // <expression>
11, 1, 0, // IF_NOT_ERROR
8, // * TEXT
5 // NIP
11, 2, 1, // IF_NOT_ERROR
2, // * POP
8, // TEXT
5 // * NIP
]));
});
});

Loading…
Cancel
Save