This commit is contained in:
Michael Matuzak 2016-10-07 10:45:55 -07:00
parent 8a077f9d66
commit cb0e34d1e3
7 changed files with 213 additions and 62 deletions

View File

@ -1,5 +1,5 @@
const mona = require('mona') const mona = require('mona')
const parameters = require('./parameters') const parameters = require('./parameters').parameters
function directiveName () { function directiveName () {
return mona.oneOf([ return mona.oneOf([
@ -23,7 +23,8 @@ function directiveName () {
function directive () { function directive () {
return mona.sequence((s) => { return mona.sequence((s) => {
const d = s(directiveName()) 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()) // const nl = s(mona.eol())
return mona.value({ return mona.value({

View File

@ -1,5 +1,5 @@
const mona = require('mona') const mona = require('mona')
const parameters = require('./parameters') const parameters = require('./parameters').parameters
const instructions = [ const instructions = [
'adc', 'and', 'asl', 'adc', 'and', 'asl',
@ -25,7 +25,8 @@ function instructionName () {
function instruction () { function instruction () {
return mona.sequence((s) => { return mona.sequence((s) => {
const i = s(instructionName()) 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()) // const nl = s(mona.eol())
return mona.value({ return mona.value({
instruction: i, instruction: i,

View File

@ -4,7 +4,7 @@ function label () {
return mona.sequence((s) => { return mona.sequence((s) => {
const label = s(mona.text(mona.alphanum())) const label = s(mona.text(mona.alphanum()))
const end = s(mona.string(':')) const end = s(mona.string(':'))
const nl = s(mona.eol()) // const nl = s(mona.eol())
return mona.value({ return mona.value({
label: `${label}${end}` label: `${label}${end}`

View File

@ -6,54 +6,101 @@ function quotedChar () {
mona.value('"'))) mona.value('"')))
} }
// pulled out, because in the future, this might be more detailed! function alphanum () {
// <parameter> ::= <alphanum>+ return mona.join(
function parameter () { mona.value('alphanum'),
const param = () => { mona.string('background3')
return mona.or( )
mona.join( }
mona.string('$'),
mona.alphanum() function bit () {
), return mona.join(
mona.join( mona.value('digit'),
mona.string('#'), mona.digit(2)
mona.string('$'), )
mona.alphanum() }
),
mona.join( function string () {
mona.string('#'), return mona.join(
mona.string('%'), mona.value('string'),
mona.alphanum() mona.between(
), mona.string('"'),
mona.join( mona.string('"'),
mona.string('%'), mona.text(quotedChar())
mona.alphanum() )
), )
mona.between( }
mona.string('"'),
mona.string('"'), function hex () {
mona.text(quotedChar()) 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() mona.alphanum()
) )
} )
}
return mona.text(param(), {min: 1}) function parameter () {
return mona.collect(
mona.or(
address(),
binary(),
hex(),
string(),
bit(),
alphanum()
)
)
} }
function parameters () { function parameters () {
return mona.collect( return mona.map(a => a.map(b => b[0]),
mona.and( mona.split(
mona.spaces(), parameter(),
mona.split( mona.or(
parameter(), mona.and(mona.string(','), mona.spaces()),
mona.or( mona.string(','),
mona.and(mona.string(','), mona.spaces()), mona.spaces()
mona.string(',')
)
) )
) )
) )
} }
module.exports = parameters module.exports = {
parameter,
parameters,
address,
binary,
hex,
string,
bit,
alphanum
}

View File

@ -6,7 +6,10 @@ tap.test('will parse a directive', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.inesprg 1'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg 1'), {
args: [ args: [
'1' [
'digit',
'1'
]
], ],
directive: '.inesprg' directive: '.inesprg'
}) })
@ -16,7 +19,10 @@ tap.test('will parse a directive with direct address', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.inesprg $0000'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg $0000'), {
args: [ args: [
'$,0000' [
'address',
'0000'
]
], ],
directive: '.inesprg' directive: '.inesprg'
}) })
@ -26,7 +32,10 @@ tap.test('will parse a directive with hex arg', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.inesprg #$FE'), { t.deepEqual(mona.parse(directiveParser(), '.inesprg #$FE'), {
args: [ args: [
'#,$,FE' [
'hex',
'FE'
]
], ],
directive: '.inesprg' directive: '.inesprg'
}) })
@ -36,14 +45,20 @@ tap.test('will parse a directive with binary arg', (t) => {
t.plan(2) t.plan(2)
t.deepEqual(mona.parse(directiveParser(), '.db %00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db %00010001'), {
args: [ args: [
'%,00010001' [
'binary',
'00010001'
]
], ],
directive: '.db' directive: '.db'
}) })
t.deepEqual(mona.parse(directiveParser(), '.db #%00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db #%00010001'), {
args: [ args: [
'#,%,00010001' [
'binary',
'00010001'
]
], ],
directive: '.db' 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) => { tap.test('will parse a directive with multiple args', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.db %00010001,%00010001,%00010001,%00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db %00010001,%00010001,%00010001'), {
args: [ args: [
'%,00010001', [
'%,00010001', 'binary',
'%,00010001', '00010001'
'%,00010001' ],
[
'binary',
'00010001'
],
[
'binary',
'00010001'
]
], ],
directive: '.db' 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) => { tap.test('will parse a directive with multiple args with spaces between them', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.db %00010001, %00010001, %00010001, %00010001'), { t.deepEqual(mona.parse(directiveParser(), '.db %00010001, %00010001, %00010001'), {
args: [ args: [
'%,00010001', [
'%,00010001', 'binary',
'%,00010001', '00010001'
'%,00010001' ],
[
'binary',
'00010001'
],
[
'binary',
'00010001'
]
], ],
directive: '.db' directive: '.db'
}) })
@ -79,7 +111,10 @@ tap.test('will parse a directive with string arg', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(directiveParser(), '.incbin "mario.chr"'), { t.deepEqual(mona.parse(directiveParser(), '.incbin "mario.chr"'), {
args: [ args: [
'mario.chr' [
'string',
'mario.chr'
]
], ],
directive: '.incbin' directive: '.incbin'
}) })

View File

@ -13,7 +13,12 @@ tap.test('will parse an instruction', (t) => {
tap.test('will parse an instruction with args', (t) => { tap.test('will parse an instruction with args', (t) => {
t.plan(1) t.plan(1)
t.deepEqual(mona.parse(instructionParser(), 'stx $2000'), { t.deepEqual(mona.parse(instructionParser(), 'stx $2000'), {
args: ['$,2000'], args: [
[
'address',
'2000'
]
],
instruction: 'stx' instruction: 'stx'
}) })
}) })

62
test/parameters.js Normal file
View File

@ -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'
]
])
})