Files
ibuild/ibuild.c

493 lines
9.4 KiB
C
Raw Normal View History

2026-01-09 21:29:18 +01:00
#include "deps/utils/src/arena_alloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#define CONFIG_FILE "IBUILD"
#define DIE(fmt, ...) die_t(__func__, __LINE__, fmt, ##__VA_ARGS__)
// Developer-reporting error interface, along with steps to reproduce the error.
// Include this log in your issues
void die_t(const char* func, int line, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
printf("ibuild exception(f:%s-l:%d) ", func, line);
vprintf(fmt, args);
printf("\n");
printf("******Last syscall error: %s\n", strerror(errno));
exit(1);
}
2026-01-09 21:29:18 +01:00
// User-reporting error interface
// func_name(): ERRMSG
void log_error(const char* func, const char* fmt, ...)
2026-01-09 21:29:18 +01:00
{
va_list args;
va_start(args, fmt);
printf("%s: ", func);
vprintf(fmt, args);
printf("\n");
va_end(args);
2026-01-09 21:29:18 +01:00
}
typedef struct
{
char* compiler_path;
char* build_dir;
char* target_exec;
char* src_dir;
char* version;
char** src_files;
2026-01-09 21:29:18 +01:00
} CompileOptions;
void launch_compile(CompileOptions* co)
{
if(co == NULL) DIE("launch_compile() co has to be valid");
2026-01-09 21:29:18 +01:00
if(co->compiler_path == NULL) DIE("launch_compile() requires a valid compiler path");
2026-01-09 21:29:18 +01:00
pid_t p = fork();
if(p<0)
{
DIE("launch_compile() fork error");
}
2026-01-09 21:29:18 +01:00
else if (p==0)
{
char* args[] = {co->compiler_path, "ibuild.c", "-o", co->target_exec, NULL};
printf("SET COMPILER: %s\n", co->compiler_path);
if((execvp(co->compiler_path, args)) == -1)
DIE("launch_compile() execvp error");
}
}
2026-01-09 21:29:18 +01:00
bool detect_config_file()
{
if(access(CONFIG_FILE, F_OK | R_OK) == 0)
2026-01-09 21:29:18 +01:00
return true;
return false;
}
/*** configuration lexer ***/
typedef struct
{
char *src;
Arena alloc;
} Tokenizer;
typedef enum
{
T_INVALID = -1,
T_IDENTIFIER = 0,
T_STRING = 1,
T_IS = 2,
T_EOF = 3,
} TokenType;
typedef struct
{
TokenType type;
char* value;
int line;
} Token;
#define TOKEN_CONST {T_INVALID, NULL, 0}
bool is_alpha_uppercase(char s)
{
return (s >= 'A' && s <= 'Z');
}
bool is_alpha_lowercase(char s)
{
return (s >= 'a' && s <= 'z');
}
bool is_whitespace(char s)
{
return s == ' ' || s == '\t' || s == '\r' || s == '\n';
}
bool is_part_of_key(char s)
{
return is_alpha_uppercase(s) || s == '_';
}
size_t skip_group(Tokenizer* t, bool (*func)(char))
{
if(func == NULL) DIE("skip_group() func invalid!");
2026-01-09 21:29:18 +01:00
size_t len = 0;
while(func(*t->src))
{
len++;
t->src += 1;
}
return len;
}
Token tokenize_identifier(Tokenizer *t)
{
char* temp = t->src;
size_t len = skip_group(t, &is_part_of_key);
char* buf = (char*)arena_alloc(&t->alloc, sizeof(char) * len + 1);
memcpy(buf, temp, len);
buf[len] = '\0';
return (Token){T_IDENTIFIER, buf};
}
Token tokenize_string(Tokenizer *t)
{
t->src += 1;
char* temp = t->src;
size_t len = 0;
while(*t->src != '"' && *t->src != '\0')
{
len++;
t->src += 1;
}
if(*t->src == '"') t->src++;
char* buf = (char*)arena_alloc(&t->alloc, (sizeof(char) * len) + 1);
memcpy(buf, temp, len);
buf[len] = '\0';
return (Token){T_STRING, buf};
}
Token tokenizer_next(Tokenizer* t)
{
if(is_whitespace(*t->src)) skip_group(t, &is_whitespace);
if(is_alpha_uppercase(*t->src))
{
return tokenize_identifier(t);
}
switch(*t->src)
{
case '"':
return tokenize_string(t);
case '=':
t->src++;
return (Token){T_IS, NULL};
}
if(*t->src == '\0') return (Token){T_EOF, NULL};
t->src++;
return (Token){T_INVALID, NULL};
}
/*** configuration parser ***/
typedef enum
{
K_NA = -1,
2026-01-09 21:29:18 +01:00
K_UNKNOWN = 0,
K_COMPILER_PATH = 1,
K_SRC_DIR = 2,
K_SRC_FILES = 3,
K_BUILD_DIR = 4,
K_TARGET_EXEC = 5,
2026-01-09 21:29:18 +01:00
} Key;
typedef enum
{
N_STRING = 0,
N_ARRAY = 1,
N_PAIR = 2,
2026-01-09 21:29:18 +01:00
} NodeType;
typedef struct
{
NodeType type;
Key key;
void* value;
} Node;
typedef struct
{
Arena* tokens_alloc;
2026-01-09 21:29:18 +01:00
size_t loc;
size_t cap;
Arena alloc;
} Parser;
typedef struct
{
Key key;
const char* value;
} KeyMap;
static KeyMap keyword_mappings[] = {
{K_COMPILER_PATH, "COMPILER_PATH"},
{K_SRC_DIR, "SRC_DIR"},
{K_SRC_FILES, "SRC_FILES"},
2026-01-09 21:29:18 +01:00
{K_BUILD_DIR, "BUILD_DIR"},
{K_TARGET_EXEC, "TARGET_EXEC"},
2026-01-09 21:29:18 +01:00
{K_UNKNOWN, NULL},
};
Key key_lookup(char* s)
{
if(s == NULL) return K_UNKNOWN;
for(const KeyMap* ptr = keyword_mappings; ptr->value != NULL; ptr++)
{
if(strcmp(ptr->value, s) == 0)
{
return ptr->key;
}
}
return K_UNKNOWN;
}
Token parser_peek(Parser* p, size_t o)
{
if (p->loc + o >= p->cap)
{
return ((Token*)p->tokens_alloc->start)[p->loc - 1];
2026-01-09 21:29:18 +01:00
}
return ((Token*)p->tokens_alloc->start)[p->loc + o];
2026-01-09 21:29:18 +01:00
}
Token parser_previous(Parser* p)
{
if(p->loc - 1 < 0)
{
DIE("parser logic error.");
2026-01-09 21:29:18 +01:00
}
return ((Token*)p->tokens_alloc->start)[p->loc - 1];
2026-01-09 21:29:18 +01:00
}
Token parser_advance(Parser* p)
{
if(p->loc + 1 >= p->cap)
{
return ((Token*)p->tokens_alloc->start)[p->loc];
2026-01-09 21:29:18 +01:00
}
p->loc++;
return ((Token*)p->tokens_alloc->start)[p->loc];
}
Token parser_expect(Parser* p, TokenType tt, char* s)
{
if(p == NULL) DIE("parser_expect() p must not be NULL");
if(p == NULL) DIE("parser_expect() s must not be NULL");
if(parser_peek(p, 1).type != tt)
DIE(s);
return parser_advance(p);
2026-01-09 21:29:18 +01:00
}
Node* parser_expression(Parser* p)
{
Token t = parser_advance(p);
2026-01-09 21:29:18 +01:00
if(t.type == T_STRING)
{
Node* node = (Node*)arena_alloc(&p->alloc, sizeof(Node));
node->key = K_NA;
2026-01-09 21:29:18 +01:00
node->value = (void*)t.value;
node->type = N_STRING;
parser_advance(p);
2026-01-09 21:29:18 +01:00
return node;
}
DIE("Syntax error: Invalid expression.");
2026-01-09 21:29:18 +01:00
}
Node* parser_statement(Parser* p)
{
Token t = parser_peek(p, 0);
2026-01-09 21:29:18 +01:00
if(t.type == T_IDENTIFIER)
{
Key k = key_lookup(t.value);
if(k == K_UNKNOWN) DIE("Syntax error: Unexpected identifer encountered");
parser_expect(p, T_IS, "Syntax error: Expected '=' after identifier");
2026-01-09 21:29:18 +01:00
Node* expression = parser_expression(p);
Node* node = (Node*)arena_alloc(&p->alloc, sizeof(Node));
node->key = k;
node->value = (void*)expression;
node->type = N_PAIR;
}
else
{
DIE("Syntax error: expected an identifier");
2026-01-09 21:29:18 +01:00
}
}
size_t parser_parse(Parser* p, Node*** out)
2026-01-09 21:29:18 +01:00
{
Arena statements_alloc = ARENA_CONST;
size_t size = 0;
while(p->loc < p->cap - 1)
{
Node** temp = (Node**)arena_alloc(&statements_alloc, sizeof(Node*));
*temp = parser_statement(p);
size++;
}
*out = (Node**)statements_alloc.start;
return size;
2026-01-09 21:29:18 +01:00
}
void debug_parser(Node* n, int indent)
{
for(int i = 0; i < indent; i++)
{
printf(" ");
}
switch(n->type)
{
case N_PAIR:
printf("PAIR STATEMENT");
printf(" Key: %10d\n", n->key);
if(n->value != NULL)
debug_parser((Node*)n->value, 2);
break;
case N_STRING:
printf("STRING EXPRESSION");
printf(" Value: %s\n", (char*)n->value);
break;
}
}
2026-01-09 21:29:18 +01:00
/*** configuration file management ***/
long get_file_size(FILE* fd)
{
if(fd == NULL) DIE("get_file_size() fd cannot be NULL.");
if(fseek(fd, 0, SEEK_END) != 0) DIE("process_config() file exists, fseek SEEK_END fail.");
2026-01-09 21:29:18 +01:00
long size = ftell(fd);
if(fseek(fd, 0, SEEK_SET) != 0) DIE("process_config() file exists, fseek SEEK_SET fail.");
2026-01-09 21:29:18 +01:00
return size;
}
typedef struct
{
Arena tokens;
} ConfigMemory;
int process_config(Node*** ast)
2026-01-09 21:29:18 +01:00
{
if(!detect_config_file())
return 1;
printf("IBUILD Configuration file detected.\n");
ConfigMemory cm = {ARENA_CONST};
2026-01-09 21:29:18 +01:00
FILE* fd = fopen("IBUILD", "r");
if(fd == NULL) DIE("process_config() file exists however fd is NULL");
2026-01-09 21:29:18 +01:00
long fs = get_file_size(fd);
printf("IBUILD file of size: %d\n", fs);
if(fs < 2) return 0;
2026-01-09 21:29:18 +01:00
char* config_mem = malloc(fs + 1);
fread(config_mem, sizeof(char), fs, fd);
config_mem[fs] = '\0';
printf("Config file:\n%s\n", config_mem);
size_t t_count = 0;
Tokenizer t = { .src = config_mem, .alloc = ARENA_CONST};
while(1)
{
Token token = tokenizer_next(&t);
if(token.type == T_INVALID) DIE("illegal token!");
2026-01-09 21:29:18 +01:00
Token* tm = arena_alloc(&cm.tokens, sizeof(Token));
2026-01-09 21:29:18 +01:00
*tm = token;
t_count++;
if(token.type == T_EOF) break;
}
Parser parser = {.tokens_alloc = &cm.tokens, .loc = 0, .cap = t_count, .alloc = ARENA_CONST};
2026-01-09 21:29:18 +01:00
Node** statements = NULL;
size_t statements_len = parser_parse(&parser, &statements);
if(statements == NULL) DIE("parser return invalid results");
for (size_t i = 0; i < statements_len; i++)
debug_parser(statements[i], 0);
2026-01-09 21:29:18 +01:00
free(config_mem);
arena_free(&cm.tokens);
*ast = statements;
return statements_len;
2026-01-09 21:29:18 +01:00
}
/*** build configuration process ***/
void set_compiler_option(CompileOptions* co, Key k, char* val)
{
switch(k)
{
case K_COMPILER_PATH: co->compiler_path = val; break;
case K_BUILD_DIR: co->build_dir = val; break;
case K_TARGET_EXEC: co->target_exec = val; break;
default:
DIE("Invalid compiler option was supplied!");
}
}
void populate_compile_options(CompileOptions* co, Node* n)
2026-01-09 21:29:18 +01:00
{
switch(n->type)
2026-01-09 21:29:18 +01:00
{
case N_PAIR:
Key k = n->key;
set_compiler_option(co, k, (char*)((Node*)n->value)->value);
break;
2026-01-09 21:29:18 +01:00
}
}
2026-01-09 21:29:18 +01:00
void build(int len, Node** st)
2026-01-09 21:29:18 +01:00
{
CompileOptions co;
co.compiler_path = "/usr/bin/gcc";
co.build_dir = ".";
co.src_dir = ".";
co.target_exec = "iexec";
2026-01-09 21:29:18 +01:00
for (size_t i = 0; i < len; i++)
populate_compile_options(&co, st[i]);
2026-01-09 21:29:18 +01:00
launch_compile(&co);
printf("Compilation finished.\n");
}
int main(int argc, char** argv)
{
printf("Build version: 0.0.1\n");
Node** ast = NULL;
int len_ast = process_config(&ast);
2026-01-09 21:29:18 +01:00
build(len_ast, ast);
2026-01-09 21:29:18 +01:00
return 0;
}