Hi all !
As I’m toying with my PicoCalc, I tried to remember my Basic from a long time ago.
I’ve read the PicoMite/MMBasic manual(s) and I don’t find any reference to a command to “include” other “.bas” files to my code.
Am I forced to make all my programs in a single .bas file ?
Thanks guys/gals !
An MMBasic program can use CHAIN. See the manual on page 108:
CHAIN fname$ [cmdline$] Allows a program to run another program
with the variable space preserved –
the command is recommended to be used in top level program and
not from within a subroutine. If the optional ‘cmdline$ parameter is
specified this is passed to the chained program in MM.CMDLINE$
Oh I see !
I’ve just tested that but I don’t think I’m getting the usage right.
Let’s say I have “file1.bas” and “file2.bas”.
- “file2” contains the “TEST” sub.
- “file1” chains “file2”
- Do I have to chain back to “file1” to be able to execute that sub from “file2” ?
I’ve tried with and without chaining back (and with the cmdline$ parameter because I didn’t want my chains to be infinite) and it doesn’t work.
I don’t think I understand correctly the use of CHAIN.
EDIT : If you could write just a tiny snippet, that would be really awesome haha
Try this:
prog1:
if a<>1 then ' Make sure we only run this the first time
a=4
chain "prog2.bas"
endif
print "We're back!"
prog2
print "Here's program 2"
print"a= ";a
a=1
chain "prog1.bas"
Probably worth linking to the continued discussion on TheBackShed, which indicates it’s a bug…
(I’m guessing an RC26 will appear for the official PicoMite code in time.)
CHAIN
and #INCLUDE
are different species of fish:
CHAIN
- runs one program from another but preserves the variable space.
#INCLUDE
- verbatim includes one file within another.
The PicoMite family of MMBasic’s doesn’t provide #INCLUDE
(see exception below) but it is available on the Colour Maximite 2, MMBasic for Windows and MMBasic for Linux.
The exception is that with a Pico 2 you can use #INCLUDE
(and other Colour Maximite 2 pre-processor features) if you run the program with CMM2 RUN <progname>
. However if you then EDIT
or LIST
the contents of the flash you will be editing/listing the flattened program (i.e. all the #INCLUDE
s are inline).
I believe the external MMEdit program may provide its own pre-processor supporting #INCLUDE
though it is not something I have personally used.
For completeness I will also mention my own sptrans pre-processor. In theory that may work (slowly) on the PicoMite (and PicoCalc) but I personally only run it on MMBasic for Linux and then transfer the pre-processed file to the desired platform.
Best wishes,
Tom
Old BASICs had a “merge” command, that would let you bring in another source file, but that wasn’t available at run time.
Thanks everyone for your answers.
I have an array of things to try now !
Even though Haddock and Tilapia are different species of fish, you can substitute one for the other, when necessary!
CHAIN can be used to replace INCLUDE to give the same effect while not working the same way.
This isn’t exactly true.
INCLUDE is usually used for sets of common routines that you want in many programs. I know that I have several already that I wish I could just INCLUDE in them instead of manually copy/pasting them into each program.
CHAIN always replaced the program while keeping all the variables intact. It was usually used to make a program that was larger than the available memory. So you’d still need to manually copy/paste those common routines into each program CHAINed.
But this is all moot since MMBASIC doesn’t have a CHAIN command really. It has a FLASH CHAIN, but that’s only uses programs in flash memory, not the SD card. So same problems, and less useful.
There is an EXECUTE command, but it’s very limited and it’s unclear if you can pass parameters to it. Probably something worth while exploring, though. I get the feeling that it’s only partially baked, though.
It looks like the closest thing to an INCLUDE is the LIBRARY command.
“… you can use the LIBRARY SAVE command. This will transfer the routines (as many as you like) to a non-visible part of flash memory where they will be available to any BASIC program but will not show when the LIST command is used…”
You can only have 1 LIBRARY in memory at any given time, but it looks like you can load the LIBRARY from disk at runtime. This is probably as close to an INCLUDE as we can get.
Since I’m aiming to do game programming, I think I’ll toy without chaining or library, waiting for the pkklib (C Library) haha
1 Like
From the draft manual for 6.00.02
CHAIN fname$ [cmdline$]
Allows a program to run another program with the variable space preserved – the command is recommended to be used in top level program and not from within a subroutine. If the optional ‘cmdline$ parameter is specified this is passed to the chained program in MM.CMDLINE$
I’ve tried that, but are the functions/subroutines declarations kept between chained program ?
It doesn’t look like it. Only the variable space is preserved, which is consistent with how the old BASICs worked.
Like I said, CHAIN was historically only used for making programs that were larger than the available memory. BASIC’s version of overlays.
IHMO: I don’t see much value with CHAIN in MMBASIC, which can use far more memory than the old BASICs.
As I understand it, common subroutines and functions could be put in the library and would then be available to all the chained programs
2 Likes
MMBasic 06.00.02RC26 and above does have a true CHAIN command. You can use it just like you’d use INCLUDE by having one set of code in common, setting a variable to let the common routine know where it was called from and setting a flag to let the calling program know enough not to CHAIN to the same program again and then CHAINing back to the calling program. It sounds more complicated than it is to code but the effect is exactly the same as INCLUDE.