Files
spl/stage1/test13_extern.spl
2026-07-05 21:45:43 +08:00

36 lines
994 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ===== 模块6函数 =====
* test13_extern — 外部 VM 函数与字符串
* 难度2/5
* 验证点:#[extern("vm")] 声明、vm_printf 调用、
* 字符串字面量、字符串参数传递
*/
#[extern("vm")] fn vm_printf(fmt: *u8, ...) void;
#[extern("vm")] fn vm_strlen(s: *i8) i32;
#[extern("vm")] fn vm_strcmp(a: *i8, b: *i8) i32;
fn main() i32 {
/* vm_printf 输出测试 */
vm_printf("hello spl\n");
vm_printf("%d %d %d\n", 1, 2, 3);
vm_printf("chars: %c%c\n", 'A', 'B');
/* 字符串字面量用于 extern */
var len := vm_strlen("hello");
if len != 5 { ret 1; }
/* 字符串比较 */
var cmp := vm_strcmp("abc", "abc");
if cmp != 0 { ret 2; }
var cmp2 := vm_strcmp("abc", "def");
if cmp2 == 0 { ret 3; }
/* 字符串作为格式化参数 */
vm_printf("string test: %s %s\n", "foo", "bar");
/* vm_printf 返回 void不关心返回值*/
vm_printf("all extern tests done\n");
ret 0;
}