Skip to content

Commit

Permalink
Add verbose output option
Browse files Browse the repository at this point in the history
  • Loading branch information
nrkn committed Oct 21, 2015
1 parent 1035475 commit f1587e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
23 changes: 19 additions & 4 deletions hrm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const compile = require( './compile' )

module.exports = ( source, inbox, floor ) => {
module.exports = ( source, inbox, floor, verbose ) => {
const outbox = []
const memory = []

var accumulator = null
var counter = 0
var steps = 0

Object.keys( floor || {} ).forEach( key =>
memory[ key ] = floor[ key ]
Expand All @@ -14,7 +15,9 @@ module.exports = ( source, inbox, floor ) => {
const cpu = {
INBOX: () => {
if( inbox.length === 0 ){
counter = Infinity
counter = Infinity
steps--

return
}

Expand Down Expand Up @@ -76,10 +79,22 @@ module.exports = ( source, inbox, floor ) => {

const instr = program[ i ]

cpu[ instr[ 0 ] ]( instr[ 1 ] )
cpu[ instr[ 0 ] ]( instr[ 1 ] )

steps++

return execute( program, counter )
}

return execute( compile( source ), 0 )
const program = compile( source )

const result = execute( program, 0 )

return verbose ? {
accumulator,
memory,
outbox: result,
size: program.length,
steps
} : result
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hrm-cpu",
"version": "0.0.4",
"version": "0.0.7",
"description": "Run Human Resources Machine programs in JavaScript",
"main": "hrm.js",
"directories": {
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ console.log( outbox )
```

Also see examples directory or try running the tests with mocha

If you have no initial floor state (like the early levels) you can omit floor:

```javascript
const outbox = hrm( source, inbox )
```

You can also get verbose output (for checking your size and steps):

```javascript
const info = hrm( source, inbox, floor, true )
```

### TODO

Expand Down

0 comments on commit f1587e5

Please sign in to comment.