⚠️ 本网站已停止维护,文档已迁移至 GitHub docs/ 目录 | ⚠️ This website is no longer maintained. Docs moved to GitHub docs/

标准库Standard Library

JVAV 标准库位于 std/ 文件夹,通过 import "std/xxx.jvl"; 引入。 安装包中 std/bin/ 同级,将 bin/ 加入 PATH 后即可从任意位置使用。 如果标准库文件缺失,编译器会给出明确的 "cannot find standard library" 提示。

The JVAV standard library lives in the std/ directory. Import via import "std/xxx.jvl";. In release packages std/ sits alongside bin/; add bin/ to PATH and use from anywhere. Missing stdlib files produce a clear "cannot find standard library" diagnostic.

Import

import "std/io.jvl";
import "std/math.jvl";
import "std/mem.jvl";
import "std/string.jvl";

std/io.jvl — Console & System I/O

基于后端 .syscall 指令生成标准函数包装器,通过 VM 系统调用邮箱(0xFFE00xFFE4)与宿主交互。支持 putcharputintgetchargetintputstrexitsleep。旧版内存映射端口(0xFFF0 / 0xFFF2)仍保持向后兼容。

Built on the backend .syscall directive, which generates standard function wrappers that interact with the host via the VM syscall mailbox (0xFFE00xFFE4). Supports putchar, putint, getchar, getint, putstr, exit, sleep. Legacy memory-mapped ports (0xFFF0 / 0xFFF2) remain backward-compatible.

Function描述Description
println(x)打印整数并换行Print integer followed by newline
print_newline()打印换行Print newline
print_space()打印空格Print space
putstr(s, n)输出字符串前 n 个字符Print first n characters of string s
exit(code)以指定退出码结束程序Terminate program with exit code
sleep(ms)休眠指定毫秒数Sleep for specified milliseconds

文件 I/O(通过自定义 syscall)File I/O (via Custom Syscall)

JVM 已内置文件系统调用(cmd 4–9)。通过 syscall 关键字即可在 JVL 中直接暴露它们,无需等待标准库更新。

The JVM already has built-in filesystem syscalls (cmd 4–9). Expose them in JVL directly via the syscall keyword, no standard library update needed.

Declaration描述Description
syscall fopen, 4, 2;打开文件(路径地址, 模式地址)Open file (path_addr, mode_addr)
syscall fclose, 5, 1;关闭文件描述符Close file descriptor
syscall fread, 6, 3;读取数据(fd, 缓冲区地址, 字数)Read data (fd, buf_addr, word_count)
syscall fwrite, 7, 3;写入数据(fd, 缓冲区地址, 字数)Write data (fd, buf_addr, word_count)
syscall fseek, 8, 2;定位文件偏移(fd, offset)Seek file offset (fd, offset)
syscall ftell, 9, 1;获取当前文件偏移Get current file offset

std/math.jvl — Integer Math

所有函数操作有符号 128-bit 整数。不支持浮点数。

All functions operate on signed 128-bit integers. No floating-point support.

Function描述Description
abs(x)绝对值Absolute value
max(a, b)两数较大值Maximum of two integers
min(a, b)两数较小值Minimum of two integers
clamp(x, lo, hi)限制在区间内Clamp value to range [lo, hi]
pow(base, exp)整数幂Integer power

std/mem.jvl — Memory

JVAV 是字寻址(word-addressable),每个地址指向一个 128-bit word。 memcpymemset 以 word 为单位操作。

JVAV is word-addressable — each address points to one 128-bit word. memcpy and memset operate in units of words.

Function描述Description
memcpy(dst, src, n)复制 n 个 wordCopy n words from src to dst
memset(p, val, n)将 n 个 word 设为 valSet n words to val

std/string.jvl — String Output

JVL 字符串字面量以 word 数组存储,不是 null-terminated,需要传入长度。

JVL string literals are stored as word arrays, not null-terminated; length must be provided.

Function描述Description
str_putn(s, n)输出字符串前 n 个字符Print first n characters of string s

完整示例Complete Example

import "std/io.jvl";
import "std/math.jvl";
import "std/string.jvl";

func main(): int {
    println(abs(-5));          // 5
    println(max(3, 7));        // 7
    println(pow(2, 3));        // 8

    var msg = "JVAV";
    str_putn(msg, 4);          // JVAV
    print_newline();

    return 0;
}