Tokenizer
This commit is contained in:
53
zig1.c
Normal file
53
zig1.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user