1
0
mirror of https://github.com/emkay/parser-6502.git synced 2024-12-30 12:31:29 +00:00
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 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({

View File

@ -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,

View File

@ -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}`

View File

@ -6,54 +6,101 @@ function quotedChar () {
mona.value('"')))
}
// pulled out, because in the future, this might be more detailed!
// <parameter> ::= <alphanum>+
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
}

View File

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

View File

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

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