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
  • Loading branch information
Mingun committed Jan 14, 2018
1 parent c38e3f7 commit a86c58e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
64 changes: 32 additions & 32 deletions lib/compiler/opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,54 @@
let 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
};

module.exports = opcodes;
10 changes: 4 additions & 6 deletions lib/compiler/passes/generate-bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ let visitor = require("../visitor");
// Stack Manipulation
// ------------------
//
// [0] PUSH c
// [0] PUSH_EMPTY_STRING
//
// stack.push(literals[c]);
// stack.push("");
//
// [1] PUSH_UNDEFINED
//
Expand Down Expand Up @@ -633,11 +633,9 @@ function generateBytecode(ast) {
[op.PUSH_FAILED]
)
);
} else {
let stringIndex = addLiteralConst("");

return [op.PUSH, stringIndex];
}

return [op.PUSH_EMPTY_STRING];
},

class(node, context) {
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 @@ -306,9 +306,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 @@ -610,17 +610,17 @@ function generateJS(ast, options) {

while (ip < end) {
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
10 changes: 6 additions & 4 deletions test/spec/unit/compiler/passes/generate-bytecode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,12 @@ 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 @@ -711,12 +711,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 a86c58e

Please sign in to comment.