UNPKG

@domdomegg/masala-parser

Version:
63 lines (41 loc) 3.29 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _genlex = require('../../lib/genlex/genlex'); var _index = require('../../lib/parsec/index'); var _stream = require('../../lib/stream'); var _stream2 = _interopRequireDefault(_stream); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function createParser() { var genlex = new _genlex.GenLex(); genlex.setSeparatorsParser(_index.F.not(_index.C.charIn('+-<>[],.'))); genlex.keywords(['+', '-', '<', '>', '[', ']', ',', '.']); var grammar = _index.F.any().map(function (token) { return token.value; }).rep(); return genlex.use(grammar); } exports.default = { setUp: function setUp(done) { done(); }, 'parser is valid': function parserIsValid(test) { var hW = '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'; var parser = createParser(); var response = parser.parse(_stream2.default.ofString(hW)); test.ok(response.isAccepted()); test.equals(response.offset, hW.length); test.done(); }, 'comments are accepted': function commentsAreAccepted(test) { // Taken from Wikipedia documentation var hW = '++++++++ Set Cell #0 to 8\n[\n >++++ Add 4 to Cell #1; this will always set Cell #1 to 4\n [ as the cell will be cleared by the loop\n >++ Add 2 to Cell #2\n >+++ Add 3 to Cell #3\n >+++ Add 3 to Cell #4\n >+ Add 1 to Cell #5\n <<<<- Decrement the loop counter in Cell #1\n ] Loop till Cell #1 is zero; number of iterations is 4\n >+ Add 1 to Cell #2\n >+ Add 1 to Cell #3\n >- Subtract 1 from Cell #4\n >>+ Add 1 to Cell #6\n [<] Move back to the first zero cell you find; this will\n be Cell #1 which was cleared by the previous loop\n <- Decrement the loop Counter in Cell #0\n] Loop till Cell #0 is zero; number of iterations is 8\n\nThe result of this is:\nCell No : 0 1 2 3 4 5 6\nContents: 0 0 72 104 88 32 8\nPointer : ^\n\n>>. Cell #2 has value 72 which is \'H\'\n>---. Subtract 3 from Cell #3 to get 101 which is \'e\'\n+++++++..+++. Likewise for \'llo\' from Cell #3\n>>. Cell #5 is 32 for the space\n<-. Subtract 1 from Cell #4 for 87 to give a \'W\'\n<. Cell #3 was set to \'o\' from the end of \'Hello\'\n+++.------.--------. Cell #3 for \'rl\' and \'d\'\n>>+. Add 1 to Cell #5 gives us an exclamation point\n>++. And finally a newline from Cell #6'; var parser = createParser(); var response = parser.parse(_stream2.default.ofString(hW)); test.ok(response.isAccepted()); test.equals(response.offset, 106); // there is 111 tokens test.done(); } }; //# sourceMappingURL=genlex-brainfuck-test.js.map