Skip to content

Commit 93e6fdf

Browse files
authored
Merge pull request #7 from xch-dev/new-pages
Add Control Flow and Debugging sections
2 parents b1e70b6 + 5e68dcc commit 93e6fdf

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

docs/control-flow.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
slug: /control-flow
3+
---
4+
5+
# Control Flow
6+
7+
Control flow is used to change the behavior of the program based on certain conditions. This is known as [branching](<https://en.wikipedia.org/wiki/Branch_(computer_science)>).
8+
9+
## If Expression
10+
11+
You can use an `if` expression to execute different blocks of code based on a condition.
12+
13+
For example:
14+
15+
```rue
16+
let size = if number > 100 {
17+
"large"
18+
} else {
19+
"small"
20+
};
21+
```
22+
23+
It's worth noting that this will only execute the branch that matches the condition. This means that you can use a [type guard](/docs/type-system/guards.md) to narrow the type of variables after the condition is evaluated.
24+
25+
However, if you don't need to narrow the type, you can use an `inline if` expression to immediately execute both branches and return the result of the branch that matches the condition. This can be an important optimization to reduce the size of the compiled program, but it can also lead to logic errors or higher runtime costs if you're not careful.
26+
27+
The example above is a great example of when you should use an `inline if` expression:
28+
29+
```rue
30+
let size = inline if number > 100 {
31+
"large"
32+
} else {
33+
"small"
34+
};
35+
```
36+
37+
## If Statement
38+
39+
To reduce the amount of indentation, you can use an `if` statement instead of an `if` expression if you're going to exit the function early if the condition is met (i.e., an early `return`).
40+
41+
This might look like this:
42+
43+
```rue
44+
if list is nil {
45+
return 0;
46+
}
47+
48+
let (first, rest) = list;
49+
```
50+
51+
## Raise
52+
53+
You can use a `raise` statement to immediately exit the program with an error.
54+
55+
```rue
56+
raise "List is empty";
57+
```
58+
59+
This will cause the program to fail with the message "List is empty".
60+
61+
:::info
62+
In release builds, error messages will automatically be omitted to reduce the size of the compiled program.
63+
:::
64+
65+
## Assert
66+
67+
The `assert` statement is a shorthand for `raise` inside of an `if` statement:
68+
69+
```rue
70+
assert !(list is nil);
71+
```
72+
73+
It's useful for checking types at runtime and raising an error if the condition is not met.
74+
75+
## Return
76+
77+
You can use a `return` statement to exit the current function early and return a value.
78+
79+
```rue
80+
return 0;
81+
```

docs/debugging.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
slug: /debugging
3+
---
4+
5+
# Debugging
6+
7+
:::note
8+
Rue's developer tools are still a work in progress, and important functionality may be missing in the meantime.
9+
:::
10+
11+
## Debug Print
12+
13+
You can use `debug` statements to print messages to the console:
14+
15+
```rue
16+
debug "Hello, world!";
17+
```
18+
19+
If you compile this with `rue build`, it will be omitted from the compiled program (since printing isn't supported in CLVM natively). However, if you compile this with `rue build -d`, special debug symbols will be included that will print the message (as an untyped CLVM value) to the console at runtime.
20+
21+
This modified version of CLVM can be executed with the `rue debug` command, with `rue test`, or with the [Wallet SDK](https://github.com/xch-dev/chia-wallet-sdk) simulator.

sidebars.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const sidebars: SidebarsConfig = {
44
docs: [
55
"installation",
66
"functions",
7+
"control-flow",
78
"bindings",
89
"constants",
910
{
@@ -20,6 +21,7 @@ const sidebars: SidebarsConfig = {
2021
},
2122
"operators",
2223
"builtins",
24+
"debugging",
2325
"security",
2426
],
2527
};

src/theme/prism-rue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Prism.languages.rue = {
2929
/[+\-*?%!\^~]|<[<=]?|>[>=]?|=[=>]?|!=?|\.(?:\.\.)?|::|->?|&&?|\|\|?/,
3030
punctuation: /[(){}[\],:]/,
3131
"control-flow": {
32-
pattern: /\b(?:if|else|return|raise|assert)\b/,
32+
pattern: /\b(?:if|else|return|raise|assert|debug)\b/,
3333
alias: "keyword",
3434
},
3535
binding: {

0 commit comments

Comments
 (0)