Skip to content

Commit

Permalink
Fixes to prepare generate-bytecode.js for ts-check
Browse files Browse the repository at this point in the history
  • Loading branch information
markw65 committed Dec 11, 2023
1 parent a0d7a3b commit dcefed0
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions lib/compiler/passes/generate-bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function generateBytecode(ast, options) {
}

function buildSimplePredicate(expression, negative, context) {
const match = expression.match | 0;
const match = expression.match || 0;

return buildSequence(
[op.PUSH_CURR_POS],
Expand Down Expand Up @@ -396,7 +396,7 @@ function generateBytecode(ast, options) {
[op.UPDATE_SAVED_POS],
buildCall(functionIndex, 0, context.env, context.sp),
buildCondition(
node.match | 0,
node.match || 0,
[op.IF],
buildSequence(
[op.POP],
Expand Down Expand Up @@ -529,7 +529,7 @@ function generateBytecode(ast, options) {
action: null,
}),
buildCondition(
delimiterNode.match | 0,
delimiterNode.match || 0,
[op.IF_NOT_ERROR], // if (item !== peg$FAILED) {
buildSequence(
[op.POP], // stack:[ pos ]
Expand Down Expand Up @@ -558,7 +558,8 @@ function generateBytecode(ast, options) {

function wrapGenerators(generators) {
if (options && options.output === "source-and-map") {
Object.entries(generators).forEach(([name, generator]) => {
Object.keys(generators).forEach(name => {
const generator = generators[name];
generators[name] = function(node, ...args) {
const generated = generator(node, ...args);
// Some generators ("grammar" and "rule") don't return anything,
Expand Down Expand Up @@ -603,10 +604,10 @@ function generateBytecode(ast, options) {
},

named(node, context) {
const match = node.match | 0;
const match = node.match || 0;
// Expectation not required if node always fail
const nameIndex = (match === NEVER_MATCH)
? null
? -1
: addExpectedConst({ type: "rule", value: node.name });

// The code generated below is slightly suboptimal because |FAIL| pushes
Expand All @@ -623,7 +624,7 @@ function generateBytecode(ast, options) {

choice(node, context) {
function buildAlternativesCode(alternatives, context) {
const match = alternatives[0].match | 0;
const match = alternatives[0].match || 0;
const first = generate(alternatives[0], {
sp: context.sp,
env: cloneEnv(context.env),
Expand Down Expand Up @@ -668,11 +669,11 @@ function generateBytecode(ast, options) {
env,
action: node,
});
const match = node.expression.match | 0;
const match = node.expression.match || 0;
// Function only required if expression can match
const functionIndex = emitCall && match !== NEVER_MATCH
? addFunctionConst(false, Object.keys(env), node)
: null;
: -1;

return emitCall
? buildSequence(
Expand Down Expand Up @@ -705,7 +706,7 @@ function generateBytecode(ast, options) {
action: null,
}),
buildCondition(
elements[0].match | 0,
elements[0].match || 0,
[op.IF_NOT_ERROR],
buildElementsCode(elements.slice(1), {
sp: context.sp + 1,
Expand Down Expand Up @@ -805,7 +806,7 @@ function generateBytecode(ast, options) {
action: null,
}),
buildCondition(
node.match | 0,
node.match || 0,
[op.IF_NOT_ERROR],
buildSequence([op.POP], [op.TEXT]),
[op.NIP]
Expand All @@ -832,7 +833,7 @@ function generateBytecode(ast, options) {
// Check expression match, not the node match
// If expression always match, no need to replace FAILED to NULL,
// because FAILED will never appeared
-(node.expression.match | 0),
-(node.expression.match || 0),
[op.IF_ERROR],
buildSequence([op.POP], [op.PUSH_NULL]),
[]
Expand Down Expand Up @@ -867,7 +868,7 @@ function generateBytecode(ast, options) {
expressionCode,
buildCondition(
// Condition depends on the expression match, not the node match
node.expression.match | 0,
node.expression.match || 0,
[op.IF_NOT_ERROR],
buildSequence(buildAppendLoop(expressionCode), [op.POP]),
buildSequence([op.POP], [op.POP], [op.PUSH_FAILED])
Expand Down Expand Up @@ -915,7 +916,7 @@ function generateBytecode(ast, options) {
: firstExpressionCode;
const bodyCode = buildRangeBody(
node.delimiter,
node.expression.match | 0,
node.expression.match || 0,
expressionCode,
context,
offset
Expand Down Expand Up @@ -970,7 +971,7 @@ function generateBytecode(ast, options) {

literal(node) {
if (node.value.length > 0) {
const match = node.match | 0;
const match = node.match || 0;
// String only required if condition is generated or string is
// case-sensitive and node always match
const needConst = match === SOMETIMES_MATCH
Expand All @@ -979,15 +980,15 @@ function generateBytecode(ast, options) {
? addLiteralConst(
node.ignoreCase ? node.value.toLowerCase() : node.value
)
: null;
: -1;
// Expectation not required if node always match
const expectedIndex = (match !== ALWAYS_MATCH)
? addExpectedConst({
type: "literal",
value: node.value,
ignoreCase: node.ignoreCase,
})
: null;
: -1;

// For case-sensitive strings the value must match the beginning of the
// remaining input exactly. As a result, we can use |ACCEPT_STRING| and
Expand All @@ -1008,9 +1009,9 @@ function generateBytecode(ast, options) {
},

class(node) {
const match = node.match | 0;
const match = node.match || 0;
// Character class constant only required if condition is generated
const classIndex = match === SOMETIMES_MATCH ? addClassConst(node) : null;
const classIndex = match === SOMETIMES_MATCH ? addClassConst(node) : -1;
// Expectation not required if node always match
const expectedIndex = (match !== ALWAYS_MATCH)
? addExpectedConst({
Expand All @@ -1019,7 +1020,7 @@ function generateBytecode(ast, options) {
inverted: node.inverted,
ignoreCase: node.ignoreCase,
})
: null;
: -1;

return buildCondition(
match,
Expand All @@ -1030,13 +1031,13 @@ function generateBytecode(ast, options) {
},

any(node) {
const match = node.match | 0;
const match = node.match || 0;
// Expectation not required if node always match
const expectedIndex = (match !== ALWAYS_MATCH)
? addExpectedConst({
type: "any",
})
: null;
: -1;

return buildCondition(
match,
Expand Down

0 comments on commit dcefed0

Please sign in to comment.