MC-Basic Libraries

From SoftMC-Wiki
Jump to: navigation, search

Some applications repeatedly use the same subroutines and functions. Such subroutines and functions can be gathered into a library file. The library file can be imported to another task with IMPORT on the .lib file at the beginning of the task. Then, each public subroutine and function defined in the imported library file can be called within the importing task.

An MC-BASIC library is an ASCII file containing only the subroutines’ and functions’ code. The file does not have a main program part. The name of a library file must have the extension, .LIB.

The format of a library file is:

Declaration of static variables
...Etc.
{PUBLIC}SUB <sub_name_1> etc…
{Declaration of variables local to the sub-program}
{sub-program code}
END SUB
…etc …
{PUBLIC} SUB <sub_name_n> etc…
{Declaration of variables local to the sub-program}
{sub-program code}
END SUB

Global Libraries

Global libraries are library files (.LIB), which, instead of being loaded from either the terminal or another task, are loaded from the configuration file (Config.Prg). Another option is loading from terminal, using LOADGLOBAL.

PUBLIC subroutines and functions defined in such libraries can be called from everywhere (i.e., terminal and other tasks), without first being imported by the calling task. IMPORT cannot be applied to global libraries.

LOAD GlobLib.lib ‘ In Config.Prg
LOADGLOBAL GlobLib.lib ‘ In terminal
IMPORT GlobLib.lib  Error ‘ In task or library

The syntax for calling a global library’s subroutine or function from either the terminal or task is the same as the syntax for calling a subroutine or function of a regular (imported) library. Because of the wide recognition scope of global, public functions and subroutines, the names of such subroutines and functions cannot be used for another function or subroutine, variable, motion element, etc. As in regular libraries, subroutines and functions declared without the PUBLIC keyword can only be used inside the scope of the library.

A global library cannot be unloaded if there are task files (.PRG) loaded in memory. To unload a global library, all .Prg files must first be unloaded. If a global library file is unloaded and then reloaded from either the terminal or a task, it becomes a regular library, which must be imported before use.

The {PUBLIC} keyword allows a subroutine to be called from within any softMC task. These subroutines are visible from outside the scope of the library. Sub-routines declared without the {PUBLIC} keyword can only be used inside the scope of the library. They are PRIVATE library functions. For example:

PUBLIC SUB public_sub(…etc…)
…etc…
END SUB
SUB private_sub(…etc…)
…etc…
END SUB

The subroutine “private_sub” can be used only inside the library. The "public_sub” can be used in any program file by importing the library into it. The scope of the library is the task that imports it. However, you can import the library into each of multiple tasks.

A library file is used like any other program file. You can send a library file to the Flash disk and you can retrieve it or delete it. In order to use a library file in a program, the library must first be loaded into memory.

LOAD MyLibrary.lib

To unload the library, enter:

UNLOAD MyLibrary.lib

If a program or library task references a subroutine in a library, the program or library task must be unloaded from memory before the library can be unloaded.

After a library is loaded in memory, it must then be imported into a program. IMPORT must be placed at the beginning of the program code before any other command or declaration.

IMPORT MyLibrary.lib

The syntax for calling a library subroutine or function from a task is the same as the syntax for calling a local subroutine from the task. You do not need to specify which library file contains the subroutine you want to call (because you can not import two library files that contain the same library function name).