@@ -78,6 +78,12 @@ public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStr
7878 public static int executeProgram (boolean trefunge , String [] args , byte [] program , long iterLimit , InputStream input , OutputStream output , FileIOSupplier fileIOSupplier ) {
7979 val interpreter = new Interpreter (trefunge , args , input , output , fileIOSupplier );
8080 interpreter .fungeSpace ().loadFileAt (0 , 0 , 0 , program , trefunge );
81+ //Init step
82+ {
83+ val ip = interpreter .IPs .get (0 );
84+ ip .position .sub (ip .delta );
85+ interpreter .step (ip );
86+ }
8187 if (iterLimit > 0 ) {
8288 long step = 0 ;
8389 while (!interpreter .stopped () && step < iterLimit ) {
@@ -148,7 +154,7 @@ public void interpret(int opcode) {
148154 }
149155
150156
151- private void wrappingStep (InstructionPointer ip ) {
157+ private boolean wrappingStep (InstructionPointer ip ) {
152158 ip .position .add (ip .delta );
153159 if (!fungeSpace ().bounds ().inBounds (ip .position )) {
154160 ip .delta .mul (-1 );
@@ -162,7 +168,9 @@ private void wrappingStep(InstructionPointer ip) {
162168 do {
163169 ip .position .add (ip .delta );
164170 } while (!fungeSpace ().bounds ().inBounds (ip .position ));
171+ return true ;
165172 }
173+ return false ;
166174 }
167175
168176 public void step (InstructionPointer ip ) {
@@ -174,19 +182,23 @@ public void step(InstructionPointer ip) {
174182 return ;
175183 }
176184 }
185+ int flipCount = 0 ;
177186 do {
178- wrappingStep (ip );
187+ flipCount += wrappingStep (ip ) ? 1 : 0 ;
179188 p = fungeSpace .get (ip .position );
180189 if (!ip .stringMode ) {
181190 while (p == ';' ) {
182191 do {
183- wrappingStep (ip );
192+ flipCount += wrappingStep (ip ) ? 1 : 0 ;
184193 p = fungeSpace .get (ip .position );
185194 } while (p != ';' );
186- wrappingStep (ip );
195+ flipCount += wrappingStep (ip ) ? 1 : 0 ;
187196 p = fungeSpace .get (ip .position );
188197 }
189198 }
199+ if (flipCount == 1000 ) {
200+ throw new IllegalStateException ("IP " + ip .UUID + " is stuck on a blank strip!\n Pos: " + ip .position + ", Delta: " + ip .delta + (ip .position .equals (0 , 0 , 0 ) && ip .UUID == 0 ? "\n Is the file empty?" : "" ));
201+ }
190202 } while (p == ' ' );
191203 }
192204
0 commit comments