Sage-Code Laboratory
index<--

Bee Code Structure

Bee has a modular architecture. A source file represents a module. A project can have many modules and can contain other files like configuration files, images and data files. Let's focus first on Bee modules. Each module has extension *.bee and has a distinct role depending on its declaration and location.

Bookmarks:

Next you can learn general concepts about Bee applications:

Projects

A Bee project is a folder with a specific structure. A project contains one or more applications that can run independent of each other on same computer or a group of computers. For example applications can be designed to collaborate with each other into n-tire architecture or can be a group of OS commands for immage manipulation.

project tree

Next project tree contains two applications: client/server and show folders where you should put your code (src+lib) and documentation (doc). This is a recommandation but not a hard rule. You can organize your project better if you are experienced developer.

$pro_home
  |-- bin
  |   |-- client.exe
  |   |-- server.exe
  |
  |-- src
  |   |-- aspect1.bee
  |   |-- aspect2.bee
  |
  |-- lib
  |   |-- library1.bee
  |   |-- library2.bee
  |
  |-- doc
  |   |-- readme.md
  |   |-- index.html
  |
  |-- client.bee
  |-- server.bee

Global Constants

Global constants are using "$" prefix. There are several predefined system constants available in Bee. These constants can be used to locate project files or connect to databases. You can define new global constants at the beginning of main module or in configuration files.

Constant Environment Description
$bee_home BEE_HOME Bee home folder
$bee_lib BEE_LIB Bee library home
$bee_path BEE_PATH Bee library path
$pro_home N/A Program home folder
$pro_lib N/A Program library home
$pro_mod N/A Program modules home
$pro_log N/A Reporting folder

Compiler directives

Compiler directives are system constants that control the compilation process. You can setup these options in compiler configuration file or in source file. You can not change these options after compilation. They are available for normal control flow statements.

System environment variables are using the same prefix "$". So is easy to remember but it can overvrite our notation. So there is a aconflict. To avoid a conflict, the developers must set-up these variables explicit in configuration file.

Constant Default Description
$precision 0.00001 Control numeric precision
$recursion 10000 Control how deep a recursion before give up
$iteration 0 Control how many iterations before give up
$timeout 10 Control time in seconds before a loop give up
$debug "Off" Control if debug information is included
$echo "Off" Control if statement is printed to console in case of error
$trace "Off" Control if @trace variable is getting populated with information
$dformat "DMY" / "MDY" Control date format: DD/MM/YYYY or MM/DD/YYYY
$tformat "T24" / "T12" Control time format: HH:MM:SS,MS am/pm or HH:MM:SS,MS
$platform "Windows" Alternative: "Linux", "Mac" is the target platform

Notes:

Global Variables

Global variables are defined usually at the beginning of a module outside of any rule.

introspection:

Following system variables are available for debugging:

@timer duration information about last statement
@stack debug information about current call stack
@trace reporting information about statements
@level how deep is the current call stack
@count query count: updated/inserted/deleted records
@query last query statement
@error last error
@threads number of active threads

Notes:

Modules

As we mentioned already Bee is modular. It means one large project can be split into parts. Each part can contain reusable rules. A module can load several other modules. An application need an entry point that orchestrate the program execution. This is called the "main" module. Below we explain each kind of module:

Main Modules

Main module contains declarations for the main rule. One project can have one or more main modules. Each main module represent one single application. Main module is located in the project root folder.

Notes:

Secondary Modules

A good designer will split a large problem into secondary modules. These modules are similar to the main module except they do not have the main rule. One secondary module can be used in one or more relate applications. Secondary modules are located in src folder. These modules can be loaded into other modules to be re-used.

Notes:

Library Modules

A library module is a file located in "lib" folder having extension *.bee. It is called simply: module. A library module can load other library modules and can execute its rules multiple times. You can install new libraries by downloding from the internet a package. Lib folder can have multiple sub-folders, where you create or modify a set of related libraries to be distributed for other people using a package creator tool.

Notes:

Optional: For small projects not have to declare secondary modules. Most Bee demo programs will not have multiple modules, however most Bee programs are using external libraries that belong to Bee, called "system modules".

Declarations

Bee is using 6 kind of declarations:

load import a module in global scope
alias declare alternative name for module
type declare data types
make declare variable
stow declare a constant
rule declare named code block

Statements

Each statement start with one imperative keyword:

Examples:

alter modify value or assign new value to variable
read accept input from console into a variable
write register in console cash a string
print output to console with end of new line
apply execute one rule in synchronous mode
begin start execution of a rule in asynchronous mode

Notes:

Code blocks

Statements can be contained in blocks of code.

do start a block statement
with qualifier suppression block
if single or multi-path decision block
given start an iteration cycle
match multi-path value selector
trial exception handler block
done end a block statement
cycle end a repetitive block

Notes:

Main rule

A module can define "rules". These are sub-programs that can be executed on demand. One special rule is the main rule that can be defined only in the main module. This rule can receive multiple parameters and is automatically executed when a program starts.

Example:

# main rule
rule main(*params ∈ S):
   ** read the number of parameters
   make c := params.count;
   panic if (c = 0);
   
   ** print comma separated parameters
   make i:= 0 ∈ Z;
   if (i < c) do
     write params[i];
     alter i += 1;
     write "," if (i < c);
   cycle;
   ** print the buffer to console
   print;  
return;

Do not try to understand this example. It is just a worm-up code!

Notes:

External code

Library modules that are reusable for multiple projects can be imported in Bee "lib" sub-folder. This folder is available in Bee as a system constant called: $bee_lib. You can install a set of libraries in a sub-folder of "$bee_lib". Then you can reuse external library by using "load" keyword. The folders can be concatenated using "." string operator.

Pattern:

# loading modules
load $bee_lib.folder_name.(*);     //load all modules from folder
load $bee_lib.folder_name.(x,y,z); //load modules x.bee, y.bee and z.bee

Notes:

Qualifier Bee can use "dot notation" to locate external members. This technique is used to avoid name colision if one library has names that colide with other library.

Pattern:

# load a single module and make a qualifier
load  qualifier:$bee_lib.folder_name.module_name; // load a single module
...
apply qualifier.member_name; // using dot notation with qualifier
...

Notes:

1. A module can be loaded using a qualifier multiple times. However I do not see a useful use-case for this feature yet. I think is unusual to have same module loaded twice. It will be a waste of resources to do this.

2. All public members must use the specified qualifier or you can use "with" block to suppress the qualifier for a region of code. Using "with" is useful but sometimes not good enough so we have also invented the "alias".

Examples:

#load examples
load cpp:$runtime.cpp_lib.(*); // load cpp library
load asm:$runtime.asm_lib.(*); // load asm library
load bee:$runtime.bee_lib.(*); // load bee core library
load pro:$program.pro_lib.(*); // load project library

Global scope

One application has a global scope where variables and constants are allocated. Each secondary module can contribute with global variables and public elements that can be merged in this single scope. Global scope can be also called application scope;

Name space

A module has its own scope, that is called name-space where you can define members and statements. Module scope can contain public or private members. Public members start with "." while private members do not have any prefix or suffix.

#define a module
module demo_module:

stow .PI := 3.14;   // public constant
make .v   ∈ N; // public variable
make str := "test"; // private variable

** expression rule foo is private
rule foo(x ∈ N) ∈ N => (x + 1);

** block rule bar is public
rule .bar(x, y ∈ N) => (r ∈ N):
  alter r := x + y;
return;

Loading:

The main module can load numerous secondary modules or libraries. After loading, all public elements of a library can be accessed on demand using dot notation. You can not have colisions of names, except if you use "with" blocks. To simplify the code you can use "alias" statement and can rename a rule belonging to loaded modules.

Aliases:

You can create an alias for a specific member to eliminate the qualifier. This rule can be used to "merge" public members into current scope. A member can have one single alias in a module. If you do it multiple times, only the last alias is used. It is a bad practice to change the alias of a member.

Pattern:

alias new_name: qualifier.member_name;

Example:

This example demonstrate how to use a rule from a module named "module_name"

#define program name
** loading a module with qualifier
load demo:$pro.src.demo_module;

** give alias to module rule
alias sum: demo.bar;

** define main rule
rule main():
    ** call rule using qualifier
    make test := demo.bar(1,1); // 2
    
    ** call rule using alias
    make result := sum(1,1); // 2
    
    ** call rule using "with" block
    with demo do
         print foo(2);   // 3
         print bar(2,1); // 3        
    done;
return;   

Hiding: To hide a public member instead of making an alias you can use keyword "hide". You can hide any public members from a loaded module. If you use a regular expression you can hide several public members that use a specific pattern.

hide qualifier.member_name;
hide qualifier.(reg_exp_pattern);

Execution

You can execute the public methods of a secondary module in two modes: synchronous mode and asynchronous mode. This is how you can split a large applications into smaller, more manageable parts that can be executed in parallel.

Example:

In next module called: test_aspect.bee we create a rule that delay execution for several seconds using "wait" then it returns a result "r" equal to the argument value "t". So the rule basicly does nothing but wait.

# secondary module: test_module.bee
rule .test(t ∈ Z) => (r ∈ N):
  alter r := t; //prepare the result
  wait t;       //wait for t seconds
return;

1.Synchron call using: apply

Let's use the module previously defined in synchron mode.

# main module
load $pro_src.test_module.(*);

/* execute test() and append result 
   at the end of "collect" list */
rule main:
   ** define a collector (list)
   make collect ∈ (N);
   
   apply collect <+ test(30);  
   apply collect <+ test(40); 
   apply collect <+ test(10); 
   apply collect <+ test(20);  
   
   ** the collector is unordered
   print collect; // (30,40,10,20)
return;

2.Asynchron call using: begin

Let's use the module previously defined in asynchron mode. For this we use keywords "begin" to start a process and "rest" to wait for the processes to synchronize.

# main module
load $pro_src.test_aspect.(*);

/* execute test() and append result 
   at the end of "collect" list */
rule main():
   ** define a collector (list)
   make collect ∈ (N);

   begin collect <+ test(30); //open one thread 
   begin collect <+ test(40); //open one thread
   begin collect <+ test(10); //open one thread
   begin collect <+ test(20); //open one thread
   
   rest; //stop and wait for the all open threads to finish
   
   ** the collector is ordered 
   print collect; // (10,20,30,40)
return;

Note: By using begin and rest you can create multi-session applications. Each aspect is executed on a different core, and the application runs them in parallel. The main thread is waithing using resolve keyword for the threads to finish.


Read next: Data Types