spotlessmind1975 / ugbasic

An isomorphic BASIC language compiler for retrocomputers
Apache License 2.0
85 stars 13 forks source link

Add support for keyword EXEC (SB+TSB only) #840

Closed spotlessmind1975 closed 1 month ago

spotlessmind1975 commented 1 month ago

Syntax:

EXEC <label>

In TSB, just like in Simons' Basic, subroutines can be given a name (see PROC). This makes them independent of their position in the program and makes it easier for the programmer to keep track of things (names are easier to assign to a purpose than line numbers). The EXEC command calls such a subroutine, processes it and returns to its starting point. It is therefore largely equivalent to the BASIC V2 command GOSUB. This is not the ugBASIC semantic.

So ugBASIC should support a pragma to define the semantic for EXEC keyword.

 OPTION EXEC AS GOTO: REM ugBASIC meaning (default)
 OPTION EXEC AS GOSUB: REM SB+TSB meaning

Unlike in Simons' Basic and TSB, the EXEC keyword canno be omitted. In addition, any other BASIC commands can follow a colon after the label in the line in which a procedure call is used. TSB-EXEC also ignores a space at the beginning of a label (the possible gap between PROC and the label name as in the example below). Procedure calls can therefore be handled much more flexibly than under Simons' Basic.

The processing speed of EXEC and CALL is much slower under Simons' Basic than with the comparable commands GOSUB and GOTO in Basic V2, because all characters of the label must be compared when searching for the jump destination. To compensate for this disadvantage, TSB creates a table of all PROC addresses (starting at address $C400) during program execution and first looks for PROCs in this table (see CHECK). The processing speed of a program with many procedures is thus significantly increased during operation and is faster than Basic V2.

If the label behind EXEC is not found in the program, the error message NO PROC ERROR appears. If a TSB program encounters an end of procedure (END PROC) without having called a procedure, the message END PROC W/O EXEC ERROR appears. If there are too many nestings (more than ten), TSB reports an OVERFLOW ERROR.

godot64 commented 1 month ago

Is there any way to deal with the fact that you can omit the EXEC keyword (so that just labelworks just like EXEC label)?

Arndt

spotlessmind1975 commented 1 month ago

Hi @godot64 , and thank you for your comment!

The current ugBASIC syntax allows you to call methods without using the EXEC keyword (TSB semantics) by using a pair of square brackets ([]).

Example:

PROCEDURE this : END PROC
this[]

I need to check whether the lexer/parser would run into trouble if the BASIC programmer omits the square brackets. It may be necessary to consider other constraints in the neighborhood, to locate the end of the statement.

spotlessmind1975 commented 1 month ago

First published on BETA UPDATE [rev. 20240730].