2025-11-22 22:22:43 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include "types.h"
|
|
|
|
|
#include "tokenizer.h"
|
|
|
|
|
#include "interpreter.h"
|
|
|
|
|
#include "native_functions.h"
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
if (argc < 2) {
|
|
|
|
|
printf("Usage: rc <file.rc>\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *f = fopen(argv[1], "rb");
|
|
|
|
|
if (!f) {
|
|
|
|
|
printf("Could not open file.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-22 23:15:15 +01:00
|
|
|
src_code = malloc(MAX_SRC + 1);
|
|
|
|
|
if (!src_code) {
|
|
|
|
|
printf("Memory allocation failed.\n");
|
|
|
|
|
fclose(f);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-22 22:22:43 +01:00
|
|
|
size_t n = fread(src_code, 1, MAX_SRC, f);
|
2025-11-22 23:15:15 +01:00
|
|
|
if (n >= MAX_SRC) n = MAX_SRC - 1;
|
2025-11-22 22:22:43 +01:00
|
|
|
src_code[n] = 0;
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
|
|
register_native_functions();
|
|
|
|
|
|
|
|
|
|
tokenize(src_code);
|
|
|
|
|
scan_functions();
|
|
|
|
|
|
|
|
|
|
int main_idx = find_func("main", 4);
|
2025-11-22 23:15:15 +01:00
|
|
|
if (main_idx == -1 || main_idx >= func_cnt) {
|
2025-11-22 22:22:43 +01:00
|
|
|
printf("No main function found.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pc = funcs[main_idx].entry_point;
|
2025-11-22 23:15:15 +01:00
|
|
|
|
|
|
|
|
if (sp + 2 >= MEM_SIZE) {
|
|
|
|
|
printf("Stack overflow during initialization.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2025-11-22 22:22:43 +01:00
|
|
|
memory[sp++] = 0;
|
|
|
|
|
memory[sp++] = 0;
|
|
|
|
|
|
|
|
|
|
ax = 0;
|
|
|
|
|
statement();
|
|
|
|
|
|
2025-11-22 23:15:15 +01:00
|
|
|
free(src_code);
|
2025-11-22 22:22:43 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|