Dino compiler: language, formatter, language server, docs, editor support
Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e2779f447
commit
eaf9567a36
117 files changed
+19970
No files matched your search
@@ -0,0 +1,140 @@
|
||||
# 3. Control flow
|
||||
|
||||
## if / else
|
||||
|
||||
No parentheses around the condition. The braces are required. The condition must be a `bool`.
|
||||
|
||||
```dino
|
||||
func main() {
|
||||
var temp = 21
|
||||
if temp > 30 {
|
||||
print("hot")
|
||||
} else if temp > 15 {
|
||||
print("nice")
|
||||
} else {
|
||||
print("cold")
|
||||
}
|
||||
}
|
||||
```
|
||||
```text
|
||||
nice
|
||||
```
|
||||
|
||||
Combine conditions with `&&`, `||` and `!`. The right side only runs when needed.
|
||||
|
||||
## for: the only loop
|
||||
|
||||
There is one keyword for every kind of loop.
|
||||
|
||||
```dino
|
||||
func main() {
|
||||
for i in 0..3 { // 0, 1, 2: the end is not included
|
||||
print("i =", i)
|
||||
}
|
||||
for i in 0..10 step 4 { // 0, 4, 8
|
||||
print(i)
|
||||
}
|
||||
|
||||
var names = ["ann", "bo"]
|
||||
for n in names { // each item
|
||||
print(n)
|
||||
}
|
||||
for i, n in names { // index and item
|
||||
print(i, n)
|
||||
}
|
||||
|
||||
var k = 0
|
||||
for k < 3 { // while a condition holds
|
||||
k += 1
|
||||
}
|
||||
print(k)
|
||||
|
||||
for { // forever, until break
|
||||
k += 1
|
||||
if k > 5 { break }
|
||||
if k == 4 { continue }
|
||||
}
|
||||
print(k)
|
||||
}
|
||||
```
|
||||
```text
|
||||
i = 0
|
||||
i = 1
|
||||
i = 2
|
||||
0
|
||||
4
|
||||
8
|
||||
ann
|
||||
bo
|
||||
0 ann
|
||||
1 bo
|
||||
3
|
||||
6
|
||||
```
|
||||
|
||||
`break` leaves the loop; `continue` jumps to the next round. There is no C-style
|
||||
`for (i = 0; i < n; i++)`.
|
||||
|
||||
Looping over a list walks it as it was when the loop started, so changing the list inside
|
||||
the loop is safe.
|
||||
|
||||
## match
|
||||
|
||||
`match` picks a branch by shape. It must cover every possibility, otherwise the compiler
|
||||
lists what is missing. `_` means "anything else".
|
||||
|
||||
```dino
|
||||
enum Shape {
|
||||
Circle(r: float)
|
||||
Rect(w, h: float)
|
||||
Point
|
||||
}
|
||||
|
||||
func area(s: Shape): float {
|
||||
return match s {
|
||||
Circle(r) => 3.0 * r * r
|
||||
Rect(w, h) => w * h
|
||||
Point => 0.0
|
||||
}
|
||||
}
|
||||
|
||||
func describe(n: int): string {
|
||||
return match n {
|
||||
0 => "zero"
|
||||
1 => "one"
|
||||
_ => "many"
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
print(area(Shape.Circle(2.0)), area(Shape.Rect(2.0, 3.0)), area(Shape.Point))
|
||||
print(describe(0), describe(1), describe(7))
|
||||
}
|
||||
```
|
||||
```text
|
||||
12.0 6.0 0.0
|
||||
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
|
||||
func main() {
|
||||
var cmd = "stop"
|
||||
match cmd {
|
||||
"go" => print("going")
|
||||
"stop" => {
|
||||
print("stopping")
|
||||
print("done")
|
||||
}
|
||||
_ => print("unknown")
|
||||
}
|
||||
}
|
||||
```
|
||||
```text
|
||||
stopping
|
||||
done
|
||||
```
|
||||
|
||||
Next: [Functions](04-functions.md)
|
||||
Reference in new issue
Block a user