Squashed 'docs/standards/playbook/' changes from c3f8137..25d895d
25d895d 🐛 fix(gitea_workflow): clean up temp repos after job steps 2bc3b11 🐛 fix(gitea_workflow): clean up temporary repo dirs in workflows 98c3f30 📝 docs(agent_rules): allow plan execution on current branch 16c7230 📝 docs(prompts): define custom verify layering 8efc4dd 🐛 fix(skills): quote commit-message description bc8498f 🐛 fix(ci): install tomli for gitea tests 55cda3b 🐛 fix(tests): report missing toml parser clearly c0729c7 🐛 fix(playbook): import Optional for cli compatibility 63e24bf 📦 deps(skills): sync thirdparty skills d2f9356 🐛 fix(ci): isolate gitea workflow repos 588b81d 🐛 fix(ci): inline gitea workflow bootstrap e0b1c3a ♻️ refactor(skills): standardize first-party skill contracts 2c5050d ♻️ refactor(skills): rename repo skills source dir f049dfb 📦 deps(skills): drop duplicate first-party superpowers skills 234b335 ✨ feat(workflow): add superpowers planning and execution state tracking c1702a6 📝 docs(markdown): format tracked markdown and drop stale templates 2325409 📝 docs(markdown): clarify optional markdownlint usage 214c44e 🔧 chore(markdown): add markdownlint baseline and lint fixes a22b324 📝 docs(templates): add execution and memory-bank prompt templates 223a797 📝 docs(templates): update README for Claude Code and current features 4ac8672 📝 docs: simplify README + platform-agnostic tools + auto-create local rules 2431c9d 📝 docs: add claude_md config and use cross-platform paths d64b248 📝 docs: fix README.md inaccuracies and add Claude Code info c8d6bf2 🐛 fix(playbook): use relative paths in CLAUDE.md when not at project root 6518f0f ✨ feat(playbook): auto-create CLAUDE.md with path discovery 6ec9a45 ✨ feat(skills): add skill_link symlink support + platform-agnostic prompt 9f8b6b5 📝 docs: update README and config example for Claude Code support 79cff6c 📝 docs(skills): add Claude Code platform support 452c6f5 ✨ feat(playbook): auto-inject AGENTS.md into CLAUDE.md e1dbf3c 🐛 fix(skills): remove dual-path from commit-message skill f3a7259 🔧 chore(ci): use prepare_repo.sh in both workflows da08212 🔧 chore(ci): extract prepare_repo.sh and clean up workflows 7ade85e 🗑️ remove(tsl): drop syntax_book/, data/ source and build script f94dba0 ♻️ refactor(skills): update playbook.py and tests for thirdparty/ layout b3df412 ♻️ refactor(skills): separate thirdparty skills into thirdparty/ subdirectory 64950e7 📦 deps(skills): sync thirdparty skills a2e3cb0 ✨ feat(playbook): add no_backup deploy controls 8609d59 🐛 fix(docs): repair reference catalog source links 956da11 🐛 fix(playbook): publish hidden ci test fixes 3f67754 📦 deps(skills): sync thirdparty skills 08ca87b 📦 deps(skills): add karpathy thirdparty sync 96b705b 📝 docs(tsl): rebuild canonical syntax and routing manual 3ed5052 📦 deps(skills): sync thirdparty skills 60108dd 📦 deps(skills): sync thirdparty skills da85d4e 🐛 fix(thirdparty): prune nested project snapshots a2a697e 📦 deps(skills): sync thirdparty skills 9df610a 🐛 fix(thirdparty): exclude duplicated superpowers skills 33dd5bb 🐛 fix(thirdparty): preserve optional manifest fields 91b0ea7 🐛 fix(thirdparty): preserve manifest during snapshot update 2e26f98 🔧 chore(thirdparty): generalize skills sync pipeline 5b9c1e3 📦 deps(skills): sync superpowers 2f2d34a 📝 docs(readme): normalize subtree command spacing 62db7db 🐛 fix(ci): serialize superpowers update and sync 3463223 🐛 fix(ci): use literal superpowers sync paths 48f6de8 📦 deps(skills): sync superpowers 4b23529 🔧 chore(ci): merge superpowers update and sync workflow a56d75b 📦 deps(skills): sync superpowers 84bcefa 🔧 chore(ci): use ci[bot] commit author name 00a07e5 📦 deps(skills): sync superpowers 7b84daf 🐛 fix(templates): enforce main loop progress tracking 51373d7 🔧 chore(ci): automate superpowers sync workflow eaaa39c 🐛 fix(ci): prevent stale superpowers sync from restoring skills block 79755c6 📦 deps(skills): sync superpowers 836d878 📦 deps(skills): sync superpowers 8216c9f 📦 deps(skills): sync superpowers 9439505 🐛 fix(playbook): address reported repo issues git-subtree-dir: docs/standards/playbook git-subtree-split: 25d895d8b3f56624ccfe99ad7289e9eb49e0f316
This commit is contained in:
@@ -0,0 +1,413 @@
|
||||
# Expressions And Operators
|
||||
|
||||
文档类型:语法主线
|
||||
是否可直接用于生成代码:是
|
||||
是否含已验证可执行示例:是
|
||||
是否含已验证反例:是
|
||||
遇到不确定时跳转到:[08_control_flow.md](08_control_flow.md)、[09_objects_and_classes.md](09_objects_and_classes.md)、[12_pitfalls.md](12_pitfalls.md)
|
||||
|
||||
手册位置:第 7 篇,共 32 篇。上一篇:[06_functions_and_calls.md](06_functions_and_calls.md)。下一篇:[08_control_flow.md](08_control_flow.md)。
|
||||
|
||||
这一篇集中放语言级表达式与运算符,避免与业务计算示例混用。
|
||||
|
||||
## 这一篇解决什么问题
|
||||
|
||||
回答“赋值、比较、条件求值、可空访问和一部分新表达式在 TSL 里怎样写”。
|
||||
|
||||
## 必须记住的规则
|
||||
|
||||
- 当前页只收已经单独验证过的基础表达式,不把未经逐条验证的扩展运算体系一次并进正文。
|
||||
- 赋值使用 `:=`。
|
||||
- 当前解释器接受 `+=`、`-=`、`*=`、`/=`、`%=` 这几种基础运算赋值。
|
||||
- 当前解释器接受语句级 `a++;` 与 `a--;`。
|
||||
- `=` 用于比较,不用于赋值。
|
||||
- 当前解释器接受字符串 `+` 拼接、字符串比较和 `like` 正则匹配。
|
||||
- 当前解释器同时接受 `flag ? true_value : false_value` 和 `if condition then true_value else false_value` 这两种条件求值写法。
|
||||
- 当前解释器接受 `@expr` 把后面的内容声明成表达式对象。
|
||||
- 当前解释器接受 `&"..."` 把字符串编译成表达式对象。
|
||||
- 当前解释器接受逗号表达式 `(exp1, exp2, ..., expN)`,并按从左到右顺序求值。
|
||||
- 当前解释器接受空安全访问 `a?.member` 与 `a?.[index]`。
|
||||
- 当前解释器接受 `not in`、`not like`、`not sqlin`、`not is` 这几种否定形式运算。
|
||||
- 当前解释器接受标量链式比较 `:>`、`:<`、`:<>`、`:==`、`:>=`、`:<=`。
|
||||
- 当前解释器接受矩阵链式比较 `::>`、`::<`、`::<>`、`::==`、`::>=`、`::<=`。
|
||||
- 当前环境里 `{$IFDEF ifexp}` 为真,可用于探测 `if ... then ... else ...` 表达式能力。
|
||||
- 当前环境里 `{$IFDEF nilinvoke}` 为真,可用于探测 nil 调用相关能力。
|
||||
- 不要把外部资料里的更深链式可空访问修正,直接当成当前解释器必然支持。
|
||||
|
||||
## 已验证语法
|
||||
|
||||
### 基础赋值和条件求值
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
a := 1;
|
||||
b := 2;
|
||||
flag := a < b;
|
||||
value := flag ? 10 : 20;
|
||||
```
|
||||
|
||||
运算赋值:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
a := 1;
|
||||
a += 2;
|
||||
WriteLn(a);
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 输出 `3`
|
||||
|
||||
基础算术复合赋值:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
a := 10;
|
||||
a -= 3;
|
||||
WriteLn(a);
|
||||
a *= 4;
|
||||
WriteLn(a);
|
||||
a /= 7;
|
||||
WriteLn(a);
|
||||
a %= 5;
|
||||
WriteLn(a);
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `a -= 3` 后输出 `7`
|
||||
- `a *= 4` 后输出 `28`
|
||||
- `a /= 7` 后输出 `4`
|
||||
- `a %= 5` 后输出 `4`
|
||||
|
||||
字符串同样支持 `+=`:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
s := "A";
|
||||
s += "B";
|
||||
WriteLn(s);
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 输出 `AB`
|
||||
|
||||
字符串拼接、比较和 `like`:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
WriteLn("222" + "888");
|
||||
WriteLn("A" < "a");
|
||||
WriteLn("AB" < "ABC");
|
||||
WriteLn("ABC" = "ABC");
|
||||
WriteLn("2009-01-01" like "\\d{4}-\\d{2}-\\d{2}");
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 依次输出 `222888`、`1`、`1`、`1`、`1`
|
||||
- 说明字符串可以直接用 `+` 拼接
|
||||
- 说明字符串比较区分字符序和大小写;当前例子里 `"A" < "a"` 为真
|
||||
- 说明当前 `like` 的右侧可以直接写正则模式
|
||||
|
||||
`like` 不要按 SQL `%` 通配去理解:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
WriteLn("abc" like "a.*");
|
||||
WriteLn("abc" like "a%");
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 第一行输出 `1`
|
||||
- 第二行输出 `0`
|
||||
- 因此当前解释器里的 `like` 更接近“正则匹配”,不是 SQL 那套 `%` / `_` 通配语义
|
||||
|
||||
自增与自减:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
a := 1;
|
||||
a++;
|
||||
WriteLn(a);
|
||||
a--;
|
||||
WriteLn(a);
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 先输出 `2`
|
||||
- 再输出 `1`
|
||||
|
||||
`if` 表达式:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
WriteLn(if 2 > 1 then 2 else 1);
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `if 2 > 1 then 2 else 1` 返回 `2`
|
||||
|
||||
`@` 表达式前导:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
A := 1;
|
||||
B := @A + 1;
|
||||
C := eval(B);
|
||||
WriteLn(C);
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 输出 `2`
|
||||
- 说明 `@A + 1` 会得到一个可交给 `eval(...)` 求值的表达式对象
|
||||
|
||||
`&"..."` 表达式常量:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
A := 1;
|
||||
B := &"A + 1";
|
||||
C := eval(B);
|
||||
WriteLn(C);
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 输出 `2`
|
||||
- 说明 `&"A + 1"` 会把字符串编译成表达式对象,再由 `eval(...)` 求值
|
||||
|
||||
逗号表达式:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
function Demo();
|
||||
begin
|
||||
return (a := 1, b := 2, c := 3, a + b + c);
|
||||
end;
|
||||
begin
|
||||
WriteLn(Demo());
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 输出 `6`
|
||||
- 说明逗号表达式会按从左到右顺序执行前面的赋值,再返回最后一个表达式结果
|
||||
|
||||
逗号表达式也可以继续参与外层计算:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
A := (b := 2, c := 3, b * c) * c;
|
||||
WriteLn(A);
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- 输出 `18`
|
||||
- 说明逗号表达式本身可以作为一个普通子表达式继续参与后续运算
|
||||
|
||||
### 空安全访问
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
type Holder = class
|
||||
value;
|
||||
end;
|
||||
begin
|
||||
a := nil;
|
||||
WriteLn(a?.value = nil);
|
||||
h := new Holder();
|
||||
h.value := 7;
|
||||
WriteLn(h?.value);
|
||||
arr := nil;
|
||||
WriteLn(arr?.[0] = nil);
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `a?.value = nil` 输出 `1`
|
||||
- `h?.value` 输出 `7`
|
||||
- `arr?.[0] = nil` 输出 `1`
|
||||
|
||||
### 否定形式运算
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
type A = class
|
||||
end;
|
||||
type B = class
|
||||
end;
|
||||
begin
|
||||
WriteLn(1 not in array(2, 3));
|
||||
WriteLn("2009-1-1" not like "\\d{4}-\\d{2}-\\d{2}");
|
||||
WriteLn(1 not sqlin array(2, 3));
|
||||
obj := new A();
|
||||
WriteLn(obj not is class(B));
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `1 not in array(2, 3)` 输出 `1`
|
||||
- `"2009-1-1" not like "\\d{4}-\\d{2}-\\d{2}"` 输出 `1`
|
||||
- `1 not sqlin array(2, 3)` 输出 `1`
|
||||
- `obj not is class(B)` 输出 `1`
|
||||
|
||||
### 标量链式比较
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
WriteLn(1 :< 2 :< 3);
|
||||
WriteLn(3 :> 2 :> 1);
|
||||
WriteLn(1 :== 1 :== 1);
|
||||
WriteLn(3 :>= 2 :>= 2);
|
||||
WriteLn(1 :<= 2 :<= 3);
|
||||
WriteLn(1 :<> 2 :<> 3);
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `1 :< 2 :< 3` 输出 `1`
|
||||
- `3 :> 2 :> 1` 输出 `1`
|
||||
- `1 :== 1 :== 1` 输出 `1`
|
||||
- `3 :>= 2 :>= 2` 输出 `1`
|
||||
- `1 :<= 2 :<= 3` 输出 `1`
|
||||
- `1 :<> 2 :<> 3` 输出 `1`
|
||||
|
||||
### 矩阵链式比较
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
r := array(1, 2, -1) ::< array(2, 1, 0) ::< array(3, 2, 1);
|
||||
WriteLn(r[0]);
|
||||
WriteLn(r[1]);
|
||||
WriteLn(r[2]);
|
||||
s := array(1, 2, -1) ::< 2 ::< array(3, 2, 1);
|
||||
WriteLn(s[0]);
|
||||
WriteLn(s[1]);
|
||||
WriteLn(s[2]);
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `array(1, 2, -1) ::< array(2, 1, 0) ::< array(3, 2, 1)` 的三个元素依次输出 `1`、`0`、`1`
|
||||
- `array(1, 2, -1) ::< 2 ::< array(3, 2, 1)` 的三个元素依次输出 `1`、`0`、`0`
|
||||
- 说明矩阵链式比较会按元素位置分别得到结果数组,并且可以和标量混用
|
||||
|
||||
### 条件编译探测
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
program test;
|
||||
begin
|
||||
{$IFDEF ifexp}
|
||||
WriteLn(1);
|
||||
{$ELSE}
|
||||
WriteLn(0);
|
||||
{$ENDIF}
|
||||
{$IFDEF nilinvoke}
|
||||
WriteLn(1);
|
||||
{$ELSE}
|
||||
WriteLn(0);
|
||||
{$ENDIF}
|
||||
end.
|
||||
```
|
||||
|
||||
已验证运行结果:
|
||||
|
||||
- `{$IFDEF ifexp}` 输出 `1`
|
||||
- `{$IFDEF nilinvoke}` 输出 `1`
|
||||
|
||||
## 最小可编译示例
|
||||
|
||||
如果你只需要最小的“比较 + 三目”例子,直接用这个:
|
||||
|
||||
代码块身份:已验证可执行示例
|
||||
|
||||
```tsl
|
||||
flag := 1 < 2;
|
||||
value := flag ? 10 : 20;
|
||||
```
|
||||
|
||||
## 常见误写
|
||||
|
||||
- 用 `=` 当赋值运算符。
|
||||
- 把 `if` 表达式写成没有 `else` 的半句。
|
||||
- 把更深链式可空访问 `c?.a?.[1]` 直接当成当前解释器已支持事实。
|
||||
|
||||
代码块身份:反例 / 不可照写
|
||||
|
||||
```text
|
||||
a = 1;
|
||||
```
|
||||
|
||||
上面这种写法会编译失败,因为单独的 `=` 在这里会被当成不成立的表达式。
|
||||
|
||||
代码块身份:反例 / 不可照写
|
||||
|
||||
```text
|
||||
v := if 2 > 1 then 2;
|
||||
```
|
||||
|
||||
上面这种写法也会编译失败;`if` 表达式当前必须带 `else`。
|
||||
|
||||
代码块身份:反例 / 不可照写
|
||||
|
||||
```text
|
||||
c := nil;
|
||||
WriteLn(c?.a?.[1] = nil);
|
||||
```
|
||||
|
||||
上面这种更深的混合可空访问,在当前已记录验证里没有通过,不要提前把后续版本的修正结果写进结论。
|
||||
|
||||
## 跳转指引
|
||||
|
||||
- 回看基本类型:见 [04_values_and_literals.md](04_values_and_literals.md)
|
||||
- 把表达式放进流程里:见 [08_control_flow.md](08_control_flow.md)
|
||||
Reference in New Issue
Block a user