Skip to content

Commit

Permalink
Merge pull request #293 from tonyfettes/migrate
Browse files Browse the repository at this point in the history
Migrate to new API, syntax, etc.
  • Loading branch information
tonyfettes authored Sep 2, 2024
2 parents 2e619c4 + 87ea9c2 commit 65591cb
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion legacy/examples/avl_tree/lib/avl.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn to_string[U : Show](self : T[U]) -> String {
match self {
Empty => "()"
Node(Empty, v, Empty, _) => v.to_string()
Node(l, v, r, _) => "(\(l), \(v), \(r))"
Node(l, v, r, _) => "(\{l}, \{v}, \{r})"
}
}

Expand Down
2 changes: 1 addition & 1 deletion legacy/examples/avl_tree/main/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main {
v = v.add(i)
}
let height = v.height()
println("height of the tree: \(height)")
println("height of the tree: \{height}")
v.print_tree()

// Check values from 0 to iter-1 in the AVL tree
Expand Down
20 changes: 10 additions & 10 deletions legacy/examples/char/lib/char_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// limitations under the License.

test {
@test.is_true!(@lib.is_alpha('a'))
@test.is_false!(@lib.is_alpha('3'))
@test.is_true!(@lib.is_numeric('1'))
@test.is_false!(@lib.is_numeric('b'))
@test.is_true!(@lib.is_alphanumeric('a'))
@test.eq!(@lib.to_lower('H'), 'h')
@test.eq!(@lib.to_upper('c'), 'C')
@test.is_true!(@lib.is_whitespace(' '))
@test.eq!(@lib.next('Z'), Some('['))
@test.eq!(@lib.prev('A'), Some('@'))
assert_true!(@lib.is_alpha('a'))
assert_false!(@lib.is_alpha('3'))
assert_true!(@lib.is_numeric('1'))
assert_false!(@lib.is_numeric('b'))
assert_true!(@lib.is_alphanumeric('a'))
assert_eq!(@lib.to_lower('H'), 'h')
assert_eq!(@lib.to_upper('c'), 'C')
assert_true!(@lib.is_whitespace(' '))
assert_eq!(@lib.next('Z'), Some('['))
assert_eq!(@lib.prev('A'), Some('@'))
}
6 changes: 3 additions & 3 deletions legacy/examples/palindrome_string/lib/str_iter_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test {
let test1 = "abc"
let test2 = "aba"
let test3 = "上海自来水来自海上"
test1 |> @lib.is_palindrome |> @test.is_false!()
test2 |> @lib.is_palindrome |> @test.is_true!()
test3 |> @lib.is_palindrome |> @test.is_true!()
test1 |> @lib.is_palindrome |> assert_false!()
test2 |> @lib.is_palindrome |> assert_true!()
test3 |> @lib.is_palindrome |> assert_true!()
}
2 changes: 1 addition & 1 deletion legacy/examples/snake/lib/snake.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn go_step(self : GameState) -> Unit {
self.setGridType(newHead, Body)
self.body.push_front(newHead)
self.setGridType(self.body.back().unwrap(), None)
self.body.pop_back_exn()
self.body.unsafe_pop_back()
}
}

Expand Down
14 changes: 7 additions & 7 deletions legacy/examples/string/lib/string_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

test {
let arr = @lib.split("a.b.c.e.f.g.h.i.j.k.l.m.n", '.')
@test.eq!(
assert_eq!(
arr,
["a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"],
)
Expand All @@ -24,11 +24,11 @@ test {
#|Some("lo W")
,
)
"Hello World" |> @lib.index_of("Wo") |> @test.eq!(Some(6))
"Hello World World Hello" |> @lib.last_index_of("Wo") |> @test.eq!(Some(12))
@test.is_true!(@lib.contains("Hello World", "Wo"))
"Hello World" |> @lib.index_of("Wo") |> assert_eq!(Some(6))
"Hello World World Hello" |> @lib.last_index_of("Wo") |> assert_eq!(Some(12))
assert_true!(@lib.contains("Hello World", "Wo"))
let arr = @lib.to_char_array("HelloWorld")
@test.eq!(arr, ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])
@test.eq!(@lib.char_at("abcde", 4), Some('e'))
@test.eq!(@lib.trim(" abcde ", ' '), "abcde")
assert_eq!(arr, ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'])
assert_eq!(@lib.char_at("abcde", 4), Some('e'))
assert_eq!(@lib.trim(" abcde ", ' '), "abcde")
}
2 changes: 1 addition & 1 deletion legacy/examples/tetris/lib/tetris.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn on_piece_collision(self : Tetris) -> Unit {
)

// Delete the complete row
let (new_grid, row_completed) = self.grid.fold_right(
let (new_grid, row_completed) = self.grid.rev_fold(
init=(@immut/list.Nil, 0),
fn {
v, (new_grid, row_completed) =>
Expand Down
6 changes: 3 additions & 3 deletions moonbit-docs/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2026,12 +2026,12 @@ MoonBit provides the test code block for writing test cases. For example:

```moonbit
test "test_name" {
@test.eq!(1 + 1, 2)
@test.eq!(2 + 2, 4)
assert_eq!(1 + 1, 2)
assert_eq!(2 + 2, 4)
}
```

A test code block is essentially a function that returns a `Unit` but may throws a `String` on error, or `Unit!String` as one would see in its signature at the position of return type. It is called during the execution of `moon test` and outputs a test report through the build system. The `@test.eq` function is from the standard library; if the assertion fails, it prints an error message and terminates the test. The string `"test_name"` is used to identify the test case and is optional. If it starts with `"panic"`, it indicates that the expected behavior of the test is to trigger a panic, and the test will only pass if the panic is triggered. For example:
A test code block is essentially a function that returns a `Unit` but may throws a `String` on error, or `Unit!String` as one would see in its signature at the position of return type. It is called during the execution of `moon test` and outputs a test report through the build system. The `assert_eq` function is from the standard library; if the assertion fails, it prints an error message and terminates the test. The string `"test_name"` is used to identify the test case and is optional. If it starts with `"panic"`, it indicates that the expected behavior of the test is to trigger a panic, and the test will only pass if the panic is triggered. For example:

```moonbit
test "panic_test" {
Expand Down
10 changes: 5 additions & 5 deletions moonbit-docs/docs/examples/gmachine-1/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ The `PushGlobal` instruction retrieves the address of the specified super combin
fn pushglobal(self : GState, name : String) {
let sc = self.globals[name]
match sc {
None => abort("pushglobal(): cant find super combinator \(name)")
None => abort("pushglobal(): cant find super combinator \{name}")
Some(addr) => {
self.putStack(addr)
}
Expand All @@ -462,7 +462,7 @@ fn pusharg(self : GState, offset : Int) {
let appaddr = nth(self.stack, offset + 1)
let arg = match self.heap[appaddr] {
NApp(_, arg) => arg
otherwise => abort("pusharg: stack offset \(offset) address \(appaddr) node \(otherwise), not a applicative node")
otherwise => abort("pusharg: stack offset \{offset} address \{appaddr} node \{otherwise}, not a applicative node")
}
self.putStack(arg)
}
Expand Down Expand Up @@ -517,7 +517,7 @@ fn unwind(self : GState) {
self.putStack(a)
self.putCode(Cons(Unwind, Nil))
}
otherwise => abort("unwind() : wrong kind of node \(otherwise), address \(addr)")
otherwise => abort("unwind() : wrong kind of node \{otherwise}, address \{addr}")
}
}
```
Expand Down Expand Up @@ -651,9 +651,9 @@ fn reify(self : GState) {
match stack {
Cons(addr, Nil) => {
let res = self.heap[addr]
println("\(res)")
println("\{res}")
}
_ => abort("wrong stack \(stack)")
_ => abort("wrong stack \{stack}")
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions moonbit-docs/docs/examples/gmachine-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn liftArith2(self : GState, op : (Int, Int) -> Int) -> Unit {
let addr = self.heap.alloc(newnode)
self.putStack(addr)
}
(node1, node2) => abort("liftArith2: \(a1) = \(node1) \(a2) = \(node2)")
(node1, node2) => abort("liftArith2: \{a1} = \{node1} \{a2} = \{node2}")
}
}

Expand All @@ -260,7 +260,7 @@ fn liftCmp2(self : GState, op : (Int, Int) -> Bool) -> Unit {
let addr = self.heap.alloc(newnode)
self.putStack(addr)
}
(node1, node2) => abort("liftCmp2: \(a1) = \(node1) \(a2) = \(node2)")
(node1, node2) => abort("liftCmp2: \{a1} = \{node1} \{a2} = \{node2}")
}
}

Expand All @@ -274,7 +274,7 @@ fn negate(self : GState) -> Unit {
}
otherwise => {
// If not NNum, throw an error
abort("negate: wrong kind of node \(otherwise), address \(addr) ")
abort("negate: wrong kind of node \{otherwise}, address \{addr} ")
}
}
}
Expand All @@ -294,7 +294,7 @@ fn condition(self : GState, i1 : List[Instruction], i2 : List[Instruction]) -> U
// If true, jump to i1
self.code = append(i1, self.code)
}
otherwise => abort("cond : \(addr) = \(otherwise)")
otherwise => abort("cond : \{addr} = \{otherwise}")
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion moonbit-docs/docs/examples/gmachine-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn casejump(self : GState, table : List[(Int, List[Instruction])]) -> Unit {
}
}
}
otherwise => abort("casejump(): addr = \(addr) node = \(otherwise)")
otherwise => abort("casejump(): addr = \{addr} node = \{otherwise}")
}
}
```
Expand Down
8 changes: 4 additions & 4 deletions moonbit-docs/docs/examples/lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn subst(self : Term, var : String, term : Term) -> Term {
Abs(variable, body) => {
if freeVars.contains(variable) {
// The variable name is in the set of free variables of the current inner Lambda, indicating variable capture
abort("subst(): error while encountering \(variable)")
abort("subst(): error while encountering \{variable}")
} else {
......
}
Expand Down Expand Up @@ -114,8 +114,8 @@ However, directly writing and reading Lambda terms in de Bruijn index form is pa
fn to_string(self : TermDBI) -> String {
match self {
Var(name, _) => name
Abs(name, body) => "(\\\(name).\(body))"
App(t1, t2) => "\(t1) \(t2)"
Abs(name, body) => "(\\\{name}.\{body})"
App(t1, t2) => "\{t1} \{t2}"
}
}
```
Expand Down Expand Up @@ -214,7 +214,7 @@ fn eval(self : TermDBI) -> TermDBI {
}
}
Abs(_) => self
otherwise => abort("eval(): \(otherwise) ")
otherwise => abort("eval(): \{otherwise} ")
// eval should not encounter free variables
}
}
Expand Down
6 changes: 3 additions & 3 deletions moonbit-docs/docs/examples/myers-diff2.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@ fn print_edit(edit : Edit) -> String {
let old_line = pad_right("", line_width)
let new_line = pad_right(edit.new.number.to_string(), line_width)
let text = edit.new.text
"\(tag) \(old_line) \(new_line) \(text)"
"\{tag} \{old_line} \{new_line} \{text}"
}
Delete(_) as edit => {
let tag = "-"
let old_line = pad_right(edit.old.number.to_string(), line_width)
let new_line = pad_right("", line_width)
let text = edit.old.text
"\(tag) \(old_line) \(new_line) \(text)"
"\{tag} \{old_line} \{new_line} \{text}"
}
Equal(_) as edit => {
let tag = " "
let old_line = pad_right(edit.old.number.to_string(), line_width)
let new_line = pad_right(edit.new.number.to_string(), line_width)
let text = edit.old.text
"\(tag) \(old_line) \(new_line) \(text)"
"\{tag} \{old_line} \{new_line} \{text}"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions moonbit-docs/docs/examples/myers-diff3.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ fn find_path(box : Box, a : Array[Line], b : Array[Line]) -> Iter[(Int, Int)]? {
let end = snake.end;
let headbox = Box { left: box.left, top: box.top, right: start.0, bottom: start.1 };
let tailbox = Box { left: end.0, top: end.1, right: box.right, bottom: box.bottom };
// println("snake = \(snake)")
// println("headbox = \(headbox)")
// println("tailbox = \(tailbox)")
// println("snake = \{snake}")
// println("headbox = \{headbox}")
// println("tailbox = \{tailbox}")
let head = find_path(headbox, a, b).or(Iter::singleton(start));
let tail = find_path(tailbox, a, b).or(Iter::singleton(end));
Some(head.concat(tail))
Expand Down
2 changes: 1 addition & 1 deletion moonbit-docs/docs/examples/sudoku/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn square_to_int(s : String) -> Int {
let col = s[1].to_int() - 49 // '1' <=> 0
return row * 9 + col
} else {
abort("square_to_int(): \(s) is not a square")
abort("square_to_int(): \{s} is not a square")
}
}

Expand Down
2 changes: 1 addition & 1 deletion moonbit-docs/docs/ffi-and-wasm-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn draw(self : Canvas_ctx) -> Unit {
// We also demonstrate the `println` functionality here
pub fn display_pi() -> Unit {
println("PI: \(pi)")
println("PI: \{pi}")
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2004,12 +2004,12 @@ MoonBit 提供了 `test` 代码块,用于编写测试用例,比如

```moonbit
test "test_name" {
@test.eq!(1 + 1, 2)
@test.eq!(2 + 2, 4)
assert_eq!(1 + 1, 2)
assert_eq!(2 + 2, 4)
}
```

`test` 代码块实际上是一个返回 `Unit` ,但是可能抛出 `String` 类型错误的函数(函数签名中记为 `Unit!String` )。它会在执行 `moon test` 的过程中被调用,并通过构建系统输出测试报告。其中 `@test.eq` 是一个标准库中的函数,如果断言失败,会打印错误信息并终止测试。字符串 `"test_name"` 用于标识测试用例,是可选项,当其以 `"panic"` 开头时,表示该测试的期望行为是触发 panic,只有在 panic 被触发的情况下才能通过测试,比如:
`test` 代码块实际上是一个返回 `Unit` ,但是可能抛出 `String` 类型错误的函数(函数签名中记为 `Unit!String` )。它会在执行 `moon test` 的过程中被调用,并通过构建系统输出测试报告。其中 `assert_eq` 是一个标准库中的函数,如果断言失败,会打印错误信息并终止测试。字符串 `"test_name"` 用于标识测试用例,是可选项,当其以 `"panic"` 开头时,表示该测试的期望行为是触发 panic,只有在 panic 被触发的情况下才能通过测试,比如:

```moonbit
test "panic_test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ PushGlobal指令从全局表中找到指定超组合子的地址,然后将地
fn pushglobal(self : GState, name : String) {
let sc = self.globals[name]
match sc {
None => abort("pushglobal(): cant find supercombinator \(name)")
None => abort("pushglobal(): cant find supercombinator \{name}")
Some(addr) => {
self.putStack(addr)
}
Expand All @@ -467,7 +467,7 @@ fn pusharg(self : GState, offset : Int) {
let appaddr = nth(self.stack, offset + 1)
let arg = match self.heap[appaddr] {
NApp(_, arg) => arg
otherwise => abort("pusharg: stack offset \(offset) address \(appaddr) node \(otherwise), not a applicative node")
otherwise => abort("pusharg: stack offset \{offset} address \{appaddr} node \{otherwise}, not a applicative node")
}
self.putStack(arg)
}
Expand Down Expand Up @@ -522,7 +522,7 @@ fn unwind(self : GState) {
self.putStack(a)
self.putCode(Cons(Unwind, Nil))
}
otherwise => abort("unwind() : wrong kind of node \(otherwise), address \(addr)")
otherwise => abort("unwind() : wrong kind of node \{otherwise}, address \{addr}")
}
}
```
Expand Down Expand Up @@ -660,9 +660,9 @@ fn reify(self : GState) {
match stack {
Cons(addr, Nil) => {
let res = self.heap[addr]
println("\(res)")
println("\{res}")
}
_ => abort("wrong stack \(stack)")
_ => abort("wrong stack \{stack}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ fn liftArith2(self : GState, op : (Int, Int) -> Int) -> Unit {
let addr = self.heap.alloc(newnode)
self.putStack(addr)
}
(node1, node2) => abort("liftArith2: \(a1) = \(node1) \(a2) = \(node2)")
(node1, node2) => abort("liftArith2: \{a1} = \{node1} \{a2} = \{node2}")
}
}

Expand All @@ -268,7 +268,7 @@ fn liftCmp2(self : GState, op : (Int, Int) -> Bool) -> Unit {
let addr = self.heap.alloc(newnode)
self.putStack(addr)
}
(node1, node2) => abort("liftCmp2: \(a1) = \(node1) \(a2) = \(node2)")
(node1, node2) => abort("liftCmp2: \{a1} = \{node1} \{a2} = \{node2}")
}
}

Expand All @@ -282,7 +282,7 @@ fn negate(self : GState) -> Unit {
}
otherwise => {
// 不是NNum 直接报错
abort("negate: wrong kind of node \(otherwise), address \(addr) ")
abort("negate: wrong kind of node \{otherwise}, address \{addr} ")
}
}
}
Expand All @@ -303,7 +303,7 @@ fn condition(self : GState, i1 : List[Instruction], i2 : List[Instruction]) -> U
// true, 跳转i1
self.code = append(i1, self.code)
}
otherwise => abort("cond : \(addr) = \(otherwise)")
otherwise => abort("cond : \{addr} = \{otherwise}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn casejump(self : GState, table : List[(Int, List[Instruction])]) -> Unit {
}
}
}
otherwise => abort("casejump(): addr = \(addr) node = \(otherwise)")
otherwise => abort("casejump(): addr = \{addr} node = \{otherwise}")
}
}
```
Expand Down
Loading

0 comments on commit 65591cb

Please sign in to comment.