git @ Cat's Eye Technologies yoob / master src / lang / Befunge93State.java
master

Tree @master (Download .tar.gz)

Befunge93State.java @masterraw · history · blame

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
 * A Befunge93State implements the semantics of Befunge-93.
 * The source code in this file has been placed into the public domain.
 */
package tc.catseye.yoob.befunge93;

import tc.catseye.yoob.*;
import tc.catseye.yoob.Error;

import java.util.List;
import java.util.ArrayList;
import java.util.Random;

import java.net.URL;
import java.net.MalformedURLException;

import java.awt.Graphics;
import java.awt.Color;


class Befunge93 implements Language {
    private ArrayList<ExampleProgram> examples = null;
  
    public String getName() {
        return "Befunge-93";
    }

    public int numPlayfields() {
        return 1;
    }

    public int numTapes() {
        return 1;
    }

    public boolean hasProgramText() {
        return false;
    }

    public boolean hasInput() {
        return true;
    }

    public boolean hasOutput() {
        return true;
    }

    private void loadExamples() {
        if (examples != null) return;

        examples = new ArrayList<ExampleProgram>();
        String[][] cpProperties = {
            {"Author", "Chris Pressey"},
            {"License", "Public Domain"},
        };

        examples.add(new ExampleProgram(
            "cascade.bf",
            ">011p013p>11g13gg:84*`#v_84*>11g13g4+p$v\n" +
            "#     13p^p11         <>    ^        >  \n" +
            "          v                            <\n" +
            "#     13pv>11g1+:85*-#^_011p13g1+:4%#^_",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "drx.bf",
            "#v       #<                                      v\n" +
            ">v\"Hello... I'm Dr. X.  How do you feel today? \"0<\n" +
            ",:        #\n" +
            "^_$v     <\n" +
            "   >~25*-|                                          > v\n" +
            "         >                                       0#v?v\n" +
            "          ^\"Do your friends find this reasonable? \"<\n" +
            "          ^\"How long have you felt this way? \"      <\n" +
            "          ^\"How do you feel about that? \"            <\n" +
            "          ^\"Are you disturbed by this? \"              <",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "ea.bf",
            "100p            v\n" +
            " v\"love\"0     <\n" +
            " v\"power\"0   <\n" +
            " v\"strength\"0?^#<            <\n" +
            " v\"success\"0 ?v\n" +
            " v\"agony\"0   <\n" +
            ">v\"beauty\"0   <>025*\".\" 1v v_^\n" +
            ",:      >00g2- |        v< #:\n" +
            "^_,00g1-|      >0\" fo \"3>00p^<\n" +
            "        >0\" eht si \"2   ^  >,^",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "easm2.bf",
            ">801p          11v\n" +
            " v\"love\"      < >+v\n" +
            "#v\"power\"    <  1$:\n" +
            " v\"strength\" ?^# <0\n" +
            " v\"success\"  ?v g 1\n" +
            " v\"agony\"    <  1 p\n" +
            " v\"beauty\"    < 0 9\n" +
            "            v ,<p 1\n" +
            " >\" eht si \">: |g +\n" +
            " >\" fo \"> v    <1 -\n" +
            " >25*\".\"^ >\" \"10^^_v\n" +
            "^        p91p81:\">\"<\n" +
            "\n",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "eterlan.bf",
            "5 52** 52** 52** 52**             1- 0v0                                     <\n" +
            ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>v>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>v  -\n" +
            "^                                                                         v  1\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"    v  |\n" +
            "^    X   Y   X   Y   X   Y   X   Y   I   X   Y   X   Y   X   Y   X   Y    v  @\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"#  \"   \"   \"   \"   \"   \"   \"   \"    v  #\n" +
            "^ \"N\"?\"S\"?\"E\"?\"W\"?\"U\"?\"D\"?\"L\"?\"R\"?\"P\"?\"G\"?\"C\"?\"C\"?\"F\"?\"O\"?\"U\"?\"R\"?\"A\"?\"M\" v  :\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"    v  $\n" +
            "^    J   U   B   I   L   A   N   T   B   E   F   U   N   G   I   N   G    v >^\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"#  \"   \"   \"   \"   \"   \"   \"   \"    v ,,\n" +
            "^ \"H\"?\"E\"?\"Y\"?\"M\"?\"Y\"?\"A\"?\"R\"?\"C\"?\"H\"?\"I\"?\"V\"?\"E\"?\"M\"?\"U\"?\"S\"?\"T\"?\"B\"?\"E\" v ,*\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   v   \"   \"   \"   \"   \"   \"   \"   \"    v :4\n" +
            "^    W   I   N   N   I   P   E   G  >v<  M   A   N   I   T   O   B   A    v *8\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   v   \"   \"   \"   \"   \"   \"   \"   \"    v 2 \n" +
            "^ \"C\"?\"O\"?\"R\"?\"R\"?\"U\"?\"P\"?\"T\"?\"X\"?\"T\"?\"H\"?\"E\"?\"S\"?\"E\"?\"F\"?\"I\"?\"L\"?\"E\"?\"S\" v 5 \n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"    v   \n" +
            "^    S   E   P   T   E   M   B   E   R   M   C   M   X   C   I   I   I    v>?^\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"    v ^ \n" +
            "^ \"A\"?\"L\"?\"L\"?\"L\"?\"O\"?\"O\"?\"K\"?\"L\"?\"I\"?\"K\"?\"E\"?\"G\"?\"A\"?\"R\"?\"B\"?\"A\"?\"G\"?\"E\" v v<\n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"    v #|\n" +
            "^    X   Y   X   Y   X   Y   X   Y   O   X   Y   X   Y   X   Y   X   Y    v ^<  \n" +
            "^    \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"   \"    v ,#  \n" +
            "^                                                                         v  :  \n" +
            "^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>^\n",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "fact.bf",
            "                                    v\n" +
            ">v\"Please enter a number (1-16) : \"0<\n" +
            ",:             >$*99g1-:99p#v_.25*,@\n" +
            "^_&:1-99p>:1-:!|10          < \n" +
            "         ^     <",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "hello.bf",
            "                 v\n" +
            ">v\"Hello world!\"0<\n" +
            ",:\n" +
            "^_25*,@",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "hex.bf",
            "~:25*-#v_@      >  >,\" \",\n" +
            "v      < >25*-\"A\"+v^+\"A\"-*52<\n" +
            ">:82*/:9`|      \" >,:82*%:9`|        \n" +
            "         >\"0\"+ #^ ^#    +\"0\"<",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "hwii.bf",
            "v       <\n" +
            ">0#v # \"Hello, World!\" # v#0  <\n" +
            "  >v    #               >v\n" +
            "  ,:                    ,:\n" +
            "  ^_25*,^               ^_25*,^",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "maze.bf",
            " v    <\n" +
            ">?\"/\",^\n" +
            " >\"\\\",^",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "numer.bf",
            "000p>~:25*-!#v_\"a\"-1+00g+00p> 00g9`#v_v\n" +
            "        @.g00<  vp00+%*52g00 /*52g00<\n" +
            "    ^           >#          ^#        <",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "pangram.bf",
            "v             <    >\"a\"-v\n" +
            "             >:\"`\"`|    >\"<\"\\5pv\n" +
            "       >:\"@\"`|#    >\"A\"-^\n" +
            ">~:91+-|     >^                <\n" +
            "       >                  v\n" +
            "vvvvvvvvvvvvvvvvvvvvvvvvvv< v\"yes\"\n" +
            "v<<<<<<<<<<<<<<<<<<<<<<<<<  ,\n" +
            ">\"on\"                       >,,91+,@\n",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "pascserp.bf",
            "58*00p010p>58*00g-|>0g#<1-10gg00g10v\n" +
            "v98p00:+1g00< v67<>    >1    v+g-1g<\n" +
            ">*7+-! #v_v>^^<  |%2pg0 1g00:<\n" +
            "v p00*58<  ^,<^48<>10g!|@\n" +
            ">52*,10g1+ :1 0p83 *- ! |\n" +
            "          v             <",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "rand.bf",
            "vv  <      <\n" +
            "    2      \n" +
            "    ^  v<\n" +
            " v1<?>3v4\n" +
            "    ^   ^\n" +
            ">  >?>  ?>5^\n" +
            "    v   v\n" +
            " v9<?>7v6\n" +
            "    v  v<\n" +
            "    8\n" +
            " .  >  >   ^\n" +
            "^<",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "rand3.bf",
            "7   $^>91+v\n" +
            "?95+vv?94+vv\n" +
            ">96+v9>93+v\n" +
            "# +  >#  v<#\n" +
            " >>>>  >>>.@\n" +
            " 123 >^456#9\n" +
            " ^?^#?#^?^ 7\n" +
            "^ ## <  8  +\n" +
            "> > ^#  < ^<",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "rand6.bf",
            "?<>>?<8>\n" +
            ">>?<>>?<\n" +
            "  2@4.+<\n" +
            "1+ + +<^",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "robot.bf",
            "vv_v#:\"*********\"*25<           01 = x coord\n" +
            "8,:  >              ^           02 = y coord\n" +
            "0>^  ^\"*     * *\"*25<\n" +
            "1    >              ^\n" +
            "p    ^\"* *** * *\"*25<\n" +
            "2    >              ^\n" +
            "0    ^\"* *     *\"*25<\n" +
            "2    >              ^\n" +
            "p    ^\"* * *   *\"*25<\n" +
            "\"    >              ^       \n" +
            "O    ^\"* ***** *\"*25<       >,v\n" +
            "\"    >              ^       |:<\"You hit a wall! Game over!\"0<\n" +
            "0    ^\"*     * *\"*25<       >25*,@                          |-*84gg20g10<\n" +
            "1    >              ^v ,*62                       pg20g10\"O\"<  <       \n" +
            "g    ^\"*   *   *\"*25<                                  >00g\"w\"-|\n" +
            "0    >              ^                          >00g\"e\"-|       >01g1-01p^\n" +
            "2    ^\"*********\"*250<                 >00g\"s\"-|       >01g1+01p        ^\n" +
            "g  > \" \"01g02gp         \"?\",~~$:00p\"n\"-|       >02g2+02p                ^\n" +
            ">p              62*, ^                 >02g2-02p                        ^",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "selflis2.bf",
            "\">:#,_66*2-,@This prints itself out backwards......  but it has to be 80x1 cells",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "testbrdg.bf",
            ">>>>v\n" +
            "@0.v>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#\n" +
            "#<<<                                                                        @.1@\n",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "testmodu.bf",
            "v The original implementation of Befunge-93 was in ANSI C (a.k.a C89).\n" +
            "v The description of Befunge-93 did not describe how modulo should be\n" +
            "v implemented for a negative modulus -- it relied on ANSI C's semantics.\n" +
            "v\n" +
            "v Unfortunately, ANSI C did not define negative modulus either.\n" +
            "v\n" +
            "v So this program tests what your Befunge-93 implementation does for\n" +
            "v modulo by negative numbers.  If it outputs:\n" +
            "v\n" +
            "v  1 -1 : result has same sign as the dividend (like C99)\n" +
            "v -2  2 : result has same sign as the divisor  (like Python)\n" +
            "v\n" +
            "v Of course, since it is undefined, other results are possible.\n" +
            "v\n" +
            ">703-%.07-3%.@\n",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "testpfcl.bf",
            "000p>00g1+01p01g00g`#v_00g.000p>00g1-01p00g01g`#v_00g.@\n" +
            "    ^p00+1g00        <         ^p00-1g00        <\n",
            cpProperties
        ));
        examples.add(new ExampleProgram(
            "toupper.bf",
            "v,         <         <       <\n" +
            ">~:\"a\"1-`!#^_:\"z\"1+`#^_\"aA\"--^",
            cpProperties
        ));

    }
      
    public List<String> exampleProgramNames() {
        loadExamples();
        ArrayList<String> names = new ArrayList<String>();
        for (ExampleProgram e : examples) {
            names.add(e.getName());
        }
        return names;
    }

    public Befunge93State loadExampleProgram(int index) {
        loadExamples();
        return importFromText(examples.get(index).getText());
    }

    public Befunge93State importFromText(String text) {
        Befunge93State s = new Befunge93State();
        s.playfield.load(text.split("\\r?\\n"));
        return s;
    }

    public List<String> getAvailableOptionNames() {
        ArrayList<String> names = new ArrayList<String>();
        return names;
    }

    private static final String[][] properties = {
        {"Author", "Chris Pressey"},
        {"Implementer", "Chris Pressey"},
        {"Implementation notes",
         "Reading integers from input (&) does not have the same semantics " +
         "as the 'bef' interpreter, but those semantics (naively " +
         "relying on C's scanf) are pretty crappy anyway, and we figure that " +
         "any Befunge-93 program that needs to read integers reliably will " +
         "read them character by character anyway.  In this implementation, & " +
         "reads digits until the first non-digit, which it consumes.  If there " +
         "are no digits at all, it consumes a character and pushes zero."},
    };

    public String[][] getProperties() {
        return properties;
    }

}

class Befunge93Playfield extends BasicPlayfield<CharacterElement> {
    protected WrapCursor<CharacterElement> pc = null;

    public Befunge93Playfield() {
        super(new CharacterElement(' '));
        clear();
    }

    public void clear() {
        super.clear();
        pc = new WrapCursor<CharacterElement>(this, IntegerElement.ZERO, IntegerElement.ZERO, IntegerElement.ONE, IntegerElement.ZERO);
    }

    public Befunge93Playfield clone() {
        Befunge93Playfield c = new Befunge93Playfield();
        c.copyBackingStoreFrom(this);
        c.pc = pc.clone();
        c.pc.setPlayfield(c);
        return c;
    }

    public IntegerElement getMinX() {
        return IntegerElement.ZERO;
    }

    public IntegerElement getMaxX() {
        return new IntegerElement(79);
    }

    public IntegerElement getMinY() {
        return IntegerElement.ZERO;
    }

    public IntegerElement getMaxY() {
        return new IntegerElement(24);
    }

    public int numCursors() {
        return 1;
    }

    public BasicCursor<CharacterElement> getCursor(int index) {
        if (index == 0)
            return pc;
        return null;
    }

    public void loadChar(int x, int y, char c) {
        if (x < 0 || x > 79 || y < 0 || y > 24) {
            return;
        }
        set(x, y, new CharacterElement(c));
    }
    
    public String dumpElement(CharacterElement e) {
        return e.getName();
    }
}

class Befunge93PlayfieldView extends BasicPlayfieldView {
}

public class Befunge93State implements State {
    protected BasicStack<Int32Element> stack;
    protected Befunge93Playfield playfield;
    protected BasicTapeView stackView;
    protected Befunge93PlayfieldView pfView;
    protected boolean stringmode = false;
    protected boolean halted = false;
    protected boolean needsInput = false;
    protected Int32Element inputIntAcc = null;
    private static Random rand = new Random();
    private static final Befunge93 language = new Befunge93();

    public Befunge93State() {
        stack = new BasicStack<Int32Element>(new Int32Element(0));
        playfield = new Befunge93Playfield();
        pfView = new Befunge93PlayfieldView();
        stackView = new BasicTapeView();
    }
    
    public Language getLanguage() {
        return language;
    }

    public Befunge93State clone() {
        Befunge93State c = new Befunge93State();
        c.playfield = playfield.clone();
        c.stack = stack.clone();
        c.stringmode = stringmode;
        c.halted = halted;
        c.needsInput = needsInput;
        c.inputIntAcc = inputIntAcc;
        return c;
    }

    /*
     * This has some limitations
     */
    private boolean readDigits(World world) {
        for (;;) {
            CharacterElement c = world.inputCharacter();
            if (c == null) {
                return false;
            } else if (c.isDigit()) {
                inputIntAcc = new Int32Element(inputIntAcc.getValue() * 10 + c.digitValue());
            } else {
                stack.push(inputIntAcc);
                inputIntAcc = null;
                return true;
            }
        }
    }

    public List<Error> step(World world) {
        ArrayList<Error> errors = new ArrayList<Error>();
        BasicCursor<CharacterElement> pc = playfield.getCursor(0);
        CharacterElement i = pc.get();
        char instruction = i.getChar();
        Int32Element a, b;
        CharacterElement c;

        if (inputIntAcc != null) {
            boolean finished = readDigits(world);
            if (finished) {
                pc.advance();
                needsInput = false;
            } else {
                needsInput = true;
            }
            return errors;
        }

        if (stringmode) {
            if (instruction == '"') {
                stringmode = false;
            } else {
                stack.push(new Int32Element(instruction));
            }
        } else if (instruction >= '0' && instruction <= '9') {
            stack.push(new Int32Element((int)instruction - (int)'0'));
        } else switch (instruction) {
            case '+':
                b = stack.pop();
                a = stack.pop();
                stack.push(a.add(b));
                break;
            case '-':
                b = stack.pop();
                a = stack.pop();
                stack.push(a.subtract(b));
                break;
            case '*':
                b = stack.pop();
                a = stack.pop();
                stack.push(a.multiply(b));
                break;
            case '/':
                b = stack.pop();
                a = stack.pop();
                if (b.isZero())
                    stack.push(Int32Element.ZERO);
                else
                    stack.push(a.divide(b));
                break;
            case '%':
                b = stack.pop();
                a = stack.pop();
                if (b.isZero())
                    stack.push(Int32Element.ZERO);
                else
                    stack.push(a.modulo(b));
                break;
            case '<':
                pc.setDelta(-1, 0);
                break;
            case '>':
                pc.setDelta(1, 0);
                break;
            case '^':
                pc.setDelta(0, -1);
                break;
            case 'v':
                pc.setDelta(0, 1);
                break;
            case '~':
                // ~ - Input an ASCII character from standard input and push onto stack.
                c = world.inputCharacter();
                if (c == null) {
                    needsInput = true;
                    return errors;
                }
                stack.push(new Int32Element(c.getChar()));
                break;
            case '&':
                // & - Input an integer (in ASCII characters, terminated by a non-digit)
                // from standard input and push onto stack.
                inputIntAcc = new Int32Element(0);
                boolean finished = readDigits(world);
                if (!finished) {
                    needsInput = true;
                    return errors;
                }
                break;
            case ',':
                // , - Pop a value off the stack and output as an ASCII character
                a = stack.pop();
                world.output(new CharacterElement(a.toChar()));
                break;
            case '.':
                // . - Pop a value off the stack and output as an decimal
                // integer followed by a space (all in ASCII)
                a = stack.pop();
                world.output(a);
                world.output(new CharacterElement(' '));
                break;
            case '#':
                // # - Jump over the next cell.
                pc.advance();
                break;
            case '@':
                // @ - End the program.
                halted = true;
                return errors;
            case '|':
                a = stack.pop();
                pc.setDelta(0, a.isZero() ? 1 : -1);
                break;
            case '_':
                a = stack.pop();
                pc.setDelta(a.isZero() ? 1 : -1, 0);
                break;
            case '$':
                a = stack.pop();
                break;
            case ':':
                a = stack.pop();
                stack.push(a);
                stack.push(a);
                break;
            case '\\':
                a = stack.pop();
                b = stack.pop();
                stack.push(a);
                stack.push(b);
                break;
            case '!':
                a = stack.pop();
                if (a.isZero())
                    stack.push(Int32Element.ONE);
                else
                    stack.push(Int32Element.ZERO);
                break;
            case '`':
                b = stack.pop();
                a = stack.pop();
                stack.push(a.getValue() > b.getValue() ?
                           Int32Element.ONE : Int32Element.ZERO);
                break;
            case '"':
                stringmode = true;
                break;
            case '?':
                switch (rand.nextInt(4)) {
                    case 0:
                        pc.setDelta(0, -1); break;
                    case 1:
                        pc.setDelta(0, 1); break;
                    case 2:
                        pc.setDelta(-1, 0); break;
                    case 3:
                        pc.setDelta(1, 0); break;
                }
                break;
            case 'g':
                b = stack.pop();
                a = stack.pop();
                c = playfield.get(a.getValue(), b.getValue());
                stack.push(new Int32Element((int)c.getChar()));
                break;
            case 'p':
                b = stack.pop();
                a = stack.pop();
                Int32Element v = stack.pop();
                c = new CharacterElement(v.getValue());
                playfield.set(a.getValue(), b.getValue(), c);
                break;
            default:
                // NOP
                break;
        }

        pc.advance();
        needsInput = false;
        return errors;
    }

    public Playfield getPlayfield(int index) {
        if (index == 0)
            return playfield;
        return null;
    }

    public Tape getTape(int index) {
        if (index == 0)
            return stack;
        return null;
    }

    public String getProgramText() {
        return "";
    }

    public int getProgramPosition() {
        return 0;
    }

    public List<Error> setProgramText(String text) {
        ArrayList<Error> errors = new ArrayList<Error>();
        return errors;
    }

    public View getPlayfieldView(int index) {
        return pfView;
    }

    public View getTapeView(int index) {
        return stackView;
    }

    public String exportToText() {
        return playfield.dump();
    }

    public boolean hasHalted() {
        return halted;
    }

    public boolean needsInput() {
        return needsInput;
    }

    public void setOption(String name, boolean value) {
    }
}