Till Module is Till program that has header and functions sections. If a module is the running module, it will be called the “main module”, or simply “the module” or “the program”.
A module can use other modules. The module will be called “the calling module”, and the other modules will be called “the called modules”. When a module calls another module, all of the called module variables and functions can be used by it. Duplicate object keys are forbidden. All of this modules connectivity results in a modules tree that is resolved by Till system.
Calling a module is done by appending the module name, relative path or full path to system call “#mod”. The extension “.till” is not mandatory in the name or path, and can be omitted.
Here is an example of a program and three modules. To test the example, put each of the following codes in a separate file (file names are in the first row of each code), and run the program (main module):
! MAIN MODULE (app.till)
#mod mod1
#mod mod2
print mod1_var
print mod2_var
print mod3_var
print mod1_func 7 / 3
print mod3_func 5 / 3
! MODULE 1 (mod1.till)
#mod mod3
mod1_var 1
mod1_func v: mod3_func v - 1
! MODULE 2 (mod2.till)
mod2_var 2
! MODULE 3 (mod3.till)
mod3_var 3
mod3_func v: mod3_var * v
The result of running “app.till”:
1
2
3
4
5
There are two main reasons for working with modules: modularity and reusability. If there are sections of code that represent cohesive coding spheres, than it makes sense to put these codes in separate programs and relate to them from the running program. Moreover, if there are objects that are used in more than one program it is useful to assemble these objects into a module that will be used by programs.
Global Modules
If objects are to be called anywhere then they should be called regardless of their location. These globally accessed modules are called “global modules” and are created by using the Till Launcher to convert a module to global module. Thence, that global module will be accessed by its name only.
In case it is needed to manage Till global modules, Till provides the Till Global Modules Manager, which is a Till executable program “gmod.exe”, also referred as gmod. With gmod you can add modules to Till global modules repository, access the modules, edit or remove a module, and even clear the repository.
Say we have program “util.till” that is to be used globally:
! global module: util.till
N newline
PI math.pi
true \t
false \f
max x y: math.max x y
sum list:
_list []
_list list
_result 0
_list item ~ _result ++ item
_result
average list:
_list []
_list list
listsum sum _list
listlen len _list
listlen ? %% listsum / listlen
0
round v:
roundfloor math.floor v
roundmedium roundfloor + 0.5
roundceil roundfloor + 1
v < roundmedium ? roundfloor \ roundceil
rand randmin randmax:
r random
round randmin + (randmax - randmin) * r
shortdec v decposition:
string.contains v 'E' ? %% v
not string.contains v '.' ? %% v
ind string.index v '.'
lenv len v
maxposition ind + decposition
lenv <= maxposition ? %% v
string.section v 0 maxposition
* all global variables and functions are in boldface
For the program to be used globally it should be able to be compiled by its own, and therefore, well defined. So, functions that have parameters that are to be used in programmatic structures like looping and conditional, should be relayed to local defined variables, that will be used instead. This modification is demonstrated in functions “sum” and “average” where parameter “list” was relayed (assigned) to local predefined list variable “_list”.
Moreover, all function parameters except from well-defined list parameters, are regarded undefined parameters in the context of global modules, and care should be taken to avoid ambiguity.
To make the program global, open it with Till Launcher, and click “GLOBAL MODULE”. If the program is compiled without errors, it will be available globally under its filename (file extension is not mandatory). Note that when a module is called by its name (without structural path) it is firstly searched in Till global modules and then in the current directory, and therefore it is recommended to give it a name that will not coincide with other programs.
Example of an application “app.till” that uses (calls) global module “util”:
! application: app.till
! find ten random numbers between 7 and 77 that
! are multiples of seven, and their deviation
! from median is less than or equal to four percent
! without using modulo system function (math.mod)
! call global module 'util'
#mod util
! user specified variables
QUANTITY 10
RANGEMIN 7
RANGEMAX 77
MODULO 7
MAXDEVPER 4
go:
nums
median (RANGEMIN + RANGEMAX) / 2
maxdev MAXDEVPER * median / 100
avg diff
! deviation looping
~
! ATTENTION: must reset on each iteration!
counter QUANTITY
nums ->->
! modulo looping
~
rnum rand RANGEMIN RANGEMAX
not isinteger rnum / MODULO ? %~
nums <- rnum
counter --
not counter ? %
^
avg average nums
diff math.abs avg - median
diff <= maxdev ? %
^
! show
m 'nums: ' + nums
m ++ N + 'median: ' + median
m ++ N + 'average: ' + avg
pdiff diff / median * 100
sdiff shortdec pdiff 4
m ++ N + 'deviation percentage: ' + sdiff + ' %'
print m
isinteger v:
roundv round v
v roundv ? %% true
false
* all global variables and functions from global module “util” are in boldface
nums: [ 7, 70, 63, 28, 21, 35, 56, 56, 28, 70 ]
median: 42
average: 43.4
deviation percentage: 3.333 %