From cb0e34d1e3359c1af8e01f53229ae4e85dbd26b0 Mon Sep 17 00:00:00 2001 From: Michael Matuzak Date: Fri, 7 Oct 2016 10:45:55 -0700 Subject: [PATCH] bleh --- parsers/directive.js | 5 +- parsers/instruction.js | 5 +- parsers/label.js | 2 +- parsers/parameters.js | 127 ++++++++++++++++++++++++++++------------- test/directive.js | 67 ++++++++++++++++------ test/instruction.js | 7 ++- test/parameters.js | 62 ++++++++++++++++++++ 7 files changed, 213 insertions(+), 62 deletions(-) create mode 100644 test/parameters.js diff --git a/parsers/directive.js b/parsers/directive.js index 851d6fc..e971ec0 100644 --- a/parsers/directive.js +++ b/parsers/directive.js @@ -1,5 +1,5 @@ const mona = require('mona') -const parameters = require('./parameters') +const parameters = require('./parameters').parameters function directiveName () { return mona.oneOf([ @@ -23,7 +23,8 @@ function directiveName () { function directive () { return mona.sequence((s) => { const d = s(directiveName()) - const args = s(mona.map((a) => (a[0]), parameters())) + const space = s(mona.spaces()) + const args = s(parameters()) // const nl = s(mona.eol()) return mona.value({ diff --git a/parsers/instruction.js b/parsers/instruction.js index 5fc7891..878eba5 100644 --- a/parsers/instruction.js +++ b/parsers/instruction.js @@ -1,5 +1,5 @@ const mona = require('mona') -const parameters = require('./parameters') +const parameters = require('./parameters').parameters const instructions = [ 'adc', 'and', 'asl', @@ -25,7 +25,8 @@ function instructionName () { function instruction () { return mona.sequence((s) => { const i = s(instructionName()) - const args = s(mona.map((a) => (a[0]), parameters())) + const space = s(mona.spaces()) + const args = s(mona.maybe(parameters())) // const nl = s(mona.eol()) return mona.value({ instruction: i, diff --git a/parsers/label.js b/parsers/label.js index 0aadfd2..6bfe881 100644 --- a/parsers/label.js +++ b/parsers/label.js @@ -4,7 +4,7 @@ function label () { return mona.sequence((s) => { const label = s(mona.text(mona.alphanum())) const end = s(mona.string(':')) - const nl = s(mona.eol()) + // const nl = s(mona.eol()) return mona.value({ label: `${label}${end}` diff --git a/parsers/parameters.js b/parsers/parameters.js index 624c79f..22d146d 100644 --- a/parsers/parameters.js +++ b/parsers/parameters.js @@ -6,54 +6,101 @@ function quotedChar () { mona.value('"'))) } -// pulled out, because in the future, this might be more detailed! -// ::= + -function parameter () { - const param = () => { - return mona.or( - mona.join( - mona.string('$'), - mona.alphanum() - ), - mona.join( - mona.string('#'), - mona.string('$'), - mona.alphanum() - ), - mona.join( - mona.string('#'), - mona.string('%'), - mona.alphanum() - ), - mona.join( - mona.string('%'), - mona.alphanum() - ), - mona.between( - mona.string('"'), - mona.string('"'), - mona.text(quotedChar()) - ), +function alphanum () { + return mona.join( + mona.value('alphanum'), + mona.string('background3') + ) +} + +function bit () { + return mona.join( + mona.value('digit'), + mona.digit(2) + ) +} + +function string () { + return mona.join( + mona.value('string'), + mona.between( + mona.string('"'), + mona.string('"'), + mona.text(quotedChar()) + ) + ) +} + +function hex () { + return mona.join( + mona.and( + mona.string('#$'), + mona.value('hex') + ), + mona.text( + mona.digit(16) + ) + ) +} + +function binary () { + return mona.join( + mona.and( + mona.maybe(mona.string('#')), + mona.string('%'), + mona.value('binary') + ), + mona.text( + mona.digit(2) + ) + ) +} + +function address () { + return mona.join( + mona.and( + mona.string('$'), + mona.value('address') + ), + mona.text( mona.alphanum() ) - } + ) +} - return mona.text(param(), {min: 1}) +function parameter () { + return mona.collect( + mona.or( + address(), + binary(), + hex(), + string(), + bit(), + alphanum() + ) + ) } function parameters () { - return mona.collect( - mona.and( - mona.spaces(), - mona.split( - parameter(), - mona.or( - mona.and(mona.string(','), mona.spaces()), - mona.string(',') - ) + return mona.map(a => a.map(b => b[0]), + mona.split( + parameter(), + mona.or( + mona.and(mona.string(','), mona.spaces()), + mona.string(','), + mona.spaces() ) ) ) } -module.exports = parameters +module.exports = { + parameter, + parameters, + address, + binary, + hex, + string, + bit, + alphanum +} diff --git a/test/directive.js b/test/directive.js index 61be3a0..96011dc 100644 --- a/test/directive.js +++ b/test/directive.js @@ -6,7 +6,10 @@ tap.test('will parse a directive', (t) => { t.plan(1) t.deepEqual(mona.parse(directiveParser(), '.inesprg 1'), { args: [ - '1' + [ + 'digit', + '1' + ] ], directive: '.inesprg' }) @@ -16,7 +19,10 @@ tap.test('will parse a directive with direct address', (t) => { t.plan(1) t.deepEqual(mona.parse(directiveParser(), '.inesprg $0000'), { args: [ - '$,0000' + [ + 'address', + '0000' + ] ], directive: '.inesprg' }) @@ -26,7 +32,10 @@ tap.test('will parse a directive with hex arg', (t) => { t.plan(1) t.deepEqual(mona.parse(directiveParser(), '.inesprg #$FE'), { args: [ - '#,$,FE' + [ + 'hex', + 'FE' + ] ], directive: '.inesprg' }) @@ -36,14 +45,20 @@ tap.test('will parse a directive with binary arg', (t) => { t.plan(2) t.deepEqual(mona.parse(directiveParser(), '.db %00010001'), { args: [ - '%,00010001' + [ + 'binary', + '00010001' + ] ], directive: '.db' }) t.deepEqual(mona.parse(directiveParser(), '.db #%00010001'), { args: [ - '#,%,00010001' + [ + 'binary', + '00010001' + ] ], directive: '.db' }) @@ -51,12 +66,20 @@ tap.test('will parse a directive with binary arg', (t) => { tap.test('will parse a directive with multiple args', (t) => { t.plan(1) - t.deepEqual(mona.parse(directiveParser(), '.db %00010001,%00010001,%00010001,%00010001'), { + t.deepEqual(mona.parse(directiveParser(), '.db %00010001,%00010001,%00010001'), { args: [ - '%,00010001', - '%,00010001', - '%,00010001', - '%,00010001' + [ + 'binary', + '00010001' + ], + [ + 'binary', + '00010001' + ], + [ + 'binary', + '00010001' + ] ], directive: '.db' }) @@ -64,12 +87,21 @@ tap.test('will parse a directive with multiple args', (t) => { tap.test('will parse a directive with multiple args with spaces between them', (t) => { t.plan(1) - t.deepEqual(mona.parse(directiveParser(), '.db %00010001, %00010001, %00010001, %00010001'), { + t.deepEqual(mona.parse(directiveParser(), '.db %00010001, %00010001, %00010001'), { args: [ - '%,00010001', - '%,00010001', - '%,00010001', - '%,00010001' + [ + 'binary', + '00010001' + ], + [ + 'binary', + '00010001' + ], + [ + 'binary', + '00010001' + ] + ], directive: '.db' }) @@ -79,7 +111,10 @@ tap.test('will parse a directive with string arg', (t) => { t.plan(1) t.deepEqual(mona.parse(directiveParser(), '.incbin "mario.chr"'), { args: [ - 'mario.chr' + [ + 'string', + 'mario.chr' + ] ], directive: '.incbin' }) diff --git a/test/instruction.js b/test/instruction.js index 00f504b..e76aa54 100644 --- a/test/instruction.js +++ b/test/instruction.js @@ -13,7 +13,12 @@ tap.test('will parse an instruction', (t) => { tap.test('will parse an instruction with args', (t) => { t.plan(1) t.deepEqual(mona.parse(instructionParser(), 'stx $2000'), { - args: ['$,2000'], + args: [ + [ + 'address', + '2000' + ] + ], instruction: 'stx' }) }) diff --git a/test/parameters.js b/test/parameters.js new file mode 100644 index 0000000..6fd8f4e --- /dev/null +++ b/test/parameters.js @@ -0,0 +1,62 @@ +const tap = require('tap') +const mona = require('mona') +const parsers = require('../parsers/parameters') + +tap.test('should parse direct memory address param', (t) => { + t.plan(1) + const input = '$C000' + t.deepEqual(mona.parse(parsers.address(), input), [ + 'address', + 'C000' + ]) +}) + +tap.test('should parse hex param', (t) => { + t.plan(1) + const input = '#$FF' + t.deepEqual(mona.parse(parsers.hex(), input), [ + 'hex', + 'FF' + ]) +}) + +tap.test('should parse binary param', (t) => { + t.plan(1) + const input = '#%00000001' + t.deepEqual(mona.parse(parsers.binary(), input), [ + 'binary', + '00000001' + ]) +}) + +tap.test('should parse param', (t) => { + t.plan(1) + + const input = '#%00000001' + t.deepEqual(mona.parse(parsers.parameter(), input), [ + [ + 'binary', + '00000001' + ] + ]) +}) + +tap.test('should parse multiple params', (t) => { + t.plan(1) + + const input = '$24,$24,$24' + t.deepEqual(mona.parse(parsers.parameters(), input), [ + [ + 'address', + '24' + ], + [ + 'address', + '24' + ], + [ + 'address', + '24' + ] + ]) +})