-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加const-match语法, 可以很好的进行非take的匹配, 也方便做模式参数
- Loading branch information
Showing
14 changed files
with
876 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "mindustry_logic_bang_lang" | ||
version = "0.15.11" | ||
version = "0.16.0" | ||
edition = "2021" | ||
|
||
authors = ["A4-Tacks <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#** | ||
* 这是0.16.0添加的语法, 很像match, 但是在前面添加const | ||
* 与match不同的是, 它不会先把参数take, 而是尽量针对参数本身匹配 | ||
* 它会把每个参数进行const, 然后以其const后的依据去对指向的值进行匹配 | ||
* | ||
* 它的var-pat和match很像, 但是它本身也是惰性的, 也就是说例如 | ||
* `[A B C]` 如果A匹配上了, 那么B和C将不会被take. | ||
* 其作用是匹配目标值为Var且其值与pat相等 | ||
* | ||
* 它还有其它几个模式: | ||
* 下划线通配模式: 例如 `_`, 这会允许任何值 | ||
* | ||
* 守卫模式: 例如 `[?foo]`, 这里里面是一个 op-expr, 当然一般也用不上. | ||
* 在这里面将会有一个独立的参数环境, 拥有一个参数指向要匹配的值 | ||
* 在匹配时会将守卫take, 得到的句柄非`0`则判定通过 | ||
* | ||
* 值模式: 它会直接将对应这个位置的被匹配值take, 然后直接对句柄匹配 | ||
* 需要注意的是, 它可能在多个分支被take多次, 只是守卫模式的一个语法糖 | ||
* 其具体原理可以使用`A`编译选项查看 | ||
* | ||
* 同时, 它还有一个take标志, 在pat前面加上`*`时, | ||
* 将会在整个分支通过时采用take而非const绑定, | ||
* 例如: `*A` | ||
*# | ||
|
||
|
||
# 在以前, 我们经常使用手动的_0 _1来接受参数, 哪怕有了match也依旧如此 | ||
# 因为match会将每个参数take, 如果某个参数不需要立即take时, | ||
# 就基本整个都用不了match来方便的接受参数了 | ||
# 而const-match可以很好的解决这个问题 | ||
# 比如一个简单的For | ||
const For = (const match @ { | ||
Start Stop F { | ||
take For[Start Stop 1 F]; | ||
} | ||
*Start *Stop *Step F { | ||
take I = (); | ||
I = Start; | ||
while I < Stop { | ||
take F[I]; | ||
I += Step; | ||
} | ||
} | ||
}); | ||
|
||
take For[0 10 ( | ||
print "a: "_0; | ||
)]; | ||
|
||
take For[0 10 2 ( | ||
print "b: "_0; | ||
)]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.