TYL PROGRAMMING LANGUAGE
▶ TYL PROGRAM STRUCTURE
There are two types of Tyl programs: Tyl program without functions is a program that does not have any function in it. In most of the examples in the guide, there are no functions, and they are valid Tyl programs that can run AS-IS.

Tyl program with functions is a program that has at least one function in it. In functions page, there are examples of program with functions.

Tyl gives the programmer the ability to choose between the two options, to enable a functionless programming. If you have only activities that are to occur in one function, than there is no need for that function at all, and the module (program) itself is the function.

Tyl program structure:

[MODULE-STATEMENTS-SECTION]
 [STATEMENT]
 [STATEMENT]
 [STATEMENT]
 [STATEMENT]
 ...

[MODULE-FUNCTIONS-SECTION]
 [FIRST-FUNCTION-SECTION]
  [STATEMENT]
  [STATEMENT]
  [STATEMENT]
  ...

 [FUNCTION-SECTION]
  [STATEMENT]
  [STATEMENT]
  [STATEMENT]
  ...

 ...


Tyl program has two optional sections:
If the module (program) is functionless, it has only a module statements section that is composed of Tyl statements, such as: declaration, assignment, conditional, looping. If it has functions, than module statements section is optional, and the module functions section, that is always after the module statements section, will be composed of functions with their statements.

Say we want to implement traffic lights activity:
actions 'stand' 'prepare to drive' 'drive' 'prepare to stand'
lights 'RED' 'RED ORANGE' 'GREEN' 'ORANGE'

index 4 ~
print 'Action: ' + actions index
print 'Street Light: ' + lights index
print


print 'Action: ' + actions 0
print 'Street Light: ' + lights 0
Action: stand
Street Light: RED

Action: prepare to drive
Street Light: RED ORANGE

Action: drive
Street Light: GREEN

Action: prepare to stand
Street Light: ORANGE

Action: stand
Street Light: RED
A full cycle is shown from first stand to last stand.
Though it works, a better idea is to use an indices list and iterate (loop) over it:
actions 'stand' 'prepare to drive' 'drive' 'prepare to stand'
lights 'RED' 'RED ORANGE' 'GREEN' 'ORANGE'
indices 0 1 2 3 0

indices index ~
print 'Action: ' + actions index
print 'Street Light: ' + lights index
print

indices list holds the scenario of the activity, and if it is only for that scenario, than the program implements it without using any function. So the program has only module statements, and when the program runs, it runs all the statements in it, and that's all program activity.

What if we want to play some scenarios?
Obviously, repeating the looping statements in code is tedious and awkward. So in this case we would gather all code that should be repeated in functions:
go »
normal_indices 0 1 2 3 0
play 'Normal Scenario' normal_indices
race_indices 0 2 0
play 'Race Scenario' race_indices

play title indices »
actions 'stand' 'prepare to drive' 'drive' 'prepare to stand'
lights 'RED' 'RED ORANGE' 'GREEN' 'ORANGE'

print title
print '------------------------'

indices index ~
  action actions index
  light lights index
  print '[' + light + '] ' + action


print
Normal Scenario
------------------------
[RED] stand
[RED ORANGE] prepare to drive
[GREEN] drive
[ORANGE] prepare to stand
[RED] stand

Race Scenario
------------------------
[RED] stand
[GREEN] drive
[RED] stand
go function was declared, that plays two scenarios. play function was declared, that holds scenario functionality. In Tyl, if a program has functions, it locates the first function, and runs it. go function is the first function in the program, so Tyl will run it.

So in this type of Tyl program, it has functions, and the program runs the first of them. If we look closely on play function, we can see that it holds actions and lights lists, and declares them each time the function is activated. Usually, when there are static variables in a program, or if there are variables that are used in various places in the program, they should be declared outside any function.

Therefore we will modify the program (including structure comments):
≡======= Module Statements =======

≡ the statements in this section will run
≡ first, on program run

≡------ declarations ------

actions 'stand' 'prepare to drive' 'drive' 'prepare to stand'
lights 'RED' 'RED ORANGE' 'GREEN' 'ORANGE'
normal_indices
race_indices 0 2 0
title_line '------------------------'

≡------ actions ------

ind 4 ~ normal_indices ind
normal_indices 0



≡======= Module Functions =======

≡------ Fisrt Function ------

≡ this function will run after running the
≡ statements in the statements section

go »
play 'Normal Scenario' normal_indices
play 'Race Scenario' race_indices


≡------ More Functions ------

play title indices »
print title
print title_line

indices index ~
  action actions index
  light lights index
  print '[' + light + '] ' + action


print
All declarations were moved to module statements section. On program run, these statements will be run first, than go function will run. The result of this program will be the same, but it will work more efficiently, and the variables that were declared in module statements can be used anywhere in the program.