Skip to content

Commit

Permalink
Replace PUSH opcode with PUSH_EMPTY_STRING opcode because it only use…
Browse files Browse the repository at this point in the history
…d with empty strings

(cherry picked from commit a86c58e)
  • Loading branch information
Mingun committed Jan 17, 2018
1 parent 030f9f6 commit a3a4e85
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
64 changes: 32 additions & 32 deletions lib/compiler/opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,54 @@ const opcodes = {

// Stack Manipulation

PUSH: 0, // PUSH c
PUSH_UNDEFINED: 1, // PUSH_UNDEFINED
PUSH_NULL: 2, // PUSH_NULL
PUSH_FAILED: 3, // PUSH_FAILED
PUSH_EMPTY_ARRAY: 4, // PUSH_EMPTY_ARRAY
PUSH_CURR_POS: 5, // PUSH_CURR_POS
POP: 6, // POP
POP_CURR_POS: 7, // POP_CURR_POS
POP_N: 8, // POP_N n
NIP: 9, // NIP
APPEND: 10, // APPEND
WRAP: 11, // WRAP n
TEXT: 12, // TEXT
PUSH_EMPTY_STRING: 0, // PUSH_EMPTY_STRING
PUSH_UNDEFINED: 1, // PUSH_UNDEFINED
PUSH_NULL: 2, // PUSH_NULL
PUSH_FAILED: 3, // PUSH_FAILED
PUSH_EMPTY_ARRAY: 4, // PUSH_EMPTY_ARRAY
PUSH_CURR_POS: 5, // PUSH_CURR_POS
POP: 6, // POP
POP_CURR_POS: 7, // POP_CURR_POS
POP_N: 8, // POP_N n
NIP: 9, // NIP
APPEND: 10, // APPEND
WRAP: 11, // WRAP n
TEXT: 12, // TEXT

// Conditions and Loops

IF: 13, // IF t, f
IF_ERROR: 14, // IF_ERROR t, f
IF_NOT_ERROR: 15, // IF_NOT_ERROR t, f
WHILE_NOT_ERROR: 16, // WHILE_NOT_ERROR b
IF: 13, // IF t, f
IF_ERROR: 14, // IF_ERROR t, f
IF_NOT_ERROR: 15, // IF_NOT_ERROR t, f
WHILE_NOT_ERROR: 16, // WHILE_NOT_ERROR b

// Matching

MATCH_ANY: 17, // MATCH_ANY a, f, ...
MATCH_STRING: 18, // MATCH_STRING s, a, f, ...
MATCH_STRING_IC: 19, // MATCH_STRING_IC s, a, f, ...
MATCH_CLASS: 20, // MATCH_CLASS c, a, f, ...
ACCEPT_N: 21, // ACCEPT_N n
ACCEPT_STRING: 22, // ACCEPT_STRING s
EXPECT: 23, // EXPECT e
MATCH_ANY: 17, // MATCH_ANY a, f, ...
MATCH_STRING: 18, // MATCH_STRING s, a, f, ...
MATCH_STRING_IC: 19, // MATCH_STRING_IC s, a, f, ...
MATCH_CLASS: 20, // MATCH_CLASS c, a, f, ...
ACCEPT_N: 21, // ACCEPT_N n
ACCEPT_STRING: 22, // ACCEPT_STRING s
EXPECT: 23, // EXPECT e

// Calls

LOAD_SAVED_POS: 24, // LOAD_SAVED_POS p
UPDATE_SAVED_POS: 25, // UPDATE_SAVED_POS
CALL: 26, // CALL f, n, pc, p1, p2, ..., pN
LOAD_SAVED_POS: 24, // LOAD_SAVED_POS p
UPDATE_SAVED_POS: 25, // UPDATE_SAVED_POS
CALL: 26, // CALL f, n, pc, p1, p2, ..., pN

// Rules

RULE: 27, // RULE r
RULE: 27, // RULE r

// Failure Reporting

SILENT_FAILS_ON: 28, // SILENT_FAILS_ON
SILENT_FAILS_OFF: 29, // SILENT_FAILS_OFF
SILENT_FAILS_ON: 28, // SILENT_FAILS_ON
SILENT_FAILS_OFF: 29, // SILENT_FAILS_OFF

EXPECT_NS_BEGIN: 38, // EXPECT_NS_BEGIN
EXPECT_NS_END: 39 // EXPECT_NS_END invert
EXPECT_NS_BEGIN: 38, // EXPECT_NS_BEGIN
EXPECT_NS_END: 39 // EXPECT_NS_END invert

};

Expand Down
7 changes: 3 additions & 4 deletions lib/compiler/passes/generate-bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const util = require( "../../util" );
// Stack Manipulation
// ------------------
//
// [0] PUSH c
// [0] PUSH_EMPTY_STRING
//
// stack.push(literals[c]);
// stack.push("");
//
// [1] PUSH_UNDEFINED
//
Expand Down Expand Up @@ -682,8 +682,7 @@ function generateBytecode( ast ) {

}

const stringIndex = addLiteralConst( "" );
return [ op.PUSH, stringIndex ];
return [ op.PUSH_EMPTY_STRING ];

},

Expand Down
14 changes: 7 additions & 7 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ function generateJS( ast, options ) {
" while (true) {",
" while (ip < end) {",
" switch (bc[ip]) {",
" case " + op.PUSH + ":", // PUSH c
" stack.push(peg$literals[bc[ip + 1]]);",
" ip += 2;",
" case " + op.PUSH_EMPTY_STRING + ":", // PUSH_EMPTY_STRING
" stack.push('');",
" ip++;",
" break;",
"",
" case " + op.PUSH_UNDEFINED + ":", // PUSH_UNDEFINED
Expand Down Expand Up @@ -696,17 +696,17 @@ function generateJS( ast, options ) {

switch ( bc[ ip ] ) {

case op.PUSH: // PUSH c
parts.push( stack.push( l( bc[ ip + 1 ] ) ) );
ip += 2;
case op.PUSH_EMPTY_STRING: // PUSH_EMPTY_STRING
parts.push( stack.push( "''" ) );
ip++;
break;

case op.PUSH_CURR_POS: // PUSH_CURR_POS
parts.push( stack.push( "peg$currPos" ) );
ip++;
break;

case op.PUSH_UNDEFINED: // PUSH_UNDEFINED
case op.PUSH_UNDEFINED: // PUSH_UNDEFINED
parts.push( stack.push( "undefined" ) );
ip++;
break;
Expand Down
8 changes: 4 additions & 4 deletions test/spec/unit/compiler/passes/generate-bytecode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,14 @@ describe( "compiler pass |generateBytecode|", function () {
it( "generates correct bytecode", function () {

expect( pass ).to.changeAST( grammar, bytecodeDetails( [
0, 0 // PUSH
0 // PUSH_EMPTY_STRING
] ) );

} );

it( "defines correct constants", function () {

expect( pass ).to.changeAST( grammar, constsDetails( [ "" ], [], [], [] ) );
expect( pass ).to.changeAST( grammar, constsDetails( [], [], [], [] ) );

} );

Expand Down Expand Up @@ -851,14 +851,14 @@ describe( "compiler pass |generateBytecode|", function () {
it( "generates correct bytecode", function () {

expect( pass ).to.changeAST( grammar, bytecodeDetails( [
0, 0 // PUSH
0 // PUSH_EMPTY_STRING
] ), {}, { reportFailures: false } );

} );

it( "defines correct constants", function () {

expect( pass ).to.changeAST( grammar, constsDetails( [ "" ], [], [], [] ), {}, { reportFailures: false } );
expect( pass ).to.changeAST( grammar, constsDetails( [], [], [], [] ), {}, { reportFailures: false } );

} );

Expand Down

0 comments on commit a3a4e85

Please sign in to comment.