Better cmdline args, start work on compiler

This commit is contained in:
2026-01-21 11:17:19 +11:00
parent c6762a7966
commit d3c03b4987
5 changed files with 131 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
#include "parser.h"
#include "interpreter.h"
#include "compiler.h"
#include <stdio.h>
char* getFileContents(const char* filename) {
@@ -39,6 +40,7 @@ char* getFileContents(const char* filename) {
}
int main(int argc, char** argv) {
/*
if (argc < 2) {
printf("Usage: ground [file]\n");
exit(1);
@@ -46,5 +48,49 @@ int main(int argc, char** argv) {
char* file = getFileContents(argv[1]);
GroundProgram program = parseFile(file);
free(file);
char* compiled = compileGroundProgram(&program);
printf("%s\n", compiled);
interpretGroundProgram(&program, NULL);
*/
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);
}
}