Files
A program can be split over several files with include:
src/
main.cfl
utils.cfl
game/
player.cfl
main.cfl:
include game\player.cfl
include utils.cfl
hp := new_hp()
hp := hurt(hp, 80)
print(hp, twice(21))
game/player.cfl:
include ..\utils.cfl
fn new_hp() { return 100 }
fn hurt(int hp, int dmg) { return hp - limit(dmg) }
priv fn limit(int dmg) {
if dmg > 50 { return 50 }
return dmg
}
utils.cfl:
fn twice(int x) { return x * 2 }
50 42
include
- The path is relative to the file that has the
include. \and/both work, on Linux and on Windows..cflcan be left out. Put the path in quotes if it has spaces:include "my files\x.cfl".- Each file is compiled once, however many times it's included, and two files may include each other.
glob and priv
A function is glob unless you say otherwise: every file that includes its file can call it. Writing glob fn means the same thing.
priv fn can only be called from its own file. Two files can each have a priv fn with the same name.
priv works on const too.
Names are checked
A function name can be defined only once in the whole program:
error: function `hurt` is defined twice
--> game/enemy.cfl:3:4
|
3 | fn hurt(int hp) { return hp - 1 }
| ^^^^
= first one: game/player.cfl:5:4
Calling a priv function from another file, or a function from a file you didn't include, is an error that says which file it's in.
Code outside functions
Only main.cfl (the file you build) runs code outside functions. Other files hold functions and constants.