Comment after line continuation

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Comment after line continuation

Post by fzabkar »

When I insert a comment after a line continuation, the compiler throws an error. It would appear that the continuation character takes precedence over the comment character. Is this the intended behaviour?

Code: Select all

Print   "text string #1"; Space(5); _
        "text string #2"; Space(5); _
        "text string #3"

Print   "text string #4"; Space(5); _
'        "text string #5"; Space(5); _
        "next text string"

Print   "text string #7"; Space(5); _
'        "text string #8"; Space(5);
        "last text string"

Sleep

Code: Select all

Command executed:
"C:\FreeBASIC\fbc.exe" "C:\FreeBASIC\FBIDE\FBIDETEMP.bas"

Compiler output:
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(7) error 119: Cannot modify a constant in '"next text string"'
C:\FreeBASIC\FBIDE\FBIDETEMP.bas(11) error 119: Cannot modify a constant in '"last text string"'

Results:
Compilation failed

System:
FBIde: 0.4.6
fbc:   FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for win32 (32bit)
OS:    Windows NT 6.2 (build 9200)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Comment after line continuation

Post by MrSwiss »

There is no problem, if it's done correctly. The ordering is important for correct working:

Code: Select all

Print   "text string #1"; Space(5); _
        "text string #2"; Space(5); _
        "text string #3"

Print   "text string #4"; Space(5); _ ' "text string #5"; Space(5); _
        "next text string"

Print   "text string #7"; Space(5); _ 
        /' "text string #8"; Space(5); '/ _	' <-- line-continue (code) outside comment block!
        "last text string"

Sleep
You cannot line-continue with the following line being a comment alone (aka: no code first).
Except, you are using block comment followed by code: /' comment '/ _
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Comment after line continuation

Post by counting_pine »

It may be worth just saying, a line with just a comment (or an entirely blank line) appearing after a continued line, is processed the same way as actual code would be: it's as if the preceding underscore (and anything on the line after it) is stripped away and the contents of the following line is appended. In the case of a blank line or just a comment, then of course nothing interesting is appended.

You can put comments directly after an underscore, so just by putting an underscore before the comment on a blank line, you can keep the line continuing over it.

Code: Select all

Print   "text string #4"; Space(5); _
_'        "text string #5"; Space(5); _
        "next text string"
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Comment after line continuation

Post by fzabkar »

Thanks for the workarounds. I had already used MrSwiss's first suggestion, but the other two suggestions are more palatable because they preserve the flow of the code.

That said, I still see the compiler's counterintuitive behaviour as more of a bug.

BTW, I don't normally intersperse comments and line continuations. I only did this because I was debugging a particular line of code and wanted to give myself the option of two versions of the code.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Comment after line continuation

Post by MrSwiss »

fzabkar wrote:I only did this because I was debugging a particular line of code and wanted to give myself the option of two versions of the code.
Well, from my point of view for stated purpose, I'd use preprocessor instead of "commenting".
Post Reply