Rename the language from Dino to Rex
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>
This commit is contained in:
1 parent
2f44f09b6a
commit
7cf9727bcd
94 files changed
+858
-858
No files matched your search
+20
-20
@@ -2,40 +2,40 @@
|
||||
|
||||
## Running a program
|
||||
|
||||
Save this as `hello.dino`:
|
||||
Save this as `hello.rex`:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
print("Hello, Dino!")
|
||||
print("Hello, Rex!")
|
||||
}
|
||||
```
|
||||
```text
|
||||
Hello, Dino!
|
||||
Hello, Rex!
|
||||
```
|
||||
|
||||
Every program starts at `func main()`. Run it from the project folder:
|
||||
|
||||
```text
|
||||
cargo run -q -- run hello.dino
|
||||
cargo run -q -- run hello.rex
|
||||
```
|
||||
|
||||
The compiler turns your Dino code into C and builds it with your C compiler (`cc`, so you
|
||||
need gcc or clang installed). If you build the `dino` binary once, the commands are:
|
||||
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 |
|
||||
|---------|--------------|
|
||||
| `dino run file.dino [args...]` | Compile and run. Extra words are given to the program (`os.args`). |
|
||||
| `dino build file.dino -o out` | Make an executable you can keep. |
|
||||
| `dino check file.dino` | Look for errors without building. |
|
||||
| `dino emit-c file.dino` | Print the generated C. |
|
||||
| `dino fmt file-or-folder...` | Tidy the code in place (`--check` only reports). |
|
||||
| `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.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
print("score:", 42, true)
|
||||
print([1, 2, 3])
|
||||
@@ -52,7 +52,7 @@ Notice that a float always shows a `.0` so you can tell `3.0` from `3`.
|
||||
|
||||
## Comments
|
||||
|
||||
```dino
|
||||
```rex
|
||||
// a comment runs to the end of the line
|
||||
func main() {
|
||||
print("hi") // so does this
|
||||
@@ -64,7 +64,7 @@ hi
|
||||
|
||||
## Reading the error messages
|
||||
|
||||
Dino tries hard to explain mistakes. For example, forgetting that `int` and `float`
|
||||
Rex tries hard to explain mistakes. For example, forgetting that `int` and `float`
|
||||
don't mix:
|
||||
|
||||
```text
|
||||
@@ -79,16 +79,16 @@ message and worth fixing.
|
||||
`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 `dino lsp`.
|
||||
server is started with `rex lsp`.
|
||||
|
||||
## Formatting your code
|
||||
|
||||
`dino fmt` rewrites your files in one standard style, so you never argue about spaces:
|
||||
`rex fmt` rewrites your files in one standard style, so you never argue about spaces:
|
||||
|
||||
```text
|
||||
dino fmt hello.dino # one file
|
||||
dino fmt src/ # every .dino file in a folder
|
||||
dino fmt --check src/ # change nothing; exit code 1 if something is untidy
|
||||
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,
|
||||
|
||||
+11
-11
@@ -4,27 +4,27 @@
|
||||
|
||||
`var` makes a variable you can change. The type is worked out from the value.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var score = 10
|
||||
score = score + 5
|
||||
score += 1
|
||||
print(score)
|
||||
|
||||
var name: string = "Dino" // you may write the type after a colon
|
||||
var ratio: float = 1 // a whole-number literal fits a float
|
||||
var name: string = "Rex" // you may write the type after a colon
|
||||
var ratio: float = 1 // a whole-number literal fits a float
|
||||
print(name, ratio)
|
||||
}
|
||||
```
|
||||
```text
|
||||
16
|
||||
Dino 1.0
|
||||
Rex 1.0
|
||||
```
|
||||
|
||||
`const` is a value that never changes. It must be known when the program is compiled
|
||||
(you can't call functions in it).
|
||||
|
||||
```dino
|
||||
```rex
|
||||
const MAX = 100
|
||||
const GREETING = "hi"
|
||||
|
||||
@@ -53,7 +53,7 @@ func main() {
|
||||
Sized numbers exist for when you need exact control: `i8 i16 i32 u8 u16 u32 u64 f32`
|
||||
(`i64` is the same as `int`, `f64` the same as `float`).
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var small: u8 = 200
|
||||
var wrapped = small + 100 // whole numbers wrap around: 300 - 256
|
||||
@@ -82,7 +82,7 @@ Rules worth remembering:
|
||||
|
||||
Put values inside text with `{ }`. Write a literal brace as `\{`.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var name = "Ada"
|
||||
var age = 36
|
||||
@@ -106,7 +106,7 @@ starts_with ends_with index_of to_int to_float`. See the [cheatsheet](11-cheatsh
|
||||
|
||||
`s[a..b]` is the part from byte `a` up to (not including) `b`. Leave a side out to go to the edge.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var s = "hello world"
|
||||
print(s[0..5])
|
||||
@@ -127,7 +127,7 @@ A slice outside the string stops the program with a message that shows the lengt
|
||||
|
||||
A `char` is **one character**, written with single quotes. Double quotes are for text.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var c = 'a'
|
||||
print(c, c == 'a', c < 'b')
|
||||
@@ -145,7 +145,7 @@ a!
|
||||
|
||||
Going through a string gives characters:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var s = "héllo 42"
|
||||
var letters = 0
|
||||
@@ -181,7 +181,7 @@ h é l
|
||||
- `char` can be compared (`== != < > <= >=`), matched with `match`, and used as a map key.
|
||||
It does no arithmetic: use `int(c)` and `char(n)` if you need that.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func kind(c: char): string {
|
||||
return match c {
|
||||
'+' => "plus"
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
No parentheses around the condition. The braces are required. The condition must be a `bool`.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var temp = 21
|
||||
if temp > 30 {
|
||||
@@ -26,7 +26,7 @@ Combine conditions with `&&`, `||` and `!`. The right side only runs when needed
|
||||
|
||||
There is one keyword for every kind of loop.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
for i in 0..3 { // 0, 1, 2: the end is not included
|
||||
print("i =", i)
|
||||
@@ -83,7 +83,7 @@ the loop is safe.
|
||||
`match` picks a branch by shape. It must cover every possibility, otherwise the compiler
|
||||
lists what is missing. `_` means "anything else".
|
||||
|
||||
```dino
|
||||
```rex
|
||||
enum Shape {
|
||||
Circle(r: float)
|
||||
Rect(w, h: float)
|
||||
@@ -119,7 +119,7 @@ zero one many
|
||||
`match` works on enums, ints, bools and strings. Used as a **value** (like above) each arm
|
||||
is one expression. Used as a **statement** an arm can be a block:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var cmd = "stop"
|
||||
match cmd {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# 4. Functions
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func add(a: int, b: int): int {
|
||||
return a + b
|
||||
}
|
||||
@@ -25,7 +25,7 @@ Names in a row can share a type: `func area(w, h: float): float`.
|
||||
|
||||
A function can return more than one value. The most common use is a value and an `error`.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func divide(a, b: int): (int, error) {
|
||||
if b == 0 {
|
||||
return 0, error("cannot divide by zero")
|
||||
@@ -53,7 +53,7 @@ A parameter is a copy as far as you can tell: changing it inside never changes t
|
||||
variable. (When a function only *reads* a parameter, the compiler passes it without copying,
|
||||
so big lists are free to pass around.)
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func grow(xs: []int): []int {
|
||||
xs.push(99)
|
||||
return xs
|
||||
@@ -73,7 +73,7 @@ func main() {
|
||||
|
||||
A function has a type like `func(int): int`. You can store it, pass it and return it.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func double(x: int): int { return x * 2 }
|
||||
|
||||
func apply(f: func(int): int, x: int): int {
|
||||
@@ -96,7 +96,7 @@ func main() {
|
||||
### Short lambdas
|
||||
|
||||
`x => x * 2` and `(a, b) => a + b` are shorthand for a one-expression function. The types
|
||||
come from where it is used, so it works where Dino already knows what function is expected
|
||||
come from where it is used, so it works where Rex already knows what function is expected
|
||||
(a parameter, a typed variable, a struct field, or another argument of a generic function).
|
||||
The short form is one expression; for several statements use the long `func(x: int): int { }`.
|
||||
|
||||
@@ -105,7 +105,7 @@ The short form is one expression; for several statements use the long `func(x: i
|
||||
A function written inside another can use the variables around it. It **copies** them when
|
||||
it is created and keeps its own copy from then on.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func make_counter(): func(): int {
|
||||
var n = 0
|
||||
return func(): int {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
An ordered, growable sequence.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var nums = [10, 20, 30]
|
||||
nums.push(40)
|
||||
@@ -35,7 +35,7 @@ Facts:
|
||||
- Going outside the list (`nums[9]`) or popping an empty list stops the program with a message.
|
||||
- **Lists are values.** `var b = a` copies the list. Changing `b` leaves `a` alone.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var a = [1, 2, 3]
|
||||
var b = a
|
||||
@@ -51,7 +51,7 @@ func main() {
|
||||
|
||||
A map finds a value by a **key** instead of a position, like a phone book.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var ages = {"alice": 30, "bob": 25}
|
||||
ages["carol"] = 41 // add a new entry
|
||||
@@ -67,7 +67,7 @@ func main() {
|
||||
|
||||
### Making a map
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var a = {"x": 1, "y": 2} // types are guessed from the entries
|
||||
var b: map[string, int] = {} // empty: the type comes from the variable
|
||||
@@ -100,7 +100,7 @@ The value can be anything: numbers, strings, lists, structs, even other maps.
|
||||
| `m.keys()` | a list of the keys |
|
||||
| `m.values()` | a list of the values |
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var stock = {"apple": 3, "pear": 0}
|
||||
print(stock.has("apple"), stock.has("fig"))
|
||||
@@ -120,7 +120,7 @@ true false
|
||||
|
||||
`m[k] = v` adds or replaces. `m.remove(k)` deletes (and does nothing if the key is missing).
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var m = {"a": 1, "b": 2}
|
||||
m["c"] = 3
|
||||
@@ -137,7 +137,7 @@ func main() {
|
||||
The most common map job is counting. `m[k] += 1` on a key that isn't there yet starts from
|
||||
zero, so you don't have to check first.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var counts: map[string, int] = {}
|
||||
for w in "a b a c b a".split(" ") {
|
||||
@@ -156,7 +156,7 @@ func main() {
|
||||
|
||||
Entries come out **in the order they were added**, so output is always predictable.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var prices = {"tea": 2.5, "cake": 4.0}
|
||||
for name, price in prices {
|
||||
@@ -178,7 +178,7 @@ cake
|
||||
|
||||
`var b = a` copies the whole map. A map inside a struct is copied with the struct.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var a = {"x": 1}
|
||||
var b = a
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
|
||||
A struct groups related values. **Methods go inside the struct** and see the fields directly.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Player {
|
||||
name: string
|
||||
hp: int
|
||||
x, y: float // names in a row share a type
|
||||
|
||||
func hurt(amount: int) { // changes hp, so Dino knows it modifies the player
|
||||
func hurt(amount: int) { // changes hp, so Rex knows it modifies the player
|
||||
hp -= amount
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ method of the same struct (`hurt(2)`), then a global function.
|
||||
|
||||
### Structs are values
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Point { x, y: int }
|
||||
|
||||
func main() {
|
||||
@@ -64,7 +64,7 @@ Point{x: 1, y: 2} Point{x: 99, y: 2}
|
||||
|
||||
### Methods that return values, and nested data
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Vec2 {
|
||||
x, y: float
|
||||
|
||||
@@ -98,7 +98,7 @@ Vec2{x: 3.0, y: 1.0} [Vec2{x: 1.0, y: 1.0}] 10.0
|
||||
|
||||
An enum is "one of these cases", and each case can carry data.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
enum Event {
|
||||
Quit
|
||||
Key(code: int)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# 7. Errors and nil
|
||||
|
||||
Dino has no exceptions. A function that can fail says so in its result, and you handle it.
|
||||
Rex has no exceptions. A function that can fail says so in its result, and you handle it.
|
||||
|
||||
## error
|
||||
|
||||
`error` is a type that is either `nil` (all fine) or holds a message.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func parse_age(s: string): (int, error) {
|
||||
var n, err = s.to_int()
|
||||
if err != nil {
|
||||
@@ -40,7 +40,7 @@ func main() {
|
||||
Writing `?` after a call means "if this failed, return the error from *my* function too".
|
||||
Otherwise you get the value.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func half(n: int): (int, error) {
|
||||
if n % 2 != 0 { return 0, error("{n} is odd") }
|
||||
return n / 2, nil
|
||||
@@ -69,9 +69,9 @@ if it returns an error, the program prints `error: message` and exits with code
|
||||
|
||||
## Optional values: `T?`
|
||||
|
||||
A `T?` is either a `T` or `nil`. It is how Dino says "this might be missing".
|
||||
A `T?` is either a `T` or `nil`. It is how Rex says "this might be missing".
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func find(names: []string, wanted: string): int? {
|
||||
for i, n in names {
|
||||
if n == wanted { return i }
|
||||
@@ -105,7 +105,7 @@ program with a message instead of misbehaving.)
|
||||
|
||||
Maps give you optionals too:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var ports = {"http": 80}
|
||||
var p = ports.get("https")
|
||||
|
||||
+4
-4
@@ -10,7 +10,7 @@ and it also happens on an early `return`, `break`, `continue` or `?`.
|
||||
|
||||
Giving a value a new owner **copies** it:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var a = [1, 2, 3]
|
||||
var b = a // b is a real copy
|
||||
@@ -36,7 +36,7 @@ All tests of the compiler run under a memory checker, so leaks and double frees
|
||||
Sometimes you really do want several owners of **one** value (a texture used by many
|
||||
sprites, a window and its buttons). Write `shared`:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Texture {
|
||||
name: string
|
||||
uses: int
|
||||
@@ -75,7 +75,7 @@ func main() {
|
||||
If a window owns its buttons and each button owns its window, neither would ever be freed.
|
||||
Make one side `weak`: it points at the value without keeping it alive.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Window {
|
||||
title: string
|
||||
buttons: []shared Button
|
||||
@@ -118,7 +118,7 @@ OK clicked, window is gone
|
||||
- To use the value as an owner, call `w.upgrade()`. It gives a `shared T?`: the value
|
||||
(now with one more owner, so it can't vanish while you hold it) or `nil`.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Node { name: string }
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
Put the type names in `[ ]` after the function name. The compiler works the real types out
|
||||
from the arguments.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func first[T](xs: []T): T {
|
||||
return xs[0]
|
||||
}
|
||||
@@ -38,7 +38,7 @@ Generics cost nothing at run time: every set of types gets its own compiled copy
|
||||
|
||||
A generic function can take functions too, which makes `map` and `filter` easy to write:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func map[T, U](xs: []T, f: func(T): U): []U {
|
||||
var out: []U
|
||||
for x in xs { out.push(f(x)) }
|
||||
@@ -59,7 +59,7 @@ func main() {
|
||||
|
||||
Structs and enums can take types too. When you **use** one, write the types out.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
struct Stack[T] {
|
||||
items: []T
|
||||
|
||||
@@ -93,7 +93,7 @@ Maybe[string].Some(hi)
|
||||
An interface lists methods. A type follows it just by having those methods, with no
|
||||
`implements` keyword.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
interface Shape {
|
||||
area(): float
|
||||
name(): string
|
||||
@@ -114,7 +114,7 @@ struct Circle {
|
||||
|
||||
### As a rule for generics
|
||||
|
||||
```dino
|
||||
```rex
|
||||
interface Shape {
|
||||
area(): float
|
||||
}
|
||||
@@ -141,7 +141,7 @@ area is 9.0
|
||||
An interface is also a type of its own. A variable of that type can hold any value that has
|
||||
the methods, and the right method runs at run time. This is how you build a mixed list.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
interface Shape {
|
||||
area(): float
|
||||
name(): string
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
## Splitting a program into files
|
||||
|
||||
`import utils` brings in the file `utils.dino` that sits next to your main file. Use its
|
||||
`import utils` brings in the file `utils.rex` that sits next to your main file. Use its
|
||||
contents with the module name in front. Only things marked `pub` can be used from outside.
|
||||
|
||||
`utils.dino`:
|
||||
`utils.rex`:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
pub const SCALE = 10
|
||||
|
||||
pub struct Vec2 {
|
||||
@@ -20,9 +20,9 @@ pub func double(n: int): int { return n * 2 }
|
||||
func secret() {} // private: only this file can call it
|
||||
```
|
||||
|
||||
`main.dino`:
|
||||
`main.rex`:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import utils
|
||||
|
||||
func main() {
|
||||
@@ -32,7 +32,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
- Folders work too: `import tools.fs` is the file `tools/fs.dino`, used as `fs.name`.
|
||||
- Folders work too: `import tools.fs` is the file `tools/fs.rex`, used as `fs.name`.
|
||||
- Fields are private as well unless written `pub x: int`.
|
||||
- Every file has its own names, so two files can both define `helper`.
|
||||
- Import loops and missing files are errors. An error message names the file it is in.
|
||||
@@ -59,7 +59,7 @@ Import them like your own files: `import os`.
|
||||
`sqrt pow floor ceil round trunc sin cos tan asin acos atan atan2 exp log log10 hypot
|
||||
min max abs`, and the values `PI` and `E`.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import math
|
||||
|
||||
func main() {
|
||||
@@ -79,7 +79,7 @@ func main() {
|
||||
`upper lower trim split lines fields replace repeat contains starts_with ends_with
|
||||
index_of to_int to_float`, plus `len()` and slicing.
|
||||
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
var csv = " a, b ,c "
|
||||
for part in csv.split(",") {
|
||||
@@ -101,7 +101,7 @@ A list of strings can be joined: `["a", "b"].join("-")`.
|
||||
|
||||
## A program that reads input
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import os
|
||||
|
||||
func main() {
|
||||
|
||||
+16
-16
@@ -1,7 +1,7 @@
|
||||
# 11. Cheatsheet
|
||||
|
||||
## Basics
|
||||
```dino
|
||||
```rex
|
||||
var x = 1 // changeable; type inferred
|
||||
var y: float = 2 // with a type
|
||||
const MAX = 100 // fixed
|
||||
@@ -15,7 +15,7 @@ print("x is {x}", y) // interpolation with { }
|
||||
Convert numbers with `float(x)`, `int(x)`, `u8(x)`.
|
||||
|
||||
## Control flow
|
||||
```dino
|
||||
```rex
|
||||
if a > b { } else if a == b { } else { }
|
||||
for i in 0..10 { } // 0..9
|
||||
for i in 0..10 step 2 { }
|
||||
@@ -28,7 +28,7 @@ match v { 1 => a, _ => b }
|
||||
```
|
||||
|
||||
## Functions
|
||||
```dino
|
||||
```rex
|
||||
func add(a, b: int): int { return a + b }
|
||||
func f(): (int, error) { return 1, nil }
|
||||
var n, err = f()
|
||||
@@ -38,14 +38,14 @@ var h = x => x * 2 // where the type is known
|
||||
```
|
||||
|
||||
## Lists
|
||||
```dino
|
||||
```rex
|
||||
var xs = [1, 2, 3] var e: []int
|
||||
xs.push(4) xs.pop() xs.len()
|
||||
xs[0] xs[0] = 9 xs[1..3]
|
||||
```
|
||||
|
||||
## Maps
|
||||
```dino
|
||||
```rex
|
||||
var m = {"a": 1} var e: map[string, int] = {}
|
||||
var e2 = map[string, int]{}
|
||||
m["b"] = 2 // add or replace
|
||||
@@ -56,7 +56,7 @@ m.get_or("z", 0) m.len() m.keys() m.values() m.remove("a")
|
||||
```
|
||||
|
||||
## Strings
|
||||
```dino
|
||||
```rex
|
||||
s.len() s.upper() s.lower() s.trim() s.split(",") s.lines() s.fields()
|
||||
s.replace(a, b) s.repeat(n) s.contains(x) s.starts_with(x) s.ends_with(x)
|
||||
s.index_of(x) s.to_int() s.to_float() s[1..4] s[2..] s[..3]
|
||||
@@ -64,7 +64,7 @@ s.index_of(x) s.to_int() s.to_float() s[1..4] s[2..] s[..3]
|
||||
```
|
||||
|
||||
## Characters
|
||||
```dino
|
||||
```rex
|
||||
var c = 'a' '\n' 'é' // one character, single quotes
|
||||
c == 'a' c < 'z' // compare
|
||||
c.is_digit() c.is_letter() c.is_alnum() c.is_space() c.is_upper() c.is_lower()
|
||||
@@ -75,7 +75,7 @@ s[i] // the char starting at byte i
|
||||
```
|
||||
|
||||
## Structs and enums
|
||||
```dino
|
||||
```rex
|
||||
struct P {
|
||||
name: string
|
||||
x, y: float
|
||||
@@ -91,19 +91,19 @@ E.B(3) match e { A => 1, B(n) => n }
|
||||
```
|
||||
|
||||
## Errors and nil
|
||||
```dino
|
||||
```rex
|
||||
error("message {x}") err != nil return 0, nil
|
||||
var v: int? = nil if v != nil { v } if v == nil { return }
|
||||
```
|
||||
|
||||
## Sharing
|
||||
```dino
|
||||
```rex
|
||||
var a = shared T{...} var b = a // same value, two owners
|
||||
var w = weak a w.upgrade() // shared T? (nil if gone)
|
||||
```
|
||||
|
||||
## Generics and interfaces
|
||||
```dino
|
||||
```rex
|
||||
func first[T](xs: []T): T { return xs[0] }
|
||||
func big[T: Ordered](a, b: T): T { ... } // Ordered, Number, or an interface
|
||||
struct Stack[T] { items: []T } Stack[int]{}
|
||||
@@ -112,15 +112,15 @@ var s: Shape = Circle{1.0}
|
||||
```
|
||||
|
||||
## Files
|
||||
```dino
|
||||
import utils // utils.dino next to this file
|
||||
import tools.fs // tools/fs.dino, used as fs.name
|
||||
```rex
|
||||
import utils // utils.rex next to this file
|
||||
import tools.fs // tools/fs.rex, used as fs.name
|
||||
import os // built-in: os math time
|
||||
pub func f() {} // visible to other files
|
||||
```
|
||||
|
||||
## C libraries
|
||||
```dino
|
||||
```rex
|
||||
import "zlib.h" // zlib.crc32(...), zlib.Z_OK, types like zlib.gzFile
|
||||
import "raylib.h" { InitWindow } // only these names, used bare
|
||||
import "foo.h" link "foo" // when pkg-config doesn't know it (libfoo.so)
|
||||
@@ -129,7 +129,7 @@ lib.fill(buf, n) // T* parameters take a variable or a list
|
||||
```
|
||||
|
||||
## Commands
|
||||
`dino run f.dino args…` · `dino build f.dino -o out` · `dino check f.dino` · `dino emit-c f.dino` · `dino fmt path` (`--check` to only report) · `dino lsp` (editor server) · `dino header x.h [word]` (what a C header offers)
|
||||
`rex run f.rex args…` · `rex build f.rex -o out` · `rex check f.rex` · `rex emit-c f.rex` · `rex fmt path` (`--check` to only report) · `rex lsp` (editor server) · `rex header x.h [word]` (what a C header offers)
|
||||
|
||||
## Things that stop the program (exit code 2)
|
||||
Divide by zero · list index/slice out of range · `pop` on an empty list · reading a missing
|
||||
|
||||
@@ -4,7 +4,7 @@ The folder `examples/calc/` holds a calculator split over five files. It uses al
|
||||
everything from these notes. Run it:
|
||||
|
||||
```text
|
||||
cargo run -q -- run examples/calc/main.dino examples/calc/session.txt
|
||||
cargo run -q -- run examples/calc/main.rex examples/calc/session.txt
|
||||
```
|
||||
|
||||
or type calculations interactively by leaving off the file name.
|
||||
@@ -21,11 +21,11 @@ sqrt(16) + max(1, 5)
|
||||
|
||||
| File | Job | Features it shows |
|
||||
|------|-----|-------------------|
|
||||
| `token.dino` | Turns text into tokens | enums, structs, `char`, string slicing, errors |
|
||||
| `ast.dino` | The shape of a calculation | an enum that holds other nodes |
|
||||
| `parser.dino` | Builds the tree | a struct with methods, `?`, recursion |
|
||||
| `eval.dino` | Computes the answer | `match`, maps, function values, closures |
|
||||
| `main.dino` | Reads lines and prints | `import`, `os`, `shared` |
|
||||
| `token.rex` | Turns text into tokens | enums, structs, `char`, string slicing, errors |
|
||||
| `ast.rex` | The shape of a calculation | an enum that holds other nodes |
|
||||
| `parser.rex` | Builds the tree | a struct with methods, `?`, recursion |
|
||||
| `eval.rex` | Computes the answer | `match`, maps, function values, closures |
|
||||
| `main.rex` | Reads lines and prints | `import`, `os`, `shared` |
|
||||
|
||||
## 1. Tokens
|
||||
|
||||
@@ -39,7 +39,7 @@ kind of token starts there: `c.is_digit()` starts a number, `c.is_letter()` a na
|
||||
|
||||
## 2. The tree
|
||||
|
||||
```dino
|
||||
```rex
|
||||
pub enum Node {
|
||||
Num(value: float)
|
||||
Var(name: string)
|
||||
@@ -63,7 +63,7 @@ with `?`.
|
||||
|
||||
The scope that remembers variables and functions is two maps:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
pub struct Scope {
|
||||
variables: map[string, float]
|
||||
builtins: map[string, Builtin]
|
||||
@@ -74,7 +74,7 @@ pub struct Scope {
|
||||
A built-in function is a struct holding its name, how many arguments it takes, and a
|
||||
function value:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
s.builtins["sqrt"] = Builtin{"sqrt", 1, a => math.sqrt(a[0])}
|
||||
```
|
||||
|
||||
@@ -86,7 +86,7 @@ which gives `nil` when it isn't there, turned into a friendly error.
|
||||
The scope is created once as `shared`, so every line sees the variables set by the
|
||||
lines before it, with no need to pass the scope back and forth:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
var scope = shared eval.new_scope()
|
||||
...
|
||||
run_line(scope, line)
|
||||
|
||||
+33
-33
@@ -1,8 +1,8 @@
|
||||
# 13. C and C++ libraries
|
||||
|
||||
A huge amount of useful code is written in C: graphics (raylib, SDL), compression (zlib),
|
||||
databases (SQLite), and the C library that comes with every system. Dino can use it
|
||||
directly. You don't write any declarations: Dino reads the library's **header file** (the
|
||||
databases (SQLite), and the C library that comes with every system. Rex can use it
|
||||
directly. You don't write any declarations: Rex reads the library's **header file** (the
|
||||
`.h` file that lists what the library has) and works out the rest.
|
||||
|
||||
## Importing a header
|
||||
@@ -10,7 +10,7 @@ directly. You don't write any declarations: Dino reads the library's **header fi
|
||||
Put the header's name in quotes. Its functions, types and constants are then used with the
|
||||
header's name in front, the same way as your own files (`import utils` gives `utils.name`).
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "string.h"
|
||||
import "stdlib.h"
|
||||
|
||||
@@ -33,7 +33,7 @@ gives `SDL`, `"my-lib.h"` gives `my_lib`. To choose another one, write it before
|
||||
When you only want some things from a header, list them in braces. Those names are then
|
||||
used **without** a prefix, and nothing else from the header is visible:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "math.h" { cbrt, hypot }
|
||||
|
||||
func main() {
|
||||
@@ -44,12 +44,12 @@ func main() {
|
||||
3.0 5.0
|
||||
```
|
||||
|
||||
(`math.h` is the C math library. Dino's own `import math` is usually nicer; this is just a
|
||||
(`math.h` is the C math library. Rex's own `import math` is usually nicer; this is just a
|
||||
small example.)
|
||||
|
||||
## How C types look in Dino
|
||||
## How C types look in Rex
|
||||
|
||||
| In C | In Dino |
|
||||
| In C | In Rex |
|
||||
|------|---------|
|
||||
| `int`, `unsigned int`, `short`, ... | `i32`, `u32`, `i16`, ... |
|
||||
| `long`, `long long`, `int64_t` | `int` |
|
||||
@@ -59,28 +59,28 @@ small example.)
|
||||
| `bool` | `bool` |
|
||||
| `const char *` and `char *` | `string` |
|
||||
| an `enum` | its values are constants (`i32`) |
|
||||
| a `struct` with plain fields | a Dino struct: `raylib.Color{255, 0, 0, 255}` |
|
||||
| a `struct` with plain fields | a Rex struct: `raylib.Color{255, 0, 0, 255}` |
|
||||
| any other pointer (`FILE *`, `SDL_Window *`) | a pointer type: `stdio.FILE`, `SDL.SDL_Window` |
|
||||
| `NULL` | `nil` |
|
||||
|
||||
Numbers follow the usual Dino rule: a number written in the code fits any number type, but
|
||||
Numbers follow the usual Rex rule: a number written in the code fits any number type, but
|
||||
a variable has to be converted, for example `i32(n)` when the C function takes an `int`.
|
||||
|
||||
Text works both ways: a Dino string is handed to C as a C string, and a C string that comes
|
||||
back becomes a Dino string (a copy, so it stays valid).
|
||||
Text works both ways: a Rex string is handed to C as a C string, and a C string that comes
|
||||
back becomes a Rex string (a copy, so it stays valid).
|
||||
|
||||
## Pointers: things you only pass around
|
||||
|
||||
Many C libraries give you a pointer to something they manage, like an open file or a
|
||||
window. In Dino you keep it in a variable, hand it back to the library, and compare it with
|
||||
window. In Rex you keep it in a variable, hand it back to the library, and compare it with
|
||||
`nil` to see if it is missing:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "stdio.h"
|
||||
import os
|
||||
|
||||
func main() {
|
||||
var f = stdio.fopen("/tmp/dino-hello.txt", "w")
|
||||
var f = stdio.fopen("/tmp/rex-hello.txt", "w")
|
||||
if f == nil {
|
||||
print("can't open the file")
|
||||
return
|
||||
@@ -88,7 +88,7 @@ func main() {
|
||||
stdio.fputs("hello from C\n", f)
|
||||
stdio.fclose(f)
|
||||
|
||||
var text, err = os.read_file("/tmp/dino-hello.txt")
|
||||
var text, err = os.read_file("/tmp/rex-hello.txt")
|
||||
print(text.trim(), err)
|
||||
}
|
||||
```
|
||||
@@ -98,16 +98,16 @@ hello from C nil
|
||||
|
||||
The type can be written like any other: `var f: stdio.FILE` starts out as `nil`.
|
||||
|
||||
**Dino does not free C things for you.** Whatever the library opens or creates, close or
|
||||
**Rex does not free C things for you.** Whatever the library opens or creates, close or
|
||||
free it with the library's own function (`fclose`, `sqlite3_close`, `UnloadTexture`, ...).
|
||||
|
||||
## Functions that write into your variables
|
||||
|
||||
Some C functions give back extra results by writing into a variable you pass them (in C:
|
||||
a pointer parameter like `double *out`). Pass a Dino **variable**, and the function changes
|
||||
a pointer parameter like `double *out`). Pass a Rex **variable**, and the function changes
|
||||
it. Pass a **list** where the C function wants room for several values (a buffer):
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "math.h" { modf }
|
||||
import "string.h" { memset }
|
||||
|
||||
@@ -139,14 +139,14 @@ sqlite3.sqlite3_open("notes.db", db) // db is now open (or still nil if it fai
|
||||
|
||||
## Constants
|
||||
|
||||
Values from `#define` and `enum` work like Dino constants: `stdio.EOF`, `zlib.Z_OK`,
|
||||
Values from `#define` and `enum` work like Rex constants: `stdio.EOF`, `zlib.Z_OK`,
|
||||
`raylib.KEY_SPACE`. raylib's colours (`raylib.RED`, `raylib.RAYWHITE`) are struct constants
|
||||
of type `raylib.Color`.
|
||||
|
||||
## Linking: where the library's code is
|
||||
|
||||
The header only describes a library; its code is in a library file that has to be linked
|
||||
in. Dino asks the system (`pkg-config`) for that, so for most installed libraries
|
||||
in. Rex asks the system (`pkg-config`) for that, so for most installed libraries
|
||||
`import "raylib.h"` or `import "zlib.h"` is all you need. If a library isn't known to
|
||||
`pkg-config`, name it yourself: for the file `libfoo.so`, write
|
||||
|
||||
@@ -161,11 +161,11 @@ If it is missing, building stops with a message that says so.
|
||||
[raylib](https://www.raylib.com) is a simple library for games and graphics. With it
|
||||
installed, this opens a window and draws a circle:
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "raylib.h"
|
||||
|
||||
func main() {
|
||||
raylib.InitWindow(800, 450, "Hello from Dino")
|
||||
raylib.InitWindow(800, 450, "Hello from Rex")
|
||||
raylib.SetTargetFPS(60)
|
||||
var x: f32 = 400.0
|
||||
for !raylib.WindowShouldClose() {
|
||||
@@ -182,12 +182,12 @@ func main() {
|
||||
```
|
||||
|
||||
A bigger example with bouncing balls is in
|
||||
[`examples/c/bounce.dino`](../examples/c/bounce.dino).
|
||||
[`examples/c/bounce.rex`](../examples/c/bounce.rex).
|
||||
|
||||
## Your own C code
|
||||
|
||||
Put a header and its `.c` file next to your program. `import "shapes.h"` reads the header,
|
||||
and Dino compiles `shapes.c` and builds it into your program.
|
||||
and Rex compiles `shapes.c` and builds it into your program.
|
||||
|
||||
```c
|
||||
// shapes.h
|
||||
@@ -203,7 +203,7 @@ double area(Size s) { return s.w * s.h; }
|
||||
void grow(Size *s, double by) { s->w *= by; s->h *= by; }
|
||||
```
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "shapes.h"
|
||||
|
||||
func main() {
|
||||
@@ -218,10 +218,10 @@ func main() {
|
||||
|
||||
## C++
|
||||
|
||||
C++ code can't be called from C or Dino directly: C++ functions get scrambled ("mangled")
|
||||
C++ code can't be called from C or Rex directly: C++ functions get scrambled ("mangled")
|
||||
names when compiled, and classes and templates don't exist in C. The usual fix is a small
|
||||
set of plain functions marked `extern "C"`. Write them in a `.cpp` file next to the header,
|
||||
and Dino compiles it with your C++ compiler (`c++`) and links in the C++ library:
|
||||
and Rex compiles it with your C++ compiler (`c++`) and links in the C++ library:
|
||||
|
||||
```c
|
||||
// counter.h: plain C, also readable by C++
|
||||
@@ -252,7 +252,7 @@ void counter_free(Counter *c) { delete c; }
|
||||
}
|
||||
```
|
||||
|
||||
```dino
|
||||
```rex
|
||||
import "counter.h"
|
||||
|
||||
func main() {
|
||||
@@ -271,11 +271,11 @@ functions.
|
||||
|
||||
## Seeing what a header has
|
||||
|
||||
`dino header` lists everything Dino can use from a header, written as Dino. Add a word to
|
||||
`rex header` lists everything Rex can use from a header, written as Rex. Add a word to
|
||||
only see names that contain it:
|
||||
|
||||
```text
|
||||
dino header zlib.h compress
|
||||
rex header zlib.h compress
|
||||
```
|
||||
```text
|
||||
import "zlib.h" // use the names below as zlib.name
|
||||
@@ -286,11 +286,11 @@ func compress(dest: []u8 or u8 variable, destLen: []u64 or u64 variable, source:
|
||||
...
|
||||
```
|
||||
|
||||
In an editor with Dino support, completion after `zlib.` and hovering a name show the same.
|
||||
In an editor with Rex support, completion after `zlib.` and hovering a name show the same.
|
||||
|
||||
## What doesn't work yet
|
||||
|
||||
Dino tells you when you call one of these, and why:
|
||||
Rex tells you when you call one of these, and why:
|
||||
|
||||
- functions with a variable number of arguments, like `printf`
|
||||
- functions that take a callback (a function pointer)
|
||||
@@ -299,6 +299,6 @@ Dino tells you when you call one of these, and why:
|
||||
- C macros that take arguments (`#define MAX(a, b) ...`)
|
||||
|
||||
When you need one of these, write a small function in your own `.c` file that does the C
|
||||
part, and call that from Dino.
|
||||
part, and call that from Rex.
|
||||
|
||||
Next: back to the [overview](README.md)
|
||||
+5
-5
@@ -1,6 +1,6 @@
|
||||
# Dino: learn the language
|
||||
# Rex: learn the language
|
||||
|
||||
Dino is a small, fast, modern language. It looks like Go, runs like C, and has no
|
||||
Rex is a small, fast, modern language. It looks like Go, runs like C, and has no
|
||||
garbage collector, no pointers, and nothing low-level to write. You write plain code;
|
||||
the compiler works out the memory.
|
||||
|
||||
@@ -25,11 +25,11 @@ These notes teach it from zero. Read them in order, or jump to the
|
||||
|
||||
## How to read the examples
|
||||
|
||||
Every `dino` block that has a `func main` is a complete program you can save and run.
|
||||
Every `rex` block that has a `func main` is a complete program you can save and run.
|
||||
The block right after it (marked `text`) shows what it prints.
|
||||
|
||||
````
|
||||
```dino
|
||||
```rex
|
||||
func main() {
|
||||
print("hello")
|
||||
}
|
||||
@@ -39,7 +39,7 @@ hello
|
||||
```
|
||||
````
|
||||
|
||||
## The four ideas behind Dino
|
||||
## The four ideas behind Rex
|
||||
|
||||
1. **Values.** `var b = a` makes a real copy. Change `b` and `a` stays the same.
|
||||
There are no hidden aliases, which makes programs easy to reason about.
|
||||
|
||||
Reference in new issue
Block a user