-
Notifications
You must be signed in to change notification settings - Fork 1
/
hrm-cpu.js
188 lines (140 loc) · 4.13 KB
/
hrm-cpu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const parseArgs = require( './parse-args' )
const runtimeErrors = require( './runtime-errors' )
require( './polyfills' )
//no rest parameters in node yet :/
const HrmCpu = function(){
const setup = parseArgs( Array.from( arguments ) )
const options = setup.options
const state = setup.state
const step = () => {
var line = state.program[ state.counter ]
var instr = line[ 0 ]
var arg = line[ 1 ]
var dereferenced = false
if( arg && ( String( arg ) ).startsWith( '[' ) ){
arg = state.memory[ parseInt( arg.substr( 1 ) ) ]
dereferenced = true
}
var errorState = ErrorState( dereferenced, options, state )
try{
runtimeErrors( instr, arg, errorState )
} catch( err ){
state.running = false
return err
}
executor( state )[ instr ]( arg )
state.running = state.counter < state.program.length
}
return {
options,
state,
run: cb => {
const isCb = typeof cb === 'function'
state.running = state.counter < state.program.length
while( state.running ){
var err = step()
if( err ){
if( isCb ){
cb( err, state )
return
}
throw err
}
}
if( isCb ){
cb( null, state.outbox, state )
return
}
return state.outbox
},
step: cb => {
const isCb = typeof cb === 'function'
state.running = state.counter < state.program.length
var err = null
if( state.running ){
err = step()
}
if( err ){
if( isCb ){
cb( err, state )
return
}
throw err
}
if( isCb ){
cb( null, state )
return
}
return state
}
}
}
const cpu = {
INBOX: state =>
state.accumulator = state.inbox.shift(),
OUTBOX: state => {
state.outbox.push( state.accumulator )
state.accumulator = null
},
COPYFROM: ( state, address ) =>
state.accumulator = state.memory[ address ],
COPYTO: ( state, address ) =>
state.memory[ address ] = state.accumulator,
ADD: ( state, address ) =>
state.accumulator = add( state.accumulator, state.memory[ address ] ),
SUB: ( state, address ) =>
state.accumulator = sub( state.accumulator, state.memory[ address ] ),
BUMPUP: ( state, address ) => {
state.memory[ address ] = add( state.memory[ address ], 1 )
state.accumulator = state.memory[ address ]
},
BUMPDN: ( state, address ) => {
state.memory[ address ] = sub( state.memory[ address ], 1 )
state.accumulator = state.memory[ address ]
},
JUMP: ( state, line ) =>
state.counter = line,
JUMPZ: ( state, line ) =>
state.counter = state.accumulator === 0 ? line : state.counter + 1,
JUMPN: ( state, line ) =>
state.counter = state.accumulator < 0 ? line : state.counter + 1
}
const executor = state =>
Object.keys( cpu ).reduce( ( execute, instr ) => {
execute[ instr ] = arg => {
if( instr === 'INBOX' && state.inbox.length === 0 ){
state.counter = Infinity
return
}
cpu[ instr ]( state, arg )
if( !jumps.includes( instr ) )
state.counter++
state.steps++
return
}
return execute
}, {})
const ErrorState = ( dereferenced, options, state ) => {
return {
dereferenced,
commands: options.commands,
dereferencing: options.dereferencing,
maxSteps: options.maxSteps,
memorySize: options.columns * options.rows,
maxSize: options.maxSize,
minValue: options.minValue,
maxValue: options.maxValue,
steps: state.steps,
accumulator: state.accumulator,
memory: state.memory,
size: state.program.length
}
}
const asNumber = s =>
typeof s === 'string' ? s.charCodeAt( 0 ) : s
const add = ( a, b ) =>
asNumber( a ) + asNumber( b )
const sub = ( a, b ) =>
asNumber( a ) - asNumber( b )
const jumps = [ 'JUMP', 'JUMPZ', 'JUMPN' ]
module.exports = HrmCpu