diff --git a/README.md b/README.md index 74bcddab..ebba9857 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,10 @@ echo '40 + 2 -> x; print(x)' | ./kvlang # pipe mode (; separates statements ```kv rwfunc main() -> () { - total = 0 # = is equivalent to <- + total = 0 1 -> i while (i <= 5) { - total <- total + i + total = total + i i + 1 -> i } println(total) @@ -95,11 +95,10 @@ rwfunc main() -> () { main() ``` -### rwir(Read-Write IR):Three Assignment Forms +### rwir(Read-Write IR):Two Write Forms ```kv -x = 40 + 2 # = : write slot on the left (≡ <-); = is NOT an expression, cannot nest in conditions -y <- x # left arrow: write slot on the left +x = 40 + 2 # = : write slot on the left; = is NOT an expression, cannot nest in conditions x × y -> z # right arrow: write slot on the right f(a, b) -> r # write-param mapping for calls; multiple: -> x, y; discard: -> _ ``` @@ -109,9 +108,9 @@ A write slot must be a **location**: a bare name (frame-local), `/abs/path` (glo **`rwfunc func(ra,rb) -> (wa,wb) { … }` = composite rwir**, the named form. Single-line rwir like `A + B -> C` is atomic (one opcode + reads + writes); `rwfunc` packs multiple rwir into a named unit with the same arrow interface — `(ra,rb)` declare read params, `-> (wa,wb)` declare write params. Calling `add(3,4) -> s` binds arguments to read slots, maps write slots back to the caller frame. No return values, only write-param mapping. `-> (C:int64)` in a `rwfunc` signature is a **write-param declaration**. The function writes results into its write-param slots; the caller maps them with `-> r`. -**Read params are read-only**: the body may not place a read param in a write slot (e.g. `A = A + 1`). This includes array element writes — `a[i] <- v` writes through `a`, so `a` must be a write param if you need to modify it. **Array/dict to mutate → write param; array/dict to read only → read param.** +**Read params are read-only**: the body may not place a read param in a write slot (e.g. `A = A + 1`). This includes array element writes — `a[i] = v` writes through `a`, so `a` must be a write param if you need to modify it. **Array/dict to mutate → write param; array/dict to read only → read param.** ```kv -# ❌ wrong: array as read param, a[i] <- v writes through read-param slot → parser rejects +# ❌ wrong: array as read param, a[i] = v writes through read-param slot → parser rejects rwfunc bad(a:int64) -> () { 99 -> a[0] } # ✅ correct: array as write param, readable and writable inside the body @@ -157,8 +156,8 @@ Data structures shared across functions (e.g. linked lists) create nodes at **ab ```kv rwfunc build() -> () { - /n1 = { val=1; next="/n2" } # = is equivalent to <- - /n2 <- { val=2; next="/n3" } + /n1 = { val=1; next="/n2" } + /n2 = { val=2; next="/n3" } { val=3; next="" } -> /n3 } @@ -224,7 +223,7 @@ Conditions may be compound expressions: `if (7 % 2 != 0)` and `while (i < string **`print` / `println` / `cerr` are NOT builtins.** In the KV world there is no terminal — only keys and values — so I/O is not a core-language primitive. They are **extension rwir**: the `term` extension runtime registers them at `/lib/` (kind `rwir`) and writes to the host process's `stdout`/`stderr`. The core runtime recognizes any `/lib/` that carries an `rwir` signature and is not a builtin as an extension rwir, and hands it off to its extension runtime. Same mechanism as `json.to` / `json.from` (the `json` extension) and tensor ops (the numpy / GPU extensions). ```kv -a:int64 = [7, 2, 9, 4] # typed 1D array, = ≡ <- +a:int64 = [7, 2, 9, 4] # typed 1D array ndarray.numel(a) -> n # 4 at(a, 2) -> e # 9 (0-indexed) set(a, 1, 99) -> a # modify element: a becomes [7, 99, 9, 4] diff --git a/README_CN.md b/README_CN.md index 7234c8ac..328fe53a 100644 --- a/README_CN.md +++ b/README_CN.md @@ -83,10 +83,10 @@ echo '40 + 2 -> x; print(x)' | ./kvlang # pipe 模式(; 分隔同行语 ```kv rwfunc main() -> () { - total = 0 # = 等价于 <- + total = 0 1 -> i while (i <= 5) { - total <- total + i + total = total + i i + 1 -> i } println(total) @@ -95,11 +95,10 @@ rwfunc main() -> () { main() ``` -### rwir(读写码):赋值三形态 +### rwir(读写码):写入两形态 ```kv -x = 40 + 2 # = :写槽在左(≡ <-);= 不是表达式,不能嵌进条件里 -y <- x # 左箭头:写槽在左 +x = 40 + 2 # = :写槽在左;= 不是表达式,不能嵌进条件里 x × y -> z # 右箭头:写槽在右 f(a, b) -> r # 函数写参映射;多写参 -> x, y;丢弃用 -> _ ``` @@ -109,9 +108,9 @@ f(a, b) -> r # 函数写参映射;多写参 -> x, y;丢弃用 -> _ **`rwfunc func(ra,rb) -> (wa,wb) { … }` = 自定义复合 rwir**,单条 rwir 如 `A + B -> C` 是原子 rwir(一个操作码 + 读参 + 写参);`rwfunc` 把多条 rwir 打包成命名单元,对外暴露相同的箭头接口——`(ra,rb)` 是读参声明,`-> (wa,wb)` 是写参声明。调用 `add(3,4) -> s` 即把实参绑入读槽、写槽映射回调用方帧。没有返回值,只有写参映射。 `rwfunc` 签名中 `-> (C:int64)` 是**写参声明**。函数把结果写进写参槽,调用方用 `-> r` 把写参映射到自己的位置。 -**读参只读**:函数体内不可把读参放进写槽(如 `A = A + 1`)。数组元素写同理——`a[i] <- v` 写穿 `a`,要修改的数组/字典必须放写参位置。 +**读参只读**:函数体内不可把读参放进写槽(如 `A = A + 1`)。数组元素写同理——`a[i] = v` 写穿 `a`,要修改的数组/字典必须放写参位置。 ```kv -# ❌ 错误:数组作读参,a[i] <- v 写读参槽 → parser 拒绝 +# ❌ 错误:数组作读参,a[i] = v 写读参槽 → parser 拒绝 rwfunc bad(a:int64) -> () { 99 -> a[0] } # ✅ 正确:数组作写参,函数内读写自由 @@ -154,8 +153,8 @@ p.val -> v # 读 /node.val → 42 ```kv rwfunc build() -> () { - /n1 = { val=1; next="/n2" } # = 等价于 <- - /n2 <- { val=2; next="/n3" } + /n1 = { val=1; next="/n2" } + /n2 = { val=2; next="/n3" } { val=3; next="" } -> /n3 } @@ -221,7 +220,7 @@ for (x in [7, 2, 9, 4]) { println(x) } **`print` / `println` / `cerr` 不是内建。** KV 世界里没有终端,只有 key 和 value——I/O 不是核心语言原语。它们是**扩展 rwir**:由 `term` 扩展运行时把签名注册到 `/lib/`(kind=`rwir`),并写宿主进程的 `stdout`/`stderr`。核心 runtime 把任何"`/lib/` 上带 `rwir` 签名、且不在 builtin 表里"的 opcode 识别为扩展 rwir,交给其扩展运行时执行。与 `json.to` / `json.from`(json 扩展)、tensor 算子(numpy / GPU 扩展)同一套机制。 ```kv -a:int64 = [7, 2, 9, 4] # 带类型 1D 数组,= ≡ <- +a:int64 = [7, 2, 9, 4] # 带类型 1D 数组 ndarray.numel(a) -> n # 4 at(a, 2) -> e # 9 set(a, 1, 99) -> a # 修改元素:a 变为 [7, 99, 9, 4] diff --git a/benchmark/README.md b/benchmark/README.md index b2099c50..c90a06e7 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -54,7 +54,7 @@ kvlang 是被测对象,**分别在三个 kvspace 后端上各跑一遍,占 | `hash_table` | 哈希表增删查 | Knuth 乘法散列,插入 + 查找求和 | | `matmul` | 浮点运算 + 循环优化 | 稠密方阵乘三重循环,float64 校验和 ×1e6 精确对齐 | | `k_nucleotide` | 字符串 + 哈希表 | 逐字符 `ord` 入哈希表统计碱基频次 | -| `iops` | 最小寻址单元往返地板价 | 单 key 读-改-写 `a<-a+1`,per-op 延迟(对齐 #204,参考基线) | +| `iops` | 最小寻址单元往返地板价 | 单 key 读-改-写 `a=a+1`,per-op 延迟(对齐 #204,参考基线) | | `prime_sieve` | 计算 / 控制流密集 | 嵌套 `while` + 取模,O(n²) 内层迭代(参考基线) | kvlang 的性能瓶颈是「PC/帧/局部全落 KV 树、每步一次往返」的架构本质(见 kvlang#194 #204 #116), diff --git a/benchmark/cases/binary_search/binary_search.kv b/benchmark/cases/binary_search/binary_search.kv index 8c1c1730..bba7f1a6 100644 --- a/benchmark/cases/binary_search/binary_search.kv +++ b/benchmark/cases/binary_search/binary_search.kv @@ -29,7 +29,7 @@ rwfunc test() -> () { i -> arr·*i i + 1 -> i } - t0 <- time·now() + t0 = time·now() 0 -> sum 0 -> found 0 -> q @@ -41,10 +41,10 @@ rwfunc test() -> () { } q + 1 -> q } - t1 <- time·now() + t1 = time·now() println("bsearch: found =", found, "sum =", sum) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__") } diff --git a/benchmark/cases/binary_trees/binary_trees.kv b/benchmark/cases/binary_trees/binary_trees.kv index ed8b013f..e3b48757 100644 --- a/benchmark/cases/binary_trees/binary_trees.kv +++ b/benchmark/cases/binary_trees/binary_trees.kv @@ -10,7 +10,7 @@ rwfunc test() -> () { bid:[int64]·int64 = {} bdep:[int64]·int64 = {} 0 -> nid - t0 <- time·now() + t0 = time·now() nid + 1 -> nid nid -> root 0 -> L·*root @@ -63,10 +63,10 @@ rwfunc test() -> () { tp + 1 -> tp } } - t1 <- time·now() + t1 = time·now() println("bintree: nodes =", count) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: depth=__SCALE__") } diff --git a/benchmark/cases/fib/fib.kv b/benchmark/cases/fib/fib.kv index ac710b51..7bdbeafd 100644 --- a/benchmark/cases/fib/fib.kv +++ b/benchmark/cases/fib/fib.kv @@ -6,21 +6,21 @@ rwfunc fib(n:int64) -> (r:int64) { if (n <= 1) { n -> r } else { - a <- n - 1 - b <- n - 2 + a = n - 1 + b = n - 2 fib(a) -> x fib(b) -> y - r <- x + y + r = x + y } } rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() fib(__SCALE__) -> ans - t1 <- time·now() + t1 = time·now() println("fib =", ans) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: n=__SCALE__") } diff --git a/benchmark/cases/hash_table/hash_table.kv b/benchmark/cases/hash_table/hash_table.kv index d6f6dc5f..0a54351c 100644 --- a/benchmark/cases/hash_table/hash_table.kv +++ b/benchmark/cases/hash_table/hash_table.kv @@ -5,7 +5,7 @@ rwfunc test() -> () { __SCALE__ -> n h:[int64]·int64 = {} - t0 <- time·now() + t0 = time·now() 0 -> i while (i < n) { i × 2654435761 -> k0 @@ -27,10 +27,10 @@ rwfunc test() -> () { } i + 1 -> i } - t1 <- time·now() + t1 = time·now() println("hash: hits =", hits, "sum =", sum) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__") } diff --git a/benchmark/cases/iops/iops.kv b/benchmark/cases/iops/iops.kv index 00bdf2c9..82aef579 100644 --- a/benchmark/cases/iops/iops.kv +++ b/benchmark/cases/iops/iops.kv @@ -1,4 +1,4 @@ -// iops benchmark — 单变量反复读改写 a<-a+1(最小寻址单元的每操作 KV 往返地板价,对齐 #204) +// iops benchmark — 单变量反复读改写 a=a+1(最小寻址单元的每操作 KV 往返地板价,对齐 #204) // 循环次数 N 由 __SCALE__ 占位(勿改逻辑) // 期望输出(N=2000 时): // iops a = 2000 @@ -6,18 +6,18 @@ rwfunc iops(n:int64) -> (a:int64) { a = 0 1 -> i while (i <= n) { - a <- a + 1 + a = a + 1 i = i + 1 } } rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() iops(__SCALE__) -> ans - t1 <- time·now() + t1 = time·now() println("iops a =", ans) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__") } diff --git a/benchmark/cases/k_nucleotide/k_nucleotide.kv b/benchmark/cases/k_nucleotide/k_nucleotide.kv index d6aa99c6..f46c20d8 100644 --- a/benchmark/cases/k_nucleotide/k_nucleotide.kv +++ b/benchmark/cases/k_nucleotide/k_nucleotide.kv @@ -13,7 +13,7 @@ rwfunc test() -> () { } string·len(s) -> n h:[int64]·int64 = {} - t0 <- time·now() + t0 = time·now() 0 -> i while (i < n) { string·char(s, i) -> ch @@ -26,14 +26,14 @@ rwfunc test() -> () { cnt -> h·*c i + 1 -> i } - t1 <- time·now() + t1 = time·now() kv·get(h, 65) -> a kv·get(h, 67) -> cc kv·get(h, 71) -> g kv·get(h, 84) -> t println("knuc: A =", a, "C =", cc, "G =", g, "T =", t) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: rep=__SCALE__") } diff --git a/benchmark/cases/matmul/matmul.kv b/benchmark/cases/matmul/matmul.kv index 759561fe..25af78d4 100644 --- a/benchmark/cases/matmul/matmul.kv +++ b/benchmark/cases/matmul/matmul.kv @@ -30,7 +30,7 @@ rwfunc test() -> () { } i + 1 -> i } - t0 <- time·now() + t0 = time·now() 0.0 -> checksum 0 -> i while (i < n) { @@ -54,12 +54,12 @@ rwfunc test() -> () { } i + 1 -> i } - t1 <- time·now() + t1 = time·now() checksum × 1000000.0 -> scaled int64(scaled) -> out println("matmul: check =", out) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__") } diff --git a/benchmark/cases/nqueens/nqueens.kv b/benchmark/cases/nqueens/nqueens.kv index 16ce4459..58314f18 100644 --- a/benchmark/cases/nqueens/nqueens.kv +++ b/benchmark/cases/nqueens/nqueens.kv @@ -21,20 +21,20 @@ rwfunc nq(cols:int64, d1:int64, d2:int64, all:int64) -> (cnt:int64) { d2 | p -> u2 u2 >> 1 -> nd2 nq(nc, nd1, nd2, all) -> sub - cnt <- cnt + sub + cnt = cnt + sub } } } rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() 1 << __SCALE__ -> sh sh - 1 -> all nq(0, 0, 0, all) -> ans - t1 <- time·now() + t1 = time·now() println("queens =", ans) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__") } diff --git a/benchmark/cases/prime_sieve/prime_sieve.kv b/benchmark/cases/prime_sieve/prime_sieve.kv index 9f7211c3..ee2bd17f 100644 --- a/benchmark/cases/prime_sieve/prime_sieve.kv +++ b/benchmark/cases/prime_sieve/prime_sieve.kv @@ -9,16 +9,16 @@ rwfunc prime_sieve(limit:int64) -> () { count = 0 2 -> n while (n <= limit) { - is_prime <- true + is_prime = true d = 2 while (d < n) { n % d -> rem - divisible <- rem == 0 + divisible = rem == 0 if (divisible) { is_prime = false break } else { - d <- d + 1 + d = d + 1 } } if (is_prime) { @@ -31,11 +31,11 @@ rwfunc prime_sieve(limit:int64) -> () { } rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() prime_sieve(__SCALE__) - t1 <- time·now() - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + t1 = time·now() + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__") } diff --git a/benchmark/cases/quicksort/quicksort.kv b/benchmark/cases/quicksort/quicksort.kv index 21c113d1..971ba537 100644 --- a/benchmark/cases/quicksort/quicksort.kv +++ b/benchmark/cases/quicksort/quicksort.kv @@ -15,7 +15,7 @@ rwfunc test() -> () { v -> arr·*i i + 1 -> i } - t0 <- time·now() + t0 = time·now() st_lo:[int64]·int64 = {} st_hi:[int64]·int64 = {} 0 -> top @@ -52,15 +52,15 @@ rwfunc test() -> () { top + 1 -> top } } - t1 <- time·now() + t1 = time·now() n ÷ 2 -> mid n - 1 -> last kv·get(arr, 0) -> a0 kv·get(arr, mid) -> am kv·get(arr, last) -> al println("qsort: a0 =", a0, "amid =", am, "alast =", al) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) println("__bench_input: N=__SCALE__,seed=1") } diff --git a/error_cases/array_param/mutate_literal.kv b/error_cases/array_param/mutate_literal.kv index f55b35c9..d4aff1a5 100644 --- a/error_cases/array_param/mutate_literal.kv +++ b/error_cases/array_param/mutate_literal.kv @@ -1,4 +1,4 @@ -// ❌ 错误:数组字面量放读参位置,函数内写 a[k] <- v 被 fix-027 拒绝。 +// ❌ 错误:数组字面量放读参位置,函数内写 a[k] = v 被 fix-027 拒绝。 // expected: // read param "a" cannot be used as write slot // kvlang 铁律:读参只读。要修改数组,必须放写参位置。 diff --git a/extensions/kvlang/syntaxes/kvlang.tmLanguage.json b/extensions/kvlang/syntaxes/kvlang.tmLanguage.json index 2b2891e4..e0128181 100644 --- a/extensions/kvlang/syntaxes/kvlang.tmLanguage.json +++ b/extensions/kvlang/syntaxes/kvlang.tmLanguage.json @@ -66,7 +66,7 @@ }, "arrow": { "name": "keyword.operator.arrow.kvlang", - "match": "<-|->|=" + "match": "->|=" } } } diff --git a/layout/src/ast.rs b/layout/src/ast.rs index e5e829a5..6f7cd720 100644 --- a/layout/src/ast.rs +++ b/layout/src/ast.rs @@ -388,17 +388,12 @@ pub struct Instruction { pub expr: Option, // None = 空指令 pub writes: Vec, pub write_types: Vec, - pub arrow_left: bool, // true = 写槽在左(<- 或 =) - pub eq: bool, // true = 源码用 = 书写 + pub arrow_left: bool, // true = 写槽在左(=) } impl Instruction { fn left_arrow(&self) -> &'static str { - if self.eq { - symbol::ARROW_EQ - } else { - symbol::ARROW_LEFT - } + symbol::ARROW_EQ } /// 扁平化 (opcode, reads)。前提:lower 已把复合子表达式展开为叶节点。 @@ -500,7 +495,7 @@ impl fmt::Display for Instruction { } return write!(f, "{s}"); } - // set(base, idx, val) → a[idx] <- val(仅 <- 形式) + // set(base, idx, val) → a[idx] = val(仅左写形式) if e.op == "set" && e.args.len() >= 3 && self.arrow_left { let base = e.args[0].to_string(); let idx = idx_string(&e.args[1]); diff --git a/layout/src/lower.rs b/layout/src/lower.rs index c562d82c..1bb74eca 100644 --- a/layout/src/lower.rs +++ b/layout/src/lower.rs @@ -126,7 +126,6 @@ fn return_inst() -> Stmt { writes: Vec::new(), write_types: Vec::new(), arrow_left: false, - eq: false, }) } @@ -400,7 +399,6 @@ fn lower_for_with_cont( writes: vec![slot.clone()], write_types: Vec::new(), arrow_left: true, - eq: true, })); slot }; @@ -412,7 +410,6 @@ fn lower_for_with_cont( writes: vec![len_slot.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, })); } else { init_body.push(Stmt::Instruction(Instruction { @@ -421,7 +418,6 @@ fn lower_for_with_cont( writes: vec![len_slot.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, })); } init_body.push(goto_label(&cond_label)); @@ -432,7 +428,6 @@ fn lower_for_with_cont( writes: vec![idx_slot.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, }; let lt_inst = Instruction { comments: Vec::new(), @@ -443,7 +438,6 @@ fn lower_for_with_cont( writes: vec![cond_slot.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, }; let cond_body = vec![ Stmt::Instruction(add_inst), @@ -467,7 +461,6 @@ fn lower_for_with_cont( writes: vec![key_slot.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, })); body_insts.push(Stmt::Instruction(Instruction { comments: Vec::new(), @@ -478,7 +471,6 @@ fn lower_for_with_cont( writes: vec![s.var.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, })); } else { body_insts.push(Stmt::Instruction(Instruction { @@ -490,7 +482,6 @@ fn lower_for_with_cont( writes: vec![s.var.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, })); } body_insts.extend(body_inner); @@ -551,7 +542,6 @@ fn flatten_expr(e: &Expr, lg: &mut LabelGen, extra: &mut Vec) -> Expr { writes: vec![tmp.clone()], write_types: Vec::new(), arrow_left: false, - eq: false, })); new_args.push(ast::leaf(&tmp)); } else { @@ -579,7 +569,6 @@ fn br_inst(cond: &str, t_label: &str, f_label: &str) -> Stmt { writes: Vec::new(), write_types: Vec::new(), arrow_left: false, - eq: false, }) } @@ -590,7 +579,6 @@ fn goto_label(label: &str) -> Stmt { writes: Vec::new(), write_types: Vec::new(), arrow_left: false, - eq: false, }) } @@ -603,7 +591,6 @@ fn expand_sparse(base: &str, e: &Expr, _lg: &mut LabelGen) -> Vec { writes: vec![base.to_string()], write_types: Vec::new(), arrow_left: true, - eq: false, }; vec![Stmt::Instruction(inst)] } @@ -615,7 +602,6 @@ fn make_copy_inst(val: &str, dest: &str) -> Stmt { writes: vec![dest.to_string()], write_types: Vec::new(), arrow_left: false, - eq: false, }) } diff --git a/layout/src/parser.rs b/layout/src/parser.rs index 6d311b57..d89bbcbd 100644 --- a/layout/src/parser.rs +++ b/layout/src/parser.rs @@ -987,13 +987,12 @@ impl Parser { let mut inst = Instruction::default(); match self.find_top_level_arrow() { - Some(v) if v == "<-" || v == "=" => { + Some(v) if v == "=" => { inst.arrow_left = true; - inst.eq = v == "="; let (writes, wtypes) = self.collect_writes_until_arrow(); inst.writes = writes; inst.write_types = wtypes; - self.advance(); // consume <- / = + self.advance(); // consume = inst.expr = self.parse_pratt(0); self.desugar_subscript_write(&mut inst); self.desugar_member_write(&mut inst); @@ -1813,7 +1812,7 @@ impl Parser { // 下标写脱糖:arr[i,j] 写槽 + 值 e → xv·set(arr, i, j, e) -> arr(compact 数组, // 读侧 arr[i,j]→xv·at 的对称)。arr· 前缀坐标或 / 路径 → kv·set。左右箭头共用: - // <- 时 e 是 pratt 右值,-> 时 e 是箭头左值,语义一致。layout 不判维数,交给 runtime。 + // = 时 e 是 pratt 右值,-> 时 e 是箭头左值,语义一致。layout 不判维数,交给 runtime。 fn desugar_subscript_write(&mut self, inst: &mut Instruction) { if inst.writes.len() != 1 || !inst.writes[0].contains('[') { return; diff --git a/layout/src/scanner.rs b/layout/src/scanner.rs index b02c2902..7d7dff5e 100644 --- a/layout/src/scanner.rs +++ b/layout/src/scanner.rs @@ -385,17 +385,6 @@ pub fn scan(src: &str) -> Vec { continue; } - // 左箭头 <- - if c == b'<' && i + 1 < src.len() && src[i + 1] == b'-' { - tokens.push(Token { - kind: Kind::Arrow, - value: "<-".to_string(), - pos: p, - quote: 0, - }); - i += 2; - continue; - } // 右箭头 -> if c == b'-' && i + 1 < src.len() && src[i + 1] == b'>' { tokens.push(Token { diff --git a/layout/src/symbol.rs b/layout/src/symbol.rs index 04066099..e4ed3f38 100644 --- a/layout/src/symbol.rs +++ b/layout/src/symbol.rs @@ -2,7 +2,6 @@ // ── 显示用字符串常量 ───────────────────────────────────────────────── -pub const ARROW_LEFT: &str = " <- "; pub const ARROW_RIGHT: &str = " -> "; pub const ARROW_EQ: &str = " = "; @@ -116,7 +115,7 @@ static ENTRIES: &[Entry] = &[ // 箭头 / 赋值(= 兼作 copy opcode) Entry { word: "assign", - glyphs: &["<-", "->", "="], + glyphs: &["->", "="], precedence: 0, arith: false, cmp: false, diff --git a/layout/tests/issue116.rs b/layout/tests/issue116.rs index 248e8525..d4fa2bcb 100644 --- a/layout/tests/issue116.rs +++ b/layout/tests/issue116.rs @@ -442,7 +442,7 @@ fn while_back_edge_is_goto_int64() { #[test] fn break_is_goto_exit() { let mut kv = compile_fn( - "rwfunc f() -> (i:int64) {\n 0 -> i\n while (i < 10) {\n if (i == 5) {\n break\n }\n i <- i + 1\n }\n}\n", + "rwfunc f() -> (i:int64) {\n 0 -> i\n while (i < 10) {\n if (i == 5) {\n break\n }\n i = i + 1\n }\n}\n", ); assert_single_plane(&mut kv, "f"); let p = plane(&mut kv, "f"); @@ -476,7 +476,7 @@ fn break_is_goto_exit() { #[test] fn continue_is_goto_cond() { let mut kv = compile_fn( - "rwfunc f() -> (acc:int64) {\n 0 -> acc\n 1 -> i\n while (i <= 5) {\n if (i == 3) {\n i <- i + 1\n continue\n }\n acc <- acc + i\n i <- i + 1\n }\n}\n", + "rwfunc f() -> (acc:int64) {\n 0 -> acc\n 1 -> i\n while (i <= 5) {\n if (i == 3) {\n i = i + 1\n continue\n }\n acc = acc + i\n i = i + 1\n }\n}\n", ); assert_single_plane(&mut kv, "f"); let p = plane(&mut kv, "f"); @@ -507,7 +507,7 @@ fn continue_is_goto_cond() { #[test] fn for_is_br_goto_single_plane() { let mut kv = compile_fn( - "rwfunc f() -> () {\n data·0 <- 0\n data·1 <- 1\n for (x in data) {\n x -> _\n }\n}\n", + "rwfunc f() -> () {\n data·0 = 0\n data·1 = 1\n for (x in data) {\n x -> _\n }\n}\n", ); assert_single_plane(&mut kv, "f"); let p = plane(&mut kv, "f"); @@ -534,7 +534,7 @@ fn for_is_br_goto_single_plane() { #[test] fn nested_if_while_single_plane() { let mut kv = compile_fn( - "rwfunc f() -> (acc:int64) {\n 0 -> acc\n 1 -> i\n while (i <= 3) {\n if (i == 2) {\n acc <- acc + i\n } else {\n acc <- acc + 1\n }\n i <- i + 1\n }\n}\n", + "rwfunc f() -> (acc:int64) {\n 0 -> acc\n 1 -> i\n while (i <= 3) {\n if (i == 2) {\n acc = acc + i\n } else {\n acc = acc + 1\n }\n i = i + 1\n }\n}\n", ); assert_single_plane(&mut kv, "f"); let p = plane(&mut kv, "f"); diff --git a/site/src/highlight.ts b/site/src/highlight.ts index 4a92b7bd..bc6a5972 100644 --- a/site/src/highlight.ts +++ b/site/src/highlight.ts @@ -42,7 +42,7 @@ function esc(s: string): string { } const TOKEN = - /("""[\s\S]*?"""|#[^\n]*|"(?:\\.|[^"\\])*"|\/[A-Za-z0-9_/·.\-]+|\b\d+(?:\.\d+)?\b|->|<-|·|[A-Za-z_][A-Za-z0-9_]*)/g; + /("""[\s\S]*?"""|#[^\n]*|"(?:\\.|[^"\\])*"|\/[A-Za-z0-9_/·.\-]+|\b\d+(?:\.\d+)?\b|->|·|[A-Za-z_][A-Za-z0-9_]*)/g; export function highlightKv(code: string): string { let out = ""; @@ -55,7 +55,7 @@ export function highlightKv(code: string): string { if (t.startsWith("#")) cls = "tok-comment"; else if (t.startsWith('"')) cls = "tok-string"; else if (t.startsWith("/")) cls = "tok-path"; - else if (t === "->" || t === "<-" || t === "·") cls = "tok-op"; + else if (t === "->" || t === "·") cls = "tok-op"; else if (/^\d/.test(t)) cls = "tok-num"; else if (KEYWORDS.has(t)) cls = "tok-kw"; else if (TYPES.has(t)) cls = "tok-type"; diff --git a/stdlib/kv.kv b/stdlib/kv.kv index 91ab7dd9..8e0ea549 100644 --- a/stdlib/kv.kv +++ b/stdlib/kv.kv @@ -5,13 +5,13 @@ lib kv { rwfunc has(path:[]char/utf32) -> (result:bool) { - v <- kv·get(path) - sentinel <- kv·get("/___kv_stdlib_null___") + v = kv·get(path) + sentinel = kv·get("/___kv_stdlib_null___") result = !(v == sentinel) } rwfunc get_or(path:[]char/utf32, default_val:[]char/utf32) -> (result:[]char/utf32) { - v <- kv·get(path) - sentinel <- kv·get("/___kv_stdlib_null___") + v = kv·get(path) + sentinel = kv·get("/___kv_stdlib_null___") if (v == sentinel) { result = default_val } else { @@ -19,8 +19,8 @@ lib kv { } } rwfunc set_default(path:[]char/utf32, val:[]char/utf32) -> () { - existing <- kv·get(path) - sentinel <- kv·get("/___kv_stdlib_null___") + existing = kv·get(path) + sentinel = kv·get("/___kv_stdlib_null___") if (existing == sentinel) { kv·set(path, val) } diff --git a/stdlib/kvlang/kvlangbrief.kv b/stdlib/kvlang/kvlangbrief.kv index 86dadcab..0ca6d7e1 100644 --- a/stdlib/kvlang/kvlangbrief.kv +++ b/stdlib/kvlang/kvlangbrief.kv @@ -19,20 +19,20 @@ kvlang 是一门全新语言,语法以下面示例为准,不要套用其它 文件入口约定是 `rwfunc test() -> () { … }`——`kvlang xxx.kv` 会自动运行 `test`,**不要**再手写 `test()` 调用。其它函数(含习惯叫的 `main`)必须被 `test` 直接或间接调用才会执行。 ```kv rwfunc test() -> () { - total = 0 // = 等价 <-,写左边这个槽 + total = 0 1 -> i // -> 写右边这个槽 - while (i <= 5) { total <- total + i; i + 1 -> i } + while (i <= 5) { total = total + i; i + 1 -> i } println(total) // 15 } ``` ## 赋值三形式与调用 -- `x = e` / `x <- e`:写左边的槽;`e -> x`:写右边的槽。`=` 不是表达式,不能嵌进条件里——条件相等判断一律用 `==`。 +- `x = e` / `x = e`:写左边的槽;`e -> x`:写右边的槽。`=` 不是表达式,不能嵌进条件里——条件相等判断一律用 `==`。 - 调用只能通过写参映射拿结果,没有返回值:`f(a,b) -> r`;丢弃 `-> _`;多个 `-> x, y`。 - 写槽必须是位置:裸名(帧局部)、`/绝对/路径`(全局)、`base·name`(成员)。字面量不能当写槽。 ## rwfunc 读参/写参 -- 读参 `(a:int64)` 只读,函数体内不能把读参放进写槽(含 `a[i] <- v`)。 +- 读参 `(a:int64)` 只读,函数体内不能把读参放进写槽(含 `a[i] = v`)。 - 写参 `-> (acc:int64)` 体内可读可写——累加器、要被修改的数组都声明为写参。 - **写参初值是 None,不是 0**:累加前必须先显式 `0 -> acc`,否则首次 `acc + x` 是 `None + x` 直接失败。 ```kv diff --git "a/stdlib/kvlang/spec/01-\350\257\215\346\263\225/08-\350\277\220\347\256\227\347\254\246\345\237\272\347\241\200.kv" "b/stdlib/kvlang/spec/01-\350\257\215\346\263\225/08-\350\277\220\347\256\227\347\254\246\345\237\272\347\241\200.kv" index 3dd99554..00551459 100644 --- "a/stdlib/kvlang/spec/01-\350\257\215\346\263\225/08-\350\277\220\347\256\227\347\254\246\345\237\272\347\241\200.kv" +++ "b/stdlib/kvlang/spec/01-\350\257\215\346\263\225/08-\350\277\220\347\256\227\347\254\246\345\237\272\347\241\200.kv" @@ -15,12 +15,12 @@ lib kvlang/spec/词法/运算符基础 { kvlang 有两种赋值/方向记号,均词法化为 `Arrow`: -- `=`——写入左侧; -- `->`——写入右侧(数据从左流向右)。 +- `->`——写右(数据从左流向右)。**契合参数轴布局**:读参在负轴、写参在正轴,箭头方向即数据流方向(负轴读入 → 零轴执行 → 正轴写出),与数学坐标系「负 → 正」直觉一致,详见 [[指令架构]]。 +- `=`——写左。**沿用主流编程语言习惯**(左写、右读),便于带着 C/Python/Rust/Go/JS 直觉的使用者上手。 ``` -x = 42 -42 -> x // 写入右侧 +x = 42 // 左写、右读 +42 -> x // 左读、右写 ``` `=` 仅为写入记号,kvlang **无**独立的等号赋值语义之外用法;相等比较写作 `==`。**无 `<-`**:历史上有第三种 `<-`(写入左侧),与 `=` 完全重复,已删除——写入左侧一律写 `=`。 diff --git "a/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/07-\346\225\260\347\273\204\344\270\244\347\247\215\347\211\251\347\220\206\345\275\242\346\200\201.kv" "b/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/07-\346\225\260\347\273\204\344\270\244\347\247\215\347\211\251\347\220\206\345\275\242\346\200\201.kv" index ff271e50..3a0be01c 100644 --- "a/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/07-\346\225\260\347\273\204\344\270\244\347\247\215\347\211\251\347\220\206\345\275\242\346\200\201.kv" +++ "b/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/07-\346\225\260\347\273\204\344\270\244\347\247\215\347\211\251\347\220\206\345\275\242\346\200\201.kv" @@ -2,7 +2,7 @@ lib kvlang/spec/kvspace模型/数组两种物理形态 { r####"# 数组的两种物理形态 -理论上的数组(含多维)在 kvlang 有两种物理落盘形态: +理论上的数组(含多维)在 kvlang 有两种物理存储形态: | 形态 | langtype | 触发写法 | 元素位置 | XValue 数 | |------|----------|---------|---------|----------| diff --git "a/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/08-\346\214\207\344\273\244\345\270\203\345\261\200\346\240\274\345\274\217.kv" "b/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/08-\346\214\207\344\273\244\345\270\203\345\261\200\346\240\274\345\274\217.kv" index ed4942dd..a83b97bb 100644 --- "a/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/08-\346\214\207\344\273\244\345\270\203\345\261\200\346\240\274\345\274\217.kv" +++ "b/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/08-\346\214\207\344\273\244\345\270\203\345\261\200\346\240\274\345\274\217.kv" @@ -2,7 +2,7 @@ lib kvlang/spec/kvspace模型/指令布局格式 { r####"# 指令布局格式 -本章界定函数经 layout 后在 `/lib` 下的 KV 布局:指令如何以坐标键落盘、签名与命名参数如何编码、调用如何经 extindex 复用指令树、参数如何经指针链解析。 +本章界定函数经 layout 后在 `/lib` 下的 KV 布局:指令如何以坐标键落入 kvspace、签名与命名参数如何编码、调用如何经 extindex 复用指令树、参数如何经指针链解析。 ## 空间布局,非线性字节码 diff --git "a/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/10-\347\263\273\347\273\237\345\217\230\351\207\217.kv" "b/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/10-\347\263\273\347\273\237\345\217\230\351\207\217.kv" index 2d2f70ab..774bf4eb 100644 --- "a/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/10-\347\263\273\347\273\237\345\217\230\351\207\217.kv" +++ "b/stdlib/kvlang/spec/02-kvspace\346\250\241\345\236\213/10-\347\263\273\347\273\237\345\217\230\351\207\217.kv" @@ -40,7 +40,7 @@ lib kvlang/spec/kvspace模型/系统变量 { ## 语法层保留名 `._` -`._` 是源码层的丢弃槽占位符:写目标为 `._` 时不落 kvspace(帧槽键构造遇此名返回空路径)。它是语法占位符,**不**是 `‥` 系统变量,不落盘。 +`._` 是源码层的丢弃槽占位符:写目标为 `._` 时不落 kvspace(帧槽键构造遇此名返回空路径)。它是语法占位符,**不**是 `‥` 系统变量,不落入 kvspace。 ## `debugger()` diff --git "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/04-\346\236\204\351\200\240\345\231\250.kv" "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/04-\346\236\204\351\200\240\345\231\250.kv" index b7c4decf..61173a85 100644 --- "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/04-\346\236\204\351\200\240\345\231\250.kv" +++ "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/04-\346\236\204\351\200\240\345\231\250.kv" @@ -21,6 +21,6 @@ u = char/ascii(t) // 非 ASCII 码点 → TypeError | float32 精度 | 收窄至单精度(`float32(3.141592653589793)`=3.1415927、`float32(16777217)`=16777216.0) | | None 输入 | TypeError(`int64(None)` 报错,不得静默产生 0) | -构造器接受字面量与变量表达式(`42.9 -> x; int8(x)`)。种类构造的结果 XValue 以该种类名为 kindexpr 落盘,精度信息随值保留。 +构造器接受字面量与变量表达式(`42.9 -> x; int8(x)`)。种类构造的结果 XValue 以该种类名为 kindexpr 落入 kvspace,精度信息随值保留。 "#### -> /lib/kvlang/spec/类型系统/构造器 } diff --git "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/05-kindexpr\347\255\276\345\220\215\347\261\273\345\236\213\350\241\250\350\276\276\345\274\217.kv" "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/05-kindexpr\347\255\276\345\220\215\347\261\273\345\236\213\350\241\250\350\276\276\345\274\217.kv" index ccf91c4f..0503af8a 100644 --- "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/05-kindexpr\347\255\276\345\220\215\347\261\273\345\236\213\350\241\250\350\276\276\345\274\217.kv" +++ "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/05-kindexpr\347\255\276\345\220\215\347\261\273\345\236\213\350\241\250\350\276\276\345\274\217.kv" @@ -2,14 +2,14 @@ lib kvlang/spec/类型系统/kindexpr签名类型表达式 { r####"# kindexpr 签名类型表达式 -**类型表达式**是函数(rwfunc)与 def rwir 签名中参数、返回值的类型标注语法。它既供人和工具读写,也逐字节落盘为运行时匹配的依据。同一套表达式贯穿 layout(静态校验)与 runtime(值匹配),故本卷统称 **kindexpr**。 +**类型表达式**是函数(rwfunc)与 def rwir 签名中参数、返回值的类型标注语法。它既供人和工具读写,也逐字节落入 kvspace 作为运行时匹配的依据。同一套表达式贯穿 layout(静态校验)与 runtime(值匹配),故本卷统称 **kindexpr**。 完整权威文法见 [[文法]](附录)的「类型表达式」小节;本章只界定其语义。 ## 锚例 - 标量并集、多维形状、动态维、字符串标注:`tutorial/10-types/`(`01-typed-map.kv`~`05-tuple-key.kv`)。 -- 嵌套 mapexpr 与元组键落盘 round-trip:`tutorial/10-types/04-nested-type.kv`、`05-tuple-key.kv`。 +- 嵌套 mapexpr 与元组键落入 kvspace round-trip:`tutorial/10-types/04-nested-type.kv`、`05-tuple-key.kv`。 - 变参 `...`:`print`/`println`/`min`/`max` 全 tutorial 广泛使用。 "#### -> /lib/kvlang/spec/类型系统/kindexpr签名类型表达式 } diff --git "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/06-\345\275\222\345\261\236\344\270\216\350\220\275\347\233\230.kv" "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/06-\345\275\222\345\261\236\344\270\216\345\255\230\345\202\250.kv" similarity index 72% rename from "stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/06-\345\275\222\345\261\236\344\270\216\350\220\275\347\233\230.kv" rename to "stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/06-\345\275\222\345\261\236\344\270\216\345\255\230\345\202\250.kv" index 20c85528..f5c5ec7c 100644 --- "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/06-\345\275\222\345\261\236\344\270\216\350\220\275\347\233\230.kv" +++ "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/06-\345\275\222\345\261\236\344\270\216\345\255\230\345\202\250.kv" @@ -1,14 +1,14 @@ // 欢迎加入kvspace世界 -lib kvlang/spec/类型系统/归属与落盘 { - r####"# 归属与落盘 +lib kvlang/spec/类型系统/归属与存储 { + r####"# 归属与存储 | 归属 | 存放位置 | 种类 | |------|---------|------| | 用户函数签名 | `/lib/·` | `rwfunc` | | def rwir 路由头 | `/lib/` | `def rwir` | -| native 算子签名 | C 注册表(不落盘) | —— | +| native 算子签名 | C 注册表(不落入 kvspace) | —— | -**铁律**:`rwfunc` 每个参数落盘的 kindexpr 与源码里的类型标注**逐字节相同**(同一文法,含 `...`);`def rwir` 源码不声明,其参数 kindexpr 由各 runtime 注册时按同一文法写入。签名**不写进 langtype 串**,也不塞进主槽 body,分两处存(详见 [[总体方案]]「签名不入 langtype 串」): +**铁律**:`rwfunc` 每个参数落入 kvspace 的 kindexpr 与源码里的类型标注**逐字节相同**(同一文法,含 `...`);`def rwir` 源码不声明,其参数 kindexpr 由各 runtime 注册时按同一文法写入。签名**不写进 langtype 串**,也不塞进主槽 body,分两处存(详见 [[总体方案]]「签名不入 langtype 串」): 1. **主槽计数头**:主槽(如 `/lib/pkg·funca`)body 只记 `[nr:u16 LE][nw:u16 LE][dynamic:u8]`——读参个数 `nr`、写参个数 `nw`、是否变参 `dynamic`(末位读参 `...`);**不含任何参数 kindexpr 串**。 2. **签名行 `[0,x]` 各槽**:每个参数的 kindexpr 串落在签名行坐标 `[0,x]`(`x<0` 读参、`x>0` 写参、`[0,0]` 签名行锚点),每槽为一个 `def kindexpr` 类型的 xvalue,body 存该参数完整 kindexpr 串。 @@ -18,5 +18,5 @@ lib kvlang/spec/类型系统/归属与落盘 { ## 强制类型标注 `rwfunc` 签名中每个参数与每个返回值**必须**声明类型(`name:type_expr`)。缺标注的签名**拒绝装载**——layout 在语法检查期报错,指出该参数/返回值无类型标注。类型标注须通过 kindexpr 合法性校验(拒绝非种类名,见下)。`def rwir` 路由头由 runtime 注册(源码不声明),其参数类型在注册接口处按同一合法性规则校验。 -"#### -> /lib/kvlang/spec/类型系统/归属与落盘 +"#### -> /lib/kvlang/spec/类型系统/归属与存储 } diff --git "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/18-\346\200\273\344\275\223\346\226\271\346\241\210.kv" "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/18-\346\200\273\344\275\223\346\226\271\346\241\210.kv" index 07d5edd3..e92f7b93 100644 --- "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/18-\346\200\273\344\275\223\346\226\271\346\241\210.kv" +++ "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/18-\346\200\273\344\275\223\346\226\271\346\241\210.kv" @@ -4,7 +4,7 @@ lib kvlang/spec/类型系统/总体方案 { > **本卷为新方案,待敲定。** 敲定后据此重构 [[种类与定宽类型]]、[[kindexpr签名类型表达式]]、[[数组形态]]、[[容器]]。 -每个 XValue 落盘时,其类别由 head 三个**正交**字段表达,彻底取代旧的单一 `kind` 分类与 kindexpr 首字符 `*`/`@` 前缀。 +每个 XValue 落入 kvspace 时,其类别由 head 三个**正交**字段表达,彻底取代旧的单一 `kind` 分类与 kindexpr 首字符 `*`/`@` 前缀。 - **`ref`(存储位置)** — 值放在哪:inline / 指针 / 扩展世界。1 字节。 - **`storetype`(物理布局,codec 视角)** — 字节怎么切。**闭合小集**,1 字节。 diff --git "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/20-ref\345\255\230\345\202\250\344\275\215\347\275\256.kv" "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/20-ref\345\255\230\345\202\250\344\275\215\347\275\256.kv" index 0e82b0b8..2c84289c 100644 --- "a/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/20-ref\345\255\230\345\202\250\344\275\215\347\275\256.kv" +++ "b/stdlib/kvlang/spec/03-\347\261\273\345\236\213\347\263\273\347\273\237/20-ref\345\255\230\345\202\250\344\275\215\347\275\256.kv" @@ -6,7 +6,7 @@ lib kvlang/spec/类型系统/ref存储位置 { | ref | body | 义 | |-----|------|----| -| 0 inline | 值 raw 本体 | 就地落盘 | +| 0 inline | 值 raw 本体 | 就地落入 kvspace | | 1 ptr | 目标 key 路径 | 软链接,单跳指向同型目标(head 的 storetype/langtype 即目标形态);`ptr → rwfunc` 即函数调用链 | | 2 @ext | 扩展句柄/定位符 | 单个值本体在扩展世界(fs 文件 / gpu tensordata),head 存元描述 | diff --git "a/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/01-\346\214\207\344\273\244\346\236\266\346\236\204.kv" "b/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/01-\346\214\207\344\273\244\346\236\266\346\236\204.kv" index 921b1600..86f7b04e 100644 --- "a/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/01-\346\214\207\344\273\244\346\236\266\346\236\204.kv" +++ "b/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/01-\346\214\207\344\273\244\346\236\266\346\236\204.kv" @@ -2,7 +2,7 @@ lib kvlang/spec/layout语义/指令架构 { r####"# 指令架构 -layout 把每个函数体布进 kvspace 的一棵子树:**指令即路径**,每条指令占据一个二维坐标 `[s0, s1]`。本章界定该坐标系、opcode 与读写槽的角色、三种赋值书写形态,以及槽值的 XValue 编码。二维坐标的线格式(TLV、head kindexpr)见 [[指令布局格式]](kvspace 模型卷);本章只界定 layout 产出的槽位语义。 +layout 把每个函数体布进 kvspace 的一棵子树:**指令即路径**,每条指令占据一个二维坐标 `[s0, s1]`。本章界定该坐标系、opcode 与读写槽的角色、两种赋值书写形态,以及槽值的 XValue 编码。二维坐标的线格式(TLV、head kindexpr)见 [[指令布局格式]](kvspace 模型卷);本章只界定 layout 产出的槽位语义。 ## 二维坐标系 `[s0, s1]` @@ -34,16 +34,21 @@ layout 把每个函数体布进 kvspace 的一棵子树:**指令即路径**, 写参扇出(同一结果写入多个位置)以多个正轴槽表示:`a + b -> sum, backup` 布出 `[s0,1]="sum"`、`[s0,2]="backup"`。 -## 三种赋值书写形态 +## 两种赋值书写形态 -赋值有三种等价书写,写槽约束完全一致: +赋值有两种书写,写槽约束完全一致,仅方向不同: -| 形态 | 写槽位置 | 例 | -|------|---------|-----| -| `expr -> writes` | 右 | `A + B -> C` | -| `writes = expr` | 左 | `C = A + B` | +| 形态 | 数据流 | 写槽位置 | 例 | +|------|--------|---------|-----| +| `expr -> writes` | 左读、右写 | 右 | `A + B -> C` | +| `writes = expr` | 左写、右读 | 左 | `C = A + B` | -`=` 与 `->` **不是**表达式,**不得**嵌套于条件或实参中;相等比较写作 `==`。两种方向记号写槽位置不同(`->` 右写、`=` 左写),同一次赋值只取一种方向。 +- `->` **契合参数轴布局**:读参在负轴 `[s0,-n]`、写参在正轴 `[s0,+n]`,数据从负轴读入、经零轴 opcode、向正轴写出(见上「二维坐标系」)。书写时同样是「左读、右写」,箭头方向即数据流方向,与数学坐标系「负 → 正」的直觉一致。 +- `=` **沿用主流编程语言习惯**:左写、右读,便于带着 C/Python/Rust/Go/JS 直觉的使用者上手。 + +两形态语义等价,同一次赋值只取一种方向,可按可读性择一。`=` 与 `->` **不是**表达式,**不得**嵌套于条件或实参中;相等比较写作 `==`。(历史上曾有第三种 `<-`(左写),与 `=` 完全重复,已删除——写槽在左一律写 `=`。) + +**落入 kvspace 一律是 `->` 轴结构。** `=` 只是单条指令「写左」的源码别名,方便书写;无论源码写 `=` 还是 `->`,layout 落入 kvspace 后的 rwir 与 rwfunc 签名**一致**按 `->` 的「左读 / 右写」轴布局——读参在负轴 `[s0,-n]`、写参在正轴 `[s0,+n]`。因此 rwfunc 签名的对外接口 `(读参) -> (写参)`、签名行 `[0,·]` 只用 `->` 形态表达,没有 `=` 形式的签名(见 [[函数]])。`=` 从不改变槽位布局,仅决定同一条指令里写槽写在源码的哪一侧。 **写槽必须是位置(location)**:裸名(帧内变量)、绝对路径(`/abs`)、成员写(`base·field` / `base·*key`)、下标写(`arr[idx]`)。字面量出现在写槽位置是错误,layout 报诊断(见 [[诊断]])。 "#### -> /lib/kvlang/spec/layout语义/指令架构 diff --git "a/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/03-\345\207\275\346\225\260.kv" "b/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/03-\345\207\275\346\225\260.kv" index 50270079..5e7c7292 100644 --- "a/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/03-\345\207\275\346\225\260.kv" +++ "b/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/03-\345\207\275\346\225\260.kv" @@ -2,7 +2,9 @@ lib kvlang/spec/layout语义/函数 { r####"# 函数 -kvlang 源码只有一种命名单元:**rwfunc**(有指令体的用户函数),暴露 `(读参) -> (写参)` 箭头接口。函数**没有返回值**,只有读参与写参。rwir 是 runtime 兑现的原子能力,源码**不声明**、只调用;其 `def rwir` 路由头由各 runtime 注册到 `/lib`(见 [[rwfunc布局与def_rwir]])。 +kvlang 源码只有一种命名单元:**rwfunc**(有指令体的用户函数),暴露 `(读参) -> (写参)` 箭头接口。函数**没有返回值**,只有读参与写参。 + +签名与体内 rwir 落入 kvspace 的布局**一律**按 `->` 的「左读 / 右写」轴实现:读参在负轴、写参在正轴(见 [[指令架构]])。`=` 只存在于 kvlang 上层 code 层,是单条指令「写左」的书写别名,不进入签名、也不改变槽位布局——所以没有 `=` 形式的 rwfunc 签名,接口只用 `(读参) -> (写参)` 表达。rwir 是 runtime 兑现的原子能力,源码**不声明**、只调用;其 `def rwir` 路由头由各 runtime 注册到 `/lib`(见 [[rwfunc布局与def_rwir]])。 ## 读参与写参 diff --git "a/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/12-rwfunc\345\270\203\345\261\200\344\270\216def-rwir.kv" "b/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/12-rwfunc\345\270\203\345\261\200\344\270\216def-rwir.kv" index 0b6b90c1..606f943b 100644 --- "a/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/12-rwfunc\345\270\203\345\261\200\344\270\216def-rwir.kv" +++ "b/stdlib/kvlang/spec/04-layout\350\257\255\344\271\211/12-rwfunc\345\270\203\345\261\200\344\270\216def-rwir.kv" @@ -20,7 +20,7 @@ lib kvlang/spec/layout语义/rwfunc布局与def_rwir { layout **不**判定某 opcode 是否在本 runtime 的 myrwircaps 内——调用目标 opcode 槽统一 langtype `rwir|rwfunc`,判定推迟到 runtime 查 `/lib/` 的 XValue langtype(见 [[执行模型]])。def rwir 路由头仅落于 `/lib/`,无 native/扩展/user 的静态归类——每个 runtime 只声明自身 myrwircaps,不在 layout 层区分。 -## native rwir 不落盘 +## native rwir 不落入 kvspace `+`、`==`、`array`、`sqrt`、`print` 等 native builtin 融合在 runtime 内建表,**不**写 kvspace、运行时也不回查 `/lib`。数值多类型算子在内建表中融合为单条(`add` 覆盖 int8…float64),派发前剥掉 `·` 前缀,输出 kind 由实参决定。 diff --git "a/stdlib/kvlang/spec/05-runtime\350\257\255\344\271\211/06-rwirext\346\211\251\345\261\225.kv" "b/stdlib/kvlang/spec/05-runtime\350\257\255\344\271\211/06-rwirext\346\211\251\345\261\225.kv" index 2c8c6c4a..c8856f40 100644 --- "a/stdlib/kvlang/spec/05-runtime\350\257\255\344\271\211/06-rwirext\346\211\251\345\261\225.kv" +++ "b/stdlib/kvlang/spec/05-runtime\350\257\255\344\271\211/06-rwirext\346\211\251\345\261\225.kv" @@ -26,7 +26,7 @@ kvlang 是**小核心 + 扩展主导**语言。中央 runtime 只实现执行核 例:`/lib/json·to`(nr=1, nw=1)→ 主槽 body=[1,1,0],`[0,-1]=def kindexpr("char/utf8")`,`[0,1]=def kindexpr("[]char/utf8")`。 -- native builtin 的签名**不落盘**——在 C 注册表直接查表(见 [[函数调用与内建]])。 +- native builtin 的签名**不落入 kvspace**——在 C 注册表直接查表(见 [[函数调用与内建]])。 - 扩展签名**落 `/lib/`**,中央 runtime 经 `isothersrwir` 识别:`opcode[0]=='/'` → false;否则读 `/lib/` 的 kind,等于 `def rwir` 即须经 def rwir 路由的 rwir。 ### 全局标记与幂等 diff --git "a/stdlib/kvlang/spec/\350\256\276\350\256\241\347\220\206\347\224\261/04-\344\273\243\347\240\201\345\261\202\347\272\247.kv" "b/stdlib/kvlang/spec/\350\256\276\350\256\241\347\220\206\347\224\261/04-\344\273\243\347\240\201\345\261\202\347\272\247.kv" index 69e17816..f9b077b4 100644 --- "a/stdlib/kvlang/spec/\350\256\276\350\256\241\347\220\206\347\224\261/04-\344\273\243\347\240\201\345\261\202\347\272\247.kv" +++ "b/stdlib/kvlang/spec/\350\256\276\350\256\241\347\220\206\347\224\261/04-\344\273\243\347\240\201\345\261\202\347\272\247.kv" @@ -26,9 +26,9 @@ rwir(read-write IR)是最小执行单元,原子性是其定义的一部分 原子性还带来 layout 层面的好处:layout 对 rwir 签名做完整的类型检查,若 rwir 可拆分则类型检查边界模糊。 -## 为什么 native rwir 不落盘 +## 为什么 native rwir 不落入 kvspace -def rwir 路由头(各 runtime myrwircaps 对外的统一声明入口)存于 `/lib/`(kindexpr 存入 kvspace),而 native rwir 的签名内联在 C runtime 注册表里不落盘。 +def rwir 路由头(各 runtime myrwircaps 对外的统一声明入口)存于 `/lib/`(kindexpr 存入 kvspace),而 native rwir 的签名内联在 C runtime 注册表里不落入 kvspace。 理由是:native rwir 是 kvlang runtime 的 ABI 契约,其签名由 runtime 版本唯一决定,不受 kvspace 内容影响。把它写入 kvspace 会造成"kvspace 里有一份 runtime 认为应该是什么样子"与"C 代码里实际注册的是什么"两份事实源,产生版本漂移风险。内联在 C 表里,签名与实现共生,不存在不一致的可能。 "#### -> /lib/kvlang/spec/设计理由/代码层级 diff --git "a/stdlib/kvlang/spec/\351\231\204\345\275\225/04-\346\214\207\344\273\244.kv" "b/stdlib/kvlang/spec/\351\231\204\345\275\225/04-\346\214\207\344\273\244.kv" index 770bc213..cab02746 100644 --- "a/stdlib/kvlang/spec/\351\231\204\345\275\225/04-\346\214\207\344\273\244.kv" +++ "b/stdlib/kvlang/spec/\351\231\204\345\275\225/04-\346\214\207\344\273\244.kv" @@ -3,9 +3,8 @@ lib kvlang/spec/附录/指令 { r##"# 指令 ``` -instruction = [ writes "=" ] expr - | [ writes "=" ] expr - | expr [ "->" writes ] +instruction = [ writes "=" ] expr (* 左写:writes = expr *) + | expr [ "->" writes ] (* 右写:expr -> writes *) | expr (* 纯副作用调用 *) writes = write_slot { "," write_slot } diff --git a/stdlib/string.kv b/stdlib/string.kv index b3e00343..70257b6d 100644 --- a/stdlib/string.kv +++ b/stdlib/string.kv @@ -18,8 +18,8 @@ lib string { string·find(s, sub) != -1 -> r } rwfunc startswith(s:[]char/utf32, pre:[]char/utf32) -> (r:bool) { - sl <- string·len(s) - pl <- string·len(pre) + sl = string·len(s) + pl = string·len(pre) if (pl > sl) { false -> r } else { @@ -28,8 +28,8 @@ lib string { } } rwfunc endswith(s:[]char/utf32, suf:[]char/utf32) -> (r:bool) { - sl <- string·len(s) - fl <- string·len(suf) + sl = string·len(s) + fl = string·len(suf) if (fl > sl) { false -> r } else { @@ -39,27 +39,27 @@ lib string { } } rwfunc reverse(s:[]char/utf32) -> (r:[]char/utf32) { - n <- string·len(s) + n = string·len(s) "" -> r - i <- n - 1 + i = n - 1 while (i >= 0) { string·char(s, i) -> c string·concat(r, c) -> r - i <- i - 1 + i = i - 1 } } rwfunc repeat(s:[]char/utf32, n:int64) -> (r:[]char/utf32) { "" -> r - i <- 0 + i = 0 while (i < n) { string·concat(r, s) -> r - i <- i + 1 + i = i + 1 } } rwfunc upper(s:[]char/utf32) -> (r:[]char/utf32) { "" -> r - n <- string·len(s) - i <- 0 + n = string·len(s) + i = 0 while (i < n) { string·char(s, i) -> c string·find("abcdefghijklmnopqrstuvwxyz", c) -> idx @@ -69,13 +69,13 @@ lib string { } else { string·concat(r, c) -> r } - i <- i + 1 + i = i + 1 } } rwfunc lower(s:[]char/utf32) -> (r:[]char/utf32) { "" -> r - n <- string·len(s) - i <- 0 + n = string·len(s) + i = 0 while (i < n) { string·char(s, i) -> c string·find("ABCDEFGHIJKLMNOPQRSTUVWXYZ", c) -> idx @@ -85,7 +85,7 @@ lib string { } else { string·concat(r, c) -> r } - i <- i + 1 + i = i + 1 } } } diff --git a/stdlib/xv.kv b/stdlib/xv.kv index e5165924..f3461a16 100644 --- a/stdlib/xv.kv +++ b/stdlib/xv.kv @@ -5,9 +5,9 @@ lib xv { rwfunc swap(arr:[]int64, i:int64, j:int64) -> (result:[]int64) { - vi <- xv·at(arr, i) - vj <- xv·at(arr, j) - tmp <- xv·set(arr, i, vj) + vi = xv·at(arr, i) + vj = xv·at(arr, j) + tmp = xv·set(arr, i, vj) result = xv·set(tmp, j, vi) } rwfunc first(arr:[]int64) -> (result:int64) { diff --git a/tutorial/01-basics/arith.kv b/tutorial/01-basics/arith.kv index 448b070f..b32cd315 100644 --- a/tutorial/01-basics/arith.kv +++ b/tutorial/01-basics/arith.kv @@ -10,16 +10,16 @@ // sqrt: 12.0 // sqrt(√): 12.0 rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() 10 + 3 -> add_r println("add:", add_r) 10 - 3 -> sub_r println("sub:", sub_r) - mul_r = 10 × 3 // = 等价于 <- + mul_r = 10 × 3 println("mul:", mul_r) - mul_r2 = 10 × 3 // = 等价于 <- + mul_r2 = 10 × 3 println("mul(×):", mul_r2) 10 ÷ 3 -> div_r @@ -30,16 +30,16 @@ rwfunc test() -> () { 10 % 3 -> mod_r println("mod:", mod_r) - pow_r <- pow(2, 5) + pow_r = pow(2, 5) println("pow:", pow_r) - sqrt_r = sqrt(144) // = 等价于 <- + sqrt_r = sqrt(144) println("sqrt:", sqrt_r) - sqrt_r2 = √(144) // = 等价于 <- + sqrt_r2 = √(144) println("sqrt(√):", sqrt_r2) - t1 <- time·now() - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + t1 = time·now() + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) } diff --git a/tutorial/01-basics/cast.kv b/tutorial/01-basics/cast.kv index ff2627a0..ee289632 100644 --- a/tutorial/01-basics/cast.kv +++ b/tutorial/01-basics/cast.kv @@ -22,7 +22,7 @@ // 42 rwfunc test() -> () { // ── int8/16/32/64 ── - int8(127) + int8(1) -> i8 // = 等价于 <-(回绕到 -128) + int8(127) + int8(1) -> i8 println(i8) println(int16(32767)) println(int32(2147483647)) @@ -35,16 +35,16 @@ rwfunc test() -> () { println(uint64(18446744073709551615)) // ── float32 ──(精度域:~7 位有效数字) - a <- float32(3.14) + a = float32(3.14) println(a) println(float32(3.141592653589793)) // ── float64 ── - b = float64(3.141592653589793) // = 等价于 <- + b = float64(3.141592653589793) println(b) // ── float→int 截断向零 ── - b -> f64 // = 等价于 <- + b -> f64 println(int64(f64)) println(int32(3.9)) println(int32(-2.7)) diff --git a/tutorial/01-basics/hello.kv b/tutorial/01-basics/hello.kv index 99aa4145..c738bf61 100644 --- a/tutorial/01-basics/hello.kv +++ b/tutorial/01-basics/hello.kv @@ -1,10 +1,10 @@ // 期望输出: // hello kvlang rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() println("hello kvlang") - t1 <- time·now() - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + t1 = time·now() + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) } diff --git a/tutorial/01-basics/numtypes.kv b/tutorial/01-basics/numtypes.kv index bfa36b05..3b572278 100644 --- a/tutorial/01-basics/numtypes.kv +++ b/tutorial/01-basics/numtypes.kv @@ -16,9 +16,9 @@ // 45 // -2 rwfunc test() -> () { - f = float32(3) // = 等价于 <- + f = float32(3) println(f) - i <- int8(0.1) + i = int8(0.1) println(i) int8(300) -> w println(w) @@ -29,11 +29,11 @@ rwfunc test() -> () { println(uint32(-1)) println(uint64(18446744073709551615)) - p32 = float32(0.1) // = 等价于 <-(float32 精度域) + p32 = float32(0.1) println(p32) println(float32(16777217)) - sum <- w + 1 + sum = w + 1 println(sum) int16(-2) -> n println(n) diff --git a/tutorial/01-basics/precision.kv b/tutorial/01-basics/precision.kv index 477d8b9d..2c47e8f0 100644 --- a/tutorial/01-basics/precision.kv +++ b/tutorial/01-basics/precision.kv @@ -24,53 +24,53 @@ // 9007199254740993 // false rwfunc test() -> () { - a = 2 + 3 // = 等价于 <- + a = 2 + 3 println(a) - b <- 2 + 3.5 + b = 2 + 3.5 println(b) 1.5 × 2 -> c 1.5 × 2 -> c_m println(c_m) println(c) - q = 7 ÷ 2 // = 等价于 <-(整除:两侧均 int) - q_m = 7 ÷ 2 // = 等价于 <-(整除:两侧均 int) + q = 7 ÷ 2 + q_m = 7 ÷ 2 println(q_m) println(q) -9 ÷ 2 -> qz -9 ÷ 2 -> qz_m println(qz_m) println(qz) - fq <- 7.0 ÷ 2 - fq_m <- 7.0 ÷ 2 + fq = 7.0 ÷ 2 + fq_m = 7.0 ÷ 2 println(fq_m) println(fq) - r = 7 % 3 // = 等价于 <- + r = 7 % 3 println(r) - ti <- int64(3.99) + ti = int64(3.99) println(ti) float64(7) -> tf println(tf) - sci = 1e3 // = 等价于 <-(科学计数法字面量恒为 float) + sci = 1e3 println(sci) - small <- 2.5e-2 + small = 2.5e-2 println(small) 0.1 + 0.2 -> sum println(sum) - eq = 3 == 3.0 // = 等价于 <-(跨类型数值比较:值提升后相等) + eq = 3 == 3.0 println(eq) - big <- 9223372036854775807 + big = 9223372036854775807 println(big) big - 1 -> bm println(bm) - precise = 9007199254740993 + 0 // = 等价于 <-(2^53+1:int 算术不经 float,精度保真) + precise = 9007199254740993 + 0 println(precise) - same <- big == 9223372036854775806 + same = big == 9223372036854775806 println(same) } diff --git a/tutorial/01-basics/random.kv b/tutorial/01-basics/random.kv index aa8ae127..4d5f1514 100644 --- a/tutorial/01-basics/random.kv +++ b/tutorial/01-basics/random.kv @@ -6,13 +6,13 @@ // intn in [0,100): true // two randoms differ: true rwfunc test() -> () { - n <- random·int63() + n = random·int63() println("int63 >= 0:", n >= 0) - m <- random·intn(100) + m = random·intn(100) println("intn in [0,100):", m >= 0 && m < 100) - a <- random·uint64() - b <- random·uint64() + a = random·uint64() + b = random·uint64() println("two randoms differ:", a != b) } diff --git a/tutorial/01-basics/strict_types.kv b/tutorial/01-basics/strict_types.kv index 9d0ec2f6..2b90a036 100644 --- a/tutorial/01-basics/strict_types.kv +++ b/tutorial/01-basics/strict_types.kv @@ -24,7 +24,7 @@ // true rwfunc test() -> () { // 算术 — 同类型 int64 - 5 + 3 -> a // = 等价于 <- + 5 + 3 -> a println(a) 7 - 2 - 2 -> b println(b) @@ -37,7 +37,7 @@ rwfunc test() -> () { println(d) // 位运算 — 仅整数 - 5 & 3 -> e // = 等价于 <- + 5 & 3 -> e println(e) neg(2) -> f println(f) @@ -45,7 +45,7 @@ rwfunc test() -> () { println(g) // 比较 — 同类型 - 5 > 3 -> h // = 等价于 <- + 5 > 3 -> h println(h) 3 > 5 -> i println(i) @@ -70,7 +70,7 @@ rwfunc test() -> () { println(o) // 字符串拼接 - "a" + "bc" -> p // = 等价于 <- + "a" + "bc" -> p println(p) // 字符串比较(C 语义) @@ -78,6 +78,6 @@ rwfunc test() -> () { println(q) // bool 显式比较 - true == true -> r // = 等价于 <- + true == true -> r println(r) } diff --git a/tutorial/01-basics/strings.kv b/tutorial/01-basics/strings.kv index 9d6c0bf3..9610e3cc 100644 --- a/tutorial/01-basics/strings.kv +++ b/tutorial/01-basics/strings.kv @@ -15,12 +15,12 @@ // l // 108 rwfunc test() -> () { - s = "hello" // = 等价于 <- + s = "hello" println(s[1]) - s[0] = "H" // = 等价于 <- + s[0] = "H" println(s) - t <- "kv" + "lang" + t = "kv" + "lang" println(t) s + " " + t -> w println(w) @@ -31,7 +31,7 @@ rwfunc test() -> () { println(string·find(w, "kv")) println(string·find(w, "zz")) - c <- w[3] + c = w[3] println(c) println(string·ord(c)) } diff --git a/tutorial/01-basics/time.kv b/tutorial/01-basics/time.kv index fe41c814..60c000f2 100644 --- a/tutorial/01-basics/time.kv +++ b/tutorial/01-basics/time.kv @@ -12,43 +12,43 @@ // 1 s = 1000000000 ns rwfunc test() -> () { println("before") - t0 <- time·now() - t1 <- time·now() + t0 = time·now() + t1 = time·now() println("after") - before <- time·before(t0, t1) + before = time·before(t0, t1) print("t0 < t1: ") println(before) - after <- time·after(t0, t1) + after = time·after(t0, t1) print("t0 > t1: ") println(after) - delta <- time·sub(t1, t0) + delta = time·sub(t1, t0) print("delta >= 0 ns: ") - zero = time/duration·nanos(0) // = 等价于 <- + zero = time/duration·nanos(0) println(time/duration·before(zero, delta)) - ms <- time/duration·as_millis(delta) + ms = time/duration·as_millis(delta) print("delta ms = ") println(ms) - s <- time/duration·as_seconds(delta) + s = time/duration·as_seconds(delta) print("delta s = ") println(s) - sum <- time·add(t0, delta) + sum = time·add(t0, delta) print("t0 + delta == t1: ") - a <- not(time·before(sum, t1)) - b <- not(time·after(sum, t1)) + a = not(time·before(sum, t1)) + b = not(time·after(sum, t1)) println(and(a, b)) - 1000 -> n // = 等价于 <- + 1000 -> n print(n) println(" ms") - one_sec <- time/duration·seconds(1) - ns <- time/duration·as_nanos(one_sec) + one_sec = time/duration·seconds(1) + ns = time/duration·as_nanos(one_sec) print("1 s = ") print(ns) println(" ns") diff --git a/tutorial/01-basics/vars.kv b/tutorial/01-basics/vars.kv index 6de65a07..95a438ef 100644 --- a/tutorial/01-basics/vars.kv +++ b/tutorial/01-basics/vars.kv @@ -2,8 +2,8 @@ // x = 42 // y = 50 rwfunc test() -> () { - x <- 42 + x = 42 println("x =", x) - y = x + 8 // = 等价于 <- + y = x + 8 println("y =", y) } diff --git a/tutorial/02-func/accumulator.kv b/tutorial/02-func/accumulator.kv index b7f57559..aacd87dc 100644 --- a/tutorial/02-func/accumulator.kv +++ b/tutorial/02-func/accumulator.kv @@ -7,17 +7,17 @@ // 10 // 10 rwfunc sum(arr:[]int64) -> (acc:int64) { - n = ndarray·numel(arr) // = 等价于 <- + n = ndarray·numel(arr) 0 -> acc // 显式初始化累加器(strict null:write parm 首读为 null,拒绝 null 算术) 0 -> i while (i < n) { acc + arr[i] -> acc - i <- i + 1 + i = i + 1 } } rwfunc rsum(arr:[]int64, i:int64, acc:int64) -> (r:int64) { - n <- ndarray·numel(arr) + n = ndarray·numel(arr) if (i >= n) { acc -> r diff --git a/tutorial/02-func/scope_isolation.kv b/tutorial/02-func/scope_isolation.kv index c707b761..b186cef9 100644 --- a/tutorial/02-func/scope_isolation.kv +++ b/tutorial/02-func/scope_isolation.kv @@ -10,8 +10,8 @@ rwfunc id_val(x:int64) -> (R:int64) { } rwfunc sum_diff(A:int64, B:int64) -> (S:int64, D:int64) { - S <- A + B - D = A - B // = 等价于 <- + S = A + B + D = A - B } rwfunc chain_call(a:int64, b:int64) -> (r:int64) { @@ -19,9 +19,9 @@ rwfunc chain_call(a:int64, b:int64) -> (r:int64) { } rwfunc test() -> () { - v1 <- id_val(5) + v1 = id_val(5) println("PASS SI-1a: x=5 returns", v1) - v2 = id_val(10) // = 等价于 <- + v2 = id_val(10) println("PASS SI-1b: x=10 returns", v2) sum_diff(5, 3) -> (s1, d1) println("PASS SI-2a: S=", s1, "D=", d1) @@ -29,5 +29,5 @@ rwfunc test() -> () { println("PASS SI-2b: S=", s2, "D=", d2) chain_call(7, 3) -> c println("PASS SI-3: chain =", c) - /last_chain <- c + /last_chain = c } diff --git a/tutorial/02-func/tco_depth.kv b/tutorial/02-func/tco_depth.kv index 0df6c382..068fa989 100644 --- a/tutorial/02-func/tco_depth.kv +++ b/tutorial/02-func/tco_depth.kv @@ -3,11 +3,11 @@ // sum = 5050 // fact = 3628800 rwfunc sum_while(N:int64) -> (total:int64) { - total = 0 // = 等价于 <- + total = 0 1 -> i while (i <= N) { - total <- total + i - i = i + 1 // = 等价于 <- + total = total + i + i = i + 1 } } @@ -15,18 +15,18 @@ rwfunc tailrec_fact(N:int64, acc:int64) -> (r:int64) { if (N <= 1) { acc + 0 -> r } else { - acc1 <- acc × N - acc1_m <- acc × N - n1 = N - 1 // = 等价于 <- + acc1 = acc × N + acc1_m = acc × N + n1 = N - 1 tailrec_fact(n1, acc1) -> r } } rwfunc test() -> () { - s <- sum_while(100) + s = sum_while(100) println("sum =", s) - f = tailrec_fact(10, 1) // = 等价于 <- + f = tailrec_fact(10, 1) println("fact =", f) s -> /last_tco_sum - /last_tco_fact <- f + /last_tco_fact = f } diff --git a/tutorial/03-control/classify.kv b/tutorial/03-control/classify.kv index d3ef9a57..2af6d63c 100644 --- a/tutorial/03-control/classify.kv +++ b/tutorial/03-control/classify.kv @@ -4,16 +4,16 @@ // grade = B rwfunc classify(score:int64) -> (grade:[]char/utf32) { if (score >= 90) { - grade = "A" // = 等价于 <- + grade = "A" } else { if (score >= 80) { "B" -> grade } else { if (score >= 70) { - grade <- "C" + grade = "C" } else { if (score >= 60) { - grade = "D" // = 等价于 <- + grade = "D" } else { "F" -> grade } @@ -24,7 +24,7 @@ rwfunc classify(score:int64) -> (grade:[]char/utf32) { rwfunc test() -> () { - ans <- classify(85) + ans = classify(85) println("grade =", ans) - /last_grade = ans // = 等价于 <- + /last_grade = ans } diff --git a/tutorial/03-control/for.kv b/tutorial/03-control/for.kv index e17fc30e..a45fb70d 100644 --- a/tutorial/03-control/for.kv +++ b/tutorial/03-control/for.kv @@ -5,26 +5,26 @@ // 0 F = -17.77777777777778 C // 300 F = 148.88888888888889 C rwfunc test() -> () { - data·0 <- 0 - data·1 = 20 // = 等价于 <- + data·0 = 0 + data·1 = 20 40 -> data·2 - data·3 <- 60 - data·4 = 80 // = 等价于 <- + data·3 = 60 + data·4 = 80 100 -> data·5 - data·6 <- 120 - data·7 = 140 // = 等价于 <- + data·6 = 120 + data·7 = 140 160 -> data·8 - data·9 <- 180 - data·10 = 200 // = 等价于 <- + data·9 = 180 + data·10 = 200 220 -> data·11 - data·12 <- 240 - data·13 = 260 // = 等价于 <- + data·12 = 240 + data·13 = 260 280 -> data·14 - data·15 <- 300 + data·15 = 300 for (fahr in data) { - factor = 5.0 ÷ 9.0 // = 等价于 <- + factor = 5.0 ÷ 9.0 fahr - 32 -> diff - cels <- factor × diff + cels = factor × diff println(fahr, "F =", cels, "C") } } diff --git a/tutorial/03-control/guess.kv b/tutorial/03-control/guess.kv index 6f50db99..27b6b65c 100644 --- a/tutorial/03-control/guess.kv +++ b/tutorial/03-control/guess.kv @@ -5,36 +5,36 @@ // found 73 in 6 guesses rwfunc guess_number() -> () { 73 -> target - lo <- 1 - hi = 100 // = 等价于 <- + lo = 1 + hi = 100 0 -> tries - found <- 0 + found = 0 while (found == 0) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid_float s ÷ 2 -> mid_float_m - mid <- int64(mid_float) - tries = tries + 1 // = 等价于 <- + mid = int64(mid_float) + tries = tries + 1 println("guess #", tries, ":", mid) mid == target -> hit if (hit) { - found <- 1 + found = 1 println(" correct!") } else { - too_low = mid < target // = 等价于 <- + too_low = mid < target if (too_low) { mid + 1 -> lo println(" too low, range:", lo, "-", hi) } else { - hi <- mid - 1 + hi = mid - 1 println(" too high, range:", lo, "-", hi) } } } println("found", target, "in", tries, "guesses") - /last_guesses = tries // = 等价于 <- + /last_guesses = tries } rwfunc test() -> () { diff --git a/tutorial/03-control/if.kv b/tutorial/03-control/if.kv index 136d6030..6842f0b6 100644 --- a/tutorial/03-control/if.kv +++ b/tutorial/03-control/if.kv @@ -6,21 +6,21 @@ lib my { if (x < 0) { -x -> r } else { - r <- x + r = x } } } rwfunc test() -> () { - a = my·abs(-5) // = 等价于 <- + a = my·abs(-5) println("abs(-5) =", a) my·abs(3) -> b println("abs(3) =", b) - c <- my·abs(-10) + c = my·abs(-10) println("abs(-10) =", c) - d = b >= 90 // = 等价于 <- - d_m = b ≥ 90 // = 等价于 <- + d = b >= 90 + d_m = b ≥ 90 println(d_m) println(d) a -> /last_abs diff --git a/tutorial/03-control/while.kv b/tutorial/03-control/while.kv index 072efa76..c5d1cddc 100644 --- a/tutorial/03-control/while.kv +++ b/tutorial/03-control/while.kv @@ -3,42 +3,42 @@ // first div7 in [1,20] = 7 // sum odds(1..10) = 25 rwfunc sum_to(n:int64) -> (total:int64) { - total <- 0 - i = 1 // = 等价于 <- + total = 0 + i = 1 while (i <= n) { total + i -> total - i <- i + 1 + i = i + 1 } } rwfunc first_div7(n:int64) -> (result:int64) { - result = 0 // = 等价于 <- + result = 0 1 -> i while (i <= n) { - rem <- i % 7 - hit = rem == 0 // = 等价于 <- + rem = i % 7 + hit = rem == 0 if (hit) { i -> result - i <- n + 1 + i = n + 1 } else { - i = i + 1 // = 等价于 <- + i = i + 1 } } } rwfunc sum_odds(n:int64) -> (total:int64) { 0 -> total - i <- 1 + i = 1 while (i <= n) { - rem = i % 2 // = 等价于 <- + rem = i % 2 rem == 1 -> is_odd if (is_odd) { - total <- total + i + total = total + i } - i = i + 1 // = 等价于 <- + i = i + 1 } } @@ -46,10 +46,10 @@ rwfunc test() -> () { sum_to(10) -> a println("sum(1..10) =", a) - b <- first_div7(20) + b = first_div7(20) println("first div7 in [1,20] =", b) - c = sum_odds(10) // = 等价于 <- + c = sum_odds(10) println("sum odds(1..10) =", c) a + b -> ab - /last_sum <- ab + c + /last_sum = ab + c } diff --git a/tutorial/04-ndarray/continuous.kv b/tutorial/04-ndarray/continuous.kv index ff086274..fd2dbfce 100644 --- a/tutorial/04-ndarray/continuous.kv +++ b/tutorial/04-ndarray/continuous.kv @@ -9,17 +9,17 @@ rwfunc test() -> () { a:[]int64 = [10, 20, 30, 40] ndarray·numel(a) -> n println("len =", n) - v0 <- a[0] + v0 = a[0] println("[0] =", v0) - a[1] <- 99 + a[1] = 99 x0 = a[0] a[1] -> x1 - x2 <- a[2] + x2 = a[2] println("after set:", x0, x1, x2) total = 0 0 -> i while (i < n) { - x <- a[i] + x = a[i] total = total + x i + 1 -> i } diff --git a/tutorial/04-ndarray/geo_coord.kv b/tutorial/04-ndarray/geo_coord.kv index 4489d072..7303b10c 100644 --- a/tutorial/04-ndarray/geo_coord.kv +++ b/tutorial/04-ndarray/geo_coord.kv @@ -4,8 +4,8 @@ // [39.9,116.4] -> Beijing // [31.23,121.47] -> Shanghai rwfunc test() -> () { - geo·[39.9,116.4] <- "Beijing" - geo·[31.23,121.47] <- "Shanghai" + geo·[39.9,116.4] = "Beijing" + geo·[31.23,121.47] = "Shanghai" geo·[39.9,116.4] -> bj geo·[31.23,121.47] -> sh println("[39.9,116.4] ->", bj) diff --git a/tutorial/04-ndarray/separated.kv b/tutorial/04-ndarray/separated.kv index d24c66fa..a7637407 100644 --- a/tutorial/04-ndarray/separated.kv +++ b/tutorial/04-ndarray/separated.kv @@ -9,12 +9,12 @@ // c[0] = 10 rwfunc test() -> () { a:[]int64 = [10, 20, 30] // 字面量初始化 → compact - b <- array·scatter(a) // 显式 scatter:compact a → 散 key b(b·[0]..b·[2]) + b = array·scatter(a) // 显式 scatter:compact a → 散 key b(b·[0]..b·[2]) ndarray·numel(b) -> n1 println("ndarray·numel(b) =", n1) println("b[0] =", b·[0]) // 散 key 元素访问 ·[i] println("b[2] =", b·[2]) - c <- array·compact(b) // 显式 compact:散 key b → compact c + c = array·compact(b) // 显式 compact:散 key b → compact c ndarray·numel(c) -> n2 println("ndarray·numel(c) =", n2) println("c[0] =", c[0]) // compact 元素访问 [i] diff --git a/tutorial/06-algo/collatz.kv b/tutorial/06-algo/collatz.kv index 4a45f851..9561871e 100644 --- a/tutorial/06-algo/collatz.kv +++ b/tutorial/06-algo/collatz.kv @@ -6,16 +6,16 @@ rwfunc collatz(n:int64) -> (steps:int64) { n -> nv 0 -> steps while (nv > 1) { - mod <- nv % 2 - even = mod == 0 // = 等价于 <- + mod = nv % 2 + even = mod == 0 if (even) { nv ÷ 2 -> nv nv ÷ 2 -> nv_m } else { - t <- nv × 3 - t_m <- nv × 3 - nv = t + 1 // = 等价于 <- + t = nv × 3 + t_m = nv × 3 + nv = t + 1 } steps + 1 -> steps @@ -24,7 +24,7 @@ rwfunc collatz(n:int64) -> (steps:int64) { rwfunc test() -> () { - ans <- collatz(27) + ans = collatz(27) println("steps =", ans) - /last_steps = ans // = 等价于 <- + /last_steps = ans } diff --git a/tutorial/06-algo/factorial.kv b/tutorial/06-algo/factorial.kv index fb40b3cb..b2ddc6a2 100644 --- a/tutorial/06-algo/factorial.kv +++ b/tutorial/06-algo/factorial.kv @@ -4,20 +4,20 @@ // fact = 3628800 rwfunc factorial(n:int64) -> (result:int64) { 1 -> result - i <- 1 + i = 1 while (i <= n) { - result = result × i // = 等价于 <- + result = result × i i + 1 -> i } } rwfunc test() -> () { - t0 <- time·now() - ans <- factorial(10) - t1 <- time·now() + t0 = time·now() + ans = factorial(10) + t1 = time·now() println("fact =", ans) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) } diff --git a/tutorial/06-algo/fibonacci.kv b/tutorial/06-algo/fibonacci.kv index 97e72914..08d64edd 100644 --- a/tutorial/06-algo/fibonacci.kv +++ b/tutorial/06-algo/fibonacci.kv @@ -6,26 +6,26 @@ rwfunc fibonacci(n:int64) -> (result:int64) { if (n <= 1) { n -> result } else { - a <- 0 - b = 1 // = 等价于 <- + a = 0 + b = 1 2 -> i while (i <= n) { - c <- a + b - a = b // = 等价于 <- + c = a + b + a = b c -> b - i <- i + 1 + i = i + 1 } - result = b // = 等价于 <- + result = b } } rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() fibonacci(10) -> ans - t1 <- time·now() + t1 = time·now() println("fib =", ans) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) } diff --git a/tutorial/06-algo/fizzbuzz.kv b/tutorial/06-algo/fizzbuzz.kv index ea4ec409..bb83e3c9 100644 --- a/tutorial/06-algo/fizzbuzz.kv +++ b/tutorial/06-algo/fizzbuzz.kv @@ -5,13 +5,13 @@ // Buzz // FizzBuzz rwfunc fizzbuzz(N:int64) -> () { - i = 1 // = 等价于 <- + i = 1 while (i <= N) { i % 3 -> m3 - m5 <- i % 5 - d3 = m3 == 0 // = 等价于 <- + m5 = i % 5 + d3 = m3 == 0 m5 == 0 -> d5 - fb <- d3 && d5 + fb = d3 && d5 if (fb) { println("FizzBuzz") @@ -27,7 +27,7 @@ rwfunc fizzbuzz(N:int64) -> () { } } - i = i + 1 // = 等价于 <- + i = i + 1 } } diff --git a/tutorial/06-algo/gcd.kv b/tutorial/06-algo/gcd.kv index a61ba149..0dfab18b 100644 --- a/tutorial/06-algo/gcd.kv +++ b/tutorial/06-algo/gcd.kv @@ -6,18 +6,18 @@ rwfunc gcd(A:int64, B:int64) -> (R:int64) { if (B == 0) { A -> R } else { - rem <- A % B - R = gcd(B, rem) // = 等价于 <- + rem = A % B + R = gcd(B, rem) } } rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() gcd(48, 18) -> ans - t1 <- time·now() + t1 = time·now() println("gcd =", ans) - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) } diff --git a/tutorial/06-algo/map_reduce.kv b/tutorial/06-algo/map_reduce.kv index f5a75562..55718616 100644 --- a/tutorial/06-algo/map_reduce.kv +++ b/tutorial/06-algo/map_reduce.kv @@ -4,27 +4,27 @@ // odd squares count: 5 // sum of squares: 165 rwfunc test() -> () { - nums·0 = 1 // = 等价于 <- + nums·0 = 1 3 -> nums·1 - nums·2 <- 5 - nums·3 = 7 // = 等价于 <- + nums·2 = 5 + nums·3 = 7 9 -> nums·4 - nums·5 <- 2 - nums·6 = 4 // = 等价于 <- + nums·5 = 2 + nums·6 = 4 6 -> nums·7 - nums·8 <- 8 - nums·9 = 10 // = 等价于 <- + nums·8 = 8 + nums·9 = 10 0 -> count - total <- 0 + total = 0 for (n in nums) { - rem = n % 2 // = 等价于 <- + rem = n % 2 rem == 1 -> is_odd if (is_odd) { println(" keep:", n) - sq <- n × n - sq_m <- n × n - total = total + sq // = 等价于 <- + sq = n × n + sq_m = n × n + total = total + sq count + 1 -> count } else { println(" skip:", n) @@ -32,5 +32,5 @@ rwfunc test() -> () { } println("odd squares count:", count) println("sum of squares:", total) - /last_sum <- total + /last_sum = total } diff --git a/tutorial/06-algo/power.kv b/tutorial/06-algo/power.kv index 7f28f529..de782a62 100644 --- a/tutorial/06-algo/power.kv +++ b/tutorial/06-algo/power.kv @@ -3,12 +3,12 @@ // 期望输出: // result = 1024 rwfunc power(base:int64, exp:int64) -> (result:int64) { - result = 1 // = 等价于 <- + result = 1 0 -> e while (e < exp) { - result <- result × base - result_m <- result × base - e = e + 1 // = 等价于 <- + result = result × base + result_m = result × base + e = e + 1 } } @@ -16,5 +16,5 @@ rwfunc test() -> () { power(2, 10) -> ans println("result =", ans) - /last_pow <- ans + /last_pow = ans } diff --git a/tutorial/06-algo/prime_sieve.kv b/tutorial/06-algo/prime_sieve.kv index 84ee1e2c..6652733a 100644 --- a/tutorial/06-algo/prime_sieve.kv +++ b/tutorial/06-algo/prime_sieve.kv @@ -6,26 +6,26 @@ // total primes up to 200 = 46 rwfunc prime_sieve(limit:int64) -> () { println("primes up to", limit) - count = 0 // = 等价于 <- + count = 0 2 -> n while (n <= limit) { - is_prime <- true - d = 2 // = 等价于 <- + is_prime = true + d = 2 while (d < n) { n % d -> rem - divisible <- rem == 0 + divisible = rem == 0 if (divisible) { - is_prime = false // = 等价于 <- + is_prime = false break } else { - d <- d + 1 + d = d + 1 } } if (is_prime) { println(" prime:", n) - count = count + 1 // = 等价于 <- + count = count + 1 } n + 1 -> n @@ -35,10 +35,10 @@ rwfunc prime_sieve(limit:int64) -> () { rwfunc test() -> () { - t0 <- time·now() + t0 = time·now() prime_sieve(200) - t1 <- time·now() - delta <- time·sub(t1, t0) - ns <- time/duration·as_nanos(delta) + t1 = time·now() + delta = time·sub(t1, t0) + ns = time/duration·as_nanos(delta) println("__bench_ns:", ns) } diff --git a/tutorial/06-algo/recursion.kv b/tutorial/06-algo/recursion.kv index 879afa44..93b9fc47 100644 --- a/tutorial/06-algo/recursion.kv +++ b/tutorial/06-algo/recursion.kv @@ -3,24 +3,24 @@ // fact(10) = 3628800 rwfunc fib(n:int64) -> (a:int64, b:int64) { if (n <= 1) { - a = 0 // = 等价于 <- + a = 0 1 -> b } else { - n1 <- n - 1 + n1 = n - 1 fib(n1) -> (a, b) - x = a + b // = 等价于 <- + x = a + b b -> a - b <- x + b = x } } rwfunc factorial(n:int64) -> (r:int64) { - r = 1 // = 等价于 <- + r = 1 1 -> i while (i <= n) { - r <- r × i - r_m <- r × i - i = i + 1 // = 等价于 <- + r = r × i + r_m = r × i + i = i + 1 } } @@ -29,6 +29,6 @@ rwfunc test() -> () { println("fib(10) =", f) factorial(10) -> ans println("fact(10) =", ans) - /last_fib <- f - /last_fact = ans // = 等价于 <- + /last_fib = f + /last_fact = ans } diff --git a/tutorial/06-algo/word_count.kv b/tutorial/06-algo/word_count.kv index 1a57a25e..6ff954e3 100644 --- a/tutorial/06-algo/word_count.kv +++ b/tutorial/06-algo/word_count.kv @@ -3,37 +3,37 @@ // 期望输出: // word count = 3 rwfunc test() -> () { - chars·0 = "h" // = 等价于 <- + chars·0 = "h" "e" -> chars·1 - chars·2 <- "l" - chars·3 = "l" // = 等价于 <- + chars·2 = "l" + chars·3 = "l" "o" -> chars·4 - chars·5 <- " " - chars·6 = "w" // = 等价于 <- + chars·5 = " " + chars·6 = "w" "o" -> chars·7 - chars·8 <- "r" - chars·9 = "l" // = 等价于 <- + chars·8 = "r" + chars·9 = "l" "d" -> chars·10 - chars·11 <- " " - chars·12 = "k" // = 等价于 <- + chars·11 = " " + chars·12 = "k" "v" -> chars·13 - chars·14 <- "l" - chars·15 = "a" // = 等价于 <- + chars·14 = "l" + chars·15 = "a" "n" -> chars·16 - chars·17 <- "g" - nw = 0 // = 等价于 <- + chars·17 = "g" + nw = 0 0 -> state for (c in chars) { - blank <- c == " " + blank = c == " " if (blank) { - state = 0 // = 等价于 <- + state = 0 } else { state == 0 -> was_out if (was_out) { - state <- 1 - nw = nw + 1 // = 等价于 <- + state = 1 + nw = nw + 1 } } } diff --git a/tutorial/07-lib/inline_lib.kv b/tutorial/07-lib/inline_lib.kv index 928df510..05e9fe6f 100644 --- a/tutorial/07-lib/inline_lib.kv +++ b/tutorial/07-lib/inline_lib.kv @@ -8,12 +8,12 @@ lib mylib { } rwfunc double(x:int64) -> (y:int64) { - y <- x × 2 - y_m <- x × 2 + y = x × 2 + y_m = x × 2 } rwfunc test() -> () { - a = mylib·add(10, 20) // = 等价于 <- + a = mylib·add(10, 20) println("mylib·add(10,20) =", a) double(a) -> b println("double(mylib·add(10,20)) =", b) diff --git a/tutorial/08-leetcode/001_two-sum-hash.kv b/tutorial/08-leetcode/001_two-sum-hash.kv index 0e8ea4ff..2b43488a 100644 --- a/tutorial/08-leetcode/001_two-sum-hash.kv +++ b/tutorial/08-leetcode/001_two-sum-hash.kv @@ -3,22 +3,22 @@ // 期望输出: // [ 0 , 1 ] rwfunc two_sum(nums:[]int64, target:int64) -> () { - n <- ndarray·numel(nums) - i = 0 // = 等价于 <- + n = ndarray·numel(nums) + i = 0 "/tmp" -> h while (i < n) { - x <- nums[i] - need = target - x // = 等价于 <- + x = nums[i] + need = target - x kv·get(h, need) != None -> exists if (exists) { kv·get(h, need) -> j - k = j - 1 // = 等价于 <- + k = j - 1 println("[", k, ",", i, "]") n -> i } else { - v <- i + 1 - _ = kv·set(h, x, v) // = 等价于 <- + v = i + 1 + _ = kv·set(h, x, v) i + 1 -> i } } diff --git a/tutorial/08-leetcode/001_two_sum.kv b/tutorial/08-leetcode/001_two_sum.kv index c9e27e0a..3976ede8 100644 --- a/tutorial/08-leetcode/001_two_sum.kv +++ b/tutorial/08-leetcode/001_two_sum.kv @@ -4,35 +4,35 @@ // [ 0 , 1 ] // [-1, -1] rwfunc two_sum(nums:[]int64, target:int64) -> () { - n <- ndarray·numel(nums) - found = 0 // = 等价于 <- + n = ndarray·numel(nums) + found = 0 0 -> i while (i < n) { - j <- 0 + j = 0 while (j < n) { - sk = i == j // = 等价于 <- + sk = i == j if (sk) { j + 1 -> j } else { - x <- nums[i] - y = nums[j] // = 等价于 <- + x = nums[i] + y = nums[j] x + y -> s - hit <- s == target + hit = s == target if (hit) { println("[", i, ",", j, "]") - found = 1 // = 等价于 <- + found = 1 n -> i - j <- n + j = n } else { - j = j + 1 // = 等价于 <- + j = j + 1 } } } i + 1 -> i } - nf <- found == 0 + nf = found == 0 if (nf) { println("[-1, -1]") diff --git a/tutorial/08-leetcode/002_add_two_numbers.kv b/tutorial/08-leetcode/002_add_two_numbers.kv index 22f1fdfb..d446d9ee 100644 --- a/tutorial/08-leetcode/002_add_two_numbers.kv +++ b/tutorial/08-leetcode/002_add_two_numbers.kv @@ -6,9 +6,9 @@ // 8 rwfunc build_lists() -> () { /l1_0 = { val=2; next="/l1_1" } - /l1_1 <- { val=4; next="/l1_2" } + /l1_1 = { val=4; next="/l1_2" } { val=3; next="" } -> /l1_2 - /l2_0 <- { val=5; next="/l2_1" } + /l2_0 = { val=5; next="/l2_1" } { val=6; next="/l2_2" } -> /l2_1 /l2_2 = { val=4; next="" } } @@ -18,10 +18,10 @@ rwfunc add(a:[]char/utf32, b:[]char/utf32) -> () { carry = 0 result:[int64]·int64 = { } 0 -> ri - pa <- a + pa = a pb = b while (pa != "" || pb != "" || carry != 0) { - sum <- carry + sum = carry if (pa != "") { pa·val -> av @@ -35,7 +35,7 @@ rwfunc add(a:[]char/utf32, b:[]char/utf32) -> () { pb·next -> pb } - digit <- sum % 10 + digit = sum % 10 kv·set(result, ri, digit) -> _ ri + 1 -> ri sum ÷ 10 -> carry @@ -43,7 +43,7 @@ rwfunc add(a:[]char/utf32, b:[]char/utf32) -> () { } i = ri - 1 while (i >= 0) { - v <- kv·get(result, i) + v = kv·get(result, i) println(v) i - 1 -> i } diff --git a/tutorial/08-leetcode/003_longest_substring.kv b/tutorial/08-leetcode/003_longest_substring.kv index 93a8ae53..7a40aeda 100644 --- a/tutorial/08-leetcode/003_longest_substring.kv +++ b/tutorial/08-leetcode/003_longest_substring.kv @@ -5,30 +5,30 @@ // 1 // 5 rwfunc length_of_longest(s:[]char/utf32) -> (max_len:int64) { - n <- string·len(s) - last:[]char/utf8·int64 = {} // = 等价于 <- - max_len = 0 // = 等价于 <- - left = 0 // = 等价于 <- - right <- 0 + n = string·len(s) + last:[]char/utf8·int64 = {} + max_len = 0 + left = 0 + right = 0 while (right < n) { s[right] -> c - exists <- kv·get(last, c) != None + exists = kv·get(last, c) != None if (exists) { - prev <- kv·get(last, c) - cand <- prev + 1 - if (cand > left) { left <- cand } + prev = kv·get(last, c) + cand = prev + 1 + if (cand > left) { left = cand } } - _ = kv·set(last, c, right) // = 等价于 <- - cur <- right - left + 1 + _ = kv·set(last, c, right) + cur = right - left + 1 if (cur > max_len) { cur -> max_len } right + 1 -> right } } rwfunc test() -> () { - r1 = length_of_longest("abcabcbb") // = 等价于 <- + r1 = length_of_longest("abcabcbb") println(r1) println(length_of_longest("bbbbb")) - r3 <- length_of_longest("abcde") + r3 = length_of_longest("abcde") println(r3) } diff --git a/tutorial/08-leetcode/007_reverse_int.kv b/tutorial/08-leetcode/007_reverse_int.kv index e6192ebe..21cdba2d 100644 --- a/tutorial/08-leetcode/007_reverse_int.kv +++ b/tutorial/08-leetcode/007_reverse_int.kv @@ -4,27 +4,27 @@ // 321 // -321 rwfunc reverse(x:int64) -> (result:int64) { - neg = x < 0 // = 等价于 <- + neg = x < 0 x -> n if (neg) { - n <- -x + n = -x } - rev = 0 // = 等价于 <- + rev = 0 while (n > 0) { rev × 10 -> r rev × 10 -> r_m - d <- n % 10 - rev = r + d // = 等价于 <- + d = n % 10 + rev = r + d n ÷ 10 -> n n ÷ 10 -> n_m } if (neg) { - result <- -rev + result = -rev } else { - result = rev // = 等价于 <- + result = rev } } @@ -32,6 +32,6 @@ rwfunc test() -> () { reverse(123) -> r1 println(r1) - r2 <- reverse(-123) + r2 = reverse(-123) println(r2) } diff --git a/tutorial/08-leetcode/008_string_to_int.kv b/tutorial/08-leetcode/008_string_to_int.kv index 56a4278b..f82b9da9 100644 --- a/tutorial/08-leetcode/008_string_to_int.kv +++ b/tutorial/08-leetcode/008_string_to_int.kv @@ -6,23 +6,23 @@ // -42 // 4193 rwfunc my_atoi(s:[]char/utf32) -> (result:int64) { - result = 0 // = 等价于 <- - n = string·len(s) // = 等价于 <- - i <- 0 - sign = 1 // = 等价于 <- + result = 0 + n = string·len(s) + i = 0 + sign = 1 if (s[i] == "-") { -1 -> sign ; i + 1 -> i } if (s[i] == "+") { i + 1 -> i } while (i < n) { - c2 = s[i] // = 等价于 <- - digit <- string·ord(c2) - 48 + c2 = s[i] + digit = string·ord(c2) - 48 if (digit >= 0 && digit <= 9) { - result = result × 10 + digit // = 等价于 <- - result_m = result × 10 + digit // = 等价于 <- + result = result × 10 + digit + result_m = result × 10 + digit i + 1 -> i } else { n -> i } } - result <- result × sign - result_m <- result × sign + result = result × sign + result_m = result × sign } rwfunc test() -> () { diff --git a/tutorial/08-leetcode/009_palindrome.kv b/tutorial/08-leetcode/009_palindrome.kv index ec8e9022..894def0f 100644 --- a/tutorial/08-leetcode/009_palindrome.kv +++ b/tutorial/08-leetcode/009_palindrome.kv @@ -4,25 +4,25 @@ // 1 // 0 rwfunc is_pal(x:int64) -> (r:int64) { - neg = x < 0 // = 等价于 <- + neg = x < 0 if (neg) { 0 -> r } else { - orig <- x - rev = 0 // = 等价于 <- + orig = x + rev = 0 while (orig > 0) { rev × 10 -> r10 rev × 10 -> r10_m - d <- orig % 10 - rev = r10 + d // = 等价于 <- + d = orig % 10 + rev = r10 + d orig ÷ 10 -> orig orig ÷ 10 -> orig_m } - ok <- rev == x + ok = rev == x if (ok) { - r = 1 // = 等价于 <- + r = 1 } else { 0 -> r } @@ -31,8 +31,8 @@ rwfunc is_pal(x:int64) -> (r:int64) { rwfunc test() -> () { - r1 <- is_pal(121) + r1 = is_pal(121) println(r1) - r2 = is_pal(-121) // = 等价于 <- + r2 = is_pal(-121) println(r2) } diff --git a/tutorial/08-leetcode/011_container_water.kv b/tutorial/08-leetcode/011_container_water.kv index 38918891..9dd27802 100644 --- a/tutorial/08-leetcode/011_container_water.kv +++ b/tutorial/08-leetcode/011_container_water.kv @@ -4,29 +4,29 @@ // 49 rwfunc max_area(h:[]int64) -> (mx:int64) { ndarray·numel(h) -> n - l <- 0 - r = n - 1 // = 等价于 <- + l = 0 + r = n - 1 0 -> mx while (l < r) { - hl <- h[l] - hr = h[r] // = 等价于 <- + hl = h[l] + hr = h[r] r - l -> w - ls <- hl < hr + ls = hl < hr if (ls) { - ar = hl × w // = 等价于 <- - ar_m = hl × w // = 等价于 <- + ar = hl × w + ar_m = hl × w l + 1 -> l } else { - ar <- hr × w - ar_m <- hr × w - r = r - 1 // = 等价于 <- + ar = hr × w + ar_m = hr × w + r = r - 1 } ar > mx -> bg if (bg) { - mx <- ar + mx = ar } } } @@ -34,6 +34,6 @@ rwfunc max_area(h:[]int64) -> (mx:int64) { rwfunc test() -> () { a:[]int64 = [1, 8, 6, 2, 5, 4, 8, 3, 7] - r = max_area(a) // = 等价于 <- + r = max_area(a) println(r) } diff --git a/tutorial/08-leetcode/012_int_to_roman.kv b/tutorial/08-leetcode/012_int_to_roman.kv index 82f1fb6b..4e88447a 100644 --- a/tutorial/08-leetcode/012_int_to_roman.kv +++ b/tutorial/08-leetcode/012_int_to_roman.kv @@ -4,37 +4,37 @@ // LVIII // MCMXCIV rwfunc int_to_roman(n:int64) -> () { - result <- "" - val <- n - while (val >= 1000) { result + "M" -> result ; val <- val - 1000 } - while (val ≥ 1000) { result + "M" -> result_m ; val <- val - 1000 } - if (val >= 900) { result + "CM" -> result ; val <- val - 900 } - if (val ≥ 900) { result + "CM" -> result_m ; val <- val - 900 } - if (val >= 500) { result + "D" -> result ; val <- val - 500 } - if (val ≥ 500) { result + "D" -> result_m ; val <- val - 500 } - if (val >= 400) { result + "CD" -> result ; val <- val - 400 } - if (val ≥ 400) { result + "CD" -> result_m ; val <- val - 400 } - while (val >= 100) { result + "C" -> result ; val <- val - 100 } - while (val ≥ 100) { result + "C" -> result_m ; val <- val - 100 } - if (val >= 90) { result + "XC" -> result ; val <- val - 90 } - if (val ≥ 90) { result + "XC" -> result_m ; val <- val - 90 } - if (val >= 50) { result + "L" -> result ; val <- val - 50 } - if (val ≥ 50) { result + "L" -> result_m ; val <- val - 50 } - if (val >= 40) { result + "XL" -> result ; val <- val - 40 } - if (val ≥ 40) { result + "XL" -> result_m ; val <- val - 40 } - while (val >= 10) { result + "X" -> result ; val <- val - 10 } - while (val ≥ 10) { result + "X" -> result_m ; val <- val - 10 } - if (val >= 9) { result + "IX" -> result ; val <- val - 9 } - if (val ≥ 9) { result + "IX" -> result_m ; val <- val - 9 } + result = "" + val = n + while (val >= 1000) { result + "M" -> result ; val = val - 1000 } + while (val ≥ 1000) { result + "M" -> result_m ; val = val - 1000 } + if (val >= 900) { result + "CM" -> result ; val = val - 900 } + if (val ≥ 900) { result + "CM" -> result_m ; val = val - 900 } + if (val >= 500) { result + "D" -> result ; val = val - 500 } + if (val ≥ 500) { result + "D" -> result_m ; val = val - 500 } + if (val >= 400) { result + "CD" -> result ; val = val - 400 } + if (val ≥ 400) { result + "CD" -> result_m ; val = val - 400 } + while (val >= 100) { result + "C" -> result ; val = val - 100 } + while (val ≥ 100) { result + "C" -> result_m ; val = val - 100 } + if (val >= 90) { result + "XC" -> result ; val = val - 90 } + if (val ≥ 90) { result + "XC" -> result_m ; val = val - 90 } + if (val >= 50) { result + "L" -> result ; val = val - 50 } + if (val ≥ 50) { result + "L" -> result_m ; val = val - 50 } + if (val >= 40) { result + "XL" -> result ; val = val - 40 } + if (val ≥ 40) { result + "XL" -> result_m ; val = val - 40 } + while (val >= 10) { result + "X" -> result ; val = val - 10 } + while (val ≥ 10) { result + "X" -> result_m ; val = val - 10 } + if (val >= 9) { result + "IX" -> result ; val = val - 9 } + if (val ≥ 9) { result + "IX" -> result_m ; val = val - 9 } println(result_m) - if (val >= 5) { result + "V" -> result ; val <- val - 5 } - if (val ≥ 5) { result + "V" -> result_m ; val <- val - 5 } + if (val >= 5) { result + "V" -> result ; val = val - 5 } + if (val ≥ 5) { result + "V" -> result_m ; val = val - 5 } println(result_m) - if (val >= 4) { result + "IV" -> result ; val <- val - 4 } - if (val ≥ 4) { result + "IV" -> result_m ; val <- val - 4 } + if (val >= 4) { result + "IV" -> result ; val = val - 4 } + if (val ≥ 4) { result + "IV" -> result_m ; val = val - 4 } println(result_m) - while (val >= 1) { result + "I" -> result ; val <- val - 1 } - while (val ≥ 1) { result + "I" -> result_m ; val <- val - 1 } + while (val >= 1) { result + "I" -> result ; val = val - 1 } + while (val ≥ 1) { result + "I" -> result_m ; val = val - 1 } println(result_m) println(result) } diff --git a/tutorial/08-leetcode/013_roman-to-integer.kv b/tutorial/08-leetcode/013_roman-to-integer.kv index af1770b2..9b12dd2c 100644 --- a/tutorial/08-leetcode/013_roman-to-integer.kv +++ b/tutorial/08-leetcode/013_roman-to-integer.kv @@ -2,24 +2,24 @@ // 期望输出: 58, 1994 rwfunc romanToInt(s:[]char/utf32) -> (val:int64) { 0 -> val - i <- 0 - n = string·len(s) // = 等价于 <- + i = 0 + n = string·len(s) "" -> prev while (i < n) { - c <- s[i] - isI = c == "I" // = 等价于 <- + c = s[i] + isI = c == "I" c == "V" -> isV - isX <- c == "X" - isL = c == "L" // = 等价于 <- + isX = c == "X" + isL = c == "L" c == "C" -> isC - isD <- c == "D" - isM = c == "M" // = 等价于 <- - pI = prev == "I" // = 等价于 <- + isD = c == "D" + isM = c == "M" + pI = prev == "I" prev == "X" -> pX - pC <- prev == "C" + pC = prev == "C" if (isI) { - val = val + 1 // = 等价于 <- + val = val + 1 } if (isV) { @@ -27,11 +27,11 @@ rwfunc romanToInt(s:[]char/utf32) -> (val:int64) { } if (isX) { - val <- val + 10 + val = val + 10 } if (isL) { - val = val + 50 // = 等价于 <- + val = val + 50 } if (isC) { @@ -39,20 +39,20 @@ rwfunc romanToInt(s:[]char/utf32) -> (val:int64) { } if (isD) { - val <- val + 500 + val = val + 500 } if (isM) { val + 1000 -> val } - subI = pI // = 等价于 <- + subI = pI pX -> subX - subC <- pC + subC = pC if (isV) { if (subI) { - val = val - 2 // = 等价于 <- + val = val - 2 } } @@ -64,13 +64,13 @@ rwfunc romanToInt(s:[]char/utf32) -> (val:int64) { if (isL) { if (subX) { - val <- val - 20 + val = val - 20 } } if (isC) { if (subX) { - val = val - 20 // = 等价于 <- + val = val - 20 } } @@ -82,12 +82,12 @@ rwfunc romanToInt(s:[]char/utf32) -> (val:int64) { if (isM) { if (subC) { - val <- val - 200 + val = val - 200 } } - prev <- c - i = i + 1 // = 等价于 <- + prev = c + i = i + 1 } } @@ -95,7 +95,7 @@ rwfunc test() -> () { romanToInt("LVIII") -> a println("LVIII =", a) - b <- romanToInt("MCMXCIV") + b = romanToInt("MCMXCIV") println("MCMXCIV =", b) - /roman_lviii = a // = 等价于 <- + /roman_lviii = a } diff --git a/tutorial/08-leetcode/013_roman_v2.kv b/tutorial/08-leetcode/013_roman_v2.kv index e9e47a92..c8c41bd5 100644 --- a/tutorial/08-leetcode/013_roman_v2.kv +++ b/tutorial/08-leetcode/013_roman_v2.kv @@ -4,36 +4,36 @@ // 58 rwfunc roman_to_int(s:[]char/utf32) -> () { string·len(s) -> n - result <- 0 - i = 0 // = 等价于 <- + result = 0 + i = 0 while (i < n) { string·char(s, i) -> c - is_I <- c == "I" - is_V = c == "V" // = 等价于 <- + is_I = c == "I" + is_V = c == "V" c == "X" -> is_X - is_L <- c == "L" - is_C = c == "C" // = 等价于 <- + is_L = c == "L" + is_C = c == "C" c == "D" -> is_D - is_M <- c == "M" + is_M = c == "M" if (is_I) { - ni = i + 1 // = 等价于 <- + ni = i + 1 ni < n -> has_next if (has_next) { - nc <- string·char(s, ni) - iv = nc == "V" // = 等价于 <- + nc = string·char(s, ni) + iv = nc == "V" nc == "X" -> ix if (iv) { - val <- 4 - i = i + 1 // = 等价于 <- + val = 4 + i = i + 1 } else { if (ix) { 9 -> val - i <- i + 1 + i = i + 1 } else { - val = 1 // = 等价于 <- + val = 1 } } } else { @@ -41,19 +41,19 @@ rwfunc roman_to_int(s:[]char/utf32) -> () { } } else { if (is_V) { - val <- 5 + val = 5 } else { if (is_X) { - val = 10 // = 等价于 <- + val = 10 } else { if (is_L) { 50 -> val } else { if (is_C) { - val <- 100 + val = 100 } else { if (is_D) { - val = 500 // = 等价于 <- + val = 500 } else { 1000 -> val } @@ -63,8 +63,8 @@ rwfunc roman_to_int(s:[]char/utf32) -> () { } } - result <- result + val - i = i + 1 // = 等价于 <- + result = result + val + i = i + 1 } println(result) } diff --git a/tutorial/08-leetcode/014_longest_common.kv b/tutorial/08-leetcode/014_longest_common.kv index 50c45185..986978a6 100644 --- a/tutorial/08-leetcode/014_longest_common.kv +++ b/tutorial/08-leetcode/014_longest_common.kv @@ -4,34 +4,34 @@ // fl rwfunc lcp() -> () { "flower" -> a - b <- "flow" - c = "flight" // = 等价于 <- + b = "flow" + c = "flight" string·len(a) -> na - nb <- string·len(b) - nc = string·len(c) // = 等价于 <- + nb = string·len(b) + nc = string·len(c) 0 -> i - ok <- true + ok = true while (ok) { - ca = i < na // = 等价于 <- + ca = i < na i < nb -> cb - cc <- i < nc - all_ok = ca && cb && cc // = 等价于 <- + cc = i < nc + all_ok = ca && cb && cc if (all_ok) { string·char(a, i) -> va - vb <- string·char(b, i) - vc = string·char(c, i) // = 等价于 <- + vb = string·char(b, i) + vc = string·char(c, i) va == vb -> ab - bc <- vb == vc - match = ab && bc // = 等价于 <- + bc = vb == vc + match = ab && bc if (match) { i + 1 -> i } else { - ok <- false + ok = false } } else { - ok = false // = 等价于 <- + ok = false } } string·slice(a, 0, i) -> ans diff --git a/tutorial/08-leetcode/020_valid_parentheses.kv b/tutorial/08-leetcode/020_valid_parentheses.kv index fb9b5378..a736a742 100644 --- a/tutorial/08-leetcode/020_valid_parentheses.kv +++ b/tutorial/08-leetcode/020_valid_parentheses.kv @@ -5,62 +5,62 @@ // true // false rwfunc is_valid(s:[]char/utf32) -> (ok:int64) { - ok <- 1 - si = -1 // = 等价于 <- - n = string·len(s) // = 等价于 <- - i <- 0 + ok = 1 + si = -1 + n = string·len(s) + i = 0 while (i < n) { - c = s[i] // = 等价于 <- + c = s[i] if (c == "(") { si + 1 -> si - _ <- kv·set("/tmp/vp", si, 1) + _ = kv·set("/tmp/vp", si, 1) } if (c == "[") { - si = si + 1 // = 等价于 <- + si = si + 1 kv·set("/tmp/vp", si, 2) -> _ } if (c == "{") { si + 1 -> si - _ = kv·set("/tmp/vp", si, 3) // = 等价于 <- + _ = kv·set("/tmp/vp", si, 3) } if (c == ")") { if (si < 0) { 0 -> ok ; n -> i } else { /tmp/vp[si] -> top si - 1 -> si - if (top != 1) { ok = 0 ; n -> i } // = 等价于 <- - if (top ≠ 1) { ok_m = 0 ; n -> i } // = 等价于 <- + if (top != 1) { ok = 0 ; n -> i } + if (top ≠ 1) { ok_m = 0 ; n -> i } } } if (c == "]") { - if (si < 0) { ok = 0 ; i <- n } // = 等价于 <- + if (si < 0) { ok = 0 ; i = n } else { /tmp/vp[si] -> top si - 1 -> si - if (top != 2) { 0 -> ok ; n -> i } // = 等价于 <- - if (top ≠ 2) { 0 -> ok_m ; n -> i } // = 等价于 <- + if (top != 2) { 0 -> ok ; n -> i } + if (top ≠ 2) { 0 -> ok_m ; n -> i } } } if (c == "}") { - if (si < 0) { 0 -> ok ; i <- n } // = 等价于 <- + if (si < 0) { 0 -> ok ; i = n } else { /tmp/vp[si] -> top - si = si - 1 // = 等价于 <- - if (top != 3) { ok <- 0 ; i = n } // = 等价于 <- - if (top ≠ 3) { ok_m <- 0 ; i = n } // = 等价于 <- + si = si - 1 + if (top != 3) { ok = 0 ; i = n } + if (top ≠ 3) { ok_m = 0 ; i = n } } } - i = i + 1 // = 等价于 <- + i = i + 1 } if (si >= 0) { 0 -> ok } if (si ≥ 0) { 0 -> ok_m } } rwfunc test() -> () { - r1 = is_valid("()") // = 等价于 <- + r1 = is_valid("()") if (r1 == 1) { println("true") } else { println("false") } - r2 <- is_valid("()[]{}") + r2 = is_valid("()[]{}") if (r2 == 1) { println("true") } else { println("false") } - r3 = is_valid("(]") // = 等价于 <- + r3 = is_valid("(]") if (r3 == 1) { println("true") } else { println("false") } } diff --git a/tutorial/08-leetcode/021_merge_two_lists.kv b/tutorial/08-leetcode/021_merge_two_lists.kv index b5c3c4a3..7391639a 100644 --- a/tutorial/08-leetcode/021_merge_two_lists.kv +++ b/tutorial/08-leetcode/021_merge_two_lists.kv @@ -8,32 +8,32 @@ // 4 // 4 rwfunc build_lists() -> () { - /a0 = { val=1; next="/a1" } // = 等价于 <- - /a1 <- { val=2; next="/a2" } + /a0 = { val=1; next="/a1" } + /a1 = { val=2; next="/a2" } { val=4; next="" } -> /a2 - /b0 = { val=1; next="/b1" } // = 等价于 <- - /b1 <- { val=3; next="/b2" } + /b0 = { val=1; next="/b1" } + /b1 = { val=3; next="/b2" } { val=4; next="" } -> /b2 } rwfunc merge(a:[]char/utf32, b:[]char/utf32) -> () { - pa <- a - pb = b // = 等价于 <- + pa = a + pb = b while (pa != "") { pa·val -> av - pick_a <- pb == "" + pick_a = pb == "" if (pick_a) { println(av) - pa = pa·next // = 等价于 <- + pa = pa·next } else { pb·val -> bv - take <- av <= bv - take_m <- av ≤ bv + take = av <= bv + take_m = av ≤ bv if (take) { println(av) - pa = pa·next // = 等价于 <- + pa = pa·next } else { println(bv) pb·next -> pb @@ -41,9 +41,9 @@ rwfunc merge(a:[]char/utf32, b:[]char/utf32) -> () { } } while (pb != "") { - bv <- pb·val + bv = pb·val println(bv) - pb = pb·next // = 等价于 <- + pb = pb·next } } diff --git a/tutorial/08-leetcode/022_generate_parens.kv b/tutorial/08-leetcode/022_generate_parens.kv index a24e9d65..b23ef765 100644 --- a/tutorial/08-leetcode/022_generate_parens.kv +++ b/tutorial/08-leetcode/022_generate_parens.kv @@ -4,16 +4,16 @@ // (()) // ()() rwfunc generate() -> () { - total <- 16 - idx <- 0 + total = 16 + idx = 0 while (idx < total) { - bal <- 0 - ok2 <- 1 - s <- "" - pow <- 8 + bal = 0 + ok2 = 1 + s = "" + pow = 8 while (pow > 0) { - bit <- (idx ÷ pow) % 2 - bit_m <- (idx ÷ pow) % 2 + bit = (idx ÷ pow) % 2 + bit_m = (idx ÷ pow) % 2 if (bit == 0) { s + "(" -> s bal + 1 -> bal diff --git a/tutorial/08-leetcode/026_remove_dupes.kv b/tutorial/08-leetcode/026_remove_dupes.kv index 7e480629..5366a3b7 100644 --- a/tutorial/08-leetcode/026_remove_dupes.kv +++ b/tutorial/08-leetcode/026_remove_dupes.kv @@ -6,19 +6,19 @@ rwfunc remove_dupes() -> (k:int64) { a:[]int64 = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4] ndarray·numel(a) -> n - k <- 1 - i = 1 // = 等价于 <- + k = 1 + i = 1 while (i < n) { a[i] -> cur - pi <- k - 1 - prev = a[pi] // = 等价于 <- + pi = k - 1 + prev = a[pi] cur == prev -> same if (same) { - i <- i + 1 + i = i + 1 } else { - a[k] <- cur - k = k + 1 // = 等价于 <- + a[k] = cur + k = k + 1 i + 1 -> i } } diff --git a/tutorial/08-leetcode/027_remove_element.kv b/tutorial/08-leetcode/027_remove_element.kv index dfbf54ed..793ad0b9 100644 --- a/tutorial/08-leetcode/027_remove_element.kv +++ b/tutorial/08-leetcode/027_remove_element.kv @@ -7,29 +7,29 @@ // 2 // 2 rwfunc remove_element() -> () { - a:[]int64 = [3, 2, 2, 3] // = 等价于 <- + a:[]int64 = [3, 2, 2, 3] 3 -> val - n <- ndarray·numel(a) - k = 0 // = 等价于 <- + n = ndarray·numel(a) + k = 0 0 -> i while (i < n) { - cur <- a[i] - skip = cur == val // = 等价于 <- + cur = a[i] + skip = cur == val if (skip) { i + 1 -> i } else { - a[k] <- cur - k <- k + 1 - i = i + 1 // = 等价于 <- + a[k] = cur + k = k + 1 + i = i + 1 } } println("k =", k) 0 -> j while (j < k) { - v <- a[j] + v = a[j] println(v) - j = j + 1 // = 等价于 <- + j = j + 1 } } diff --git a/tutorial/08-leetcode/028_strstr.kv b/tutorial/08-leetcode/028_strstr.kv index f9bdb65a..be08cb64 100644 --- a/tutorial/08-leetcode/028_strstr.kv +++ b/tutorial/08-leetcode/028_strstr.kv @@ -5,29 +5,29 @@ // 2 rwfunc str_str(hay:[]char/utf32, needle:[]char/utf32) -> () { string·len(hay) -> nh - nn <- string·len(needle) - i = 0 // = 等价于 <- + nn = string·len(needle) + i = 0 -1 -> ans - limit <- nh - nn + limit = nh - nn while (i <= limit) { - match = true // = 等价于 <- + match = true 0 -> j while (j < nn) { - hc <- string·char(hay, i + j) - nc = string·char(needle, j) // = 等价于 <- + hc = string·char(hay, i + j) + nc = string·char(needle, j) hc == nc -> eq if (eq) { - j <- j + 1 + j = j + 1 } else { - match = false // = 等价于 <- + match = false nn -> j } } if (match) { - ans <- i - i = limit + 1 // = 等价于 <- + ans = i + i = limit + 1 } else { i + 1 -> i } diff --git a/tutorial/08-leetcode/029_divide_two_ints.kv b/tutorial/08-leetcode/029_divide_two_ints.kv index 39409c98..dd36b3bf 100644 --- a/tutorial/08-leetcode/029_divide_two_ints.kv +++ b/tutorial/08-leetcode/029_divide_two_ints.kv @@ -4,34 +4,34 @@ // 3 // -2 rwfunc divide(dividend:int64, divisor:int64) -> (result:int64) { - dend <- dividend - dsor = divisor // = 等价于 <- - neg = 0 // = 等价于 <- + dend = dividend + dsor = divisor + neg = 0 if (dend < 0) { - neg <- 1 - neg - dend <- 0 - dend + neg = 1 - neg + dend = 0 - dend } if (dsor < 0) { - neg = 1 - neg // = 等价于 <- - dsor <- 0 - dsor + neg = 1 - neg + dsor = 0 - dsor } - result = 0 // = 等价于 <- + result = 0 while (dend >= dsor) { - temp <- dsor - multiple = 1 // = 等价于 <- + temp = dsor + multiple = 1 while (dend >= temp + temp) { - temp <- temp + temp - multiple <- multiple + multiple + temp = temp + temp + multiple = multiple + multiple } - dend <- dend - temp - result = result + multiple // = 等价于 <- + dend = dend - temp + result = result + multiple } - if (neg == 1) { result <- 0 - result } + if (neg == 1) { result = 0 - result } } rwfunc test() -> () { - r1 = divide(10, 3) // = 等价于 <- + r1 = divide(10, 3) println(r1) - r2 <- divide(7, -3) + r2 = divide(7, -3) println(r2) } diff --git a/tutorial/08-leetcode/033_search_rotated.kv b/tutorial/08-leetcode/033_search_rotated.kv index 36d4ea5e..cb3860d4 100644 --- a/tutorial/08-leetcode/033_search_rotated.kv +++ b/tutorial/08-leetcode/033_search_rotated.kv @@ -4,48 +4,48 @@ // 期望输出: // 4 rwfunc search() -> () { - a:[]int64 <- [4, 5, 6, 7, 0, 1, 2] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [4, 5, 6, 7, 0, 1, 2] + n = ndarray·numel(a) 0 -> target - lo <- 0 - hi = n - 1 // = 等价于 <- + lo = 0 + hi = n - 1 -1 -> result while (lo <= hi) { - s <- lo + hi - mid = s ÷ 2 // = 等价于 <- - mid_m = s ÷ 2 // = 等价于 <- + s = lo + hi + mid = s ÷ 2 + mid_m = s ÷ 2 a[mid] -> mv - eq <- mv == target + eq = mv == target if (eq) { - result = mid // = 等价于 <- + result = mid hi + 1 -> lo } else { - lv <- a[lo] - rv = a[hi] // = 等价于 <- + lv = a[lo] + rv = a[hi] lv <= mv -> left_sorted lv ≤ mv -> left_sorted_m if (left_sorted) { - t1 <- lv <= target - t1_m <- lv ≤ target - t2 = target < mv // = 等价于 <- + t1 = lv <= target + t1_m = lv ≤ target + t2 = target < mv if (t1 && t2) { mid - 1 -> hi } else { - lo <- mid + 1 + lo = mid + 1 } } else { - t3 = mv <= target // = 等价于 <- - t3_m = mv ≤ target // = 等价于 <- + t3 = mv <= target + t3_m = mv ≤ target target <= rv -> t4 target ≤ rv -> t4_m if (t3 && t4) { - lo <- mid + 1 + lo = mid + 1 } else { - hi = mid - 1 // = 等价于 <- + hi = mid - 1 } } } diff --git a/tutorial/08-leetcode/034_search_range.kv b/tutorial/08-leetcode/034_search_range.kv index 488f4484..a1b28833 100644 --- a/tutorial/08-leetcode/034_search_range.kv +++ b/tutorial/08-leetcode/034_search_range.kv @@ -5,42 +5,42 @@ // [ 3 , 4 ] rwfunc search_range() -> () { [5, 7, 7, 8, 8, 10] -> a:[]int64 - n <- ndarray·numel(a) - target = 8 // = 等价于 <- + n = ndarray·numel(a) + target = 8 // find left 0 -> lo - hi <- n - 1 - left = -1 // = 等价于 <- + hi = n - 1 + left = -1 while (lo <= hi) { lo + hi -> s - mid <- s ÷ 2 - mid_m <- s ÷ 2 - mv = a[mid] // = 等价于 <- + mid = s ÷ 2 + mid_m = s ÷ 2 + mv = a[mid] mv < target -> lt if (lt) { - lo <- mid + 1 + lo = mid + 1 } else { - left = mid // = 等价于 <- + left = mid mid - 1 -> hi } } - // find right - lo2 <- 0 - hi2 = n - 1 // = 等价于 <- + // find right + lo2 = 0 + hi2 = n - 1 -1 -> right while (lo2 <= hi2) { - s2 <- lo2 + hi2 - mid2 = s2 ÷ 2 // = 等价于 <- - mid2_m = s2 ÷ 2 // = 等价于 <- + s2 = lo2 + hi2 + mid2 = s2 ÷ 2 + mid2_m = s2 ÷ 2 a[mid2] -> mv2 - gt <- mv2 > target + gt = mv2 > target if (gt) { - hi2 = mid2 - 1 // = 等价于 <- + hi2 = mid2 - 1 } else { mid2 -> right - lo2 <- mid2 + 1 + lo2 = mid2 + 1 } } println("[", left, ",", right, "]") diff --git a/tutorial/08-leetcode/035_search_insert.kv b/tutorial/08-leetcode/035_search_insert.kv index 6d0ada1e..0fe64cc2 100644 --- a/tutorial/08-leetcode/035_search_insert.kv +++ b/tutorial/08-leetcode/035_search_insert.kv @@ -4,33 +4,33 @@ // 期望输出: // index = 2 rwfunc search_insert() -> () { - a:[]int64 = [1, 3, 5, 6] // = 等价于 <- + a:[]int64 = [1, 3, 5, 6] 5 -> target - n <- ndarray·numel(a) - lo = 0 // = 等价于 <- + n = ndarray·numel(a) + lo = 0 n - 1 -> hi - pos <- 0 + pos = 0 while (lo <= hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid s ÷ 2 -> mid_m - mv <- a[mid] - eq = mv == target // = 等价于 <- + mv = a[mid] + eq = mv == target if (eq) { mid -> pos - lo <- hi + 1 + lo = hi + 1 } else { - lt = mv < target // = 等价于 <- + lt = mv < target if (lt) { mid + 1 -> lo } else { - hi <- mid - 1 + hi = mid - 1 } } } - not_found = pos == 0 // = 等价于 <- + not_found = pos == 0 if (not_found) { lo -> pos diff --git a/tutorial/08-leetcode/038_count_and_say.kv b/tutorial/08-leetcode/038_count_and_say.kv index 0bb5572e..39462fa4 100644 --- a/tutorial/08-leetcode/038_count_and_say.kv +++ b/tutorial/08-leetcode/038_count_and_say.kv @@ -8,12 +8,12 @@ // 1211 // 111221 rwfunc next_seq(src_len:int64) -> (dst_len:int64) { - i = 0 // = 等价于 <- - di = 0 // = 等价于 <- + i = 0 + di = 0 while (i < src_len) { /tmp/s[i] -> cur - count = 1 // = 等价于 <- - j <- i + 1 + count = 1 + j = i + 1 while (j < src_len) { /tmp/s[j] -> v if (v == cur) { count + 1 -> count ; j + 1 -> j } @@ -28,11 +28,11 @@ rwfunc next_seq(src_len:int64) -> (dst_len:int64) { } rwfunc print_seq_len(seq_len:int64) -> () { - i = 0 // = 等价于 <- + i = 0 while (i < seq_len) { - cnt <- /tmp/s[i] - val = /tmp/s[i+1] // = 等价于 <- - k = 0 // = 等价于 <- + cnt = /tmp/s[i] + val = /tmp/s[i+1] + k = 0 while (k < cnt) { print(val) k + 1 -> k @@ -43,10 +43,10 @@ rwfunc print_seq_len(seq_len:int64) -> () { } rwfunc copy_from_s2_to_s(s2_len:int64) -> () { - i = 0 // = 等价于 <- + i = 0 while (i < s2_len) { /tmp/s2[i] -> v - _ = kv·set("/tmp/s", i, v) // = 等价于 <- + _ = kv·set("/tmp/s", i, v) i + 1 -> i } } @@ -54,10 +54,10 @@ rwfunc copy_from_s2_to_s(s2_len:int64) -> () { rwfunc count_and_say(N:int64) -> () { _ = kv·set("/tmp/s", 0, 1) // count=1 of digit 1 _ = kv·set("/tmp/s", 1, 1) - cur_len = 2 // = 等价于 <- - round = 1 // = 等价于 <- + cur_len = 2 + round = 1 while (round < N) { - l <- next_seq(cur_len) + l = next_seq(cur_len) copy_from_s2_to_s(l) l -> cur_len round + 1 -> round diff --git a/tutorial/08-leetcode/048_rotate_image.kv b/tutorial/08-leetcode/048_rotate_image.kv index b577735c..08319a83 100644 --- a/tutorial/08-leetcode/048_rotate_image.kv +++ b/tutorial/08-leetcode/048_rotate_image.kv @@ -13,46 +13,46 @@ rwfunc rotate(N:int64) -> () { mat:[]int64 = [1, 2, 3, 4, 5, 6, 7, 8, 9] // transpose - i = 0 // = 等价于 <- + i = 0 while (i < N) { - j = i + 1 // = 等价于 <- + j = i + 1 while (j < N) { - ri <- i × N + j - ri_m <- i × N + j - rj = j × N + i // = 等价于 <- - rj_m = j × N + i // = 等价于 <- + ri = i × N + j + ri_m = i × N + j + rj = j × N + i + rj_m = j × N + i mat[ri] -> t mat[rj] -> u - mat[ri] <- u - mat[rj] <- t + mat[ri] = u + mat[rj] = t j + 1 -> j } i + 1 -> i } // reverse each row - r = 0 // = 等价于 <- + r = 0 while (r < N) { - l <- r × N - l_m <- r × N - h = l + N - 1 // = 等价于 <- + l = r × N + l_m = r × N + h = l + N - 1 while (l < h) { mat[l] -> v1 mat[h] -> v2 - mat[l] <- v2 - mat[h] <- v1 + mat[l] = v2 + mat[h] = v1 l + 1 -> l h - 1 -> h } r + 1 -> r } // print - row = 0 // = 等价于 <- + row = 0 while (row < N) { - col = 0 // = 等价于 <- + col = 0 while (col < N) { - idx = row × N + col // = 等价于 <- - idx_m = row × N + col // = 等价于 <- - v <- mat[idx] + idx = row × N + col + idx_m = row × N + col + v = mat[idx] println(v) col + 1 -> col } diff --git a/tutorial/08-leetcode/050_pow.kv b/tutorial/08-leetcode/050_pow.kv index f6fbfdb5..ad754201 100644 --- a/tutorial/08-leetcode/050_pow.kv +++ b/tutorial/08-leetcode/050_pow.kv @@ -3,25 +3,25 @@ // 期望输出: // 1024.0 rwfunc my_pow(x:float64, n:int64) -> () { - base <- x - exp = n // = 等价于 <- + base = x + exp = n 1.0 -> result - neg <- exp < 0 + neg = exp < 0 if (neg) { - exp = -exp // = 等价于 <- + exp = -exp } while (exp > 0) { exp % 2 -> odd if (odd != 0) { - result <- result × base - result_m <- result × base + result = result × base + result_m = result × base } - base = base × base // = 等价于 <- - base_m = base × base // = 等价于 <- + base = base × base + base_m = base × base exp ÷ 2 -> exp exp ÷ 2 -> exp_m } diff --git a/tutorial/08-leetcode/053_max_subarray.kv b/tutorial/08-leetcode/053_max_subarray.kv index 58ca1f15..41b86b72 100644 --- a/tutorial/08-leetcode/053_max_subarray.kv +++ b/tutorial/08-leetcode/053_max_subarray.kv @@ -4,29 +4,29 @@ // 期望输出: // 6 rwfunc max_subarray(nums:[]int64) -> () { - a:[]int64 <- [-2, 1, -3, 4, -1, 2, 1, -5, 4] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [-2, 1, -3, 4, -1, 2, 1, -5, 4] + n = ndarray·numel(a) a[0] -> cur - max <- a[0] - i = 1 // = 等价于 <- + max = a[0] + i = 1 while (i < n) { a[i] -> x - curx <- cur + x - take_x = x > curx // = 等价于 <- + curx = cur + x + take_x = x > curx if (take_x) { x -> cur } else { - cur <- curx + cur = curx } - bigger = cur > max // = 等价于 <- + bigger = cur > max if (bigger) { cur -> max } - i <- i + 1 + i = i + 1 } println(max) } diff --git a/tutorial/08-leetcode/055_jump_game.kv b/tutorial/08-leetcode/055_jump_game.kv index 33d6806b..464b1677 100644 --- a/tutorial/08-leetcode/055_jump_game.kv +++ b/tutorial/08-leetcode/055_jump_game.kv @@ -4,16 +4,16 @@ // true // false rwfunc can_jump(nums:[]int64) -> (ok:int64) { - ok = 1 // = 等价于 <- - n <- ndarray·numel(nums) + ok = 1 + n = ndarray·numel(nums) 0 -> reach - i = 0 // = 等价于 <- + i = 0 while (i < n) { if (i > reach) { 0 -> ok n -> i } else { - dist <- nums[i] + i + dist = nums[i] + i if (dist > reach) { dist -> reach } if (reach >= n - 1) { n -> i @@ -25,9 +25,9 @@ rwfunc can_jump(nums:[]int64) -> (ok:int64) { rwfunc test() -> () { a1:[]int64 = [2, 3, 1, 1, 4] - a2:[]int64 <- [3, 2, 1, 0, 4] - r1 = can_jump(a1) // = 等价于 <- + a2:[]int64 = [3, 2, 1, 0, 4] + r1 = can_jump(a1) if (r1 == 1) { println("true") } else { println("false") } - r2 <- can_jump(a2) + r2 = can_jump(a2) if (r2 == 1) { println("true") } else { println("false") } } diff --git a/tutorial/08-leetcode/056_merge_intervals.kv b/tutorial/08-leetcode/056_merge_intervals.kv index 2cffb774..64b95e76 100644 --- a/tutorial/08-leetcode/056_merge_intervals.kv +++ b/tutorial/08-leetcode/056_merge_intervals.kv @@ -6,9 +6,9 @@ // [8,10] // [15,18] rwfunc bubble_sort(n:int64) -> () { - i = 0 // = 等价于 <- + i = 0 while (i < n - 1) { - j = 0 // = 等价于 <- + j = 0 while (j < n - 1 - i) { /tmp/mi[j][0] -> a /tmp/mi[j+1][0] -> b @@ -16,8 +16,8 @@ rwfunc bubble_sort(n:int64) -> () { // swap start /tmp/mi[j][1] -> a1 /tmp/mi[j+1][1] -> b1 - _ = kv·set("/tmp/mi", j, [b, b1]) // = 等价于 <- - _ = kv·set("/tmp/mi", j+1, [a, a1]) // = 等价于 <- + _ = kv·set("/tmp/mi", j, [b, b1]) + _ = kv·set("/tmp/mi", j+1, [a, a1]) } j + 1 -> j } @@ -27,10 +27,10 @@ rwfunc bubble_sort(n:int64) -> () { rwfunc merge(n:int64) -> (m:int64) { bubble_sort(n) - result:object = {} // = 等价于 <- - ki = 0 // = 等价于 <- + result:object = {} + ki = 0 /tmp/mi[0] -> cur - idx = 1 // = 等价于 <- + idx = 1 while (idx < n) { /tmp/mi[idx] -> nxt cur[1] -> cur_end @@ -38,12 +38,12 @@ rwfunc merge(n:int64) -> (m:int64) { nxt[1] -> nxt_end if (cur_end >= nxt_start) { if (nxt_end > cur_end) { - _ = kv·set("/tmp/mr", ki, [cur[0], nxt_end]) // = 等价于 <- + _ = kv·set("/tmp/mr", ki, [cur[0], nxt_end]) /tmp/mr[ki] -> cur } } else { _ = kv·set("/tmp/mr", ki, cur) - ki = ki + 1 // = 等价于 <- + ki = ki + 1 nxt -> cur } idx + 1 -> idx @@ -58,8 +58,8 @@ rwfunc test() -> () { _ = kv·set("/tmp/mi", 1, [2, 6]) _ = kv·set("/tmp/mi", 2, [8, 10]) _ = kv·set("/tmp/mi", 3, [15, 18]) - cnt <- merge(4) - i = 0 // = 等价于 <- + cnt = merge(4) + i = 0 while (i < cnt) { /tmp/mr[i] -> iv print("[", iv[0], ",", iv[1], "]"); println() diff --git a/tutorial/08-leetcode/058_length_last_word.kv b/tutorial/08-leetcode/058_length_last_word.kv index 6c447e2a..5a62e93b 100644 --- a/tutorial/08-leetcode/058_length_last_word.kv +++ b/tutorial/08-leetcode/058_length_last_word.kv @@ -3,17 +3,17 @@ // 期望输出: // 5 rwfunc last_word_len(s:[]char/utf32) -> () { - n = string·len(s) // = 等价于 <- + n = string·len(s) n - 1 -> i - count <- 0 + count = 0 while (i >= 0) { - c = string·char(s, i) // = 等价于 <- + c = string·char(s, i) c == " " -> sp if (sp) { - i <- -1 + i = -1 } else { - count = count + 1 // = 等价于 <- + count = count + 1 } i - 1 -> i diff --git a/tutorial/08-leetcode/062_unique_paths.kv b/tutorial/08-leetcode/062_unique_paths.kv index fe8352d5..4462c6f0 100644 --- a/tutorial/08-leetcode/062_unique_paths.kv +++ b/tutorial/08-leetcode/062_unique_paths.kv @@ -4,18 +4,18 @@ // 28 // 3 rwfunc dp(m:int64, n:int64) -> (result:int64) { - i = 0 // = 等价于 <- + i = 0 while (i < n) { - _ = kv·set("/tmp/up", i, 1) // = 等价于 <- + _ = kv·set("/tmp/up", i, 1) i + 1 -> i } - j <- 1 + j = 1 while (j < m) { - k = 1 // = 等价于 <- + k = 1 while (k < n) { /tmp/up[k] -> v /tmp/up[k-1] -> prev - _ = kv·set("/tmp/up", k, prev + v) // = 等价于 <- + _ = kv·set("/tmp/up", k, prev + v) k + 1 -> k } j + 1 -> j @@ -24,8 +24,8 @@ rwfunc dp(m:int64, n:int64) -> (result:int64) { } rwfunc test() -> () { - r1 <- dp(3, 7) + r1 = dp(3, 7) println(r1) - r2 = dp(3, 2) // = 等价于 <- + r2 = dp(3, 2) println(r2) } diff --git a/tutorial/08-leetcode/064_min_path_sum.kv b/tutorial/08-leetcode/064_min_path_sum.kv index 357bef63..5bd6ded4 100644 --- a/tutorial/08-leetcode/064_min_path_sum.kv +++ b/tutorial/08-leetcode/064_min_path_sum.kv @@ -3,30 +3,30 @@ // 期望输出: // 7 rwfunc min_path_sum() -> (result:int64) { - rows = 3 // = 等价于 <- - cols <- 3 + rows = 3 + cols = 3 grid:[]int64 = [1, 3, 1, 1, 5, 1, 4, 2, 1] // first row - j = 1 // = 等价于 <- + j = 1 while (j < cols) { - grid[j] <- grid[j] + grid[j-1] + grid[j] = grid[j] + grid[j-1] j + 1 -> j } // first col and rest - r = 1 // = 等价于 <- + r = 1 while (r < rows) { - idx0 = r × cols // = 等价于 <- - idx0_m = r × cols // = 等价于 <- - grid[idx0] <- grid[idx0] + grid[idx0-cols] - c = 1 // = 等价于 <- + idx0 = r × cols + idx0_m = r × cols + grid[idx0] = grid[idx0] + grid[idx0-cols] + c = 1 while (c < cols) { - idx = r × cols + c // = 等价于 <- - idx_m = r × cols + c // = 等价于 <- - up <- grid[idx-cols] - left = grid[idx-1] // = 等价于 <- - min = up // = 等价于 <- + idx = r × cols + c + idx_m = r × cols + c + up = grid[idx-cols] + left = grid[idx-1] + min = up if (left < up) { left -> min } - grid[idx] <- grid[idx] + min + grid[idx] = grid[idx] + min c + 1 -> c } r + 1 -> r @@ -35,6 +35,6 @@ rwfunc min_path_sum() -> (result:int64) { } rwfunc test() -> () { - r <- min_path_sum() + r = min_path_sum() println(r) } diff --git a/tutorial/08-leetcode/066_plus_one.kv b/tutorial/08-leetcode/066_plus_one.kv index 2995fb81..22808951 100644 --- a/tutorial/08-leetcode/066_plus_one.kv +++ b/tutorial/08-leetcode/066_plus_one.kv @@ -9,60 +9,60 @@ // 0 // 0 rwfunc plus_one() -> () { - a:[]int64 <- [1, 2, 3] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [1, 2, 3] + n = ndarray·numel(a) n - 1 -> i - carry <- 1 + carry = 1 while (i >= 0) { - d = a[i] // = 等价于 <- + d = a[i] d + carry -> s - overflow <- s == 10 + overflow = s == 10 if (overflow) { - carry = 0 // = 等价于 <- - a[i] <- 0 + carry = 0 + a[i] = 0 } else { - a[i] <- s + a[i] = s 0 -> carry - i <- -1 + i = -1 } - i = i - 1 // = 等价于 <- + i = i - 1 } carry == 1 -> need_extend - len_val <- n - j = 0 // = 等价于 <- + len_val = n + j = 0 while (j < n) { a[j] -> v println(v) - j <- j + 1 + j = j + 1 } } rwfunc plus_one_nines() -> () { - a:[]int64 = [9, 9, 9] // = 等价于 <- + a:[]int64 = [9, 9, 9] ndarray·numel(a) -> n - i <- n - 1 - carry = 1 // = 等价于 <- + i = n - 1 + carry = 1 while (i >= 0) { a[i] -> d - s <- d + carry - overflow = s == 10 // = 等价于 <- + s = d + carry + overflow = s == 10 if (overflow) { - a[i] <- 0 + a[i] = 0 1 -> carry } else { - a[i] <- s - carry <- 0 - i = -1 // = 等价于 <- + a[i] = s + carry = 0 + i = -1 } i - 1 -> i } - j <- 0 + j = 0 while (j < n) { - v = a[j] // = 等价于 <- + v = a[j] println(v) j + 1 -> j } diff --git a/tutorial/08-leetcode/067_add_binary.kv b/tutorial/08-leetcode/067_add_binary.kv index 7669ffbd..942704fd 100644 --- a/tutorial/08-leetcode/067_add_binary.kv +++ b/tutorial/08-leetcode/067_add_binary.kv @@ -4,24 +4,24 @@ // 100 // 10101 rwfunc add_binary(a:[]char/utf32, b:[]char/utf32) -> () { - na = string·len(a) // = 等价于 <- - nb <- string·len(b) - carry = 0 // = 等价于 <- - i = na - 1 // = 等价于 <- - j <- nb - 1 - result = "" // = 等价于 <- + na = string·len(a) + nb = string·len(b) + carry = 0 + i = na - 1 + j = nb - 1 + result = "" while (i >= 0 || j >= 0 || carry > 0) { - sum <- carry + sum = carry if (i >= 0) { if (a[i] == "1") { sum + 1 -> sum } - i = i - 1 // = 等价于 <- + i = i - 1 } if (j >= 0) { if (b[j] == "1") { 1 + sum -> sum } j - 1 -> j } if (sum % 2 == 1) { - result = "1" + result // = 等价于 <- + result = "1" + result } else { "0" + result -> result } diff --git a/tutorial/08-leetcode/069_sqrt.kv b/tutorial/08-leetcode/069_sqrt.kv index 57a2d342..a2b29cfd 100644 --- a/tutorial/08-leetcode/069_sqrt.kv +++ b/tutorial/08-leetcode/069_sqrt.kv @@ -3,23 +3,23 @@ // 期望输出: // 2 rwfunc my_sqrt(x:int64) -> () { - lo <- 0 - hi = x // = 等价于 <- + lo = 0 + hi = x 0 -> ans while (lo <= hi) { - s <- lo + hi - mid = s ÷ 2 // = 等价于 <- - mid_m = s ÷ 2 // = 等价于 <- + s = lo + hi + mid = s ÷ 2 + mid_m = s ÷ 2 mid × mid -> sq mid × mid -> sq_m - ok <- sq <= x - ok_m <- sq ≤ x + ok = sq <= x + ok_m = sq ≤ x if (ok) { - ans = mid // = 等价于 <- + ans = mid mid + 1 -> lo } else { - hi <- mid - 1 + hi = mid - 1 } } println(ans) diff --git a/tutorial/08-leetcode/070_climb_stairs.kv b/tutorial/08-leetcode/070_climb_stairs.kv index 060ba359..85e51a2e 100644 --- a/tutorial/08-leetcode/070_climb_stairs.kv +++ b/tutorial/08-leetcode/070_climb_stairs.kv @@ -3,14 +3,14 @@ // 期望输出: // 8 rwfunc climb(n:int64) -> () { - a = 1 // = 等价于 <- + a = 1 1 -> b - i <- 2 + i = 2 while (i <= n) { - c = a + b // = 等价于 <- + c = a + b b -> a - b <- c - i = i + 1 // = 等价于 <- + b = c + i = i + 1 } println(c) } diff --git a/tutorial/08-leetcode/075_sort_colors.kv b/tutorial/08-leetcode/075_sort_colors.kv index 8e79f98a..d9a4ef65 100644 --- a/tutorial/08-leetcode/075_sort_colors.kv +++ b/tutorial/08-leetcode/075_sort_colors.kv @@ -9,30 +9,30 @@ // 2 rwfunc sort_colors() -> () { nums:[]int64 = [2, 0, 2, 1, 1, 0] - n = ndarray·numel(nums) // = 等价于 <- - lo = 0 // = 等价于 <- - mid <- 0 - hi = n - 1 // = 等价于 <- + n = ndarray·numel(nums) + lo = 0 + mid = 0 + hi = n - 1 while (mid <= hi) { nums[mid] -> v if (v == 0) { nums[lo] -> t - nums[mid] <- t - nums[lo] <- v + nums[mid] = t + nums[lo] = v lo + 1 -> lo - mid <- mid + 1 + mid = mid + 1 } else if (v == 1) { mid + 1 -> mid } else { nums[hi] -> t - nums[mid] <- t - nums[hi] <- v + nums[mid] = t + nums[hi] = v hi - 1 -> hi } } - i = 0 // = 等价于 <- + i = 0 while (i < n) { println(nums[i]) i + 1 -> i diff --git a/tutorial/08-leetcode/083_remove_dup_list.kv b/tutorial/08-leetcode/083_remove_dup_list.kv index dad6262b..5650e5cc 100644 --- a/tutorial/08-leetcode/083_remove_dup_list.kv +++ b/tutorial/08-leetcode/083_remove_dup_list.kv @@ -7,21 +7,21 @@ // 2 // 3 rwfunc build() -> () { - /e0 = { val=1; next="/e1" } // = 等价于 <- - /e1 <- { val=1; next="/e2" } + /e0 = { val=1; next="/e1" } + /e1 = { val=1; next="/e2" } { val=2; next="" } -> /e2 - /f0 = { val=1; next="/f1" } // = 等价于 <- - /f1 <- { val=1; next="/f2" } + /f0 = { val=1; next="/f1" } + /f1 = { val=1; next="/f2" } { val=2; next="/f3" } -> /f2 - /f3 = { val=3; next="/f4" } // = 等价于 <- - /f4 <- { val=3; next="" } + /f3 = { val=3; next="/f4" } + /f4 = { val=3; next="" } } rwfunc dedup(head:[]char/utf32) -> () { head -> cur while (cur != "") { cur·next -> nxt - advance = true // = 等价于 <- + advance = true if (nxt != "") { cur·val -> v @@ -29,7 +29,7 @@ rwfunc dedup(head:[]char/utf32) -> () { if (nv == v) { nxt·next -> nn - cur·next <- nn + cur·next = nn false -> advance } } diff --git a/tutorial/08-leetcode/088_merge_sorted.kv b/tutorial/08-leetcode/088_merge_sorted.kv index 9c78d86f..eca7bbbb 100644 --- a/tutorial/08-leetcode/088_merge_sorted.kv +++ b/tutorial/08-leetcode/088_merge_sorted.kv @@ -10,42 +10,42 @@ // 6 rwfunc merge() -> () { [1, 2, 3, 0, 0, 0] -> a:[]int64 - m <- 3 - b:[]int64 = [2, 5, 6] // = 等价于 <- + m = 3 + b:[]int64 = [2, 5, 6] 3 -> n - i <- m - 1 - j = n - 1 // = 等价于 <- + i = m - 1 + j = n - 1 m + n -> total - k <- total - 1 + k = total - 1 while (j >= 0) { - i_ok = i >= 0 // = 等价于 <- - i_ok_m = i ≥ 0 // = 等价于 <- + i_ok = i >= 0 + i_ok_m = i ≥ 0 if (i_ok) { a[i] -> av - bv <- b[j] - a_bigger = av > bv // = 等价于 <- + bv = b[j] + a_bigger = av > bv if (a_bigger) { - a[k] <- av + a[k] = av i - 1 -> i } else { - a[k] <- bv - j <- j - 1 + a[k] = bv + j = j - 1 } } else { - bv2 = b[j] // = 等价于 <- - a[k] <- bv2 + bv2 = b[j] + a[k] = bv2 j - 1 -> j } - k <- k - 1 + k = k - 1 } - x = 0 // = 等价于 <- + x = 0 while (x < 6) { a[x] -> v println(v) - x <- x + 1 + x = x + 1 } } diff --git a/tutorial/08-leetcode/089_gray_code.kv b/tutorial/08-leetcode/089_gray_code.kv index 9a4f023b..d4630886 100644 --- a/tutorial/08-leetcode/089_gray_code.kv +++ b/tutorial/08-leetcode/089_gray_code.kv @@ -7,17 +7,17 @@ // 3 // 2 rwfunc gray_code(n:int64) -> () { - total = 1 // = 等价于 <- - k = 0 // = 等价于 <- + total = 1 + k = 0 while (k < n) { - total <- total × 2 - total_m <- total × 2 + total = total × 2 + total_m = total × 2 k + 1 -> k } - i = 0 // = 等价于 <- + i = 0 while (i < total) { - g <- i ^ (i ÷ 2) - g_m <- i ^ (i ÷ 2) + g = i ^ (i ÷ 2) + g_m = i ^ (i ÷ 2) println(g_m) println(g) i + 1 -> i diff --git a/tutorial/08-leetcode/094_inorder_traversal.kv b/tutorial/08-leetcode/094_inorder_traversal.kv index 6de0ec1c..e6a83000 100644 --- a/tutorial/08-leetcode/094_inorder_traversal.kv +++ b/tutorial/08-leetcode/094_inorder_traversal.kv @@ -6,15 +6,15 @@ // 3 rwfunc build_tree() -> () { /t0 = { val=1; left="/t1"; right="/t2" } - /t1 <- { val=2; left=""; right="" } + /t1 = { val=2; left=""; right="" } { val=3; left=""; right="" } -> /t2 } rwfunc inorder(root:[]char/utf32) -> () { // iterative stack simulation - si = 0 // = 等价于 <- - stack:[int64]·[]char/utf32 = {} // = 等价于 <- - cur <- root + si = 0 + stack:[int64]·[]char/utf32 = {} + cur = root while (cur != "" || si > 0) { while (cur != "") { _ = kv·set(stack, si, cur) diff --git a/tutorial/08-leetcode/100_same_tree.kv b/tutorial/08-leetcode/100_same_tree.kv index c7b44b5b..bfe37de2 100644 --- a/tutorial/08-leetcode/100_same_tree.kv +++ b/tutorial/08-leetcode/100_same_tree.kv @@ -4,11 +4,11 @@ // true // false rwfunc build() -> () { - /t0 <- { val=1; left="/t1"; right="/t2" } + /t0 = { val=1; left="/t1"; right="/t2" } { val=2; left=""; right="" } -> /t1 - /t2 <- { val=3; left=""; right="" } - /v0 <- { val=1; left="/v1"; right="/v2" } - /v1 <- { val=2; left=""; right="" } + /t2 = { val=3; left=""; right="" } + /v0 = { val=1; left="/v1"; right="/v2" } + /v1 = { val=2; left=""; right="" } { val=4; left=""; right="" } -> /v2 } diff --git a/tutorial/08-leetcode/104_max_tree_depth.kv b/tutorial/08-leetcode/104_max_tree_depth.kv index 721dccb0..66d51507 100644 --- a/tutorial/08-leetcode/104_max_tree_depth.kv +++ b/tutorial/08-leetcode/104_max_tree_depth.kv @@ -5,23 +5,23 @@ rwfunc build_tree() -> () { /t0 = { val=3; left="/t1"; right="/t2" } { val=9; left=""; right="" } -> /t1 - /t2 <- { val=20; left="/t3"; right="/t4" } - /t3 = { val=15; left=""; right="" } // = 等价于 <- + /t2 = { val=20; left="/t3"; right="/t4" } + /t3 = { val=15; left=""; right="" } { val=7; left=""; right="" } -> /t4 } rwfunc max_depth(root:[]char/utf32) -> (depth:int64) { if (root == "") { 0 -> depth } else { - ld = max_depth(root·left) // = 等价于 <- - rd <- max_depth(root·right) + ld = max_depth(root·left) + rd = max_depth(root·right) if (ld > rd) { ld + 1 -> depth } - else { depth <- rd + 1 } + else { depth = rd + 1 } } } rwfunc test() -> () { build_tree() - d <- max_depth("/t0") + d = max_depth("/t0") println(d) } diff --git a/tutorial/08-leetcode/118_pascal_triangle.kv b/tutorial/08-leetcode/118_pascal_triangle.kv index 62921b47..9e81747c 100644 --- a/tutorial/08-leetcode/118_pascal_triangle.kv +++ b/tutorial/08-leetcode/118_pascal_triangle.kv @@ -22,15 +22,15 @@ rwfunc generate(num_rows:int64) -> () { _ = kv·set("/tmp/pt", 0, 1) println(1) if (num_rows == 1) { } - r = 1 // = 等价于 <- + r = 1 while (r < num_rows) { println(1) _ = kv·set("/tmp/cur", 0, 1) - i = 1 // = 等价于 <- + i = 1 while (i < r) { /tmp/pt[i-1] -> a /tmp/pt[i] -> b - v <- a + b + v = a + b println(v) _ = kv·set("/tmp/cur", i, v) i + 1 -> i @@ -38,7 +38,7 @@ rwfunc generate(num_rows:int64) -> () { println(1) _ = kv·set("/tmp/cur", r, 1) // copy cur to pt - k = 0 // = 等价于 <- + k = 0 while (k <= r) { /tmp/cur[k] -> val _ = kv·set("/tmp/pt", k, val) diff --git a/tutorial/08-leetcode/121_buy_sell_stock.kv b/tutorial/08-leetcode/121_buy_sell_stock.kv index fb4635b6..932b7ef3 100644 --- a/tutorial/08-leetcode/121_buy_sell_stock.kv +++ b/tutorial/08-leetcode/121_buy_sell_stock.kv @@ -4,27 +4,27 @@ // 期望输出: // 5 rwfunc max_profit() -> () { - a:[]int64 = [7, 1, 5, 3, 6, 4] // = 等价于 <- + a:[]int64 = [7, 1, 5, 3, 6, 4] ndarray·numel(a) -> n - min_price <- a[0] - max_profit = 0 // = 等价于 <- + min_price = a[0] + max_profit = 0 1 -> i while (i < n) { - price <- a[i] - new_min = price < min_price // = 等价于 <- + price = a[i] + new_min = price < min_price if (new_min) { price -> min_price } else { - profit <- price - min_price - bigger = profit > max_profit // = 等价于 <- + profit = price - min_price + bigger = profit > max_profit if (bigger) { profit -> max_profit } } - i <- i + 1 + i = i + 1 } println(max_profit) } diff --git a/tutorial/08-leetcode/125_valid_palindrome.kv b/tutorial/08-leetcode/125_valid_palindrome.kv index 40285bcd..1b6ca824 100644 --- a/tutorial/08-leetcode/125_valid_palindrome.kv +++ b/tutorial/08-leetcode/125_valid_palindrome.kv @@ -5,44 +5,44 @@ // 0 // 用整数数组模拟字符串,1='a', 2='b', etc. rwfunc is_pal() -> () { - s:[]int64 = [1, 2, 1] // = 等价于 <- + s:[]int64 = [1, 2, 1] ndarray·numel(s) -> n - l <- 0 - r = n - 1 // = 等价于 <- + l = 0 + r = n - 1 1 -> ok while (l < r) { - lc <- s[l] - rc = s[r] // = 等价于 <- + lc = s[l] + rc = s[r] lc == rc -> match if (match) { - l <- l + 1 - r = r - 1 // = 等价于 <- + l = l + 1 + r = r - 1 } else { 0 -> ok - l <- n + l = n } } println(ok) } rwfunc not_pal() -> () { - s:[]int64 = [1, 2] // = 等价于 <- + s:[]int64 = [1, 2] ndarray·numel(s) -> n - l <- 0 - r = n - 1 // = 等价于 <- + l = 0 + r = n - 1 1 -> ok while (l < r) { - lc <- s[l] - rc = s[r] // = 等价于 <- + lc = s[l] + rc = s[r] lc == rc -> match if (match) { - l <- l + 1 - r = r - 1 // = 等价于 <- + l = l + 1 + r = r - 1 } else { 0 -> ok - l <- n + l = n } } println(ok) diff --git a/tutorial/08-leetcode/125_valid_palindrome_str.kv b/tutorial/08-leetcode/125_valid_palindrome_str.kv index cfab721e..5cdceb68 100644 --- a/tutorial/08-leetcode/125_valid_palindrome_str.kv +++ b/tutorial/08-leetcode/125_valid_palindrome_str.kv @@ -4,20 +4,20 @@ // 1 // 0 rwfunc is_pal(s:[]char/utf32) -> () { - n = string·len(s) // = 等价于 <- + n = string·len(s) 0 -> l - r <- n - 1 - ok = 1 // = 等价于 <- + r = n - 1 + ok = 1 while (l < r) { string·char(s, l) -> lc - rc <- string·char(s, r) - match = lc == rc // = 等价于 <- + rc = string·char(s, r) + match = lc == rc if (match) { l + 1 -> l - r <- r - 1 + r = r - 1 } else { - ok = 0 // = 等价于 <- + ok = 0 n -> l } } diff --git a/tutorial/08-leetcode/136_single_number.kv b/tutorial/08-leetcode/136_single_number.kv index 379b3bad..5308ca53 100644 --- a/tutorial/08-leetcode/136_single_number.kv +++ b/tutorial/08-leetcode/136_single_number.kv @@ -4,14 +4,14 @@ // 期望输出: // 4 rwfunc single_number() -> () { - a:[]int64 <- [4, 1, 2, 1, 2] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [4, 1, 2, 1, 2] + n = ndarray·numel(a) a[0] -> result - i <- 1 + i = 1 while (i < n) { - x = a[i] // = 等价于 <- + x = a[i] result ^ x -> result - i <- i + 1 + i = i + 1 } println(result) } diff --git a/tutorial/08-leetcode/141_linked_list_cycle.kv b/tutorial/08-leetcode/141_linked_list_cycle.kv index ab3bd491..28884a53 100644 --- a/tutorial/08-leetcode/141_linked_list_cycle.kv +++ b/tutorial/08-leetcode/141_linked_list_cycle.kv @@ -4,22 +4,22 @@ // 1 // 0 rwfunc has_cycle(head:[]char/utf32) -> (result:int64) { - result = 0 // = 等价于 <- + result = 0 head -> slow - fast <- head + fast = head while (fast != "") { - slow = slow·next // = 等价于 <- + slow = slow·next fast·next -> n1 - at_end <- n1 == "" + at_end = n1 == "" if (at_end) { - fast = "" // = 等价于 <- + fast = "" } else { n1·next -> fast - match <- slow == fast + match = slow == fast if (match) { - result = 1 // = 等价于 <- + result = 1 "" -> fast } } @@ -28,19 +28,19 @@ rwfunc has_cycle(head:[]char/utf32) -> (result:int64) { rwfunc build_cycle() -> () { { val=3; next="/c1" } -> /c0 - /c1 = { val=2; next="/c2" } // = 等价于 <- - /c2 <- { val=0; next="/c0" } + /c1 = { val=2; next="/c2" } + /c2 = { val=0; next="/c0" } } rwfunc build_no_cycle() -> () { - /n0 = { val=1; next="/n1" } // = 等价于 <- + /n0 = { val=1; next="/n1" } { val=2; next="" } -> /n1 } rwfunc test() -> () { build_cycle() - r1 = has_cycle("/c0") // = 等价于 <- + r1 = has_cycle("/c0") println(r1) build_no_cycle() has_cycle("/n0") -> r2 diff --git a/tutorial/08-leetcode/153_find_min_rotated.kv b/tutorial/08-leetcode/153_find_min_rotated.kv index 9a8d869b..f4c225e7 100644 --- a/tutorial/08-leetcode/153_find_min_rotated.kv +++ b/tutorial/08-leetcode/153_find_min_rotated.kv @@ -3,21 +3,21 @@ // 期望输出: // 0 rwfunc find_min() -> () { - a:[]int64 <- [4, 5, 6, 7, 0, 1, 2] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [4, 5, 6, 7, 0, 1, 2] + n = ndarray·numel(a) 0 -> lo - hi <- n - 1 + hi = n - 1 while (lo < hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid - mv <- a[mid] - rv = a[hi] // = 等价于 <- + mv = a[mid] + rv = a[hi] mv < rv -> ok if (ok) { - hi <- mid + hi = mid } else { - lo = mid + 1 // = 等价于 <- + lo = mid + 1 } } a[lo] -> ans diff --git a/tutorial/08-leetcode/160_intersection_list.kv b/tutorial/08-leetcode/160_intersection_list.kv index 5e151246..611fff41 100644 --- a/tutorial/08-leetcode/160_intersection_list.kv +++ b/tutorial/08-leetcode/160_intersection_list.kv @@ -5,23 +5,23 @@ // 8 rwfunc build_lists() -> () { // common tail: 8→4→5 - /t8 = { val=8; next="/t4" } // = 等价于 <- - /t4 <- { val=4; next="/t5" } + /t8 = { val=8; next="/t4" } + /t4 = { val=4; next="/t5" } { val=5; next="" } -> /t5 // list A: 4→1→/t8 - /a0 = { val=4; next="/a1" } // = 等价于 <- + /a0 = { val=4; next="/a1" } { val=1; next="/t8" } -> /a1 // list B: 5→6→1→/t8 - /b0 = { val=5; next="/b1" } // = 等价于 <- - /b1 <- { val=6; next="/b2" } + /b0 = { val=5; next="/b1" } + /b1 = { val=6; next="/b2" } { val=1; next="/t8" } -> /b2 } rwfunc get_intersection(ha:[]char/utf32, hb:[]char/utf32) -> () { - pa = ha // = 等价于 <- - pb <- hb - switch_a = 0 // = 等价于 <- - switch_b <- 0 + pa = ha + pb = hb + switch_a = 0 + switch_b = 0 while (pa != pb) { pa·next -> nxt if (nxt == "") { @@ -38,7 +38,7 @@ rwfunc get_intersection(ha:[]char/utf32, hb:[]char/utf32) -> () { if (nxt2 == "") { if (switch_b == 0) { ha -> pb - switch_b <- 1 + switch_b = 1 } else { "" -> pb } diff --git a/tutorial/08-leetcode/162_find_peak.kv b/tutorial/08-leetcode/162_find_peak.kv index 3ccc279a..1641644c 100644 --- a/tutorial/08-leetcode/162_find_peak.kv +++ b/tutorial/08-leetcode/162_find_peak.kv @@ -3,22 +3,22 @@ // 期望输出: // 2 rwfunc find_peak() -> () { - a:[]int64 <- [1, 2, 3, 1] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [1, 2, 3, 1] + n = ndarray·numel(a) 0 -> lo - hi <- n - 1 + hi = n - 1 while (lo < hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid s ÷ 2 -> mid_m - mv <- a[mid] - nv = a[mid + 1] // = 等价于 <- + mv = a[mid] + nv = a[mid + 1] mv < nv -> up if (up) { - lo <- mid + 1 + lo = mid + 1 } else { - hi = mid // = 等价于 <- + hi = mid } } println(lo) diff --git a/tutorial/08-leetcode/167_two_sum_ii.kv b/tutorial/08-leetcode/167_two_sum_ii.kv index 4e2b6006..828e0612 100644 --- a/tutorial/08-leetcode/167_two_sum_ii.kv +++ b/tutorial/08-leetcode/167_two_sum_ii.kv @@ -4,28 +4,28 @@ // [ 1 , 2 ] rwfunc two_sum_ii() -> () { [2, 7, 11, 15] -> a:[]int64 - target <- 9 - n = ndarray·numel(a) // = 等价于 <- + target = 9 + n = ndarray·numel(a) 0 -> l - r <- n - 1 + r = n - 1 while (l < r) { - lv = a[l] // = 等价于 <- + lv = a[l] a[r] -> rv - s <- lv + rv - hit = s == target // = 等价于 <- + s = lv + rv + hit = s == target if (hit) { l + 1 -> i1 - i2 <- r + 1 + i2 = r + 1 println("[", i1, ",", i2, "]") - l = n // = 等价于 <- + l = n } else { s < target -> lt if (lt) { - l <- l + 1 + l = l + 1 } else { - r = r - 1 // = 等价于 <- + r = r - 1 } } } diff --git a/tutorial/08-leetcode/168_excel_title.kv b/tutorial/08-leetcode/168_excel_title.kv index 8c868c3b..16d3506e 100644 --- a/tutorial/08-leetcode/168_excel_title.kv +++ b/tutorial/08-leetcode/168_excel_title.kv @@ -6,22 +6,22 @@ rwfunc convert(n:int64) -> () { // Build reversed chars into array, then print in reverse [0, 0, 0, 0, 0] -> a:[]int64 - idx <- 0 - num = n // = 等价于 <- + idx = 0 + num = n while (num > 0) { num - 1 -> n1 - r <- n1 % 26 - ch = 65 + r // = 等价于 <- - a[idx] <- ch + r = n1 % 26 + ch = 65 + r + a[idx] = ch idx + 1 -> idx - num <- n1 ÷ 26 - num_m <- n1 ÷ 26 + num = n1 ÷ 26 + num_m = n1 ÷ 26 } - k = idx - 1 // = 等价于 <- + k = idx - 1 while (k >= 0) { a[k] -> v println(v) - k <- k - 1 + k = k - 1 } } diff --git a/tutorial/08-leetcode/169_majority.kv b/tutorial/08-leetcode/169_majority.kv index eec61be7..60589d88 100644 --- a/tutorial/08-leetcode/169_majority.kv +++ b/tutorial/08-leetcode/169_majority.kv @@ -4,29 +4,29 @@ // 期望输出: // 3 rwfunc majority() -> () { - a:[]int64 = [3, 2, 3] // = 等价于 <- + a:[]int64 = [3, 2, 3] ndarray·numel(a) -> n - candidate <- a[0] - count = 1 // = 等价于 <- + candidate = a[0] + count = 1 1 -> i while (i < n) { - x <- a[i] - reset = count == 0 // = 等价于 <- + x = a[i] + reset = count == 0 if (reset) { x -> candidate - count <- 1 + count = 1 } else { - match = x == candidate // = 等价于 <- + match = x == candidate if (match) { count + 1 -> count } else { - count <- count - 1 + count = count - 1 } } - i = i + 1 // = 等价于 <- + i = i + 1 } println(candidate) } diff --git a/tutorial/08-leetcode/171_excel_column.kv b/tutorial/08-leetcode/171_excel_column.kv index 2e7886e7..275f3ad8 100644 --- a/tutorial/08-leetcode/171_excel_column.kv +++ b/tutorial/08-leetcode/171_excel_column.kv @@ -4,16 +4,16 @@ // 28 rwfunc title_to_num(s:[]char/utf32) -> () { string·len(s) -> n - result <- 0 - i = 0 // = 等价于 <- + result = 0 + i = 0 while (i < n) { result × 26 -> r26 result × 26 -> r26_m - c <- string·char(s, i) - v = string·ord(c) - 65 // = 等价于 <- + c = string·char(s, i) + v = string·ord(c) - 65 v + 1 -> d - result <- r26 + d - i = i + 1 // = 等价于 <- + result = r26 + d + i = i + 1 } println(result) } diff --git a/tutorial/08-leetcode/172_fact_zeroes.kv b/tutorial/08-leetcode/172_fact_zeroes.kv index dc40b84e..764a7020 100644 --- a/tutorial/08-leetcode/172_fact_zeroes.kv +++ b/tutorial/08-leetcode/172_fact_zeroes.kv @@ -6,9 +6,9 @@ rwfunc trailing(n:int64) -> () { n -> nv 0 -> count while (nv >= 5) { - nv <- nv ÷ 5 - nv_m <- nv ÷ 5 - count = count + nv // = 等价于 <- + nv = nv ÷ 5 + nv_m = nv ÷ 5 + count = count + nv } println(count) } diff --git a/tutorial/08-leetcode/189_rotate_array.kv b/tutorial/08-leetcode/189_rotate_array.kv index 6b32a9ab..8450948c 100644 --- a/tutorial/08-leetcode/189_rotate_array.kv +++ b/tutorial/08-leetcode/189_rotate_array.kv @@ -15,57 +15,57 @@ rwfunc swap(a_len:int64, lo:int64, hi:int64) -> () { hi -> hiv while (lov < hiv) { a[lov] -> t - u <- a[hiv] - a[lov] <- u - a[hiv] <- t - lov = lov + 1 // = 等价于 <- + u = a[hiv] + a[lov] = u + a[hiv] = t + lov = lov + 1 hiv - 1 -> hiv } } rwfunc rotate() -> () { - a:[]int64 <- [1, 2, 3, 4, 5, 6, 7] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [1, 2, 3, 4, 5, 6, 7] + n = ndarray·numel(a) 3 -> k - k_eff <- k % n + k_eff = k % n // reverse all - lo = 0 // = 等价于 <- + lo = 0 n - 1 -> hi while (lo < hi) { - t <- a[lo] - u = a[hi] // = 等价于 <- - a[lo] <- u - a[hi] <- t + t = a[lo] + u = a[hi] + a[lo] = u + a[hi] = t lo + 1 -> lo - hi <- hi - 1 + hi = hi - 1 } // reverse first k - lo2 = 0 // = 等价于 <- + lo2 = 0 k_eff - 1 -> hi2 while (lo2 < hi2) { - t2 <- a[lo2] - u2 = a[hi2] // = 等价于 <- - a[lo2] <- u2 - a[hi2] <- t2 + t2 = a[lo2] + u2 = a[hi2] + a[lo2] = u2 + a[hi2] = t2 lo2 + 1 -> lo2 - hi2 <- hi2 - 1 + hi2 = hi2 - 1 } // reverse last n-k - lo3 = k_eff // = 等价于 <- + lo3 = k_eff n - 1 -> hi3 while (lo3 < hi3) { - t3 <- a[lo3] - u3 = a[hi3] // = 等价于 <- - a[lo3] <- u3 - a[hi3] <- t3 + t3 = a[lo3] + u3 = a[hi3] + a[lo3] = u3 + a[hi3] = t3 lo3 + 1 -> lo3 - hi3 <- hi3 - 1 + hi3 = hi3 - 1 } - j = 0 // = 等价于 <- + j = 0 while (j < n) { a[j] -> v println(v) - j <- j + 1 + j = j + 1 } } diff --git a/tutorial/08-leetcode/191_hamming_weight.kv b/tutorial/08-leetcode/191_hamming_weight.kv index 08f79647..2dd886a5 100644 --- a/tutorial/08-leetcode/191_hamming_weight.kv +++ b/tutorial/08-leetcode/191_hamming_weight.kv @@ -4,15 +4,15 @@ // 3 rwfunc hamming(n:int64) -> () { n -> nv - count = 0 // = 等价于 <- + count = 0 while (nv > 0) { nv % 2 -> bit if (bit != 0) { - count <- count + 1 + count = count + 1 } - nv = nv ÷ 2 // = 等价于 <- + nv = nv ÷ 2 } println(count) } diff --git a/tutorial/08-leetcode/198_house_robber.kv b/tutorial/08-leetcode/198_house_robber.kv index 50699f0e..17c4ddfa 100644 --- a/tutorial/08-leetcode/198_house_robber.kv +++ b/tutorial/08-leetcode/198_house_robber.kv @@ -4,37 +4,37 @@ // 4 rwfunc rob() -> () { [1, 2, 3, 1] -> a:[]int64 - n <- ndarray·numel(a) - one = n == 1 // = 等价于 <- + n = ndarray·numel(a) + one = n == 1 n == 2 -> two if (one) { - ans <- a[0] + ans = a[0] println(ans) } else { - p1 = a[0] // = 等价于 <- + p1 = a[0] a[1] -> p2 - bigger <- p1 > p2 + bigger = p1 > p2 if (bigger) { - p2 = p1 // = 等价于 <- + p2 = p1 } 2 -> i while (i < n) { - cur <- a[i] - take = p1 + cur // = 等价于 <- + cur = a[i] + take = p1 + cur take > p2 -> better if (better) { - tmp <- take + tmp = take } else { - tmp = p2 // = 等价于 <- + tmp = p2 } p2 -> p1 - p2 <- tmp - i = i + 1 // = 等价于 <- + p2 = tmp + i = i + 1 } println(p2) } diff --git a/tutorial/08-leetcode/202_happy_number.kv b/tutorial/08-leetcode/202_happy_number.kv index 23e0be4e..32dad2b5 100644 --- a/tutorial/08-leetcode/202_happy_number.kv +++ b/tutorial/08-leetcode/202_happy_number.kv @@ -5,24 +5,24 @@ rwfunc is_happy(n:int64) -> (result:int64) { n -> nv 0 -> slow - fast <- nv - found = 0 // = 等价于 <- + fast = nv + found = 0 while (found == 0) { nv -> x - s <- 0 + s = 0 while (x > 0) { - d = x % 10 // = 等价于 <- + d = x % 10 d × d -> d2 d × d -> d2_m - s <- s + d2 - x = x ÷ 10 // = 等价于 <- - x_m = x ÷ 10 // = 等价于 <- + s = s + d2 + x = x ÷ 10 + x_m = x ÷ 10 } s -> nv - done <- nv == 1 + done = nv == 1 if (done) { - found = 1 // = 等价于 <- + found = 1 1 -> result } } @@ -30,6 +30,6 @@ rwfunc is_happy(n:int64) -> (result:int64) { rwfunc test() -> () { - r <- is_happy(19) + r = is_happy(19) println(r) } diff --git a/tutorial/08-leetcode/203_remove_linked_elements.kv b/tutorial/08-leetcode/203_remove_linked_elements.kv index ceaecd99..578bdde6 100644 --- a/tutorial/08-leetcode/203_remove_linked_elements.kv +++ b/tutorial/08-leetcode/203_remove_linked_elements.kv @@ -7,19 +7,19 @@ // 4 // 5 rwfunc build_list() -> () { - /n0 <- { val=1; next="/n1" } - /n1 = { val=2; next="/n2" } // = 等价于 <- + /n0 = { val=1; next="/n1" } + /n1 = { val=2; next="/n2" } { val=6; next="/n3" } -> /n2 - /n3 <- { val=3; next="/n4" } - /n4 = { val=4; next="/n5" } // = 等价于 <- + /n3 = { val=3; next="/n4" } + /n4 = { val=4; next="/n5" } { val=5; next="/n6" } -> /n5 - /n6 <- { val=6; next="" } + /n6 = { val=6; next="" } } rwfunc remove(head:[]char/utf32, val:int64) -> () { - p <- "/n0" + p = "/n0" while (p != "") { - v = p·val // = 等价于 <- + v = p·val v == val -> skip if (skip) { @@ -27,7 +27,7 @@ rwfunc remove(head:[]char/utf32, val:int64) -> () { println(v) } - p <- p·next + p = p·next } } diff --git a/tutorial/08-leetcode/204_count_primes.kv b/tutorial/08-leetcode/204_count_primes.kv index ca995374..96f4559f 100644 --- a/tutorial/08-leetcode/204_count_primes.kv +++ b/tutorial/08-leetcode/204_count_primes.kv @@ -3,25 +3,25 @@ // 期望输出: // 4 rwfunc count_primes(n:int64) -> () { - count = 0 // = 等价于 <- + count = 0 2 -> i while (i < n) { - is_p <- true - d = 2 // = 等价于 <- + is_p = true + d = 2 while (d < i) { i % d -> rem - div <- rem == 0 + div = rem == 0 if (div) { - is_p = false // = 等价于 <- + is_p = false i -> d } else { - d <- d + 1 + d = d + 1 } } if (is_p) { - count = count + 1 // = 等价于 <- + count = count + 1 } i + 1 -> i diff --git a/tutorial/08-leetcode/206_reverse_linked_list.kv b/tutorial/08-leetcode/206_reverse_linked_list.kv index f7c524cd..b5649620 100644 --- a/tutorial/08-leetcode/206_reverse_linked_list.kv +++ b/tutorial/08-leetcode/206_reverse_linked_list.kv @@ -7,27 +7,27 @@ // 2 // 1 rwfunc build_list() -> () { - /n0 = { val=1; next="/n1" } // = 等价于 <- + /n0 = { val=1; next="/n1" } { val=2; next="/n2" } -> /n1 - /n2 <- { val=3; next="/n3" } - /n3 = { val=4; next="/n4" } // = 等价于 <- + /n2 = { val=3; next="/n3" } + /n3 = { val=4; next="/n4" } { val=5; next="" } -> /n4 } rwfunc reverse(head:[]char/utf32) -> () { - prev = "" // = 等价于 <- + prev = "" head -> cur while (cur != "") { - nxt <- cur·next - cur·next = prev // = 等价于 <- + nxt = cur·next + cur·next = prev cur -> prev - cur <- nxt + cur = nxt } - p = prev // = 等价于 <- + p = prev while (p != "") { p·val -> v println(v) - p <- p·next + p = p·next } } diff --git a/tutorial/08-leetcode/217_contains_dup.kv b/tutorial/08-leetcode/217_contains_dup.kv index f2377dca..b00fdcc8 100644 --- a/tutorial/08-leetcode/217_contains_dup.kv +++ b/tutorial/08-leetcode/217_contains_dup.kv @@ -5,26 +5,26 @@ // true // false rwfunc has_dup() -> () { - a:[]int64 = [1, 2, 3, 1] // = 等价于 <- + a:[]int64 = [1, 2, 3, 1] ndarray·numel(a) -> n - found <- false - i = 0 // = 等价于 <- + found = false + i = 0 while (i < n) { i + 1 -> j while (j < n) { - x <- a[i] - y = a[j] // = 等价于 <- + x = a[i] + y = a[j] x == y -> dup if (dup) { - found <- true - i = n // = 等价于 <- + found = true + i = n n -> j } else { - j <- j + 1 + j = j + 1 } } - i = i + 1 // = 等价于 <- + i = i + 1 } found -> f @@ -36,28 +36,28 @@ rwfunc has_dup() -> () { } rwfunc no_dup() -> () { - a:[]int64 <- [1, 2, 3, 4] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [1, 2, 3, 4] + n = ndarray·numel(a) false -> found - i <- 0 + i = 0 while (i < n) { - j = i + 1 // = 等价于 <- + j = i + 1 while (j < n) { a[i] -> x - y <- a[j] - dup = x == y // = 等价于 <- + y = a[j] + dup = x == y if (dup) { true -> found - i <- n - j = n // = 等价于 <- + i = n + j = n } else { j + 1 -> j } } - i <- i + 1 + i = i + 1 } - f = found // = 等价于 <- + f = found if (f) { println("true") diff --git a/tutorial/08-leetcode/217_contains_dup_hash.kv b/tutorial/08-leetcode/217_contains_dup_hash.kv index 224500d5..07886b9f 100644 --- a/tutorial/08-leetcode/217_contains_dup_hash.kv +++ b/tutorial/08-leetcode/217_contains_dup_hash.kv @@ -5,22 +5,22 @@ // false rwfunc has_dup(a:[]int64) -> () { ndarray·numel(a) -> n - seen:[]char/utf8·int64 = {} // = 等价于 <- - found <- false - i = 0 // = 等价于 <- + seen:[]char/utf8·int64 = {} + found = false + i = 0 while (i < n) { a[i] -> x kv·get(seen, x) != None -> exists if (exists) { true -> found - i <- n + i = n } else { - _ = kv·set(seen, x, 1) // = 等价于 <- + _ = kv·set(seen, x, 1) i + 1 -> i } } - f <- found + f = found if (f) { println("true") @@ -30,24 +30,24 @@ rwfunc has_dup(a:[]int64) -> () { } rwfunc no_dup() -> () { - a:[]int64 = [1, 2, 3, 4] // = 等价于 <- + a:[]int64 = [1, 2, 3, 4] ndarray·numel(a) -> n - seen:[]char/utf8·int64 = {} // = 等价于 <- - found <- false - i = 0 // = 等价于 <- + seen:[]char/utf8·int64 = {} + found = false + i = 0 while (i < n) { a[i] -> x kv·get(seen, x) != None -> exists if (exists) { true -> found - i <- n + i = n } else { - _ = kv·set(seen, x, 1) // = 等价于 <- + _ = kv·set(seen, x, 1) i + 1 -> i } } - f <- found + f = found if (f) { println("true") diff --git a/tutorial/08-leetcode/219_contains_dup_ii.kv b/tutorial/08-leetcode/219_contains_dup_ii.kv index fbd95edf..9d5ae25b 100644 --- a/tutorial/08-leetcode/219_contains_dup_ii.kv +++ b/tutorial/08-leetcode/219_contains_dup_ii.kv @@ -4,9 +4,9 @@ // true // false rwfunc near_dup(nums:[]int64, k:int64) -> () { - n <- ndarray·numel(nums) - h:[]char/utf8·int64 = {} // = 等价于 <- - found = false // = 等价于 <- + n = ndarray·numel(nums) + h:[]char/utf8·int64 = {} + found = false 0 -> i while (i < n) { nums[i] -> x @@ -15,7 +15,7 @@ rwfunc near_dup(nums:[]int64, k:int64) -> () { if (exists) { h·*x -> j if (j > 0) { - d <- i - j + 1 + d = i - j + 1 if (d <= k) { true -> found @@ -26,7 +26,7 @@ rwfunc near_dup(nums:[]int64, k:int64) -> () { if (found == false) { kv·set(h, x, i + 1) -> _ } - i <- i + 1 + i = i + 1 } if (found) { println("true") diff --git a/tutorial/08-leetcode/231_power_of_two.kv b/tutorial/08-leetcode/231_power_of_two.kv index b5e68800..839262a8 100644 --- a/tutorial/08-leetcode/231_power_of_two.kv +++ b/tutorial/08-leetcode/231_power_of_two.kv @@ -4,29 +4,29 @@ // 1 // 0 rwfunc is_power(n:int64) -> (r:int64) { - result = 0 // = 等价于 <- + result = 0 n > 0 -> pos if (pos) { - t <- 1 + t = 1 while (t < n) { - t = t × 2 // = 等价于 <- - t_m = t × 2 // = 等价于 <- + t = t × 2 + t_m = t × 2 } t == n -> eq if (eq) { - result <- 1 + result = 1 } } - r = result // = 等价于 <- + r = result } rwfunc test() -> () { is_power(16) -> r1 println(r1) - r2 <- is_power(18) + r2 = is_power(18) println(r2) } diff --git a/tutorial/08-leetcode/234_palindrome_list.kv b/tutorial/08-leetcode/234_palindrome_list.kv index 9fbad90f..235de97f 100644 --- a/tutorial/08-leetcode/234_palindrome_list.kv +++ b/tutorial/08-leetcode/234_palindrome_list.kv @@ -4,18 +4,18 @@ // true // false rwfunc is_pal(head:[]char/utf32) -> () { - arr:[int64]·int64 = {} // = 等价于 <- + arr:[int64]·int64 = {} 0 -> n head -> p while (p != "") { p·val -> v kv·set(arr, n, v) -> _ - n <- n + 1 + n = n + 1 p·next -> p } - ok = true // = 等价于 <- + ok = true 0 -> l - r <- n - 1 + r = n - 1 while (l < r) { kv·get(arr, l) -> x kv·get(arr, r) -> y @@ -24,8 +24,8 @@ rwfunc is_pal(head:[]char/utf32) -> () { false -> ok r -> l } - l <- l + 1 - r = r - 1 // = 等价于 <- + l = l + 1 + r = r - 1 } if (ok) { println("true") @@ -35,11 +35,11 @@ rwfunc is_pal(head:[]char/utf32) -> () { } rwfunc build() -> () { - /p0 = { val=1; next="/p1" } // = 等价于 <- - /p1 <- { val=2; next="/p2" } + /p0 = { val=1; next="/p1" } + /p1 = { val=2; next="/p2" } { val=2; next="/p3" } -> /p2 - /p3 = { val=1; next="" } // = 等价于 <- - /q0 <- { val=1; next="/q1" } + /p3 = { val=1; next="" } + /q0 = { val=1; next="/q1" } { val=2; next="" } -> /q1 } diff --git a/tutorial/08-leetcode/238_product_except_self.kv b/tutorial/08-leetcode/238_product_except_self.kv index 9b220083..ad9a9ff6 100644 --- a/tutorial/08-leetcode/238_product_except_self.kv +++ b/tutorial/08-leetcode/238_product_except_self.kv @@ -7,37 +7,37 @@ // 8 // 6 rwfunc product() -> () { - a:[]int64 = [1, 2, 3, 4] // = 等价于 <- + a:[]int64 = [1, 2, 3, 4] ndarray·numel(a) -> n - prefix <- 1 - suffix = 1 // = 等价于 <- + prefix = 1 + suffix = 1 // build prefix products into result [1, 1, 1, 1] -> result:[]int64 - i <- 0 + i = 0 while (i < n) { - result[i] <- prefix - x = a[i] // = 等价于 <- + result[i] = prefix + x = a[i] prefix × x -> prefix prefix × x -> prefix_m - i <- i + 1 + i = i + 1 } // multiply by suffix products - j = n - 1 // = 等价于 <- + j = n - 1 while (j >= 0) { result[j] -> rv - newv <- suffix × rv - newv_m <- suffix × rv - result[j] <- newv - y = a[j] // = 等价于 <- + newv = suffix × rv + newv_m = suffix × rv + result[j] = newv + y = a[j] suffix × y -> suffix suffix × y -> suffix_m - j <- j - 1 + j = j - 1 } - k = 0 // = 等价于 <- + k = 0 while (k < n) { result[k] -> v println(v) - k <- k + 1 + k = k + 1 } } diff --git a/tutorial/08-leetcode/242_valid_anagram.kv b/tutorial/08-leetcode/242_valid_anagram.kv index ee5263c8..572b1745 100644 --- a/tutorial/08-leetcode/242_valid_anagram.kv +++ b/tutorial/08-leetcode/242_valid_anagram.kv @@ -4,14 +4,14 @@ // true // false rwfunc is_anagram(s:[]char/utf32, t:[]char/utf32) -> () { - ns = string·len(s) // = 等价于 <- - nt <- string·len(t) - ok = true // = 等价于 <- + ns = string·len(s) + nt = string·len(t) + ok = true if (ns != nt) { false -> ok } else { - h:[]char/utf8·int64 = {} // = 等价于 <- + h:[]char/utf8·int64 = {} 0 -> i while (i < ns) { string·char(s, i) -> c @@ -21,7 +21,7 @@ rwfunc is_anagram(s:[]char/utf32, t:[]char/utf32) -> () { } else { 1 -> h·*c } - i <- i + 1 + i = i + 1 } 0 -> i while (i < nt) { @@ -45,7 +45,7 @@ rwfunc is_anagram(s:[]char/utf32, t:[]char/utf32) -> () { false -> ok ns -> i } - i <- i + 1 + i = i + 1 } } if (ok) { diff --git a/tutorial/08-leetcode/258_add_digits.kv b/tutorial/08-leetcode/258_add_digits.kv index 0b63a76a..43208ab2 100644 --- a/tutorial/08-leetcode/258_add_digits.kv +++ b/tutorial/08-leetcode/258_add_digits.kv @@ -5,15 +5,15 @@ rwfunc add_digits(n:int64) -> () { n -> nv while (nv >= 10) { - s = 0 // = 等价于 <- + s = 0 nv -> x while (x > 0) { - d <- x % 10 - s = s + d // = 等价于 <- + d = x % 10 + s = s + d x ÷ 10 -> x x ÷ 10 -> x_m } - nv <- s + nv = s } println(nv) } diff --git a/tutorial/08-leetcode/263_ugly_number.kv b/tutorial/08-leetcode/263_ugly_number.kv index 81468d40..5a40bf7c 100644 --- a/tutorial/08-leetcode/263_ugly_number.kv +++ b/tutorial/08-leetcode/263_ugly_number.kv @@ -4,29 +4,29 @@ // 1 rwfunc is_ugly(n:int64) -> () { n -> nv - bad = nv <= 0 // = 等价于 <- - bad_m = nv ≤ 0 // = 等价于 <- + bad = nv <= 0 + bad_m = nv ≤ 0 if (bad) { println(0) } else { nv % 2 -> r2 while (r2 == 0) { - nv <- nv ÷ 2 - nv_m <- nv ÷ 2 - r2 = nv % 2 // = 等价于 <- + nv = nv ÷ 2 + nv_m = nv ÷ 2 + r2 = nv % 2 } nv % 3 -> r3 while (r3 == 0) { - nv <- nv ÷ 3 - nv_m <- nv ÷ 3 - r3 = nv % 3 // = 等价于 <- + nv = nv ÷ 3 + nv_m = nv ÷ 3 + r3 = nv % 3 } nv % 5 -> r5 while (r5 == 0) { - nv <- nv ÷ 5 - nv_m <- nv ÷ 5 - r5 = nv % 5 // = 等价于 <- + nv = nv ÷ 5 + nv_m = nv ÷ 5 + r5 = nv % 5 } nv == 1 -> ok diff --git a/tutorial/08-leetcode/268_missing_number.kv b/tutorial/08-leetcode/268_missing_number.kv index c7c19c90..e0a34713 100644 --- a/tutorial/08-leetcode/268_missing_number.kv +++ b/tutorial/08-leetcode/268_missing_number.kv @@ -4,21 +4,21 @@ // 期望输出: // 2 rwfunc missing() -> () { - a:[]int64 <- [3, 0, 1] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [3, 0, 1] + n = ndarray·numel(a) 0 -> s - i <- 0 + i = 0 while (i < n) { - x = a[i] // = 等价于 <- + x = a[i] s + x -> s - i <- i + 1 + i = i + 1 } - nn = n × n // = 等价于 <- - nn_m = n × n // = 等价于 <- + nn = n × n + nn_m = n × n nn + n -> total - expected <- total ÷ 2 - expected_m <- total ÷ 2 - result = expected - s // = 等价于 <- + expected = total ÷ 2 + expected_m = total ÷ 2 + result = expected - s println(result) } diff --git a/tutorial/08-leetcode/278_first_bad.kv b/tutorial/08-leetcode/278_first_bad.kv index 879643a1..b64d6a0f 100644 --- a/tutorial/08-leetcode/278_first_bad.kv +++ b/tutorial/08-leetcode/278_first_bad.kv @@ -5,16 +5,16 @@ rwfunc first_bad(n:int64) -> () { // isBadVersion(i) = i >= 4 1 -> lo - hi <- n + hi = n while (lo < hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid s ÷ 2 -> mid_m - bad <- mid >= 4 - bad_m <- mid ≥ 4 + bad = mid >= 4 + bad_m = mid ≥ 4 if (bad) { - hi = mid // = 等价于 <- + hi = mid } else { mid + 1 -> lo } diff --git a/tutorial/08-leetcode/283_move_zeroes.kv b/tutorial/08-leetcode/283_move_zeroes.kv index 467fb30f..d3e53591 100644 --- a/tutorial/08-leetcode/283_move_zeroes.kv +++ b/tutorial/08-leetcode/283_move_zeroes.kv @@ -7,30 +7,30 @@ // 0 // 0 rwfunc move_zeroes() -> () { - a:[]int64 = [0, 1, 0, 3, 12] // = 等价于 <- - n <- ndarray·numel(a) + a:[]int64 = [0, 1, 0, 3, 12] + n = ndarray·numel(a) 0 -> w 0 -> i while (i < n) { a[i] -> x - nz = x != 0 // = 等价于 <- - nz_m = x ≠ 0 // = 等价于 <- + nz = x != 0 + nz_m = x ≠ 0 if (nz) { - a[w] <- x - w <- w + 1 + a[w] = x + w = w + 1 } i + 1 -> i } while (w < n) { - a[w] <- 0 - w = w + 1 // = 等价于 <- + a[w] = 0 + w = w + 1 } 0 -> i while (i < n) { a[i] -> v println(v) - i <- i + 1 + i = i + 1 } } diff --git a/tutorial/08-leetcode/292_nim_game.kv b/tutorial/08-leetcode/292_nim_game.kv index 0d682e20..8d50ed9c 100644 --- a/tutorial/08-leetcode/292_nim_game.kv +++ b/tutorial/08-leetcode/292_nim_game.kv @@ -3,7 +3,7 @@ // 期望输出: // 0 rwfunc can_win(n:int64) -> () { - rem = n % 4 // = 等价于 <- + rem = n % 4 rem == 0 -> lose if (lose) { diff --git a/tutorial/08-leetcode/303_range_sum.kv b/tutorial/08-leetcode/303_range_sum.kv index 04dc4feb..06ba4c7b 100644 --- a/tutorial/08-leetcode/303_range_sum.kv +++ b/tutorial/08-leetcode/303_range_sum.kv @@ -4,25 +4,25 @@ // 1 // -1 rwfunc range_sum() -> () { - a:[]int64 <- [-2, 0, 3, -5, 2, -1] - n = ndarray·numel(a) // = 等价于 <- + a:[]int64 = [-2, 0, 3, -5, 2, -1] + n = ndarray·numel(a) // build prefix sums [0, 0, 0, 0, 0, 0] -> p:[]int64 - s <- a[0] - p[0] <- s - i = 1 // = 等价于 <- + s = a[0] + p[0] = s + i = 1 while (i < n) { a[i] -> v - s <- s + v - p[i] <- s - i = i + 1 // = 等价于 <- + s = s + v + p[i] = s + i = i + 1 } // sumRange(0, 2) = p[2] p[2] -> r1 println(r1) // sumRange(2, 5) = p[5] - p[1] - t2 <- p[5] - t1 = p[1] // = 等价于 <- + t2 = p[5] + t1 = p[1] t2 - t1 -> r2 println(r2) } diff --git a/tutorial/08-leetcode/326_power_of_three.kv b/tutorial/08-leetcode/326_power_of_three.kv index dcf54078..d535928a 100644 --- a/tutorial/08-leetcode/326_power_of_three.kv +++ b/tutorial/08-leetcode/326_power_of_three.kv @@ -4,15 +4,15 @@ // 1 // 0 rwfunc is_power3(n:int64) -> () { - pos <- n > 0 + pos = n > 0 if (pos) { - t = 1 // = 等价于 <- + t = 1 while (t < n) { t × 3 -> t t × 3 -> t_m } - eq <- t == n + eq = t == n if (eq) { println(1) diff --git a/tutorial/08-leetcode/342_power_of_four.kv b/tutorial/08-leetcode/342_power_of_four.kv index 31a4b36b..2775d041 100644 --- a/tutorial/08-leetcode/342_power_of_four.kv +++ b/tutorial/08-leetcode/342_power_of_four.kv @@ -3,15 +3,15 @@ // 期望输出: // 1 rwfunc is_power4(n:int64) -> () { - pos = n > 0 // = 等价于 <- + pos = n > 0 if (pos) { 1 -> t while (t < n) { - t <- t × 4 - t_m <- t × 4 + t = t × 4 + t_m = t × 4 } - eq = t == n // = 等价于 <- + eq = t == n if (eq) { println(1) diff --git a/tutorial/08-leetcode/344_reverse_string.kv b/tutorial/08-leetcode/344_reverse_string.kv index 23468145..5c5935f2 100644 --- a/tutorial/08-leetcode/344_reverse_string.kv +++ b/tutorial/08-leetcode/344_reverse_string.kv @@ -9,23 +9,23 @@ rwfunc reverse_str() -> () { // char 值数组: "hello" → ASCII [104, 101, 108, 108, 111] a:[]int64 = [104, 101, 108, 108, 111] - n <- ndarray·numel(a) - l = 0 // = 等价于 <- + n = ndarray·numel(a) + l = 0 n - 1 -> r while (l < r) { - tl <- a[l] - tr = a[r] // = 等价于 <- - a[l] <- tr - a[r] <- tl + tl = a[l] + tr = a[r] + a[l] = tr + a[r] = tl l + 1 -> l - r <- r - 1 + r = r - 1 } - i = 0 // = 等价于 <- + i = 0 while (i < n) { a[i] -> c string·char("abcdefghijklmnopqrstuvwxyz", c - 97) -> s println(s) - i <- i + 1 + i = i + 1 } } diff --git a/tutorial/08-leetcode/349_intersection.kv b/tutorial/08-leetcode/349_intersection.kv index 3f0081ce..00f5e8ae 100644 --- a/tutorial/08-leetcode/349_intersection.kv +++ b/tutorial/08-leetcode/349_intersection.kv @@ -3,37 +3,37 @@ // 期望输出: // 2 rwfunc intersection() -> () { - a:[]int64 = [1, 2, 2, 1] // = 等价于 <- + a:[]int64 = [1, 2, 2, 1] ndarray·numel(a) -> na - b:[]int64 <- [2, 2] - nb = ndarray·numel(b) // = 等价于 <- + b:[]int64 = [2, 2] + nb = ndarray·numel(b) [0, 0] -> r:[]int64 - idx <- 0 - count = 0 // = 等价于 <- + idx = 0 + count = 0 0 -> i while (i < na) { - av <- a[i] - j = 0 // = 等价于 <- + av = a[i] + j = 0 false -> found while (j < nb) { - bv <- b[j] - match = av == bv // = 等价于 <- + bv = b[j] + match = av == bv if (match) { true -> found - j <- nb + j = nb } else { - j = j + 1 // = 等价于 <- + j = j + 1 } } if (found) { - r[idx] <- av + r[idx] = av idx + 1 -> idx - count <- count + 1 + count = count + 1 } - i = i + 1 // = 等价于 <- + i = i + 1 } r[0] -> v println(v) diff --git a/tutorial/08-leetcode/367_valid_square.kv b/tutorial/08-leetcode/367_valid_square.kv index 0ec5a6a7..f033a445 100644 --- a/tutorial/08-leetcode/367_valid_square.kv +++ b/tutorial/08-leetcode/367_valid_square.kv @@ -4,33 +4,33 @@ // 1 // 0 rwfunc is_square(n:int64) -> () { - small <- n < 2 + small = n < 2 if (small) { println(1) } else { - lo = 1 // = 等价于 <- + lo = 1 n ÷ 2 -> hi n ÷ 2 -> hi_m - ans <- 0 + ans = 0 while (lo <= hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid s ÷ 2 -> mid_m - sq <- mid × mid - sq_m <- mid × mid - eq = sq == n // = 等价于 <- + sq = mid × mid + sq_m = mid × mid + eq = sq == n if (eq) { 1 -> ans - lo <- hi + 1 + lo = hi + 1 } else { - lt = sq < n // = 等价于 <- + lt = sq < n if (lt) { mid + 1 -> lo } else { - hi <- mid - 1 + hi = mid - 1 } } } diff --git a/tutorial/08-leetcode/371_sum_two.kv b/tutorial/08-leetcode/371_sum_two.kv index a9044d33..70307168 100644 --- a/tutorial/08-leetcode/371_sum_two.kv +++ b/tutorial/08-leetcode/371_sum_two.kv @@ -6,10 +6,10 @@ rwfunc get_sum(a:int64, b:int64) -> () { a -> av b -> bv while (bv != 0) { - carry = av & bv // = 等价于 <- + carry = av & bv carry << 1 -> c2 - av <- av ^ bv - bv = c2 // = 等价于 <- + av = av ^ bv + bv = c2 } println(av) } diff --git a/tutorial/08-leetcode/374_guess_number.kv b/tutorial/08-leetcode/374_guess_number.kv index ce1bd065..5c65677b 100644 --- a/tutorial/08-leetcode/374_guess_number.kv +++ b/tutorial/08-leetcode/374_guess_number.kv @@ -4,23 +4,23 @@ // 6 rwfunc guess_number(n:int64) -> () { 1 -> lo - hi <- n + hi = n while (lo <= hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid s ÷ 2 -> mid_m - low <- mid < 6 + low = mid < 6 if (low) { - lo = mid + 1 // = 等价于 <- + lo = mid + 1 } else { mid > 6 -> high if (high) { - hi <- mid - 1 + hi = mid - 1 } else { println(mid) - lo = hi + 1 // = 等价于 <- + lo = hi + 1 } } } diff --git a/tutorial/08-leetcode/412_fizz_buzz.kv b/tutorial/08-leetcode/412_fizz_buzz.kv index aac607a1..6acd8918 100644 --- a/tutorial/08-leetcode/412_fizz_buzz.kv +++ b/tutorial/08-leetcode/412_fizz_buzz.kv @@ -6,12 +6,12 @@ // Fizz rwfunc fizzbuzz(n:int64) -> () { [1, 2, 3, 4, 5] -> a:[]int64 - i <- 1 + i = 1 while (i <= n) { - m3 = i % 3 // = 等价于 <- + m3 = i % 3 i % 5 -> m5 - d3 <- m3 == 0 - d5 = m5 == 0 // = 等价于 <- + d3 = m3 == 0 + d5 = m5 == 0 d3 && d5 -> fb if (fb) { @@ -28,7 +28,7 @@ rwfunc fizzbuzz(n:int64) -> () { } } - i <- i + 1 + i = i + 1 } } diff --git a/tutorial/08-leetcode/414_third_max.kv b/tutorial/08-leetcode/414_third_max.kv index 7fe73ef5..f349cda8 100644 --- a/tutorial/08-leetcode/414_third_max.kv +++ b/tutorial/08-leetcode/414_third_max.kv @@ -3,46 +3,46 @@ // 期望输出: // 1 rwfunc third_max() -> () { - a:[]int64 = [2, 2, 3, 1] // = 等价于 <- + a:[]int64 = [2, 2, 3, 1] ndarray·numel(a) -> n // track top 3 - first <- 0 - second = 0 // = 等价于 <- + first = 0 + second = 0 0 -> third - i <- 0 + i = 0 while (i < n) { - x = a[i] // = 等价于 <- + x = a[i] x > first -> gt1 if (gt1) { - third <- second - second = first // = 等价于 <- + third = second + second = first x -> first } else { - lt1 <- x < first - gt2 = x > second // = 等价于 <- + lt1 = x < first + gt2 = x > second if (lt1 && gt2) { second -> third - second <- x + second = x } else { - lt2 = x < second // = 等价于 <- + lt2 = x < second x > third -> gt3 if (lt2 && gt3) { - third <- x + third = x } } } - i = i + 1 // = 等价于 <- + i = i + 1 } third > 0 -> ok if (ok) { println(third) } else { - ans <- first + ans = first println(ans) } } diff --git a/tutorial/08-leetcode/441_arrange_coins.kv b/tutorial/08-leetcode/441_arrange_coins.kv index e84bb7e8..9271e7d3 100644 --- a/tutorial/08-leetcode/441_arrange_coins.kv +++ b/tutorial/08-leetcode/441_arrange_coins.kv @@ -3,26 +3,26 @@ // 期望输出: // 2 rwfunc arrange(n:int64) -> () { - lo = 1 // = 等价于 <- + lo = 1 n -> hi - ans <- 0 + ans = 0 while (lo <= hi) { - s = lo + hi // = 等价于 <- + s = lo + hi s ÷ 2 -> mid s ÷ 2 -> mid_m - m2 <- mid × mid - m2_m <- mid × mid - total = m2 + mid // = 等价于 <- + m2 = mid × mid + m2_m = mid × mid + total = m2 + mid total ÷ 2 -> t total ÷ 2 -> t_m - ok <- t <= n - ok_m <- t ≤ n + ok = t <= n + ok_m = t ≤ n if (ok) { - ans = mid // = 等价于 <- + ans = mid mid + 1 -> lo } else { - hi <- mid - 1 + hi = mid - 1 } } println(ans) diff --git a/tutorial/08-leetcode/507_perfect_number.kv b/tutorial/08-leetcode/507_perfect_number.kv index 33dc51a3..efa88840 100644 --- a/tutorial/08-leetcode/507_perfect_number.kv +++ b/tutorial/08-leetcode/507_perfect_number.kv @@ -3,24 +3,24 @@ // 期望输出: // 1 rwfunc is_perfect(n:int64) -> () { - sum = 1 // = 等价于 <- + sum = 1 2 -> i - sq <- i × i - sq_m <- i × i + sq = i × i + sq_m = i × i while (sq <= n) { - rem = n % i // = 等价于 <- + rem = n % i rem == 0 -> div if (div) { - sum <- sum + i - j = n ÷ i // = 等价于 <- - j_m = n ÷ i // = 等价于 <- + sum = sum + i + j = n ÷ i + j_m = n ÷ i sum + j -> sum } - i <- i + 1 - sq = i × i // = 等价于 <- - sq_m = i × i // = 等价于 <- + i = i + 1 + sq = i × i + sq_m = i × i } sum == n -> ok diff --git a/tutorial/08-leetcode/509_fib.kv b/tutorial/08-leetcode/509_fib.kv index b7c51328..44261525 100644 --- a/tutorial/08-leetcode/509_fib.kv +++ b/tutorial/08-leetcode/509_fib.kv @@ -3,20 +3,20 @@ // 期望输出: // 55 rwfunc fib(n:int64) -> () { - base <- n <= 1 - base_m <- n ≤ 1 + base = n <= 1 + base_m = n ≤ 1 if (base) { println(n) } else { - a = 0 // = 等价于 <- + a = 0 1 -> b - i <- 2 + i = 2 while (i <= n) { - c = a + b // = 等价于 <- + c = a + b b -> a - b <- c - i = i + 1 // = 等价于 <- + b = c + i = i + 1 } println(c) } diff --git a/tutorial/08-leetcode/728_self_dividing.kv b/tutorial/08-leetcode/728_self_dividing.kv index cda6f850..1d9bced3 100644 --- a/tutorial/08-leetcode/728_self_dividing.kv +++ b/tutorial/08-leetcode/728_self_dividing.kv @@ -15,39 +15,39 @@ // 15 rwfunc is_self(n:int64) -> (r:bool) { n -> num - ok <- true + ok = true while (num > 0) { - d = num % 10 // = 等价于 <- + d = num % 10 d == 0 -> zero if (zero) { - ok <- false - num = 0 // = 等价于 <- + ok = false + num = 0 } else { n % d -> rem - div <- rem == 0 + div = rem == 0 if (div) { - num = num ÷ 10 // = 等价于 <- - num_m = num ÷ 10 // = 等价于 <- + num = num ÷ 10 + num_m = num ÷ 10 } else { false -> ok - num <- 0 + num = 0 } } } - r = ok // = 等价于 <- + r = ok } rwfunc test() -> () { 1 -> i while (i <= 15) { - r <- is_self(i) + r = is_self(i) if (r) { println(i) } - i = i + 1 // = 等价于 <- + i = i + 1 } } diff --git a/tutorial/11-string/01-basic.kv b/tutorial/11-string/01-basic.kv index 3f47a437..bb73a3aa 100644 --- a/tutorial/11-string/01-basic.kv +++ b/tutorial/11-string/01-basic.kv @@ -8,6 +8,6 @@ rwfunc test() -> () { s = "hello" println(s) println(string·len(s)) - t <- "kv" + "lang" + t = "kv" + "lang" println(t) } diff --git a/tutorial/11-string/06-convert.kv b/tutorial/11-string/06-convert.kv index 90d5572b..57b053c7 100644 --- a/tutorial/11-string/06-convert.kv +++ b/tutorial/11-string/06-convert.kv @@ -1,5 +1,5 @@ // 编码转换:char/utf32 / char/utf8 / char/ascii 就是转换函数(kind(x),同创建函数) -// 转换函数有写参(读参只读):t <- char/utf32(s) +// 转换函数有写参(读参只读):t = char/utf32(s) // utf8 转 utf32 后可索引;char/ascii 拒非 ASCII 码点(U+00E9 报错) // 期望输出: // hello @@ -7,9 +7,9 @@ // world rwfunc test() -> () { s:[]char/utf8 = "hello" // 变宽,禁索引 - t <- char/utf32(s) // 转 utf32 定宽,可索引 + t = char/utf32(s) // 转 utf32 定宽,可索引 println(t) println(string·char(t, 1)) - u <- char/ascii("world") // 转 ascii(非 ASCII 会报错) + u = char/ascii("world") // 转 ascii(非 ASCII 会报错) println(u) } diff --git a/tutorial/11-string/07-multiline.kv b/tutorial/11-string/07-multiline.kv index db22b007..a69f429b 100644 --- a/tutorial/11-string/07-multiline.kv +++ b/tutorial/11-string/07-multiline.kv @@ -10,13 +10,13 @@ // raw\nliteral // he said "hi" rwfunc test() -> () { - s <- "line1 + s = "line1 line2" println(s) - t <- "one\ntwo" + t = "one\ntwo" println(t) - r <- r"raw\nliteral" + r = r"raw\nliteral" println(r) - q <- r#"he said "hi""# + q = r#"he said "hi""# println(q) } diff --git a/tutorial/13-stdlib/kv/get_or.kv b/tutorial/13-stdlib/kv/get_or.kv index c27a0581..a838a6ff 100644 --- a/tutorial/13-stdlib/kv/get_or.kv +++ b/tutorial/13-stdlib/kv/get_or.kv @@ -4,9 +4,9 @@ // get_or existing: world rwfunc test() -> () { - v1 <- kv·get_or("/missing_key", "default_val") + v1 = kv·get_or("/missing_key", "default_val") println("get_or missing:", v1) kv·set("/exists_key", "world") - v2 <- kv·get_or("/exists_key", "default_val") + v2 = kv·get_or("/exists_key", "default_val") println("get_or existing:", v2) } diff --git a/tutorial/13-stdlib/kv/has.kv b/tutorial/13-stdlib/kv/has.kv index 1b96e123..bf7504d0 100644 --- a/tutorial/13-stdlib/kv/has.kv +++ b/tutorial/13-stdlib/kv/has.kv @@ -4,9 +4,9 @@ // has existing: true rwfunc test() -> () { - h1 <- kv·has("/missing_key") + h1 = kv·has("/missing_key") println("has missing:", h1) kv·set("/exists_key", "hello") - h2 <- kv·has("/exists_key") + h2 = kv·has("/exists_key") println("has existing:", h2) } diff --git a/tutorial/13-stdlib/kv/set_default.kv b/tutorial/13-stdlib/kv/set_default.kv index d67301b4..44078e36 100644 --- a/tutorial/13-stdlib/kv/set_default.kv +++ b/tutorial/13-stdlib/kv/set_default.kv @@ -5,9 +5,9 @@ rwfunc test() -> () { kv·set_default("/sd_key", "first_val") - sd1 <- kv·get("/sd_key") + sd1 = kv·get("/sd_key") println("set_default new:", sd1) kv·set_default("/sd_key", "second_val") - sd2 <- kv·get("/sd_key") + sd2 = kv·get("/sd_key") println("set_default existing:", sd2) } diff --git a/tutorial/13-stdlib/xv/first.kv b/tutorial/13-stdlib/xv/first.kv index fc967530..5a875800 100644 --- a/tutorial/13-stdlib/xv/first.kv +++ b/tutorial/13-stdlib/xv/first.kv @@ -4,6 +4,6 @@ rwfunc test() -> () { a:[]int64 = [10, 20, 30, 40] - f <- xv·first(a) + f = xv·first(a) println("first:", f) } diff --git a/tutorial/13-stdlib/xv/swap.kv b/tutorial/13-stdlib/xv/swap.kv index a55d064b..8419d4d4 100644 --- a/tutorial/13-stdlib/xv/swap.kv +++ b/tutorial/13-stdlib/xv/swap.kv @@ -6,7 +6,7 @@ rwfunc test() -> () { a:[]int64 = [10, 20, 30, 40] - b <- xv·swap(a, 0, 2) + b = xv·swap(a, 0, 2) println("swap(0,2)[0]:", b[0]) println("swap(0,2)[2]:", b[2]) println("original[0]:", a[0])