2025-11-23 13:37:08 +11:00
|
|
|
#include "parser.h"
|
2025-11-23 18:34:30 +11:00
|
|
|
#include "interpreter.h"
|
2026-01-21 11:17:19 +11:00
|
|
|
#include "compiler.h"
|
2025-11-23 13:37:08 +11:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
char* getFileContents(const char* filename) {
|
|
|
|
|
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
|
|
|
|
|
FILE* fp;
|
|
|
|
|
long lSize;
|
|
|
|
|
char* file;
|
|
|
|
|
|
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
|
if (!fp) {
|
|
|
|
|
perror(filename);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek(fp, 0L, SEEK_END);
|
|
|
|
|
lSize = ftell(fp);
|
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
|
|
file = calloc(1, lSize + 1);
|
|
|
|
|
if (!file) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
fputs("memory allocation fail when reading file", stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (1!=fread(file, lSize, 1, fp)) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
free(file);
|
|
|
|
|
fputs("couldn't read entire file", stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we done
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2026-01-21 11:17:19 +11:00
|
|
|
/*
|
2025-11-23 13:37:08 +11:00
|
|
|
if (argc < 2) {
|
|
|
|
|
printf("Usage: ground [file]\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
char* file = getFileContents(argv[1]);
|
|
|
|
|
GroundProgram program = parseFile(file);
|
2025-11-23 19:59:22 +11:00
|
|
|
free(file);
|
2026-01-21 11:17:19 +11:00
|
|
|
char* compiled = compileGroundProgram(&program);
|
|
|
|
|
printf("%s\n", compiled);
|
2025-12-02 09:00:21 +11:00
|
|
|
interpretGroundProgram(&program, NULL);
|
2026-01-21 11:17:19 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
|
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool compile = false;
|
|
|
|
|
char* fileName = NULL;
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
|
if (strcmp("--compile", argv[i]) == 0 || strcmp("-c", argv[i]) == 0) {
|
|
|
|
|
compile = true;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp("--help", argv[i]) == 0 || strcmp("-h", argv[i]) == 0) {
|
|
|
|
|
printf("GroundVM help\n");
|
|
|
|
|
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
|
|
|
|
printf("Options:\n");
|
|
|
|
|
printf(" -c or --compile: Outputs Linux x86_64 assembly instead of interpreting (WIP)\n");
|
|
|
|
|
printf(" -h or --help: Shows this help message\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
fileName = argv[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fileName == NULL) {
|
|
|
|
|
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
|
|
|
|
printf("Error: No file name provided\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* file = getFileContents(fileName);
|
|
|
|
|
GroundProgram program = parseFile(file);
|
|
|
|
|
free(file);
|
|
|
|
|
|
|
|
|
|
if (compile) {
|
|
|
|
|
char* compiled = compileGroundProgram(&program);
|
|
|
|
|
printf("%s\n", compiled);
|
|
|
|
|
} else {
|
|
|
|
|
interpretGroundProgram(&program, NULL);
|
|
|
|
|
}
|
2025-11-23 13:37:08 +11:00
|
|
|
}
|