1
0
forked from ground/ground-old

.grbc file serialization/deserialization

This commit is contained in:
2026-03-04 10:40:54 +11:00
parent d5d79d4b5b
commit ede2f06ef8
3 changed files with 388 additions and 5 deletions

View File

@@ -2,7 +2,9 @@
#include "interpreter.h"
#include "compiler.h"
#include "types.h"
#include "serialize.h"
#include <stdio.h>
#include <string.h>
char* getFileContents(const char* filename) {
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
@@ -47,19 +49,42 @@ int main(int argc, char** argv) {
}
bool compile = false;
bool writeBytecode = false;
bool readBytecode = false;
char* fileName = NULL;
char* outFileName = NULL;
List groundArgs = createList();
for (int i = 1; i < argc; i++) {
if (strcmp("--compile", argv[i]) == 0 || strcmp("-c", argv[i]) == 0) {
if (writeBytecode) {
printf("Cannot choose both bytecode and compilation");
exit(1);
}
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("Usage: %s <file> [-c] [--compile] [-h] [--help] [-w <output>] [--writeBytecode <output>] [-b] [--bytecode]\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");
printf(" -w <output> or --writebytecode <output>: Outputs binary Ground bytecode");
printf(" -b <output> or --bytecode <output>: Inputs binary Ground bytecode");
exit(0);
} else if (strcmp("--writebytecode", argv[i]) == 0 || strcmp("-w", argv[i]) == 0) {
if (compile) {
printf("Cannot choose both bytecode and compilation");
exit(1);
}
writeBytecode = true;
if (i + 1 >= argc) {
printf("Usage: %s %s <output>", argv[0], argv[i]);
exit(1);
}
i++;
outFileName = argv[i];
} else if (strcmp("--bytecode", argv[i]) == 0 || strcmp("-b", argv[i]) == 0) {
readBytecode = true;
} else {
if (fileName == NULL) {
fileName = argv[i];
@@ -70,18 +95,26 @@ int main(int argc, char** argv) {
}
if (fileName == NULL) {
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
printf("Usage: %s <file> [-c] [--compile] [-h] [--help] [-w <output>] [--writeBytecode <output>] [-b] [--bytecode]\n", argv[0]);
printf("Error: No file name provided\n");
exit(1);
}
char* file = getFileContents(fileName);
GroundProgram program = parseFile(file);
free(file);
GroundProgram program;
if (readBytecode) {
deserializeProgramFromFile(fileName, &program);
} else {
char* file = getFileContents(fileName);
program = parseFile(file);
free(file);
}
if (compile) {
char* compiled = compileGroundProgram(&program);
printf("%s\n", compiled);
} else if (writeBytecode) {
serializeProgramToFile(outFileName, &program);
} else {
GroundVariable* variables = NULL;
GroundLabel* labels = NULL;