Page 1 of 1

End if

PostPosted: Fri Apr 12, 2013 4:22 pm
by Darkstar
Is there a way so you can write:

IF a = b THEN PRINT "A and B are equal";

Instead of:
IF a = b THEN PRINT "A and B are equal";: END IF

To keep compatibillty with the original ZX BASIC?

Then you will not have to change all of those IF/THEN lines in old sorce code, and also perhaps some kind of swithc or a #pragma directive so
you will not have to rewrite draw and plot commands while also keeping the new way of using those commands and then without the directive
of course.

The original BASIC version would also not allow to PLOT or DRAW on the bottom two lines.
Example:
PLOT x+100,y-100 ' original BASIC version
PLOT x+100,96-y ' zxb version

This would greatly simplify things and keep bugs to the minimum.

READ/RESTORE/DATA is not that important compared to the other stuff.

Re: End if

PostPosted: Fri Apr 12, 2013 4:34 pm
by Darkstar
Like

#Pragma Compat

that works with the --sinclair option or any "optional" machine for that matter.

Then READ/RESTORE/DATA would only be allowed in that mode.

Kind of like -lang qb in FreeBasic.

Re: End if

PostPosted: Fri Apr 12, 2013 6:55 pm
by boriel
This topic has been debated before. Unfortunately, no.
Doing that is very ambiguous. Eg.

  Code:
IF A = B THEN PRINT "1st"
PRINT A
PRINT B
IF B = C THEN PRINT "2nd"
PRINT C
END IF

Which IF is closed by END IF? The 1st or the 2nd one?
There is a rule (when this is hardly implemented) which states it must be the 2nd if.

I've been seriously thinking in this, and the only solution I've come up with is that implementing "Single line" IFs will be exclusive (that is, when enable, END IF will be disabled).

Re: End if

PostPosted: Fri Apr 12, 2013 8:46 pm
by Darkstar
boriel wrote:I've been seriously thinking in this, and the only solution I've come up with is that implementing "Single line" IFs will be exclusive (that is, when enable, END IF will be disabled).


Yes, I agree. All things considered then this is the ONLY viable solution in my view.

Also, it might also be sensible to enable Single Line IFs in Compat only mode along with the old versions of PLOT/DRAW and the READ/RESTORE/DATA
commands.

Or do seperate directives like: #Pragma CompatPrimitives and #Pragma CompatSingleIFs and #Pragma CompatDATALists.
Then the user can control what to rewrite and whatnot, and the CompatPrimitives (PLOT etc) is then coupled with the --sinclair option.
Then you can turn it all on at once by using #Pragma CompatOriginalBASIC and that makes four #Pragma(s) in total. No need to specify
"CompatOriginalZXBASIC" because that is specified implicitly with the --sinclair option.

Of course, in SingleIFs mode with END IF disabled then the current format is disabled but that is OK because I suspect that the orginal BASIC dialog
in the other machines do not support it.

Re: End if

PostPosted: Wed May 14, 2014 9:18 pm
by programandala.net
Darkstar wrote:
boriel wrote:I've been seriously thinking in this, and the only solution I've come up with is that implementing "Single line" IFs will be exclusive (that is, when enable, END IF will be disabled).


Yes, I agree. All things considered then this is the ONLY viable solution in my view.


I think there's another solution: SAM Coupé's SAM BASIC and MasterBASIC, written by Andy Wright. already solved the same problem: An IF without a THEN or with no command after its THEN would be multiline and would need an END IF. In ZX BASIC, this behaviour would be optional, as you said.

  Code:
#pragma sinclair_basic_compat ' ...or whatever name

IF a=1 THEN PRINT "This is old code pasted from Sinclair BASIC"

if b=2
  print "This is modern code"
end if

if b=3 then
  print "this is modern code too"
end if

IF PI THEN BEEP 1,2:PRINT "MORE OLD CODE"



What do you think? Would it be feasible?

Re: End if

PostPosted: Thu May 15, 2014 9:32 am
by boriel
Yes. ;-)
This is exactly what I was thinking! and believe it could be done. :)
An IF followed by a THEN in the same line will be a single line IF, so all old BASIC listings will be more compatible.

What do the others think?

Re: End if

PostPosted: Thu May 15, 2014 11:56 am
by programandala.net
boriel wrote:An IF followed by a THEN in the same line will be a single line IF


An IF followed by a THEN and at least one command, all in the same (logical) line. Otherwise compatibility with current code would be broken, and both kinds of IF could not be mixed unless you switch on and off the option flag.

  Code:
' Example of old and new IF structures in the same source,
' all of them with THEN.
#pragma short_if=TRUE ' ...or whatever name
IF PI THEN PRINT "PURE SINCLAIR BASIC IF":BORDER 1:PAPER 0:CLS
IF PI THEN
  PRINT "MULTILINE IF":BEEP 3,3
END IF
if pi _
    then _
    print "This is a single-line IF from old sinclair code...":_
    border 2: _
    print "Sure!"
if pi then
    print "This is modern multiline IF"
    border 1
    print "Sure!"
end if

Re: End if

PostPosted: Wed Nov 22, 2017 11:24 pm
by boriel
Well, after *SEVEN* years :oops: (sorry), what I though impossible became possible :!:

I finally achieved it. ZX Basic now allows omitting the trailing END IF for single line IF THEN sentences (like Sinclair BASIC). Moreover, it allows also ELSE in the same line. This can lead to really strange (but valid) code:
  Code:
10 IF a < b THEN LET a = a + 1: IF a < c THEN a = a + c: ELSE a = a - 1: LET c = c - 1: ELSE b = b + c

is now *VALID* and it's equivalent to:
  Code:
IF a < b THEN
   LET a = a + 1
   IF a < c THEN
       a = a + c
   ELSE
       a = a - 1
       LET c = c - 1
   END IF
ELSE
   b = b + c
END IF

The 2nd way, despite being longer, can be more readable. It's up to you

The END IF for a single-line if is now optional (so you current code is still compatible):
  Code:
10 REM lines 20 and 30 are equivalent
20 IF a < b THEN a = a + 1
30 IF a < b THEN a = a + 1: END IF

Re: End if

PostPosted: Mon Dec 11, 2017 2:19 pm
by britlion
Oh wow.

What are we going to talk about in the support and bug report forums now though?

Re: End if

PostPosted: Mon Dec 11, 2017 7:47 pm
by boriel
:mrgreen: :mrgreen: Haha

Well, this will be in 1.8 (about to be released).
The optimizer -O3 now has even more optimizations (really tested) and the compiler is a bit faster.
Also the plugin for VSCode is under development to enable syntax checking (syntax highlight already done). This plugin is not mine (I just made a small contribution).