.\" Made by website/plain/build.mjs from website/words/sg.md; change that file. .TH SG 1 2026-11-28 "SG" "SG commands" .SH NAME sg \- runs a program in the interpreter, from a file or a folder; the words after it are the program's own .SH SYNOPSIS .TP \fBsg file.sg\fR runs the program of one file, with the folders beside it as its modules .TP \fBsg folder\fR runs the program of a folder: every .sg file directly in it, and the folders in it as its modules .TP \fBsg file.sg word...\fR the words after the program are its own: void main(List args) gets them .TP \fBsg \-L folder file.sg, sg \-Lfolder file.sg\fR looks for the libraries of extern blocks in that folder first; written before the program, once for each folder .TP \fBsg \-\-help, sg \-h\fR prints this page .TP \fBsg\fR with nothing after it, writes how sg is used and ends with code 2 .SH DESCRIPTION \fBsg\fR checks the whole program, then runs it in the interpreter, at once and without making a program file. The interpreter is the reference: a program built with \fBsg build\fR prints the same, and it checks what a debug build checks, integer overflow among the rest. It needs no C compiler, unless the program calls C through an \fBextern\fR block: then the wrappers of those functions are built with the C compiler \fBCC\fR names, else \fBcc\fR (Clang on Windows), and \fB\-L\fR names a folder where a library lies. .PP A file is a program with the folders beside it as its modules, brought in with \fBuse\fR\&. A folder is a program whose root is every \fB\&.sg\fR file directly in it; one of them has \fBmain\fR\&. Either way, when that folder has an \fBsg.project\fR, the packages it names come in too, as \fBsg.lock\fR says (see \fBsg add\fR). The first word after the folders of \fB\-L\fR is the program, and every word after it is the program's own, even one \fBsg\fR would read itself (\fB\-L\fR, \fB\-\-help\fR, \fB\-\-\fR). A word comes in as the system gave it; a byte in it that is not UTF-8 becomes a character from U+10FF80 to U+10FFFF, and goes back to the system as it came. The words \fBtest\fR, \fBbuild\fR, \fBemit\-c\fR, \fBlsp\fR, \fBhelp\fR, \fBadd\fR, \fBupdate\fR, \fBremove\fR and \fBlock\fR in the first place are commands, so a folder with one of those names is run as \fBsg ./test\fR\&. .PP The program's input is the input of \fBsg\fR, which \fBStdin\fR reads. \fBprint\fR and \fBStdout\fR write to the output; \fBprintError\fR, \fBStderr\fR, a panic, an error of the checker and what \fBsg\fR itself has to say go to the standard error. What was printed goes out before any message on the standard error. Into a pipe whose reader is gone (\fBsg main.sg | head\fR), the program ends quietly, by \fBSIGPIPE\fR on Linux and macOS, as compiled code does. .PP The exit code says how it went: .TP \fB0\fR main ended, or returned Ok() .TP \fBthe code of exit(code)\fR the program called exit: a byte, 0 to 255 .TP \fB1\fR the checker refused the program; it panicked; main returned an error, which is written after error:; or it ended with shared values still alive, which are named .TP \fB2\fR the command line is not one sg takes, or a path in it leads nowhere: a file that cannot be read, a folder with no .sg file, a folder of -L that is not there .PP Deep recursion ends in a panic too, \fBstack overflow\fR, which names the call: the interpreter looks at what is left of its stack of 1 GB before each call. .SH EXAMPLES .SS "A file" The file hello.sg: .PP .RS 4 .nf void main() { print("Hello, world") } .fi .RE .PP .RS 4 .nf $ sg hello.sg Hello, world $ sg build hello.sg -o hello $ ./hello Hello, world .fi .RE .PP \fBsg\fR runs the file at once. \fBsg build\fR compiles the same program, which prints the same. .SS "The program's own words" The file words.sg: .PP .RS 4 .nf void main(List args) { print($"{args.length()} words") for word in read args { print($"[{word}]") } } .fi .RE .PP .RS 4 .nf $ sg words.sg 0 words $ sg words.sg red "two words" "" 3 words [red] [two words] [] $ sg words.sg --help -L vendor -- --release 5 words [--help] [-L] [vendor] [--] [--release] .fi .RE .PP The list holds the words after the program, not its name. The quotes are the shell's: \fB"two words"\fR is one word, and \fB""\fR an empty one. After the program \fBsg\fR reads no word, so \fB\-\-help\fR and \fB\-L\fR are the program's; a program that takes flags of its own reads them with \fBCommandLine\fR\&. .SS "Input, output and the exit code" The file sum.sg: .PP .RS 4 .nf Result main() { long total = 0 var bad = 0 var line = string.withCapacity(80) while try Stdin.readLine(ref line) { var cents = long.parse(read line) ?? -1 if cents < 0 { printError($"not a price: {line}") bad += 1 continue } total += cents } print($"{total} cents") if bad > 0 { exit(3) } return Ok() } .fi .RE .PP The file prices.txt: .PP .RS 4 .nf 450 1200 tea 75 .fi .RE .PP .RS 4 .nf $ printf '450\en1200\en' | sg sum.sg 1650 cents $ sg sum.sg < prices.txt; echo "exit code $?" not a price: tea 1725 cents exit code 3 $ sg sum.sg < prices.txt 2>/dev/null > total.txt $ cat total.txt 1725 cents $ printf '45\e377\en' | sg sum.sg; echo "exit code $?" error: invalid text at byte 2 exit code 1 .fi .RE .PP \fBStdin\fR reads what comes into \fBsg\fR: a pipe, a file, or the terminal. The line about \fBtea\fR goes to the standard error, so \fB2>/dev/null\fR leaves it out and \fBtotal.txt\fR gets the total alone. \fBexit(3)\fR ends the program with that code. A byte that is not UTF-8 is an error of \fBreadLine\fR, which \fBtry\fR hands to \fBmain\fR: \fBsg\fR writes it after \fBerror:\fR and ends with code 1. .SS "A folder" The file shop/main.sg: .PP .RS 4 .nf use stock void main(List args) { var count = int.parse(args.get(0) ?? "2") ?? 2 print($"{count} cups: {price(count)} cents, {left(count)} left") } .fi .RE .PP The file shop/prices.sg: .PP .RS 4 .nf int price(int count) { return count * 450 } .fi .RE .PP The file shop/stock/count.sg: .PP .RS 4 .nf public int left(int sold) { return 40 - sold } .fi .RE .PP .RS 4 .nf $ sg shop 2 cups: 900 cents, 38 left $ sg shop 5 5 cups: 2250 cents, 35 left $ cd shop $ sg . 3 3 cups: 1350 cents, 37 left .fi .RE .PP The program is \fBmain.sg\fR and \fBprices.sg\fR, which see each other's names with nothing written; \fBstock\fR is a module, which \fBmain.sg\fR brings in with \fBuse\fR\&. The words after the folder are the program's, as after a file. .SS "When the program stops" The file stock.sg: .PP .RS 4 .nf void main(List args) { var shelves = List { 12, 0, 7 } for word in read args { var at = int.parse(word) ?? panic($"not a shelf: {word}") print($"shelf {at}: {shelves[at]}") } } .fi .RE .PP .RS 4 .nf $ sg stock.sg 0 2 5; echo "exit code $?" shelf 0: 12 shelf 2: 7 panic: index 5 is out of range: the length is 3 --> stock.sg:5:30 5 | print($"shelf {at}: {shelves[at]}") | ^^^^^^^^^^^ hint: `.get(i)` reads with `null` instead; before a write, compare the index with `.length()` exit code 1 $ sg stock.sg 1 x 2>/dev/null; echo "exit code $?" shelf 1: 0 exit code 1 $ sg build stock.sg -o stock $ ./stock 1 x 2>/dev/null; echo "exit code $?" shelf 1: 0 exit code 1 .fi .RE .PP The lines printed before the panic go out first. The panic names the fault and the line, on the standard error, and the program ends with code 1. A compiled program panics with the same text and the same code. .SS "A library of C in a folder: -L" The file dice/vendor/dice.c: .PP .RS 4 .nf #include int32_t dice_mean(int32_t count, int32_t sides) { return count * (sides + 1) / 2; } .fi .RE .PP The file dice/main.sg: .PP .RS 4 .nf extern "dice" { int dice_mean(int count, int sides) } void main(List args) { var count = int.parse(args.get(0) ?? "3") ?? 3 print(unsafe dice_mean(count, 6)) } .fi .RE .PP .RS 4 .nf $ cd dice $ cc -fPIC -c -o vendor/dice.o vendor/dice.c $ ar rcs vendor/libdice.a vendor/dice.o $ sg -L vendor main.sg 4 14 $ sg -Lvendor main.sg 10 .fi .RE .PP \fBextern "dice"\fR links \fB\-ldice\fR, which the C compiler looks for where it looks for libraries, and in the folder \fB\-L\fR names first. \fB\-L\fR comes before the program; \fBsg build\fR and \fBsg test\fR take it too. .SH "MISTAKES IT TELLS" .SS "A file of a folder, run alone" With the folder \fBshop\fR of the example above: .PP .RS 4 .nf $ sg shop/main.sg error: there is no function named `price` --> shop/main.sg:5:28 5 | print($"{count} cups: {price(count)} cents, {left(count)} left") | ^^^^^ hint: `price` is in `shop/prices.sg`; the files of a folder are one program: name the folder, `shop`, in place of the file .fi .RE .PP A file alone is a program of that file and the modules beside it: \fBprices.sg\fR is not part of it. The folder is the program, \fBsg shop\fR; when a name not found is in a file beside the one given, the hint says so. .SS "-L after the program" With the folder \fBdice\fR of the example above: .PP .RS 4 .nf $ cd dice $ sg main.sg -L vendor 2>&1 | head -1 error: the library `dice` was not found .fi .RE .PP After the program, \fB\-L\fR and \fBvendor\fR are the program's own words, so the library is looked for where the C compiler looks alone. The message ends with the hint \fB`\-L folder` names a folder where the library lies\fR\&. .SS "A folder named as a command" The file test/main.sg: .PP .RS 4 .nf void main() { print("the program in test") } .fi .RE .PP .RS 4 .nf $ sg test 2>/dev/null; echo "exit code $?" exit code 2 $ sg ./test the program in test .fi .RE .PP \fBsg test\fR is the command that runs tests, and it needs a path. \fB\&./test\fR is a path, so \fBsg\fR runs it. .SS "A path that leads nowhere" .RS 4 .nf $ sg nothere.sg; echo "exit code $?" error: cannot read nothere.sg: No such file or directory (os error 2) exit code 2 $ mkdir -p empty && sg empty; echo "exit code $?" error: there is no `.sg` file in empty exit code 2 $ sg -L nothere hello.sg; echo "exit code $?" error: -L nothere: no such folder exit code 2 .fi .RE .SS "A module run as a program" .RS 4 .nf $ sg shop/stock error: there is no `main` function --> shop/stock/count.sg:3:2 3 | } | ^ hint: a program starts at `void main() { ... }` .fi .RE .PP \fBshop/stock\fR is a module of the program \fBshop\fR: it has no \fBmain\fR\&. .SS "A main that returns a number" .RS 4 .nf int main() { return 3 } .fi .RE .PP .RS 4 .nf error: `main` is written `void main()` or `Result main()` to see the command line: `void main(List args)`; to await, `async` in front --> example.sg:1:5 1 | int main() { | ^^^^ .fi .RE .PP The exit code is given with \fBexit(code)\fR, and an error by returning it from \fBResult main()\fR\&. .SH "SEE ALSO" .BR sg-build (1), .BR sg-test (1), .BR sg-help (1), .BR sg-use (7), .BR sg-extern (7) .PP https://sg\-lang.org/reference/sg.html