Skip to content

Commit

Permalink
Bug in runtime error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
nrkn committed Oct 25, 2015
1 parent cb7bb98 commit 1131406
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
40 changes: 20 additions & 20 deletions hrm.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,54 +49,54 @@ module.exports = ( source, inbox, floor, verbose ) => {
counter++
},

COPYFROM: i => {
accumulator = memory[ i ]
COPYFROM: address => {
accumulator = memory[ address ]

counter++
},

COPYTO: i => {
memory[ i ] = accumulator
COPYTO: address => {
memory[ address ] = accumulator

counter++
},

ADD: i => {
accumulator = add( accumulator, memory[ i ] )
ADD: address => {
accumulator = add( accumulator, memory[ address ] )

counter++
},

SUB: i => {
accumulator = sub( accumulator, memory[ i ] )
SUB: address => {
accumulator = sub( accumulator, memory[ address ] )

counter++
},

BUMPUP: i => {
memory[ i ] = add( memory[ i ], 1 )
BUMPUP: address => {
memory[ address ] = add( memory[ address ], 1 )

accumulator = memory[ i ]
accumulator = memory[ address ]

counter++
},

BUMPDN: i => {
memory[ i ] = sub( memory[ i ], 1 )
BUMPDN: address => {
memory[ address ] = sub( memory[ address ], 1 )

accumulator = memory[ i ]
accumulator = memory[ address ]

counter++
},

JUMP: i =>
counter = i,
JUMP: line =>
counter = line,

JUMPZ: i =>
counter = accumulator === 0 ? i : counter + 1,
JUMPZ: line =>
counter = accumulator === 0 ? line : counter + 1,

JUMPN: i =>
counter = accumulator < 0 ? i : counter + 1
JUMPN: line =>
counter = accumulator < 0 ? line : counter + 1
}

const execute = ( program, i ) => {
Expand Down
26 changes: 17 additions & 9 deletions runtime-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,29 @@ const checks = {
}
}

const emptyHands = name =>
`Empty value! You can't ${ name } with empty hands!`

const emptyTile = name =>
`Empty value! You can't ${ name } with an empty tile on the floor! Try writing something to that tile first.`
const EmptyHandsError = instrName => {
return {
name: 'Empty Hands',
message: `Empty value! You can't ${ instrName } with empty hands!`
}
}

const EmptyTileError = instrName => {
return {
name: 'Empty Tile',
message: `Empty value! You can't ${ instrName } with an empty tile on the floor! Try writing something to that tile first.`
}
}

module.exports = ( instr, arg, state ) => {
const errorChecks = {
accumulator: name => {
if( state.accumulator === null )
throw Error( emptyHands( name ) )
throw EmptyHandsError( name )
},
memory: ( name, i ) => {
if( state.memory[ i ] === null )
throw Error( emptyTile( name ) )
memory: ( name, address ) => {
if( state.memory[ address ] === undefined || state.memory[ address ] === null )
throw EmptyTileError( name )
}
}

Expand Down
29 changes: 14 additions & 15 deletions test/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ const floor = {
}

const fails = {
"OUTBOX empty hands": `
"OUTBOX: Empty Hands": `
OUTBOX
`,
"COPYFROM empty floor": `
"COPYFROM: Empty Floor": `
COPYFROM 0
`,
"COPYTO empty hands": `
"COPYTO: Empty Hands": `
COPYTO 0
`,
"ADD empty hands": `
"ADD: Empty Hands": `
ADD 1
`,
"ADD empty floor": `
"ADD: Empty Floor": `
COPYFROM 1
ADD 0
`,
"SUB empty hands": `
"SUB: Empty Hands": `
SUB 1
`,
"SUB empty floor": `
"SUB: Empty Floor": `
COPYFROM 1
SUB 0
`,
"BUMPUP empty floor": `
"BUMPUP: Empty Floor": `
BUMPUP 0
`,
"BUMPDN empty floor": `
"BUMPDN: Empty Floor": `
BUMPDN 0
`,
"JUMPZ empty hands": `
"JUMPZ: Empty Hands": `
a:
JUMPZ a
`,
"JUMPN empty hands": `
"JUMPN: Empty Hands": `
a:
JUMPN a
`
Expand All @@ -48,10 +48,9 @@ a:
describe( 'hrm-cpu runtime errors', () =>
Object.keys( fails ).forEach( key => {
const source = fails[ key ]
it( 'throws on ' + key, done => {
assert.throws( () => {
hrm( source, floor )
})

it( key, done => {
assert.throws( () => hrm( source, [ 1 ], floor ) )
done()
})
})
Expand Down

0 comments on commit 1131406

Please sign in to comment.