MoonBit 编译器(留档学习)
MoonBit 编译器
MoonBit 是一个用户友好,构建快,产出质量高的编程语言。
MoonBit | Documentation | Tour | Core
This is the source code repository for MoonBit, a programming language that is user-friendly, builds fast, and produces high quality output.
安装
先安装依赖:
sudo apt install ocaml
安装opam
bash -c "sh <(curl -fsSL https://opam.ocaml.org/install.sh)"
如果这个下载不下来,可以使用apt安装:
sudo apt install opam
初始化opam
如果是第一次用opam,那就就要先初始化:
opam init
提示完成初始化
<><> Creating initial switch 'default' (invariant ["ocaml" {>= "4.05.0"}] - initially with ocaml-system)
<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><><><>
Switch invariant: ["ocaml" {>= "4.05.0"}]
<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
∗ installed base-bigarray.base
∗ installed base-threads.base
∗ installed base-unix.base
∗ installed host-arch-x86_64.1
∗ installed host-system-other.1
⬇ retrieved ocaml-system.4.14.1 (https://opam.ocaml.org/cache)
⬇ retrieved ocaml-config.2 (2 extra sources)
∗ installed ocaml-system.4.14.1
∗ installed ocaml-config.2
∗ installed ocaml.4.14.1
Done.
# Run eval $(opam env --switch=default) to update the current shell environment
安装
sudo apt install ocaml-dune
下载源代码
git clone https://github.com/moonbitlang/moonbit-compiler
编译安装
进入源代码目录moonbit-compiler,安装:
opam switch create 4.14.2
opam install -y dune
dune build -p moonbit-lang
opam的知识
Using opam
This document starts with a quick introduction, then covers most commonly-used opam features.
If you are a developer and want to get a project packaged or change an existing package, see the step-by-step packaging guide.
The full documentation is available inline, using
opam --help
opam <command> --help
This document is intended as a quicker overview, use the above to dig into the details.
Basics
# ** Get started **
opam init # Initialize ~/.opam
# ** Lookup **
opam list -a # List all available packages
opam search QUERY # List packages with QUERY in their name or description
opam show PACKAGE # Display information about PACKAGE
# ** Install **
opam install PACKAGE # Download, build and install the latest version of PACKAGE
# and all its dependencies
opam remove PACKAGE # Uninstall the given package
# ** Upgrade **
opam update # Update the packages database
opam upgrade # Bring everything to the latest version possible
# ** More **
opam CMD --help # Command-specific manpage
MoonBit
MoonBit 新手之旅 | MoonBit Docs
构建系统教程 - Moon 手册
开始编码
本文中我们会把以下所有的代码都写在 main.mbt
中。main
函数是程序的入口点,并且只能出现在 main
包中。 更细致的介绍可以在我们的《MoonBit 构建系统配置》一文中找到。
变量
let e = 2.718281828459045 // double
let int_min = -2147483648 // int
let int_max : Int = 2147483647 // 显式类型标注
let tuple = (1, 2) // 2-tuple
fn init {
let array = [1, 2, 3, 4, 5]
// array = [4, 5, 6, 7, 8] // 错误:`let` 创建的是不可变绑定。
let mut mut_array = [1, 2, 3, 4, 5]
mut_array = [4, 5, 6, 7, 8]
println(mut_array)
}
调试
安装opam下载文件报错curl -fsSL https://opam.ocaml.org/install.sh
直接使用wget下载,也报错
wget https://opam.ocaml.org/install.sh
--2024-12-22 19:33:18-- https://opam.ocaml.org/install.sh
Resolving opam.ocaml.org (opam.ocaml.org)... 51.158.232.133, 151.115.76.159, 2001:bc8:5090:bf9:dc00:ff:fe2e:c1e9
Connecting to opam.ocaml.org (opam.ocaml.org)|51.158.232.133|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh [following]
--2024-12-22 19:33:19-- https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 0.0.0.0, ::
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|0.0.0.0|:443... failed: Connection refused.
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|::|:443... failed: Connection refused.
使用apt安装
opam build moonbit报错
dune build -p moonbit-lang
Command 'dune' not found, but can be installed with:
sudo apt install ocaml-dune
安装ocaml-dune
sudo apt install ocaml-dune
dune build -p moonbit-lang 报错
dune build -p moonbit-lang
Error: I don't know about package moonbit-lang (passed through
--only-packages)
原来是要在moonbit-lang的目录下执行:
dune build -p moonbit-lang