Skip to content

Latest commit

 

History

History
193 lines (138 loc) · 5.91 KB

File metadata and controls

193 lines (138 loc) · 5.91 KB

kvlang

CI Go Version License: MIT Tutorial Examples

deepx 的 VM(原 dxlang),agent-native 训推一体自迭代强人工智能计算架构。 以 kvspace 树形路径为统一地址空间,同种语法同时承担 VM 指令、高级语言、编译器 IR、人类可读源码四种职能。

中文文档: README_CN.md | 设计规范: deepx-design(架构细节、模块职责、深度理解均在此仓)


核心模型:一屏看懂

不分 IR 层,源码即 IR。 程序计数器是 kvspace 路径字符串,调用栈深度 = 路径深度:

PC   = "/vthread/tid/[0,0]/.fn/[1,0]"    程序计数器是 KV 路径
指令 = kv.Get(PC)                         取指是一次 KV 读
调用 = 创建子树;返回 = 清理子树           崩溃后按 PC 重启继续

每条指令占据二维坐标 [s0, s1][s0,0] 恒为操作码,[s0,-j] 读参,[s0,+j] 写参。

def add(A: int, B: int) -> (C: int) { A + B -> C }
/func/main/add/[0,0]  = "+"     /func/main/add/[0,-1] = "A"
/func/main/add/[0,-2] = "B"     /func/main/add/[0,1]  = "C"

地址空间四域:/src(源码)/func(编译后函数)/vthread(运行时栈帧)/sys(基础设施)。


Quick Start

# 依赖: Go 1.24+, Redis
make build

./kvlang tutorial/01-basics/hello.kv         # 运行文件
./kvlang -c 'print("hello, world")'          # inline 模式
echo '40 + 2 -> x; print(x)' | ./kvlang      # pipe 模式(; 分隔同行语句)
./kvlang vet my.kv                           # 语法检查
./kvlang format my.kv                        # 格式化

Language Guide

程序结构(先读这条)

顶层只能写两种东西:单条指令(赋值/内建调用)和函数调用。if / while / for 必须写在 def 函数体内。 惯例是定义 main 再调用它:

def main() -> () {
    total = 0  # = 等价于 <-
    1 -> i
    while (i <= 5) {
        total <- total + i
        i + 1 -> i
    }
    print(total)
}

main()

读写码:赋值三形态

x = 40 + 2            # = :写槽在左(≡ <-);= 不是表达式,不能嵌进条件里
y <- x                # 左箭头:写槽在左
x * y -> z            # 右箭头:写槽在右
f(a, b) -> r          # 函数写参映射;多写参 -> x, y;丢弃用 -> _

写槽必须是位置:裸名(帧内变量)、/abs/path(全局键)、base.名(成员)。字面量不是位置。

函数:没有返回值,只有写参

def 签名中 -> (C: int)写参声明。函数把结果写进写参槽,调用方用 -> r 把写参映射到自己的位置:

def add(A: int, B: int) -> (C: int) {
    A + B -> C
}

def main() -> () {
    add(3, 4) -> s
    print(s)          # 7
}

main()

dict、成员访问与链表

d = { name="kv"; ver=1 }    # dict 字面量:成员是平坦键族 d.name、d.ver
print(d.name)               # 成员读
d.ver = 2                   # 成员写;动态键用 d.*k(k 的值作键名)

链表等跨函数共享的数据结构,节点用绝对路径创建(帧内变量随函数返回销毁):

def build() -> () {
    /n1 = { val=1; next="/n2" }  # = 等价于 <-
    /n2 <- { val=2; next="/n3" }
    { val=3; next="" } -> /n3
}

def main() -> () {
    build()
    "/n1" -> p                   # p 存路径字符串(指针)
    while (p != "") {
        p.val -> v               # 指针解引用:读 /n1.val
        print(v)
        p.next -> p
    }
}

main()

数字类型(可选精度声明)

f = float32(3)        # int8/16/32/64 uint8/16/32/64 float32/64 十算子,既创建也转换
w = int8(300)         # 44:窄化补码回绕;float→int 截断向零;算术域统一 int64/float64

控制流(仅限 def 体内)

if (cond) { ... } else { ... }
while (cond) { ... }
for (x in arr) { ... }        # 遍历键族数组

条件支持复合表达式:if (7 % 2 != 0)while (i < strlen(s)) 均可(编译期自动展平为临时槽)。

操作符

类别 符号
算术 + - * / %
比较 == != < > <= >=
逻辑 && || !
位运算 & | ^ << >>

/:两侧均 int → 整除(C 风格,7/2=3、-9/2=-4);任一侧 float → 浮除(7.0/2=3.5)。

内建函数

abs neg sign pow sqrt exp log min max print cerr input
int float bool 及十个精度算子 · char ord strlen slice concat · array len at set has sort dict kvat kvhas

字符串按字符处理:strlen(s) 取长度,char(s, i) 取第 i 个字符(单字符字符串,可与 "a" 直接比较),ord(s, i) 取字节码(做算术用)。


Tutorial

93 个自包含示例(92 例带期望输出,CI 全量验证),按主题组织:

01-basics/        hello, vars, arith, precision, numtypes  (5 files)
02-func/          def, call, nested calls                  (1 file)
03-control/       if, while, for, guess game               (5 files)
04-algo/          fibonacci, gcd, collatz, ...             (13 files)
05-leetcode/      LeetCode solutions                       (69 files)
./kvlang tutorial/01-basics/hello.kv         # hello kvlang
./kvlang tutorial/04-algo/fibonacci.kv       # fib = 55
./kvlang tutorial/05-leetcode/001_two_sum.kv # LeetCode

python3 tutorial/test.py                     # 全部 92 例 — CI 验证

License

MIT — see LICENSE