-
Notifications
You must be signed in to change notification settings - Fork 2
Bite Language Design
This section deals with more on the documenting the internals of the language and its current behavior.
If you are looking for how to code using Bite, see Syntax
A module houses functions, variables, classes and code. The last part, code is important, as Bite executes code inside a module as it is imported.
When you compile a Bite program, you pass in the list of modules that are part of the program. Each module may import one or more of the other modules included in the program.
You can pass the modules in any order to Compile
, but for the program to compile correctly, the modules must be processed in the correct order. To do this a dependency tree is generated based on the imports of each module.
For example, a program might have 2 modules A and B, where A imports B. When you compile the modules using the BiteCompiler
class, you would pass the modules code as an argument into Compile. The order you pass them in does not matter.
graph TD;
A-->B;
B-->C;
Prior to the compilation phase, the module code is parsed to identify module names and imports. The dependency graph is built using this information. In this case, A imports B, so B is a dependency of A. The order of compilation will be the leaf nodes first. In this case it will be { B, A }
This also means that any code in module B will be executed first before module A.
In this way, you can use the child module to setup code, data or classes that the parent module can use.
Example:
Suppose you had two modules and each one prints a message.
module ModuleA;
import ModuleB;
using ModuleB;
PrintLine("Starting Module A...");
module ModuleB;
PrintLine("Starting Module B...");
Once compiled and run, you would get the following output
Starting Module B...
Starting Module A...
Module A can be called a "root" module as no other modules depend on it, and it will always run last.
If a module imports more than one child module, the order they will be executed will depend on the order they are imported.
module ModuleA;
import System;
using System;
import ModuleC;
using ModuleC;
import ModuleB;
using ModuleB;
PrintLine("Starting Module A...");
module ModuleB;
import System;
using System;
PrintLine("Starting Module B...");
module ModuleC;
import System;
using System;
PrintLine("Starting Module B...");
Starting Module C...
Starting Module B...
Starting Module A...