Super BASIC coding style for Minerva

Anything QL Software or Programming Related.
tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Super BASIC coding style for Minerva

Post by tcat »

Hi,

I have just started exploring `Minerva' system. I was surprised that some pieces of my Super BASIC code need a bit of altering to run.

Program gets sometimes exited from procedures, rather then returning to the caller.

This example will never print "done", when running from the `main' procedure.
The remedy seems to place explicit return statement on line 120.

Code: Select all

100 DEFine PROCedure try
110  IF 1 THEN
120   PRINT "was here 1"
130  ELSE IF 1 THEN
140   PRINT "was here 2"
150  ELSE
160   PRINT "was not here"
170  END IF
180 END DEFine try
190 :
200 DEFine PROCedure main
210  try
220  PRINT "done"
230 END DEFine main
240 :
250 REMark END
What are the general tips for Minerva coding style?

Many thanks
Tomas


User avatar
janbredenbeek
Super Gold Card
Posts: 629
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: Super BASIC coding style for Minerva

Post by janbredenbeek »

tcat wrote:Hi,

I have just started exploring `Minerva' system. I was surprised that some pieces of my Super BASIC code need a bit of altering to run.

Program gets sometimes exited from procedures, rather then returning to the caller.

This example will never print "done", when running from the `main' procedure.
The remedy seems to place explicit return statement on line 120.

Code: Select all

100 DEFine PROCedure try
110  IF 1 THEN
120   PRINT "was here 1"
130  ELSE IF 1 THEN
140   PRINT "was here 2"
150  ELSE
160   PRINT "was not here"
170  END IF
180 END DEFine try
190 :
200 DEFine PROCedure main
210  try
220  PRINT "done"
230 END DEFine main
240 :
250 REMark END
Your IF clauses are not matched by END IFs. The correct syntax is:

Code: Select all

IF condition THEN statement1 [:ELSE statement2]
or

Code: Select all

IF condition THEN
  statement1
ELSE
  statement2
END IF
In other words: if the statement after THEN is on the same line then it's an inline clause and you don't need END IF, if it's not on the same line you need to end your IF clause with an END IF.

Jan.


tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Super BASIC coding style for Minerva

Post by tcat »

Hi Jan,

Ah, unbalanced `END-IF(s)'.
Minerva is less forgiving than other ROMs where the `1st END-IF' seems to terminate them all.

Perhaps I am being silly, but is there a better way to format the code without need of deep indents in nesting?
(In cases where SELect ON's cannot be used)

Code: Select all

100 DEFine PROCedure try_if_else
110  IF 1 THEN
120   PRINT 1
130  ELSE IF 2 THEN
140   PRINT 2
150  ELSE IF 3 THEN
160   PRINT 3
170  ELSE IF 4 THEN
180   PRINT 4
190  ELSE IF 5 THEN
200   PRINT 5
210  ELSE IF 6 THEN
220   PRINT 6
230  ELSE IF 7 THEN
240   PRINT 7
250  ELSE IF 7 THEN
260   PRINT 8
270  ELSE IF 8 THEN
280   PRINT 8
290  ELSE IF 9 THEN
300   PRINT 9
310  ELSE
320   PRINT 10
330  END IF : REMark END IF:END IF:END IF:END IF:END IF:END IF:END IF:END IF
340 END DEFine try_if_else
Tomas
Last edited by tcat on Sun Feb 26, 2017 7:18 pm, edited 1 time in total.


RWAP
RWAP Master
Posts: 2834
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Super BASIC coding style for Minerva

Post by RWAP »

Of course, if it was such a simple example, SELECT on would be much easier to write for the multiple IF statements...

Turbo would also probably report issues with the IF statements if you tried to compile it without all of the matching END IF statements...


User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: Super BASIC coding style for Minerva

Post by dilwyn »

tcat wrote:Hi Jan,
Ah, unbalanced `END-IF(s)'.
Minerva is less forgiving than other ROMs where the `1st END-IF' seems to terminate them all.
Perhaps I am being silly, but is there a better way to format the code without need of deep indents in nesting?
(In cases where SELect ON's cannot be used)
"Less Forgiving" is probably the wrong way of describing this, the older ROMs were a nightmare for not being rigid enough with these, sometime meaning a program would only run on one ROM system. This is one perfect example of why so many older programs seem to fail on modern systems, the older ROMs let you get away with murder.

Deep indents in nesting might look ungainly, but at least you can see where the nesting goes wrong. Each stage here has a one space indent and by the time you get to line 338 at least you can see where the nesting goes wrong. Admittedly, the big indents and nesting look ungainly and horrible, but at least you can see what's going on.

A while back I was trying to convert a program written in an old M$ BASIC with multiple nested single line IF THEN ELSE. Absolute nightmare to debug - I ended up doing just this in order to be able to follow what was (supposed to be) going on. As RWAP says, that particular example could have been converted into a SELect structure, but often there is no easier way where the structure and nesting is so complex.

Code: Select all

100 DEFine PROCedure try_if_else
110  IF 1 THEN
120   PRINT 1
130  ELSE
135   IF 2 THEN
140    PRINT 2
150   ELSE 
155    IF 3 THEN
160     PRINT 3
170    ELSE 
175     IF 4 THEN
180      PRINT 4
190     ELSE 
195      IF 5 THEN
200       PRINT 5
210      ELSE 
215       IF 6 THEN
220        PRINT 6
230       ELSE 
235        IF 7 THEN
240         PRINT 7
250        ELSE 
255         IF 7 THEN
260          PRINT 8
270         ELSE 
275          IF 8 THEN
280           PRINT 8
290          ELSE 
295           IF 9 THEN
300            PRINT 9
310           ELSE
320            PRINT 10
330           END IF
331          END IF
332         END IF
333        END IF
334       END IF
335      END IF
336     END IF
337    END IF
338   END IF
340 END DEFine try_if_else


swensont
Forum Moderator
Posts: 252
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: Super BASIC coding style for Minerva

Post by swensont »

There is old joke about the headline "man eating tiger". It is a man eating a tiger, or a tiger that eats a man.

Despite what code will run, it is best to wrote code that is clear about that is intended. Adding the right "END IF's" can be critical and should be done for clarity. Intending is also a way of showing intent by having lines at the same level have the same indent.

Tim


tcat
Super Gold Card
Posts: 633
Joined: Fri Jan 18, 2013 5:27 pm
Location: Prague, Czech Republic

Re: Super BASIC coding style for Minerva

Post by tcat »

Hi,

As it seems, current Turbo version only gives warnings about obvious `IF-ELSE' termination.
EDIT that I am glad for.
`IF-ELSE' obvious termination
`IF-ELSE' obvious termination
if-else_turbo_report.png (8.96 KiB) Viewed 3638 times
Tomas


User avatar
ql_freak
Gold Card
Posts: 353
Joined: Sun Jan 18, 2015 1:29 am

Re: Super BASIC coding style for Minerva

Post by ql_freak »

The problem with SuperBASIC is, that it doesn't have an "ELSE IF" (compound) statement, like other languages (e. g. C), but just an ELSE. So you normally should write it so:

Code: Select all

IF a$="first"
  PRINT 1
ELSE
  IF a$="second"
    PRINT 2
  ELSE
    PRINT -1
  END IF
END IF

REMark Above is explicit form, you may abbreviate it to:

IF a$="first":REMark 1st IF
  PRINT 1
ELSE IF a$="second":REMark 2nd IF
  PRINT 2
ELSE
  PRINT -1
END IF: END IF:REMark END IFs must match the no of IFs above
This is indeed a shortage of SuperBASIC, I don't know if this (ELSE IF compound statement) was added for SBASIC.

Well, if one isn't afraid of using GOTO (there is a good reason why even modern languages like C++, Java C# have it!), one could use:

Code: Select all

100 IF a$="first"
110   PRINT 1:GO TO 1000:END IF
120 IF a$="second"
130  PRINT 2:GO TO 1000:END IF
140 REMark Add other clauses before line 999
999 PRINT -1
1000
Such a use of GO TO (all go to the same line number) is IMO acceptable, because of the shortage of SuperBASIC. In C (Java, C#) you need a GOTO, if you want jump out of a nested loop to an outer loop, which isn't required (NOT ACCEPTABLE) in SuperBASIC, as all loops have a name and you can use "EXIT loopname".

On Minerva (and afaik with Turbo) there is often a better solution (afaik): You can use SELect ON with integer and string variables, unfortunately this is not possible with SBASIC.


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
User avatar
XorA
Site Admin
Posts: 1358
Joined: Thu Jun 02, 2011 11:31 am
Location: Shotts, North Lanarkshire, Scotland, UK

Re: Super BASIC coding style for Minerva

Post by XorA »

Code: Select all

Such a use of GO TO (all go to the same line number) is IMO acceptable, because of the shortage of SuperBASIC. In C (Java, C#) you need a GOTO, if you want jump out of a nested loop to an outer loop, which isn't required (NOT ACCEPTABLE) in SuperBASIC, as all loops have a name and you can use "EXIT loopname".
You don't in C you know ;-)

https://www.tutorialspoint.com/cprogram ... tement.htm


Post Reply