core: Add initial state management

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-02-13 23:44:48 -05:00
parent 0ddd6d6e23
commit e6cc68b8d4
3 changed files with 91 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "rifle/state.h"
/* Compiler version */
#define RIFLE_VERSION "0.0.1"
@@ -28,9 +29,23 @@ version(void)
);
}
static int
compile(const char *in_path)
{
struct rifle_state state;
if (rifle_state_init(&state, in_path) < 0) {
return -1;
}
rifle_state_destroy(&state);
return 0;
}
int
main(int argc, char **argv)
{
size_t files_compiled = 0;
int opt;
/* Begin parsing command line options */
@@ -45,5 +60,17 @@ main(int argc, char **argv)
}
}
while (optind < argc) {
if (compile(argv[optind++]) < 0) {
break;
}
++files_compiled;
}
if (files_compiled == 0) {
printf("fatal: no input files\n");
return -1;
}
return 0;
}

29
core/state.c Normal file
View File

@@ -0,0 +1,29 @@
#include <fcntl.h>
#include <unistd.h>
#include "rifle/state.h"
int
rifle_state_init(struct rifle_state *state, const char *in_path)
{
if (state == NULL || in_path == NULL) {
return -1;
}
state->in_fd = open(in_path, O_RDONLY);
if (state->in_fd < 0) {
return -1;
}
return 0;
}
void
rifle_state_destroy(struct rifle_state *state)
{
if (state == NULL) {
return;
}
close(state->in_fd);
state->in_fd = -1;
}

35
inc/rifle/state.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef RIFLE_STATE_H
#define RIFLE_STATE_H 1
#include <stdint.h>
#include <stddef.h>
/*
* Represents the compiler state
*
* @in_fd: Input file descriptor
* @line_num: Current line number
*/
struct rifle_state {
int in_fd;
size_t line_num;
};
/*
* Initialize the compiler state
*
* @state: State to initialize
* @in_path: Input path
*
* Returns zero on success
*/
int rifle_state_init(struct rifle_state *state, const char *in_path);
/*
* Destroy the compiler state
*
* @state: Compiler state to destroy
*/
void rifle_state_destroy(struct rifle_state *state);
#endif /* !RIFLE_STATE_H */