Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ stdlib/kvlang/reference/

# tutorial shm 测试产物(相对路径 DSN)
kvtut*

# tutorial bench 空产物
/tutorial/benchmark.csv
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ Strings support indexing and concatenation with `+`; `at(s, i)` reads the i-th c
02-func/ rwfunc, call, accumulator (2 files)
03-control/ if, while, for, guess (5 files)
03-debugger/ chain_array, debugger builtin (4 files)
04-algo/ fibonacci, gcd, collatz, (13 files)
06-algo/ gcd, collatz, power, factorial, … (8 files)
06-lib/ lib block, nested, cross-lib, anon (11 files)
07-leetcode/ LeetCode solutions (90 files)
error_cases/ type_error, index_error, zero_division, … (36 files)
```

```bash
./kvlang tutorial/01-basics/hello.kv # hello kvlang
./kvlang tutorial/04-algo/fibonacci.kv # fib = 55
./kvlang tutorial/06-algo/gcd.kv # gcd = 6
./kvlang tutorial/07-leetcode/001_two_sum.kv # LeetCode

python3 tutorial/test.py # all positive examples — CI verification
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ string.slice(s, 0, 2) -> p # "he"
02-func/ rwfunc, call, accumulator (2 files)
03-control/ if, while, for, guess (5 files)
03-debugger/ chain_array, debugger builtin (4 files)
04-algo/ fibonacci, gcd, collatz, (13 files)
06-algo/ gcd, collatz, power, factorial, … (8 files)
06-lib/ lib block, nested, cross-lib, anon (11 files)
07-leetcode/ LeetCode solutions (90 files)
error_cases/ type_error, index_error, zero_division, … (36 files)
```

```bash
./kvlang tutorial/01-basics/hello.kv # hello kvlang
./kvlang tutorial/04-algo/fibonacci.kv # fib = 55
./kvlang tutorial/06-algo/gcd.kv # gcd = 6
./kvlang tutorial/07-leetcode/001_two_sum.kv # LeetCode

python3 tutorial/test.py # 全部正例 — CI 验证
Expand Down
1 change: 0 additions & 1 deletion layout/tests/issue116.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ fn tutorial_control_files_single_plane() {
("../tutorial/03-control/guess.kv", &["guess_number"][..]),
("../tutorial/03-control/classify.kv", &["classify"][..]),
("../tutorial/03-control/for.kv", &["test"][..]),
("../tutorial/06-algo/prime_sieve.kv", &["prime_sieve"][..]),
] {
let mut kv = fresh_kv();
let src = std::fs::read_to_string(file).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions runtime-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ fn drive_vid(eng: &Engine, vid: &str) {

// pc 可能属子 vthread(native vthread·run 冒泡上来):目标 vid 一律由 pc 导出,非固定主 vid。
let sub = c.split('/').nth(2).unwrap_or(vid);
// 外部扩展 rwir(如 numpy):handoff;native/控制帧/帧结束:写回 pc 让 runtime 继续。
if !stop_op.is_empty() && rwir::is_others_rwir(&stop_op) {
// 停在本 myrwircaps 内的 op:handoff;native/控制帧/帧结束:写回 pc 让 runtime 继续。
if !stop_op.is_empty() && rwir::in_myrwircaps(&stop_op) {
if unsafe { kvlang_rwirextHandoff(kv, cs(sub).as_ptr(), cs(&c).as_ptr()) } != 0 {
kvlang_rs::elog!("handoff {stop_op} 失败 @ {c}");
std::process::exit(1);
Expand Down
4 changes: 2 additions & 2 deletions runtime-rs/src/rwir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ pub fn is_inproc(op: &str) -> bool {
)
}

/// 判「别人的 rwir」:opcode 是否已注册(进程内 map)。
pub fn is_others_rwir(op: &str) -> bool {
/// op ∈ 本 runtime myrwircaps(已登记进程内 map);其取反即 notinmycaps
pub fn in_myrwircaps(op: &str) -> bool {
rwirmap().contains_key(op)
}

Expand Down
46 changes: 28 additions & 18 deletions runtime/src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ static int kvlangBuiltinCastCharAscii(kvlangFrame_t *f) { return kvlangBuiltinCa

/* ── 注册表 ───────────────────────────────────────────────────────── */

/* 数字多类型运算融合为单条:派发前 strip_num_kind 剥掉 <numkind>. 前缀
* int64.add / float32.add … 全部归到同一条 add(union 语义),kvlangBuiltin* 按操作数 kind 归约。 */
/* 精度前缀(int64·add / float32·add …)保留:CapIndex 两级查表——先按完整 opcode 命中特化
* 未命中且前缀是 C native 数字 kind 时才剥前缀归到裸 op(如 add),kvlangBuiltin* 按操作数 kind 归约。 */
static const struct { const char *op; kvlangBuiltinFn fn; } myrwircaps[] = {
{"add", kvlangBuiltinAdd}, {"+", kvlangBuiltinAdd},
{"sub", kvlangBuiltinSub}, {"-", kvlangBuiltinSub},
Expand Down Expand Up @@ -595,23 +595,35 @@ static const struct { const char *op; kvlangBuiltinFn fn; } myrwircaps[] = {

static const size_t myrwircaps_n = sizeof(myrwircaps) / sizeof(myrwircaps[0]);

/* 剥离 <numkind>. 前缀(int64.add → add),使融合后的单条 add 覆盖全部数字类型
* 非数字前缀(array./string./time/duration. 等)与裸类型 cast(int64)原样保留。 */
/* C 原生数值 langtype 集:仅这些精度的算术走 kvlangBuiltinAdd 等通用实现
* int4/fp8/bf16 等 C 表达不了的量化精度不在此列,其 <langtype>·op 走专精 fn 或 handoff。 */
static const char *NUM_KINDS[] = {"int8", "int16", "int32", "int64", "uint8",
"uint16", "uint32", "uint64", "float32", "float64"};
static const char *strip_num_kind(const char *op) {
const char *dot = strstr(op, MEMBER_SEP);
if (!dot) return op;
size_t n = (size_t)(dot - op);
static bool is_num_kind_prefix(const char *op, size_t n) {
for (size_t i = 0; i < sizeof(NUM_KINDS) / sizeof(NUM_KINDS[0]); i++)
if (strlen(NUM_KINDS[i]) == n && strncmp(op, NUM_KINDS[i], n) == 0) return dot + MEMBER_SEP_LEN;
return op;
if (strlen(NUM_KINDS[i]) == n && strncmp(op, NUM_KINDS[i], n) == 0) return true;
return false;
}

bool kvlangBuiltinIsNative(const char *opcode) {
const char *op = strip_num_kind(opcode);
for (size_t i = 0; i < myrwircaps_n; i++) if (strcmp(myrwircaps[i].op, op) == 0) return true;
return false;
/* rwirtable 两级查找:先查完整 opcode(命中=专精实现,如 fp8·add,或 time·add 等成员算子),
* 未命中且末段前缀是 C 原生数值 langtype 才拆前缀查裸算子(int64·add → add,通用实现)。
* 非数值前缀(time/duration·max 等用户 rwfunc)不剥离,两级皆 miss → 非本 runtime native。 */
int kvlangBuiltinCapIndex(const char *opcode) {
for (size_t i = 0; i < myrwircaps_n; i++)
if (strcmp(myrwircaps[i].op, opcode) == 0) return (int)i;
const char *dot = strstr(opcode, MEMBER_SEP);
if (dot && is_num_kind_prefix(opcode, (size_t)(dot - opcode))) {
const char *bare = dot + MEMBER_SEP_LEN;
for (size_t i = 0; i < myrwircaps_n; i++)
if (strcmp(myrwircaps[i].op, bare) == 0) return (int)i;
}
return -1;
}

/* notinmycaps:查 myrwircaps table。opcode 不在本 runtime 能力表内 → true
* (即须经 /lib/<op> 的 def rwir 路由给能兑现它的其它 runtime,或为用户 rwfunc)。 */
bool notinmycaps(const char *opcode) {
return kvlangBuiltinCapIndex(opcode) < 0;
}

bool kvlangBuiltinNumOp(const char *opcode) {
Expand All @@ -631,10 +643,8 @@ bool kvlangBuiltinNumOp(const char *opcode) {
}

int kvlangBuiltinNative(kvlangFrame_t *f) {
const char *op = strip_num_kind(f->inst->opcode);
for (size_t i = 0; i < myrwircaps_n; i++) {
if (strcmp(myrwircaps[i].op, op) == 0) return myrwircaps[i].fn(f);
}
int i = f->inst->op_id >= 0 ? f->inst->op_id : kvlangBuiltinCapIndex(f->inst->opcode);
if (i >= 0) return myrwircaps[i].fn(f);
return kvlangBuiltinSetErr(f, "unknown builtin op: %s", f->inst->opcode);
}

Expand Down
Loading
Loading