60 lines
1.3 KiB
C
60 lines
1.3 KiB
C
#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(const char* program, char** msg)
|
|
{
|
|
(void)program;
|
|
(void)msg;
|
|
return 0;
|
|
}
|
|
|
|
// API: run and:
|
|
// code = 3: abnormal error, expect something in stderr.
|
|
int zig1_run_file(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 = zig1_run(program, msg);
|
|
free(program);
|
|
return code;
|
|
}
|