Init
This commit is contained in:
commit
2a9968c4ce
8 changed files with 302 additions and 0 deletions
89
src/main.c
Normal file
89
src/main.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "hydroforth/hydroforth.h"
|
||||
|
||||
struct ReadSrcResult
|
||||
{
|
||||
bool success;
|
||||
char *const src;
|
||||
};
|
||||
|
||||
struct ReadSrcResult read_src(FILE *fp)
|
||||
{
|
||||
if (fseek(fp, 0L, SEEK_END) != 0)
|
||||
{
|
||||
fputs("Error seeking to file end!", stderr);
|
||||
return (struct ReadSrcResult){
|
||||
.success = false,
|
||||
};
|
||||
}
|
||||
const long bufsize = ftell(fp);
|
||||
if (bufsize == -1)
|
||||
{
|
||||
fputs("Error getting file size!", stderr);
|
||||
return (struct ReadSrcResult){
|
||||
.success = false,
|
||||
};
|
||||
}
|
||||
char *const src = malloc(sizeof(char) * (bufsize + 1));
|
||||
if (fseek(fp, 0L, SEEK_SET) != 0)
|
||||
{
|
||||
fputs("Error rewinding file to start!", stderr);
|
||||
return (struct ReadSrcResult){
|
||||
.success = false,
|
||||
};
|
||||
}
|
||||
const unsigned long new_len = fread(src, sizeof(char), bufsize, fp);
|
||||
if (ferror(fp) != 0)
|
||||
{
|
||||
fputs("Error reading file!", stderr);
|
||||
return (struct ReadSrcResult){
|
||||
.success = false,
|
||||
};
|
||||
}
|
||||
src[new_len + 1] = '\0';
|
||||
|
||||
return (struct ReadSrcResult){
|
||||
.success = true,
|
||||
.src = src,
|
||||
};
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fputs("No source file specified!", stderr);
|
||||
return 1;
|
||||
}
|
||||
FILE *fp = fopen(argv[1], "rb");
|
||||
if (fp == NULL)
|
||||
{
|
||||
fputs("Error opening file!", stderr);
|
||||
return 1;
|
||||
}
|
||||
const struct ReadSrcResult res = read_src(fp);
|
||||
fclose(fp);
|
||||
if (res.success)
|
||||
{
|
||||
HYDROFORTH__INTERPRETER interpreter = {
|
||||
.src = res.src,
|
||||
.pos = 0,
|
||||
};
|
||||
const bool run_res = hydroforth.run(&interpreter);
|
||||
free(res.src);
|
||||
if (!run_res)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
free(res.src);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue