Add 'stage0/' from commit 'b3d106ec971300a9c745f4681fab3df7518c4346'

git-subtree-dir: stage0
git-subtree-mainline: 3db960767d
git-subtree-split: b3d106ec97
This commit is contained in:
2026-02-13 23:32:08 +02:00
26 changed files with 26186 additions and 0 deletions

59
stage0/zig0.c Normal file
View File

@@ -0,0 +1,59 @@
#include "common.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// API:
// - code = 0: program successfully terminated.
// - code = 1: panicked, panic message in msg. Caller should free msg.
// - code = 2: interpreter error, error in msg. Caller should free msg.
static int zig0Run(const char* program, char** msg) {
(void)program;
(void)msg;
return 0;
}
// API: run and:
// code = 3: abnormal error, expect something in stderr.
int zig0RunFile(const char* fname, char** msg) {
FILE* f = fopen(fname, "r");
if (f == NULL) {
perror("fopen");
return 3;
}
fseek(f, 0, SEEK_END);
long fsizel = ftell(f);
if (fsizel == -1) {
perror("ftell");
fclose(f);
return 3;
}
unsigned long fsize = (unsigned long)fsizel;
fseek(f, 0, SEEK_SET);
char* program = malloc(fsize + 1);
if (program == NULL) {
perror("malloc");
fclose(f);
return 3;
}
size_t bytes_read = fread(program, 1, fsize, f);
if (bytes_read < fsize) {
if (ferror(f)) {
perror("fread");
} else {
fprintf(stderr, "Unexpected end of file\n");
}
free(program);
fclose(f);
return 3;
}
fclose(f);
program[fsize] = 0;
int code = zig0Run(program, msg);
free(program);
return code;
}