DavidKinder / Inform6

The latest version of the Inform 6 compiler, used for generating interactive fiction games.
http://inform-fiction.org/
Other
199 stars 32 forks source link

Platform-independent linefeed handling #224

Closed erkyrath closed 1 year ago

erkyrath commented 1 year ago

The compiler opens source files with fopen(name,"r"). This can behave differently on different platforms.

On Windows (I should really say, with the standard Windows C environment), fopen() in text mode converts DOS line breaks \r\n to \n. On Mac/Unix, it doesn't. (Probably on Classic MacOS, fopen() converts \r to \n, not that anyone cares any more.)

This is mostly transparent to Inform because (a) source_to_iso_grid[] converts \r to \n anyhow; and (b) the I6 language always treats a double line break the same as a single line break. So if you compile a DOS-style text file on Unix, the \r\n line breaks come in as \n\n, but the game compiles the same.

However, this messes up error reporting. Every \n is a call to reached_new_line(). So error/warning messages report double the line number.

[ Main; ];
! Comment
#Message error "Line three.";

line 5: Error: Line three.

Message error "Line three."

It would be better in principle to behave exactly the same on every platform. We would open source files with fopen(name,"rb") and then do our own conversion of either \r\n or \r to \n. Probably this would happen in get_next_char_from_pipeline()?

Forum thread: https://intfiction.org/t/i6-compiler-bug-line-error-reference/62300

Kroc commented 1 year ago

This would particularly benefit retro systems that didn't have a consist standard for newlines. Mac Classic uses \r only, for example.

erkyrath commented 1 year ago

As far as I can tell without testing, the compiler behaves correctly on Mac Classic (and when opening \r source files).

(a) source_to_iso_grid[] converts \r to \n anyhow

erkyrath commented 1 year ago

Fixed with the above merge.