stage1 人工重构ast.c

This commit is contained in:
zzy
2026-08-12 10:29:38 +08:00
parent 314100afbc
commit b0e6b406ac
22 changed files with 1619 additions and 2895 deletions

View File

@@ -1,4 +1,4 @@
/* spl_syscall.c built-in syscall implementations + registration
/* spl_syscall.c - built-in syscall implementations + registration
*
* All syscalls validate nargs, cast spl_val_t args to the expected C types,
* execute, and return the result as spl_val_t.
@@ -23,7 +23,7 @@
* Each SYSCALL_N macro:
* 1. Validates nargs (prints error and returns -1 on mismatch)
* 2. Casts args[n] from spl_val_t to the specified C type via
* (type)(uintptr_t) this handles both integer and pointer types
* (type)(uintptr_t) - this handles both integer and pointer types
* 3. Evaluates the expression and returns the result as spl_val_t
* ================================================================= */
@@ -80,34 +80,34 @@
* OS syscalls
* ================================================================= */
/* vm_exit terminate process with the given exit code */
/* vm_exit - terminate process with the given exit code */
SYSCALL_1(vm_exit, int, (exit(a1), (spl_val_t)0))
/* vm_putchar write a single character to stdout */
/* vm_putchar - write a single character to stdout */
SYSCALL_1(vm_putchar, int, putchar(a1))
/* vm_getchar read a single character from stdin */
/* vm_getchar - read a single character from stdin */
SYSCALL_0(vm_getchar, getchar())
/* vm_putint print an integer to stdout */
/* vm_putint - print an integer to stdout */
SYSCALL_1(vm_putint, int, (fprintf(stdout, "%d", a1), (spl_val_t)0))
/* vm_putstr print a string to stdout (no trailing newline) */
/* vm_putstr - print a string to stdout (no trailing newline) */
SYSCALL_1(vm_putstr, const char *, (fputs(a1, stdout), (spl_val_t)0))
/* vm_fopen open a file, returns FILE* as spl_val_t */
/* vm_fopen - open a file, returns FILE* as spl_val_t */
SYSCALL_2(vm_fopen, const char *, const char *, (uintptr_t)fopen(a1, a2))
/* vm_fclose close a file, returns 0 on success */
/* vm_fclose - close a file, returns 0 on success */
SYSCALL_1(vm_fclose, FILE *, fclose(a1))
/* vm_fread read from file, returns number of items read */
/* vm_fread - read from file, returns number of items read */
SYSCALL_4(vm_fread, void *, size_t, size_t, FILE *, fread(a1, a2, a3, a4))
/* vm_fwrite write to file, returns number of items written */
/* vm_fwrite - write to file, returns number of items written */
SYSCALL_4(vm_fwrite, const void *, size_t, size_t, FILE *, fwrite(a1, a2, a3, a4))
/* vm_fsize get file size in bytes */
/* vm_fsize - get file size in bytes */
static spl_val_t vm_fsize(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_fsize", 1);
FILE *f = (FILE *)(uintptr_t)args[0];
@@ -122,7 +122,7 @@ SYSCALL_0(vm_stdin, stdin)
SYSCALL_0(vm_stdout, stdout)
SYSCALL_0(vm_stderr, stderr)
/* vm_read_file read entire file into a malloc'd, null-terminated buffer */
/* vm_read_file - read entire file into a malloc'd, null-terminated buffer */
static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_read_file", 1);
const char *path = (const char *)(uintptr_t)args[0];
@@ -149,22 +149,22 @@ static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
return (spl_val_t)(uintptr_t)buf;
}
/* vm_alloc allocate memory (malloc) */
/* vm_alloc - allocate memory (malloc) */
SYSCALL_1(vm_alloc, size_t, (uintptr_t)malloc(a1))
/* vm_free free memory */
/* vm_free - free memory */
SYSCALL_1(vm_free, void *, (free(a1), (spl_val_t)0))
/* vm_realloc reallocate memory (realloc) */
/* vm_realloc - reallocate memory (realloc) */
SYSCALL_2(vm_realloc, void *, size_t, (uintptr_t)realloc(a1, a2))
/* vm_strlen get string length */
/* vm_strlen - get string length */
SYSCALL_1(vm_strlen, const char *, strlen(a1))
/* vm_strcmp compare two strings */
/* vm_strcmp - compare two strings */
SYSCALL_2(vm_strcmp, const char *, const char *, strcmp(a1, a2))
/* vm_memcpy copy memory, returns dst */
/* vm_memcpy - copy memory, returns dst */
SYSCALL_3(vm_memcpy, void *, const void *, size_t, (memcpy(a1, a2, a3), (uintptr_t)a1))
static spl_val_t vm_printf(int nargs, spl_val_t *args) {
@@ -228,13 +228,13 @@ static spl_val_t vm_printf(int nargs, spl_val_t *args) {
}
/* =================================================================
* VM syscalls (for comptime compile-time code execution)
* VM syscalls (for comptime - compile-time code execution)
*
* These let SPL code create isolated sub-VMs, load compiled .sir
* programs into them, push arguments, call functions, and run.
* ================================================================= */
/* vm_new create a new sub-VM instance, returns spl_vm_t* */
/* vm_new - create a new sub-VM instance, returns spl_vm_t* */
static spl_val_t vm_new(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_new", 0);
(void)args;
@@ -245,7 +245,7 @@ static spl_val_t vm_new(int nargs, spl_val_t *args) {
return (spl_val_t)(uintptr_t)vm;
}
/* vm_drop destroy a sub-VM and its loaded program */
/* vm_drop - destroy a sub-VM and its loaded program */
static spl_val_t vm_drop(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_drop", 1);
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
@@ -261,7 +261,7 @@ static spl_val_t vm_drop(int nargs, spl_val_t *args) {
return 0;
}
/* vm_load load a .sir file, register syscalls, return spl_prog_t* */
/* vm_load - load a .sir file, register syscalls, return spl_prog_t* */
static spl_val_t vm_load(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_load", 2);
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
@@ -285,7 +285,7 @@ static spl_val_t vm_load(int nargs, spl_val_t *args) {
return (spl_val_t)(uintptr_t)prog;
}
/* vm_push push a value onto the sub-VM's stack (for passing arguments) */
/* vm_push - push a value onto the sub-VM's stack (for passing arguments) */
static spl_val_t vm_push(int nargs, spl_val_t *args) {
CHECK_NARGS("vm_push", 2);
spl_vm_t *vm = (spl_vm_t *)(uintptr_t)args[0];
@@ -300,7 +300,7 @@ static spl_val_t vm_push(int nargs, spl_val_t *args) {
return 0;
}
/* vm_call call a function by name with pre-pushed arguments
/* vm_call - call a function by name with pre-pushed arguments
*
* Args (3): vm, function_name, nargs
* The arguments should already be on the sub-VM's stack via vm_push.
@@ -347,7 +347,7 @@ static spl_val_t vm_call(int nargs, spl_val_t *args) {
return 0;
}
/* vm_run run a sub-VM to completion, returns exit_code */
/* vm_run - run a sub-VM to completion, returns exit_code */
SYSCALL_1(vm_run, spl_vm_t *, spl_vm_run_until(a1, 0))
/* =================================================================