Skip to content

Commit 662f181

Browse files
authored
Merge pull request #8 from xch-dev/multi-file-update
Multi file update
2 parents 93e6fdf + 75f1fa7 commit 662f181

File tree

11 files changed

+864
-38
lines changed

11 files changed

+864
-38
lines changed

docs/bindings.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,40 @@ This should be used sparingly, since it can come with either a performance hit,
4242
:::tip
4343
The compiler will automatically inline `let` bindings if they are only referenced a single time.
4444
:::
45+
46+
## Destructuring
47+
48+
You can destructure values (including `let` bindings and function parameters) into multiple bindings at once.
49+
50+
For example, if you have a pair, you can destructure the first and rest values into separate bindings:
51+
52+
```rue
53+
let (first, rest) = (100, 200);
54+
```
55+
56+
This also works for lists (as long as the values are known to not be nil):
57+
58+
```rue
59+
let [a, b, c] = [100, 200, 300];
60+
```
61+
62+
If you have a struct, you can destructure the fields:
63+
64+
```rue
65+
struct Point {
66+
x: Int,
67+
y: Int,
68+
}
69+
70+
let { x, y } = Point { x: 100, y: 200 };
71+
```
72+
73+
These destructuring patterns can be combined arbitrarily:
74+
75+
```rue
76+
let ([a, b, c], { x, y }) = ([100, 200, 300], Point { x: 400, y: 500 });
77+
```
78+
79+
:::note
80+
When you destructure a value, it's first assigned to a temporary binding, and then that binding is referenced by each of the bindings in the destructuring pattern. Thus, if you're destructuring a non-inline variable, it's best to mark the destructured bindings as `inline` to reduce the size of the compiled program.
81+
:::

docs/builtins.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ You can click on each of these types to go to their corresponding page and learn
1313
1. [**Atom**](/docs/type-system/atoms.md#atom)
1414
2. [**Bytes**](/docs/type-system/atoms.md#bytes)
1515
3. [**Bytes32**](/docs/type-system/atoms.md#bytes32)
16-
4. [**PublicKey**](/docs/type-system/atoms.md#publickey)
17-
5. [**Signature**](/docs/type-system/atoms.md#signature)
18-
6. [**K1PublicKey**](/docs/type-system/atoms.md#k1publickey)
19-
7. [**K1Signature**](/docs/type-system/atoms.md#k1signature)
20-
8. [**R1PublicKey**](/docs/type-system/atoms.md#r1publickey)
21-
9. [**R1Signature**](/docs/type-system/atoms.md#r1signature)
22-
10. [**Int**](/docs/type-system/atoms.md#int)
23-
11. [**Bool**](/docs/type-system/atoms.md#bool)
24-
12. [**Any**](/docs/type-system/pairs.md#any)
25-
13. [**List**](/docs/type-system/pairs.md#lists)
26-
14. [**AlternatingList**](/docs/type-system/pairs.md#alternating-lists)
16+
4. [**String**](/docs/type-system/atoms.md#string)
17+
5. [**PublicKey**](/docs/type-system/atoms.md#publickey)
18+
6. [**Signature**](/docs/type-system/atoms.md#signature)
19+
7. [**K1PublicKey**](/docs/type-system/atoms.md#k1publickey)
20+
8. [**K1Signature**](/docs/type-system/atoms.md#k1signature)
21+
9. [**R1PublicKey**](/docs/type-system/atoms.md#r1publickey)
22+
10. [**R1Signature**](/docs/type-system/atoms.md#r1signature)
23+
11. [**Int**](/docs/type-system/atoms.md#int)
24+
12. [**Bool**](/docs/type-system/atoms.md#bool)
25+
13. [**Any**](/docs/type-system/pairs.md#any)
26+
14. [**List**](/docs/type-system/pairs.md#lists)
27+
15. [**AlternatingList**](/docs/type-system/pairs.md#alternating-lists)
2728

2829
## Functions
2930

docs/constants.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ They are similar to [Bindings](/docs/bindings.md), which the primary difference
1111
As a simple example, you might want to reuse a string multiple times:
1212

1313
```rue
14-
const GREET: Bytes = "Hello, ";
14+
const GREET: String = "Hello, ";
1515
16-
fn main(person_a: Bytes, person_b: Bytes) -> (Bytes, Bytes) {
16+
fn main(person_a: String, person_b: String) -> (String, String) {
1717
(GREET + person_a, GREET + person_b)
1818
}
1919
```
@@ -25,9 +25,9 @@ You can mark a constant as `inline`, which forces its value to be inserted every
2525
As an example, this will behave the same as if you had written the string multiple times:
2626

2727
```rue
28-
inline const GREET: Bytes = "Hello, ";
28+
inline const GREET: String = "Hello, ";
2929
30-
fn main(person_a: Bytes, person_b: Bytes) -> (Bytes, Bytes) {
30+
fn main(person_a: String, person_b: String) -> (String, String) {
3131
(GREET + person_a, GREET + person_b)
3232
}
3333
```

docs/installation.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import TabItem from '@theme/TabItem';
77

88
# Installation
99

10-
:::warning
11-
Rue is currently in alpha. Please use it with caution and report any bugs you find in doing so.
10+
:::note
11+
Rue is still in active development. If you run into any issues, please report them in [GitHub issues](https://github.com/xch-dev/rue/issues).
1212
:::
1313

1414
First, you will need to install the [Rust toolchain](https://rustup.rs).
@@ -66,20 +66,26 @@ Any editor that supports Open VSX and VSCode extensions will likely support this
6666

6767
## Hello World
6868

69-
The simplest example of a program in Rue is the classic hello world example.
69+
The simplest program in Rue is the classic "hello world" example.
7070

71-
Write the following program in a file named `hello.rue`:
71+
You can run the following command to setup a new project:
7272

73-
```rue title="hello.rue"
74-
fn main() -> Bytes {
75-
"Hello, world!"
76-
}
73+
```bash
74+
rue init
7775
```
7876

79-
Now, run the following command to run the file:
77+
And then you can build it:
8078

8179
```bash
82-
rue build hello.rue
80+
rue build
81+
```
82+
83+
The `puzzles/main.rue` file should contain something like this:
84+
85+
```rue
86+
fn main() -> String {
87+
"Hello, world!"
88+
}
8389
```
8490

85-
The output should be `(q . "Hello, world!)"`, which when run with CLVM outputs the string on its own.
91+
And the CLVM output when compiling it is `(q . "Hello, world!)"`.

docs/modules.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Modules
2+
3+
Modules are a way to organize your code into separate files.
4+
5+
## File Structure
6+
7+
As long as you have a `main.rue` file in the root of your project, the compiler will automatically include all of the `.rue` files adjacent to it (including subdirectories) in the compilation.
8+
9+
All files are modules and can reference each other by name:
10+
11+
1. Each file includes every other adjacent file or directory in its scope.
12+
2. Each directory's module automatically exports all of its children.
13+
3. Each file has access to the parent directory's module with `super`.
14+
15+
If there is no `main.rue` file, then each file ending in `.rue` will be compiled completely independently, rather than as a single program.
16+
17+
## Paths
18+
19+
You can reference exported symbols and types from other modules by using the module name followed by the path to the symbol or type.
20+
21+
```rue
22+
fn main() -> String {
23+
utils::greet("Alice")
24+
}
25+
```
26+
27+
In this case, we are referencing the `greet` function from the `utils` module.
28+
29+
## Imports
30+
31+
Instead of referencing by path every time, you can import symbols and types from other modules into the current scope so that they can be referenced directly.
32+
33+
```rue
34+
import utils::greet;
35+
36+
fn main() -> String {
37+
greet("Alice")
38+
}
39+
```
40+
41+
You can import multiple paths:
42+
43+
```rue
44+
import utils::{greet, exclaim};
45+
46+
fn main() -> String {
47+
exclaim(greet("Alice"))
48+
}
49+
```
50+
51+
You can also import all symbols and types from a module by using the `*` wildcard:
52+
53+
```rue
54+
import utils::*;
55+
56+
fn main() -> String {
57+
exclaim(greet("Alice"))
58+
}
59+
```
60+
61+
Finally, it's possible to re-export things that you import:
62+
63+
```rue
64+
export utils::*;
65+
```
66+
67+
## Module Block
68+
69+
You can create a module by using the `mod` keyword followed by the name of the module. This is here for completeness more than anything, since most of the time you'll want to use multiple files instead.
70+
71+
```rue
72+
mod utils {
73+
export fn add(a: Int, b: Int) -> Int {
74+
a + b
75+
}
76+
}
77+
78+
fn main() -> Int {
79+
utils::add(42, 34)
80+
}
81+
```

0 commit comments

Comments
 (0)