Skip to content

Commit

Permalink
add loop label
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh committed Dec 25, 2024
1 parent eceaaa4 commit 37f9cc1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
11 changes: 11 additions & 0 deletions next/language/fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,17 @@ A functional loop consumes arguments and returns a value. It is defined using th
Currently in `loop exprs { ... }`, `exprs` is nonempty list, while `for { ... }` is accepted for infinite loop.
```

### Labelled Continue/Break

When a loop is labelled, it can be referenced from a `break` or `continue` from
within a nested loop. For example:

```{literalinclude} /sources/language/src/controls/top.mbt
:language: moonbit
:start-after: start loop label
:end-before: end loop label
```

## Iterator

An iterator is an object that traverse through a sequence while providing access
Expand Down
48 changes: 46 additions & 2 deletions next/sources/language/src/controls/top.mbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
///|
fn a() -> Int {
let x = 1
let y = 1
Expand All @@ -16,6 +17,7 @@ fn a() -> Int {
// end conditional expressions 1
}

///|
fn c() -> Unit {
let size = 0
// start conditional expressions 3
Expand Down Expand Up @@ -108,6 +110,7 @@ test "for loop 1" (t : @test.T) {
t.snapshot!(filename="for_loop_1")
}

///|
fn d() -> Unit {
// start for loop 2
for i = 0, j = 0; i + j < 100; i = i + 1, j = j + 1 {
Expand All @@ -116,6 +119,7 @@ fn d() -> Unit {
// end for loop 2
}

///|
fn infinite_loop() -> Unit {
// start for loop 3
for i = 1; ; i = i + 1 {
Expand Down Expand Up @@ -143,6 +147,7 @@ test "for loop 4" (t : @test.T) {
t.snapshot!(filename="for_loop_4")
}

///|
fn e() -> Unit {
// start for loop 5
for x in [1, 2, 3] {
Expand Down Expand Up @@ -211,7 +216,6 @@ test {
i += j
}
assert_eq!(i, 45)

let mut k = 0
for l in 0..=10 {
k += l
Expand All @@ -220,7 +224,42 @@ test {
}
// end for loop 10

// start loop label
test "break label" {
let mut count = 0
let xs = [1, 2, 3]
let ys = [4, 5, 6]
let res = outer~: for i in xs {
for j in ys {
count = count + i
break outer~ j
}
} else {
-1
}
assert_eq!(res, 4)
assert_eq!(count, 1)
}

test "continue label" {
let mut count = 0
let init = 10
let res =outer~: loop init {
0 => 42
i => {
for {
count = count + 1
continue outer~ i - 1
}
}
}
assert_eq!(res, 42)
assert_eq!(count, 10)
}
// end loop label

// start guard 1
///|
fn guarded_get(array : Array[Int], index : Int) -> Int? {
guard index >= 0 && index < array.length() else { None }
Some(array[index])
Expand All @@ -231,17 +270,20 @@ test {
}
// end guard 1

///|
fn process(string : String) -> String {
string
}

// start guard 2
///|
enum Resource {
Folder(Array[String])
PlainText(String)
JsonConfig(Json)
}

///|
fn getProcessedText(
resources : Map[String, Resource],
path : String
Expand All @@ -255,6 +297,7 @@ fn getProcessedText(
}
// end guard 2

///|
fn g() -> Unit {
let condition = true
let expr = Some(5)
Expand All @@ -266,6 +309,7 @@ fn g() -> Unit {
}

// start match 1
///|
fn decide_sport(weather : String, humidity : Int) -> String {
match weather {
"sunny" => "tennis"
Expand All @@ -277,4 +321,4 @@ fn decide_sport(weather : String, humidity : Int) -> String {
test {
assert_eq!(decide_sport("sunny", 0), "tennis")
}
// end match 1
// end match 1

0 comments on commit 37f9cc1

Please sign in to comment.