0

what is the expected behaviour for the ~ instruction in befunge-98, when EOF is encountered?

Intuitively, it should place -1 on the stack, but I discovered some variation in this respect:

  • the "intuitive" way is followed by the Befunge-93 JS interpreter. (The below script outputs: "-1 -1 97")
  • Michael Riley's interpreter treats EOF as LF character (ASCII 10) and keeps placing it on top when an extra (non-existent) character is read. (Outputs: "10 10 97")
  • Matti Niemenmaa's interpreter also treats EOF as LF, but keeps waiting for user input when an extra character is read. (Outputs nothing)

Here is the test:

echo "a" | funge test.fg

with test.fg as follows (reads three chars and outputs their codes):

~~~...@

Are there actually interpreters that do treat EOF correctly (i.e. differently from LF) and still support the full befunge-98 specs?

Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
alephreish
  • 490
  • 3
  • 15

1 Answers1

2

CCBI is following the specification:

In the case of an end-of-file or other file error condition, the & and ~ both act like r.

As can be verified using its built-in tracer/debugger:

$ echo "~~~...@" > test.fg
$ echo "a" > input
$ ccbi --trace test.fg

Instruction: 126 0x7e '~'
Position: (0,0) -- Delta: (1,0) -- Offset: (0,0)
Stack: 0 cell(s): [  -   -   -   -   -   -   -   -] ""
Tick: 0 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

(Tracer) stdin < input
Successfully set stdin to file 'input'.
(Tracer) s

Instruction: 126 0x7e '~'
Position: (1,0) -- Delta: (1,0) -- Offset: (0,0)
Stack: 1 cell(s): [  -   -   -   -   -   -   -  97] "a"
Tick: 1 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

(Tracer) s

Instruction: 126 0x7e '~'
Position: (2,0) -- Delta: (1,0) -- Offset: (0,0)
Stack: 2 cell(s): [  -   -   -   -   -   -  97  10] "a^J"
Tick: 2 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

(Tracer) s

Instruction: 126 0x7e '~'
Position: (1,0) -- Delta: (-1,0) -- Offset: (0,0)
Stack: 2 cell(s): [  -   -   -   -   -   -  97  10] "a^J"
Tick: 3 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

(Tracer) s

Instruction: 126 0x7e '~'
Position: (2,0) -- Delta: (1,0) -- Offset: (0,0)
Stack: 2 cell(s): [  -   -   -   -   -   -  97  10] "a^J"
Tick: 4 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

(Tracer) s

Instruction: 126 0x7e '~'
Position: (1,0) -- Delta: (-1,0) -- Offset: (0,0)
Stack: 2 cell(s): [  -   -   -   -   -   -  97  10] "a^J"
Tick: 5 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

(Tracer) s

Instruction: 126 0x7e '~'
Position: (2,0) -- Delta: (1,0) -- Offset: (0,0)
Stack: 2 cell(s): [  -   -   -   -   -   -  97  10] "a^J"
Tick: 6 -- IPs: 1 -- Index/ID: 0/0 -- Stacks: 1 -- Mode:

On tick 3, the delta has changed from (1,0) to (-1,0), i.e. the ~ instruction on column 3 (position (2,0)) reflected on EOF as expected. After that, the code infloops between the two ~ instructions.

Your code could be amended to check for conformant ~-on-EOF behaviour e.g. like so:

~~#v~...a"tcelfer ton did">:#,_@
   >..a"detcelfer">:#,_@
Deewiant
  • 106
  • 1
  • 3
  • Great! I will pay more attention to --trace :) – alephreish Apr 07 '14 at 08:26
  • 1
    Also, if you want a second opinion on these kinds of things, the [cfunge](http://sourceforge.net/projects/cfunge/) interpreter's Funge-98 conformance is pretty much on par with CCBI's. – Deewiant Apr 07 '14 at 12:21