nctref/src/ntc.c

111 lines
2.1 KiB
C
Raw Permalink Normal View History

2023-08-27 19:48:06 +03:00
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<libgen.h>
2023-08-27 19:48:06 +03:00
#include"lexer.h"
#include"parse.h"
#include"ntc.h"
#include"reporting.h"
#include"cg.h"
2024-02-13 21:33:49 +02:00
#include"dumberdowner.h"
2025-02-27 20:10:02 +02:00
#include"x86.h"
#include"utils.h"
2023-08-27 19:48:06 +03:00
static int argc;
static char **argv;
static char **includePaths;
const char **ntc_get_import_paths() {
return (const char**) includePaths;
}
static const char *ntc_get_arg_from(size_t *i, const char *name) {
for(; *i < argc; (*i)++) {
if(strstr(argv[*i], name) == argv[*i]) {
return argv[*i] + strlen(name) + 1;
2023-08-27 19:48:06 +03:00
}
}
return NULL;
}
const char* ntc_get_arg(const char *name) {
size_t i = 1;
return ntc_get_arg_from(&i, name);
}
intmax_t ntc_get_int(const char *name) {
const char *val = ntc_get_arg(name);
if(!val) {
return 0;
}
long result = 0;
if(!unstupid_strtol(val, (const char**) &val, 0, &result)) {
return 0;
}
return result;
}
2023-08-27 19:48:06 +03:00
int main(int argc_, char **argv_) {
argc = argc_;
argv = argv_;
2025-02-27 20:10:02 +02:00
if(x86_target() == IUNKNOWN86) {
stahp(0, 0, "Unknown architecture %s", ntc_get_arg("target"));
}
2023-08-27 19:48:06 +03:00
const char *in = ntc_get_arg("in");
size_t includePathsCount = 1;
includePaths = malloc(sizeof(*includePaths));
includePaths[0] = strdup(in ? dirname(strdup(in)) : ".");
for(size_t i = 1; i < argc; i++) {
const char *path = ntc_get_arg_from(&i, "inc");
if(!path) {
break;
}
includePaths = realloc(includePaths, sizeof(*includePaths) * (++includePathsCount));
includePaths[includePathsCount - 1] = path;
}
2023-08-27 19:48:06 +03:00
FILE *f = in ? fopen(in, "rb") : stdin;
if(!f) {
stahp(0, 0, "Failed to read input (%s)", strerror(errno));
}
2023-08-27 19:48:06 +03:00
Token *tokens = nct_lex(f);
if(in) fclose(f);
AST *chunk = nct_parse(tokens);
free(tokens);
if(ntc_get_int("pdbg")) {
char *astdump = ast_dump(chunk);
fprintf(stderr, "### ORIGINAL ###\n%s\n", astdump);
free(astdump);
}
2024-11-25 17:36:03 +02:00
2024-12-14 18:13:33 +02:00
dumben_pre(chunk);
2024-02-13 21:33:49 +02:00
dumben_go(chunk);
2024-12-14 18:13:33 +02:00
while(!cg_go(chunk)) {
dumben_go(chunk);
2025-02-27 20:10:02 +02:00
if(ntc_get_int("pdbg")) {
char *astdump = ast_dump(chunk);
fprintf(stderr, "### CG FAILURE ###\n%s\n", astdump);
free(astdump);
}
2024-12-14 18:13:33 +02:00
}
2023-08-27 19:48:06 +03:00
return 0;
}