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
+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)
|
||||
Reference in new issue
Block a user