@@ -14,28 +14,46 @@ const IMMEDIATE_MODE = '1';
1414const RELATIVE_MODE = '2' ;
1515
1616class Computer {
17+ static ADD = ADD ;
18+ static MUL = MUL ;
19+ static INP = INP ;
20+ static OUT = OUT ;
21+ static JIT = JIT ;
22+ static JIF = JIF ;
23+ static LTH = LTH ;
24+ static EQU = EQU ;
25+ static ARB = ARB ;
26+ static STP = STP ;
27+
28+ static POSITION_MODE = POSITION_MODE ;
29+ static IMMEDIATE_MODE = IMMEDIATE_MODE ;
30+ static RELATIVE_MODE = RELATIVE_MODE ;
31+
1732 constructor ( {
1833 memory,
1934 inputs = [ ] ,
20- replenish_input = undefined ,
21- pause_on_output = true ,
22- id = 0 ,
35+ // Called with computer as it's only arg
36+ defaultInput,
37+ pause_on = { [ OUT ] : true } ,
38+ address,
2339 clone_memory = false ,
2440 } ) {
25- // For debugging
26- this . id = String . fromCharCode ( 'A' . charCodeAt ( 0 ) + id ) ;
41+ // Must match initial input
42+ this . address = address ;
43+ if ( this . address !== inputs [ 0 ] ) {
44+ throw new Error ( `Invalid address: ${ address } , inputs: ${ JSON . stringify ( inputs ) } ` ) ;
45+ }
2746
2847 this . original_memory = clone_memory && memory . slice ( 0 ) ;
2948 this . memory = memory . slice ( 0 ) ;
3049 this . pointer = 0 ;
3150 this . relative_base = 0 ;
32- this . pause_on_output = pause_on_output ;
51+ this . pause_on = pause_on ;
3352
3453 this . inputs = Array . isArray ( inputs ) ? inputs . slice ( 0 ) : [ inputs ] ;
35- this . replenish_input = replenish_input ;
3654 this . outputs = [ ] ;
3755
38- this . parseOpTime = 0 ;
56+ this . defaultInput = defaultInput ;
3957
4058 this . OPS = {
4159 [ ADD ] : {
@@ -63,10 +81,11 @@ class Computer {
6381 realName : 'INP' ,
6482 params : 1 ,
6583 fn : ( a ) => {
84+ if ( this . defaultInput && this . inputs . length === 0 ) {
85+ let default_input_value = this . defaultInput ( this ) ;
86+ this . inputs . push ( default_input_value ) ;
87+ }
6688 this . memory [ a ] = this . inputs . shift ( ) ;
67- if ( this . replenish_input !== undefined ) {
68- this . inputs . push ( this . replenish_input ) ;
69- }
7089 } ,
7190 write : true ,
7291 } ,
@@ -177,10 +196,10 @@ class Computer {
177196 this . runOp ( op ) ;
178197
179198 /**
180- * In circuits, computer execution should be paused on outout so that value can be passed to the next computer.
199+ * In circuits, computer execution should be paused on output so that value can be passed to the next computer.
181200 * Additionally, execution should immediately stopped if we have halted.
182201 */
183- if ( ( this . pause_on_output && op . name === OUT ) || this . halted ) {
202+ if ( this . pause_on [ op . name ] || this . halted ) {
184203 break ;
185204 }
186205
0 commit comments