Exit status
The smallest meaningful esque program: return a number from main
and let the OS see it as the exit code.
The program
# hello.esq
fn main() -> i32 = (10 + 3) * 2 - 1
Run
$ ./esquec build hello.esq -o hello
$ ./hello; echo $?
25
What is happening
fn main() -> i32 = …declares a zero-argument function returningi32. This is the entry point.(10 + 3) * 2 - 1is a single expression. It evaluates to25.- The compiler emits a tiny
_startthat callsmain, then issues the Linuxexit_groupsyscall withmain's return value.
Try
- Replace the body with
42. Re-run.echo $?→ 42. - Replace it with
'A'. (Char literals are i32 codepoints.) → 65. - Replace it with
99_i32. (Explicit type suffix.) → 99. - Replace it with
1 + 1.0. The compiler refuses to compile it.
Inspect the build
$ ./esquec build hello.esq --emit=ceir
fn main() -> i32 {
%0 = ConstInt 13
%1 = ConstInt 2
%2 = Mul %0 %1
%3 = ConstInt 1
%4 = Sub %2 %3
Return %4
}
13 and 26 - 1 have already been computed at the AST level by the
constant-fold pass.
$ ./esquec build hello.esq --emit=asm
main:
b8 19 00 00 00 mov eax, 0x19 ; 25
c3 ret
The whole program is two instructions.