Tokenizer

This commit is contained in:
2024-12-13 09:39:02 +02:00
commit 6863e34fbc
9 changed files with 2209 additions and 0 deletions

53
zig1.c Normal file
View File

@@ -0,0 +1,53 @@
#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.
int zig1_run(char* program, char** msg) { return 0; }
// API: run and:
// code = 3: abnormal error, expect something in stderr.
int zig1_run_file(char* fname, char** msg)
{
FILE* f = fopen(fname, "r");
if (f == NULL) {
perror("fopen");
return 3;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
if (fsize == -1) {
perror("ftell");
fclose(f);
return 3;
}
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 = zig1_run(program, msg);
free(program);
return code;
}