Difference between inline and full version of BASIC constructs

Anything QL Software or Programming Related.
georgeo
Bent Pin Expansion Port
Posts: 92
Joined: Wed Aug 03, 2016 8:49 am

Difference between inline and full version of BASIC constructs

Postby georgeo » Mon Dec 11, 2017 3:01 pm

Hi everyone,

I have a naive question to ask about inline constructions in BASIC (apologies in advance, if this is obvious).

Based on the SuperBASIC online manual [http://superbasic-manual.readthedocs.io/] many of the SuperBASIC flow-control constructions have both an inline short hand and a full form. So for example, the entry for IF notes two forms:

Code: Select all

IF condition {THEN | :} statement *[:statement]* [:ELSE statement *[:statement]*]


or

Code: Select all

IF condition [{THEN | :}] *[:statement]* ... [ELSE] *[:statement]* ... END IF


However, I can't work out how the runtime works out which version is used, since END IF (and possibly ELSE) seems to be the only difference, and the interpreter could find an END IF related to a number of different IF statements, if you had nested conditions.

For example:

Code: Select all

100 INPUT a
110 IF a > 0 THEN PRINT "POSITIVE"
130 IF a > 20 THEN PRINT "REALLY POSITIVE!"
140 END IF
150 GOTO 100


In this case, which one is inline and which one is full-form?

Anyone shed any light on this? Thanks in advance,
Georgeo.


User avatar
Pr0f
Gold Card
Posts: 284
Joined: Thu Oct 12, 2017 9:54 am

Re: Difference between inline and full version of BASIC constructs

Postby Pr0f » Mon Dec 11, 2017 3:05 pm

From memory, I think END IF always matches the previous IF in the program, so your 2nd one would be seen as long form


georgeo
Bent Pin Expansion Port
Posts: 92
Joined: Wed Aug 03, 2016 8:49 am

Re: Difference between inline and full version of BASIC constructs

Postby georgeo » Mon Dec 11, 2017 3:09 pm

Thanks, though re-reading the description carefully, I wonder if the long form is actually only triggered if there is nothing (other than THEN or : ) after the condition, so I should write:

Code: Select all

100 INPUT a
110 IF a > 0 THEN
120 PRINT "POSITIVE"
130 END IF
140 IF a > 20 THEN PRINT "REALLY POSITIVE!"
150 GOTO 100


The first condition being long form and the second being inline form, in which case my original example actually contained an orphaned END IF, with no corresponding IF, which ideally would create a warning/ error.

The fact that:

Code: Select all

100 INPUT a
110 END IF
120 GOTO 100


runs suggests this is the case.


User avatar
tofro
QL Wafer Drive
Posts: 1378
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Difference between inline and full version of BASIC constructs

Postby tofro » Mon Dec 11, 2017 3:54 pm

Georgeo,

the long form is triggered as soon as nothing (except a single colon or a REMark) follows the condition (or the optional THEN) on the IF line:

Code: Select all

100 IF a = 0
100 IF A = 0 THEN
100 IF a = 0 : REMark test for condition
100 IF a = 0 :

all trigger a long form and expect an END IF somewhere down the lines

As soon as the line contains anything but a single colon or REM after condition and optional THEN, a short form is assumed, no END IF is expected (but allowed on the very same line)

Code: Select all

100 IF a = 0 THEN PRINT "Hello":END IF
100 IF a = 0 : PRINT "Hello"


And your first example actually triggers a "No ENDIF allowed here" error

In the short form, the END IF is completely optional and can be replaced by a line end.

These are the SBASIC rules - original QL's were slightly different, and depending on ROM versions, some errors were not properly caught or the interpreter was much more willing to digest malformed lines.

Tobias


georgeo
Bent Pin Expansion Port
Posts: 92
Joined: Wed Aug 03, 2016 8:49 am

Re: Difference between inline and full version of BASIC constructs

Postby georgeo » Mon Dec 11, 2017 4:38 pm

Hi Tobias,

Thanks. I'm working on a real QL, so that may explain why I don't see an error, even with a syntactically wrong version of the program.

Can I check if the same rules apply to SELECT statements--that is, does the following represent robust code that should work across various ROM versions:

Code: Select all

100 SELECT ON keyPress
110 ON keyPress = 82: REM "R"
120 PRINT "RAPIER"
130 IF PW(1) > 0 THEN LET weapon=1 : ELSE PRINT "NOT OWNED": END IF
140 ON keyPress = 83: REM "S"
150 PRINT "SHIELD"
160 IF PW(2) > 0 THEN LET weapon=2 : ELSE PRINT "NOT OWNED": END IF
170 ...
220 REMAINDER
230 PRINT "HAND"
240 LET weapon=0
250 END SELECT


Thanks again,
George.


stevepoole
Trump Card
Posts: 166
Joined: Mon Nov 24, 2014 2:03 pm

Re: Difference between inline and full version of BASIC constructs

Postby stevepoole » Tue Dec 12, 2017 12:04 am

Hi George,

It is possible to interweave many different SELect and/ or IF statements in both long and inline codes.
Inline code can be useful when you want to keep a lot of code on one page without needing lots of scrolling. But it can be horrible to debug later...
As always, the key to good coding is 'Keep it Simple'... That is, use inline code at first, then string it out into long form once debugged!

'ON' keywords are not necessary and can be confused with 'ON x GOTO 150,200' or 'ON y GOSUB 1000,2000' constructs. So,
500 SELect weapon
510 = gun: fire
520 = sword: stab
530 = REMAINDER : die : REMark the = sign is necessary for compatibility....
540 END SELect

Hope this helps,
Steve.


georgeo
Bent Pin Expansion Port
Posts: 92
Joined: Wed Aug 03, 2016 8:49 am

Re: Difference between inline and full version of BASIC constructs

Postby georgeo » Tue Dec 12, 2017 8:01 am

Hi Steve (everyone),

Thanks for your help. Hopefully these tips will help me debug the dodgy logic in Akalabeth both.

Speak to you soon,
George.


User avatar
tofro
QL Wafer Drive
Posts: 1378
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Difference between inline and full version of BASIC constructs

Postby tofro » Tue Dec 12, 2017 8:23 am

I'm always a bit confused when I see inline conditionals mixed with the long form, hard to see which is which.

I would rather recommend: Split it all out into long form statements, that is much more readable and shows you what it's meant to do at a single glance.

Tobias



Return to “Software & Programming”

Who is online

Users browsing this forum: No registered users and 1 guest