Mach/builder/src-c/config-reader.c

44 lines
1.1 KiB
C

#include <mach.h>
#include <parser.h>
#include <stdio.h>
#include <stdlib.h>
i32_t mach_parse_script(char *string, usz_t len_string, MachScript *out_script)
{
MachTokenStream token_stream;
token_stream.len_source = len_string;
token_stream.source = string;
i32_t tokenization_status = mach_tokenize(&token_stream);
if(tokenization_status < 0)
return tokenization_status - 1024;
mach_display_token_stream(&token_stream);
free(token_stream.tokens);
return 0;
}
i32_t mach_read_script(const char *path, MachScript *out_script)
{
FILE *script_file = fopen(path, "r");
if(script_file == NULL)
return -1;
fseek(script_file, 0, SEEK_END);
usz_t len_script_string = ftell(script_file);
fseek(script_file, 0, SEEK_SET);
char *script_string = malloc(len_script_string + 1);
fread(script_string, 1, len_script_string, script_file);
fclose(script_file);
i32_t parse_status = mach_parse_script(script_string, len_script_string, out_script);
free(script_string);
if(parse_status < 0)
return parse_status - 1024;
return parse_status;
}