Files use .rex, the command is rex, and the crate, runtime prefixes, docs, editor files and examples all use the new name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
2.7 KiB
Markdown
101 lines
2.7 KiB
Markdown
# 1. First steps
|
|
|
|
## Running a program
|
|
|
|
Save this as `hello.rex`:
|
|
|
|
```rex
|
|
func main() {
|
|
print("Hello, Rex!")
|
|
}
|
|
```
|
|
```text
|
|
Hello, Rex!
|
|
```
|
|
|
|
Every program starts at `func main()`. Run it from the project folder:
|
|
|
|
```text
|
|
cargo run -q -- run hello.rex
|
|
```
|
|
|
|
The compiler turns your Rex code into C and builds it with your C compiler (`cc`, so you
|
|
need gcc or clang installed). If you build the `rex` binary once, the commands are:
|
|
|
|
| Command | What it does |
|
|
|---------|--------------|
|
|
| `rex run file.rex [args...]` | Compile and run. Extra words are given to the program (`os.args`). |
|
|
| `rex build file.rex -o out` | Make an executable you can keep. |
|
|
| `rex check file.rex` | Look for errors without building. |
|
|
| `rex emit-c file.rex` | Print the generated C. |
|
|
| `rex fmt file-or-folder...` | Tidy the code in place (`--check` only reports). |
|
|
|
|
## print
|
|
|
|
`print` takes any number of values, separates them with spaces, and ends the line.
|
|
It can print numbers, strings, lists, maps, structs, enums and more.
|
|
|
|
```rex
|
|
func main() {
|
|
print("score:", 42, true)
|
|
print([1, 2, 3])
|
|
print(1.5 + 1.5)
|
|
}
|
|
```
|
|
```text
|
|
score: 42 true
|
|
[1, 2, 3]
|
|
3.0
|
|
```
|
|
|
|
Notice that a float always shows a `.0` so you can tell `3.0` from `3`.
|
|
|
|
## Comments
|
|
|
|
```rex
|
|
// a comment runs to the end of the line
|
|
func main() {
|
|
print("hi") // so does this
|
|
}
|
|
```
|
|
```text
|
|
hi
|
|
```
|
|
|
|
## Reading the error messages
|
|
|
|
Rex tries hard to explain mistakes. For example, forgetting that `int` and `float`
|
|
don't mix:
|
|
|
|
```text
|
|
error: can't use `int` and `float` together (convert with `float(x)`)
|
|
```
|
|
|
|
When a message offers a hint, try it. If it doesn't make sense, that is a bug in the
|
|
message and worth fixing.
|
|
|
|
## Your editor
|
|
|
|
`editors/` has colours, live error underlines and format-on-save for VS Code, Neovim, Vim and
|
|
Helix (any editor that can run a language server works). See `editors/README.md`. It is the
|
|
compiler itself doing the checking, so you see the same messages as in the terminal. The
|
|
server is started with `rex lsp`.
|
|
|
|
## Formatting your code
|
|
|
|
`rex fmt` rewrites your files in one standard style, so you never argue about spaces:
|
|
|
|
```text
|
|
rex fmt hello.rex # one file
|
|
rex fmt src/ # every .rex file in a folder
|
|
rex fmt --check src/ # change nothing; exit code 1 if something is untidy
|
|
```
|
|
|
|
It uses 4-space indents, one space around operators, `a: int` colons, `Point{x: 1}` literals,
|
|
and at most one blank line in a row. It keeps your comments and **never moves a line break**,
|
|
so how you split things across lines is up to you. Trailing `//` comments on neighbouring
|
|
lines are lined up. Before saving, it checks that the code means exactly the same thing, and
|
|
it refuses files that don't parse.
|
|
|
|
Next: [Values and types](02-values-and-types.md)
|