Skip to content

Commit

Permalink
添加op-expr的三元表达式系统
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Oct 3, 2023
1 parent a447a56 commit f6a24b6
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.11.7"
version = "0.12.0"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
118 changes: 91 additions & 27 deletions examples/op_expr.mdtlbl
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,46 @@
* 在sets左值为单个Value时, 右值将成为一个op-expr
*
* 以下为各种运算符的优先级与结合性:
* | 符号 | 实际运算(op) | 优先级 | 结合性 |
* | --------- | ------------ | ------ | ------ |
* | `a ** b` | `a ** b` | -1 | RL |
* | `! x` | `x != false` | -2 | R |
* | `- x` | `0 - x` | -2 | R |
* | `~ x` | `~ x` | -2 | R |
* | `a * b` | `a * b` | -3 | LR |
* | `a / b` | `a / b` | -3 | LR |
* | `a % b` | `a % b` | -3 | LR |
* | `a // b` | `a // b` | -3 | LR |
* | `a + b` | `a + b` | -4 | LR |
* | `a - b` | `a - b` | -4 | LR |
* | `a << b` | `a << b` | -5 | LR |
* | `a >> b` | `a >> b` | -5 | LR |
* | `a & b` | `a & b` | -6 | LR |
* | `a ^ b` | `a ^ b` | -7 | LR |
* | `a | b` | `a | b` | -8 | LR |
* | `a == b` | `a == b` | -9 | LR |
* | `a != b` | `a != b` | -9 | LR |
* | `a < b` | `a < b` | -9 | LR |
* | `a > b` | `a > b` | -9 | LR |
* | `a <= b` | `a <= b` | -9 | LR |
* | `a >= b` | `a >= b` | -9 | LR |
* | `a === b` | `a === b` | -9 | LR |
* | `a !== b` | `a !== b` | -9 | LR |
* | `a && b` | `a && b` | -10 | LR |
* | `a || b` | `a + b` | -11 | LR |
* | 符号 | 实际运算(op) | 优先级 | 结合性 |
* | ---------- | ------------ | ------ | ------ |
* | `a ** b` | `a ** b` | -1 | RL |
* | `! x` | `x != false` | -2 | R |
* | `- x` | `0 - x` | -2 | R |
* | `~ x` | `~ x` | -2 | R |
* | `a * b` | `a * b` | -3 | LR |
* | `a / b` | `a / b` | -3 | LR |
* | `a % b` | `a % b` | -3 | LR |
* | `a // b` | `a // b` | -3 | LR |
* | `a + b` | `a + b` | -4 | LR |
* | `a - b` | `a - b` | -4 | LR |
* | `a << b` | `a << b` | -5 | LR |
* | `a >> b` | `a >> b` | -5 | LR |
* | `a & b` | `a & b` | -6 | LR |
* | `a ^ b` | `a ^ b` | -7 | LR |
* | `a | b` | `a | b` | -8 | LR |
* | `a == b` | `a == b` | -9 | LR |
* | `a != b` | `a != b` | -9 | LR |
* | `a < b` | `a < b` | -9 | LR |
* | `a > b` | `a > b` | -9 | LR |
* | `a <= b` | `a <= b` | -9 | LR |
* | `a >= b` | `a >= b` | -9 | LR |
* | `a === b` | `a === b` | -9 | LR |
* | `a !== b` | `a !== b` | -9 | LR |
* | `a && b` | `a && b` | -10 | LR |
* | `a || b` | `a + b` | -11 | LR |
* | `if c?a:b` | ... | -12 | LR |
*
* 以上表格外, 还有一元与二元函数(其实就是op)
* 它们的优先级与括号平级,
* 二元函数有: `max` `min` `angle` `len` `noise`,
* 一元函数的部分列举有: `log` `rand` ...,
*
* 具体参考op中没有符号的运算
*
* 对于三元表达式, 也就是`if c?a:b`,
* 它是低优先级的, 所以参与高优先级运算时需要括号括起.
* 就算是`1+if x != y ? x : y`这样的, 也需要写成`1+(if x != y ? x : y)`
* 想要取消这个设计的方案有几个但都极其复杂, 所以就如现在这样
*#

x = 1 + 2 * 3;
Expand Down Expand Up @@ -74,3 +80,61 @@ a, b, c = x, -y, z+2*3;
# 因为op-expr的内联set目前仅为它自己的op服务
#
# 起码我们在数学运算方面的舒适度上来了不是吗


# 三元表达式可以在分支运算赋值上进行简化
x = if a < b ? 2 + 3 : 4;
#* A >>>
{
take '___0' = 'x';
goto :'___0' 'a' < 'b';
`'set'` '___0' '4';
goto :'___1' _;
:'___0'
op '___0' '2' + '3';
:'___1'
}
*#

x = if a < b ? if c < d ? c : d : if e < f ? e : f;
#* A >>>
{
take '___2' = 'x';
goto :'___4' 'a' < 'b';
{
take '___1' = '___2';
goto :'___2' 'e' < 'f';
`'set'` '___1' 'f';
goto :'___3' _;
:'___2'
`'set'` '___1' 'e';
:'___3'
}
goto :'___5' _;
:'___4'
{
take '___0' = '___2';
goto :'___0' 'c' < 'd';
`'set'` '___0' 'd';
goto :'___1' _;
:'___0'
`'set'` '___0' 'c';
:'___1'
}
:'___5'
}
*#

x = 1 + (if y ? y : z);
#* A >>>
op 'x' '1' + (
take '___0' = $;
goto :'___0' 'y' != `'false'`;
`'set'` '___0' 'z';
goto :'___1' _;
:'___0'
`'set'` '___0' 'y';
:'___1'
);
*#
# 由于前面解释过的原因, 你需要添加括号
Loading

0 comments on commit f6a24b6

Please sign in to comment.