用月兔语言编写猜数字——出师未捷.mbt
类似之前 用仓颉试写猜数字例程,尤其想看看反馈方面如报错等的表现。
刚安装vsc插件,弹窗报错5分钟内崩溃三次,再看还有个弹窗说需要安装月兔工具链。按 官网 在vsc内安装后,仍弹窗报错5分钟内崩溃三次,暂时不管。
输入 mbt
可以插入代码块,看似插件起效了。
let a = 1
let b = 2
println(a)
点 Fmt, 报”Client is not running”
在终端运行 % source ~/.zshrc
后,可运行 moon 命令,再重启插件,点 Fmt 似乎不报错了,但也没看到格式化代码的效果。
% moon test
error: could not find a moon.mod.json file in the source directory or its ancestors
插入测试代码块:
test {
}
空代码块点 Test:
* 正在执行任务: moon test '2025-06-13-用月兔语言编写猜数字.mbt.md' -i 0
zsh:1: command not found: moon
* 终端进程“/bin/zsh '-l', '-c', 'moon test '2025-06-13-用月兔语言编写猜数字.mbt.md' -i 0'”启动失败(退出代码: 127)。
* 终端将被任务重用,按任意键关闭。
加了两行:
test {
let a = 1
inspect(a, content="1")
}
inspect有代码补全,还是无效。
感觉是工具链路径问题,于是重启vsc,这下mbt test代码块的 Test 有效了:
* 正在执行任务: moon test '2025-06-13-用月兔语言编写猜数字.mbt.md' -i 0
failed: moonc build-package -error-format json /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/__generated_driver_for_blackbox_test.mbt -o /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/single.blackbox_test.core -pkg moon/test/single_blackbox_test -is-main -std-path /Users/xuanwu/.moon/lib/core/target/wasm-gc/release/bundle -i /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/single.mi:single -pkg-sources moon/test/single_blackbox_test:/Users/xuanwu/work/文章/team_website/_posts -target wasm-gc -g -O0 -source-map -blackbox-test -no-mi -patch-file /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/__moonbit_internal_md_test.json -test-mode
Warning: [0002]
╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:8:5]
│
8 │ let a = 1
│ ┬
│ ╰── Warning: Unused toplevel variable 'a'. Note if the body contains side effect, it will not happen. Use `fn init { .. }` to wrap the effect.
───╯
Warning: [0019]
╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:9:2]
│
9 │ let b = 2
│ ────┬────
│ ╰────── Warning: Toplevel declaration is not left aligned.
───╯
Warning: [0002]
╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:9:6]
│
9 │ let b = 2
│ ┬
│ ╰── Warning: Unused toplevel variable 'b'. Note if the body contains side effect, it will not happen. Use `fn init { .. }` to wrap the effect.
───╯
Warning: [0019]
╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:10:3]
│
10 │ println(a)
│ ─────┬────
│ ╰────── Warning: Toplevel declaration is not left aligned.
────╯
Error: [3002]
╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:10:3]
│
10 │ println(a)
│ ───┬───
│ ╰───── Parse error, unexpected token `identifier`, you may expect `pub` or `priv` or `type` or `suberror` or `typealias` or `async` or `fn` or `fnalias` or `struct` or `enum` or `let` or `const` or `extern` or `test` or `impl` or `trait` or `traitalias`.
────╯
error: failed when testing
报警里有缩进未对齐信息,但是代码块的 Fmt 还是无效。
于是手动改成对齐:
let a = 1
let b = 2
println(a)
点 Test 结果报错:
Error: [4051]
╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:102:5]
│
102 │ let b = 2
│ ┬
│ ╰── The toplevel identifier b is declared twice: it was previously defined at /Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:9:6.
看起来,两个代码块的同名变量会被认为是重复定义。这在开发时也许合理,但技术文档常会有这种情况。不知如何取消。更重要的是还不知如何单独运行某个代码块。
其他日志
* 正在执行任务: curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s '0.1.20250612+600461c34'
Downloading moonbit ...
######################################################################## 100.0%
Downloading core ...
######################################################################## 100.0%
Bundling core ...
Finished. moon: ran 57 tasks, now up to date
Finished. moon: ran 57 tasks, now up to date
Finished. moon: ran 57 tasks, now up to date
Finished. moon: ran 57 tasks, now up to date
moonbit was installed successfully to ~/.moon
To verify the downloaded binaries, check https://www.moonbitlang.com/download#verifying-binaries for instructions.
To know how to add shell completions, run 'moon shell-completion --help'
Added "~/.moon/bin" to $PATH in "~/.zshrc"
To get started, run:
source ~/.zshrc
moon help
* 按任意键关闭终端。