Examples
Four short programs. Each is a complete file — copy one into hello.lomt
and run it. What you see under each is what it actually printed.
The smallest program
_start is the entry point, because there is no runtime to call one for
you. Output goes through the write system call: file descriptor, pointer,
length. The second call is exit.
module hello fn _start() { let s: str = "hello from Loment\n"; syscall4(1, 1, str_ptr(s) as u64, str_len(s) as u64); syscall4(60, 0, 0, 0);}
hello from Loment
A function with a return value
Types are written out and never inferred; there is no implicit numeric conversion, so
an integer that needs to be a different width says so with as.
module funct fn write_str(fd: u64, s: str) -> i64 { return syscall4(1, fd, str_ptr(s) as u64, str_len(s) as u64);} fn larger(a: u32, b: u32) -> u32 { if a > b { return a; } return b;} fn _start() { let m: u32 = larger(7, 9); if m == 9 { write_str(1, "larger(7, 9) came back with 9\n"); syscall4(60, 0, 0, 0); } write_str(1, "unexpected\n"); syscall4(60, 1, 0, 0);}
larger(7, 9) came back with 9
A constant and a loop
for walks a range and binds the index. const is a
compile-time value, usable as a bound.
module counting const LIMIT: u32 = 5; fn write_str(fd: u64, s: str) -> i64 { return syscall4(1, fd, str_ptr(s) as u64, str_len(s) as u64);} fn _start() { let sum: u32 = 0; for i in 0..LIMIT { sum = sum + i; } if sum == 10 { write_str(1, "the loop summed 0 through 4\n"); syscall4(60, 0, 0, 0); } write_str(1, "unexpected\n"); syscall4(60, 1, 0, 0);}
the loop summed 0 through 4
An array, passed as a slice
[u32; 3] is an array with its length in the type; [u32] is a
slice, which carries no length of its own. &xs hands the array over as
a slice, and slice_len asks how long it is.
module slice fn write_str(fd: u64, s: str) -> i64 { return syscall4(1, fd, str_ptr(s) as u64, str_len(s) as u64);} fn sum(xs: [u32]) -> u32 { let acc: u32 = 0; let i: u32 = 0; while i < slice_len(xs) { acc = acc + xs[i]; i = i + 1; } return acc;} fn _start() { let xs: [u32; 3] = [1, 2, 3]; let s: u32 = sum(&xs); if s == 6 { write_str(1, "the slice summed to 6\n"); syscall4(60, 0, 0, 0); } write_str(1, "unexpected\n"); syscall4(60, 1, 0, 0);}
the slice summed to 6
More
The repository carries a larger set under loment/examples/. Not all of them link in this release.
Everything on this page was compiled and run with the published
0.1.4 package. The capability system — the thing the
front page opens with — is not among these four,
because it does not link in this release. That is a known defect in the
self-hosted linker, not in the language: the reference implementation links the
same program and runs it. See
issue #102 and
#38 for the same
failure class.