Include other .bbc files in the main file?

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
User avatar
zachnoland
Posts: 11
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Include other .bbc files in the main file?

Post by zachnoland »

I have a question. Can BBCSDL include other files inside the main file like MAIN.BBC and EXMATH.BBC, In EXMATH.BBC there are functions or procedures that can be called in MAIN.BBC.I don't know if this is possible on the original BBC Micro or not and i know this sounds like a modern programming language. But is this possible?
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Include other .bbc files in the main file?

Post by Richard Russell »

zachnoland wrote: Thu 27 Feb 2025, 01:30 I have a question. Can BBCSDL include other files inside the main file like MAIN.BBC and EXMATH.BBC, In EXMATH.BBC there are functions or procedures that can be called in MAIN.BBC.I don't know if this is possible on the original BBC Micro or not and i know this sounds like a modern programming language. But is this possible?
If I have understood your question correctly you can load such sub-modules using the INSTALL statement, in just the same way as you would the supplied libraries. The only difference is likely to be the file path from which the module is loaded:

Code: Select all

      INSTALL @lib$ + "aagfxlib" : REM Load supplied system library
      INSTALL @dir$ + "mymodule" : REM Load user-written sub-module
There's no difference, in essence, between a library and a sub-module, except that libraries have to follow more strict 'rules' because the circumstances in which they are going to be used are not known in advance. For example libraries should not normally create or access global variables whereas that may be acceptable with a sub-module.

There are a number of restrictions governing an INSTALLed sub-module or library which do not apply to regular programs:
  1. It must be an internal-format (tokenised) program file, not a plain-text file.
  2. You cannot use GOTO or GOSUB (lines may be numbered, but only for the purposes of debugging).
  3. You can only use the 'relative' form of RESTORE.
This facility was not available on the BBC Micro (or 8-bit versions of BBC BASIC generally). The INSTALL and LIBRARY keywords were introduced by Acorn in ARM BASIC V in 1986; BB4W, BBCSDL and BBCTTY have INSTALL but they don't have LIBRARY (the difference is that a module loaded by INSTALL remains in memory for the rest of the 'session' whereas one loaded by LIBRARY is discarded as soon as the calling program terminates).

As a rule, when you see references to libraries in the documentation they apply just the same to sub-modules.
User avatar
zachnoland
Posts: 11
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: Include other .bbc files in the main file?

Post by zachnoland »

Richard Russell wrote: Thu 27 Feb 2025, 03:15

Code: Select all

      INSTALL @lib$ + "aagfxlib" : REM Load supplied system library
      INSTALL @dir$ + "mymodule" : REM Load user-written sub-module
I'm trying to understand how to load external files with:

Code: Select all

INSTALL @dir$ + "MYMATH2"
And it produces an error:

Code: Select all

file or path not found in line 10
I seem to have a problem with the file path issue in BBCSDL Android. The location of the MYMATH1.BBC and MYMATH2.BBC files are adjacent, meaning they were not changed before being saved in the same place or default. Maybe I should show you the code I typed.

Code: Select all

   10 REM MYMATH1.BBC
   20 INSTALL @dir$ + "MYMATH2" : REM LOAD SUB-MODULE
   30 A% = 5
   40 B% = 5
   50 PRINT FNMYADD(A%, B%)
   60 END
   

Code: Select all

   REM MYMATH2.BBC
   DEF FNMYADD(NUM1%, NUM2%)
      RESULTS% = NUM1% + NUM2%
   =RESULTS%
    
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Include other .bbc files in the main file?

Post by Richard Russell »

zachnoland wrote: Thu 27 Feb 2025, 10:53 I seem to have a problem with the file path issue in BBCSDL Android. The location of the MYMATH1.BBC and MYMATH2.BBC files are adjacent, meaning they were not changed before being saved in the same place or default.
My guess is that the issue here is that your files have a ".BBC" extension (capitals) but BBC BASIC is looking for a file with a ".bbc" extension (lowercase). If you omit the extension from the INSTALL statement, which you seem to be doing:

Code: Select all

   20 INSTALL @dir$ + "MYMATH2" : REM LOAD SUB-MODULE
BBC BASIC will automatically append the extension ".bbc", but since the Android file system is case-sensitive (as most are, apart from Windows and MacOS) that won't match your file. Either change the extension of your file to lowercase, or specify it explicitly in the INSTALL statement:

Code: Select all

   20 INSTALL @dir$ + "MYMATH2.BBC" : REM LOAD SUB-MODULE
User avatar
zachnoland
Posts: 11
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: Include other .bbc files in the main file?

Post by zachnoland »

Richard Russell wrote: Thu 27 Feb 2025, 11:42 My guess is that the issue here is that your files have a ".BBC" extension (capitals) but BBC BASIC is looking for a file with a ".bbc" extension (lowercase). If you omit the extension from the INSTALL statement, which you seem to be doing:

Code: Select all

   20 INSTALL @dir$ + "MYMATH2" : REM LOAD SUB-MODULE
BBC BASIC will automatically append the extension ".bbc", but since the Android file system is case-sensitive (as most are, apart from Windows and MacOS) that won't match your file. Either change the extension of your file to lowercase, or specify it explicitly in the INSTALL statement:

Code: Select all

   20 INSTALL @dir$ + "MYMATH2.BBC" : REM LOAD SUB-MODULE
I have tried changing the external file MYMATH2.BBC to mymathlib.bbc but it still produces the same error. Likewise, the main file MYMATH1.BBC becomes headertest.bbc and still gives error.

The question:
Could this be a bug?
If yes can you fix it?
Or maybe you have a solution for this.
One way I thought about but requires complete notes is, Maybe another solution is to add the complete file path structure of android but this will be very long and confusing.

Code: Select all

20 INSTALL @dir$ + "/storage/emulate/0/etc" : REM I don't remember all that path directory into the BBCSDL app lol
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Include other .bbc files in the main file?

Post by Richard Russell »

zachnoland wrote: Thu 27 Feb 2025, 15:20 Could this be a bug?
A bug in BBC BASIC? No, many of the supplied example programs would not work if it was, and a bug of that severity would have been found years ago (there are no known bugs in BBC BASIC for SDL 2.0).

It will almost certainly be something that you are doing wrong. Use the debugging capabilities of BBC BASIC (entering commands in Immediate Mode is probably the most useful in this case) to find out what.

For confirmation, I've just tested the INSTALL statement on my Android phone and it works perfectly:

Screenshot_2025-02-27-15-47-40-58_2f330f9510023cd121b7f55db302c4d3.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
zachnoland
Posts: 11
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: Include other .bbc files in the main file?

Post by zachnoland »

Richard Russell wrote: Thu 27 Feb 2025, 15:56 It will almost certainly be something that you are doing wrong. Use the debugging capabilities of BBC BASIC (entering commands in Immediate Mode is probably the most useful in this case) to find out what.

For confirmation, I've just tested the INSTALL statement on my Android phone and it works perfectly:

Screenshot_2025-02-27-15-47-40-58_2f330f9510023cd121b7f55db302c4d3.jpg
I just realized something, the directory path is read depending on when opening the file directly or using interprenter immediate.bbc. This may be the reason why the code works if run directly. I just need to add double Dot like this.

Code: Select all

INSTALL @dir$ + "../mymathlib" : REM back to previous directory
this is the result i got:

Thank you the solution has been solved. :D
You do not have the required permissions to view the files attached to this post.
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Include other .bbc files in the main file?

Post by Richard Russell »

zachnoland wrote: Thu 27 Feb 2025, 16:22 I just realized something, the directory path is read depending on when opening the file directly or using immediate.bbc.
Yes, if you are entering immediate mode that way you will almost certainly find that @dir$ is pointing to where the immediate.bbc program itself is stored! It's usually better to enter immediate mode from the program that is in need of debugging, which is typically what will happen if an untrapped error occurs.

You could always put a copy of immediate.bbc in the same directory as the program you want to debug. 8-)
I just need to add double Dot like this.
Hmm. 'Resource' files (that is, files that programs need in addition to the program itself, such as sub-modules, graphics images, sound effects etc.) should be stored either in the same directory as the program, or in a sub-directory thereof. So they would be loaded something like this:

Code: Select all

      INSTALL @dir$ + "mymodule.bbc" : REM Same directory as program
      INSTALL @dir$ + "myresources/modules/mymodule.bbc" : REM Sub-directory
They should not be stored in a parent directory (@dir$ + "../parent...") because if you do that any attempt to build a standalone executable (or in the case of Android a self-contained APK) will fail.

So whilst it may be working for you at the moment, I would review how you are organising your project so that you would be able to build a standalone executable or application if you wanted to.