Code Examples¶
Hello World Program¶
A simple “Hello World” program written in TL5:
module hello-world
func ! show()
sys.println(user "hello world")!
main!
show()!
Testing the Hello World Program¶
Testing code for the “Hello World” program:
module hello-world-test
var Bool println-raise
var String printed-text
mock ! sys.println(user Buffer text)
if println-raise
raise! "error in println"
printed-text.concat(user text)!
test show-hello-world-test()
println-raise := false
printed-text.clear()
hello-world.show()!
assert! printed-text.equal(user "hello world")
test show-raise-test()
println-raise := true
assert-error! hello-world.show(), "error in println"
Fibonacci Function¶
func fibonacci(copy Uint64 n)->(var Uint64 res)
var Uint64 prev(copy 1)
res := 0
repeat n
var Uint64 sum(copy res clamp+ prev)
prev := res
res := sum
Complex number Type¶
struct Complex
var Sint64 real
var Sint64 imaginary
new(copy Sint64 real, copy Sint64 imaginary)
self.real := real
self.imaginary := imaginary
func user ! str(user String out-str)
self.real.str(user out-str)!
out-str.append(copy ' ')!
if self.imaginary > 0
out-str.append(copy '+')!
else
out-str.append(copy '-')!
out-str.append(copy ' ')!
var String imaginary-str
if self.imaginary > 0
self.imaginary.str(user imaginary-str)!
else
(- self.imaginary).str(user imaginary-str)!
out-str.concat(user imaginary-str)!
out-str.append(copy 'i')!
func ! usage-example()
var Complex complex(copy 5, copy 3)
var String complex-str
complex.str(user complex-str)!
sys.println(user complex-str)!