some cool things

This commit is contained in:
2026-06-25 18:57:53 +10:00
parent fa2c205f33
commit 9bb083a6f8
19 changed files with 4288 additions and 1 deletions

45
src/Stringify/Arg.c Normal file
View File

@@ -0,0 +1,45 @@
#include "../../include/ground.h"
char* _GroundStringifyArg(GroundArg* arg) {
if (arg->type == GroundArg_Value) {
return Ground.Stringify.Value(&arg->as.value);
} else {
char* buf = malloc(strlen(arg->as.ref->string) + 2); // add 2 for sigil + null byte
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Arg");
return NULL;
}
switch (arg->type) {
case GroundArg_ValueRef: {
buf[0] = '$';
break;
}
case GroundArg_DirectRef: {
buf[0] = '&';
break;
}
case GroundArg_LineRef: {
buf[0] = '%';
break;
}
case GroundArg_Label: {
buf[0] = '@';
break;
}
case GroundArg_FunctionRef: {
buf[0] = '!';
break;
}
case GroundArg_TypeRef: {
buf[0] = '-';
break;
}
case GroundArg_Value: {
break;
}
}
memcpy(buf + 1, arg->as.ref->string, strlen(arg->as.ref->string) + 1);
return buf;
}
}

29
src/Stringify/Heap.c Normal file
View File

@@ -0,0 +1,29 @@
#include "../../include/ground.h"
#include "../include/estr.h"
#include <inttypes.h>
char* _GroundStringifyHeap(GroundBytecodeHeap* heap) {
Estr heapString = CREATE_ESTR("");
for (GroundSize i = 0; i < heap->len; i++) {
char* string = Ground.Stringify.Value(&heap->heap[i]);
if (Ground.Flags.error) {
return NULL;
}
if (string == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("Ground.Stringify.Value unexpectedly returned NULL in Ground.Stringify.Heap");
return NULL;
}
char* buf = malloc(snprintf(NULL, 0, "#%" PRIu64 ": %s\n", i, string) + 1);
sprintf(buf, "#%" PRIu64 ": %s\n", i, string);
APPEND_ESTR(heapString, buf);
free(string);
}
return heapString.str;
}

298
src/Stringify/Instruction.c Normal file
View File

@@ -0,0 +1,298 @@
#include "../../include/ground.h"
#include "../include/estr.h"
char* _GroundStringifyInstruction(GroundInstruction* instruction) {
Estr estr;
switch (instruction->type) {
case GroundInstruction_IF: {
estr = CREATE_ESTR("IF ");
break;
}
case GroundInstruction_JUMP: {
estr = CREATE_ESTR("JUMP ");
break;
}
case GroundInstruction_END: {
estr = CREATE_ESTR("END ");
break;
}
case GroundInstruction_INPUT: {
estr = CREATE_ESTR("INPUT ");
break;
}
case GroundInstruction_PRINT: {
estr = CREATE_ESTR("PRINT ");
break;
}
case GroundInstruction_PRINTLN: {
estr = CREATE_ESTR("PRINTLN ");
break;
}
case GroundInstruction_SET: {
estr = CREATE_ESTR("SET ");
break;
}
case GroundInstruction_GETTYPE: {
estr = CREATE_ESTR("GETTYPE ");
break;
}
case GroundInstruction_EXISTS: {
estr = CREATE_ESTR("EXISTS ");
break;
}
case GroundInstruction_SETLIST: {
estr = CREATE_ESTR("SETLIST ");
break;
}
case GroundInstruction_SETLISTAT: {
estr = CREATE_ESTR("SETLISTAT ");
break;
}
case GroundInstruction_GETLISTAT: {
estr = CREATE_ESTR("GETLISTAT ");
break;
}
case GroundInstruction_GETLISTSIZE: {
estr = CREATE_ESTR("GETLISTSIZE ");
break;
}
case GroundInstruction_LISTAPPEND: {
estr = CREATE_ESTR("LISTAPPEND ");
break;
}
case GroundInstruction_GETSTRSIZE: {
estr = CREATE_ESTR("GETSTRSIZE ");
break;
}
case GroundInstruction_GETSTRCHARAT: {
estr = CREATE_ESTR("GETSTRCHARAT ");
break;
}
case GroundInstruction_ADD: {
estr = CREATE_ESTR("ADD ");
break;
}
case GroundInstruction_SUBTRACT: {
estr = CREATE_ESTR("SUBTRACT ");
break;
}
case GroundInstruction_MULTIPLY: {
estr = CREATE_ESTR("MULTIPLY ");
break;
}
case GroundInstruction_DIVIDE: {
estr = CREATE_ESTR("DIVIDE ");
break;
}
case GroundInstruction_EQUAL: {
estr = CREATE_ESTR("EQUAL ");
break;
}
case GroundInstruction_INEQUAL: {
estr = CREATE_ESTR("INEQUAL ");
break;
}
case GroundInstruction_NOT: {
estr = CREATE_ESTR("NOT ");
break;
}
case GroundInstruction_GREATER: {
estr = CREATE_ESTR("GREATER ");
break;
}
case GroundInstruction_LESSER: {
estr = CREATE_ESTR("LESSER ");
break;
}
case GroundInstruction_AND: {
estr = CREATE_ESTR("AND ");
break;
}
case GroundInstruction_OR: {
estr = CREATE_ESTR("OR ");
break;
}
case GroundInstruction_XOR: {
estr = CREATE_ESTR("XOR ");
break;
}
case GroundInstruction_NEG: {
estr = CREATE_ESTR("NEG ");
break;
}
case GroundInstruction_SHIFT: {
estr = CREATE_ESTR("SHIFT ");
break;
}
case GroundInstruction_STOI: {
estr = CREATE_ESTR("STOI ");
break;
}
case GroundInstruction_STOD: {
estr = CREATE_ESTR("STOD ");
break;
}
case GroundInstruction_ITOC: {
estr = CREATE_ESTR("ITOC ");
break;
}
case GroundInstruction_CTOI: {
estr = CREATE_ESTR("CTOI ");
break;
}
case GroundInstruction_TOSTRING: {
estr = CREATE_ESTR("TOSTRING ");
break;
}
case GroundInstruction_FUN: {
estr = CREATE_ESTR("FUN ");
break;
}
case GroundInstruction_RETURN: {
estr = CREATE_ESTR("RETURN ");
break;
}
case GroundInstruction_ENDFUN: {
estr = CREATE_ESTR("ENDFUN ");
break;
}
case GroundInstruction_CALL: {
estr = CREATE_ESTR("CALL ");
break;
}
case GroundInstruction_CALLMETHOD: {
estr = CREATE_ESTR("CALLMETHOD ");
break;
}
case GroundInstruction_STRUCT: {
estr = CREATE_ESTR("STRUCT ");
break;
}
case GroundInstruction_ENDSTRUCT: {
estr = CREATE_ESTR("ENDSTRUCT ");
break;
}
case GroundInstruction_INIT: {
estr = CREATE_ESTR("INIT ");
break;
}
case GroundInstruction_GETFIELD: {
estr = CREATE_ESTR("GETFIELD ");
break;
}
case GroundInstruction_SETFIELD: {
estr = CREATE_ESTR("SETFIELD ");
break;
}
case GroundInstruction_USE: {
estr = CREATE_ESTR("USE ");
break;
}
case GroundInstruction_EXTERN: {
estr = CREATE_ESTR("EXTERN ");
break;
}
case GroundInstruction_CREATELABEL: {
estr = CREATE_ESTR("CREATELABEL ");
break;
}
case GroundInstruction_PAUSE: {
estr = CREATE_ESTR("PAUSE ");
break;
}
case GroundInstruction_DROP: {
estr = CREATE_ESTR("DROP ");
break;
}
case GroundInstruction_LICENSE: {
estr = CREATE_ESTR("LICENSE ");
break;
}
case GroundInstruction_ERROR: {
estr = CREATE_ESTR("ERROR ");
break;
}
case GroundInstruction_THROW: {
estr = CREATE_ESTR("THROW ");
break;
}
case GroundInstruction_CATCH: {
estr = CREATE_ESTR("CATCH ");
break;
}
}
for (GroundSize i = 0; i < instruction->args.len; i++) {
char* arg = Ground.Stringify.Arg(&instruction->args.at[i]);
if (Ground.Flags.error) {
DESTROY_ESTR(estr);
return NULL;
}
if (arg == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("Ground.Stringify.Arg unexpectedly returned NULL in Ground.Stringify.Instruction");
DESTROY_ESTR(estr);
return NULL;
}
APPEND_ESTR(estr, arg);
free(arg);
}
return estr.str;
}

25
src/Stringify/Program.c Normal file
View File

@@ -0,0 +1,25 @@
#include "../../include/ground.h"
#include "../include/estr.h"
char* _GroundStringifyProgram(GroundProgram* program) {
Estr estr = CREATE_ESTR("");
for (size_t i = 0; i < program->len; i++) {
char* arg = Ground.Stringify.Instruction(&program->at[i]);
if (Ground.Flags.error) {
DESTROY_ESTR(estr);
return NULL;
}
if (arg == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("Ground.Stringify.Instruction unexpectedly returned null in Ground.Stringify.Program");
return NULL;
}
APPEND_ESTR(estr, arg);
APPEND_ESTR(estr, " ");
free(arg);
}
return estr.str;
}

12
src/Stringify/String.c Normal file
View File

@@ -0,0 +1,12 @@
#include "../../include/ground.h"
char* _GroundStringifyString(GroundString* string) {
char* out = malloc(string->len + 1);
if (out == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.String");
return NULL;
}
memcpy(out, string->cstr, string->len + 1);
return out;
}

67
src/Stringify/Value.c Normal file
View File

@@ -0,0 +1,67 @@
#include "../../include/ground.h"
#include <inttypes.h>
char* _GroundStringifyValue(GroundValue* value) {
switch (value->type.type) {
case GroundType_Int: {
char* buf = malloc(snprintf(NULL, 0, "%" PRId64, value->as.Int) + 1);
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "%" PRId64, value->as.Int);
return buf;
}
case GroundType_Double: {
char* buf = malloc(snprintf(NULL, 0, "%f", value->as.Double) + 1);
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "%f", value->as.Double);
return buf;
}
case GroundType_Char: {
char* buf = malloc(snprintf(NULL, 0, "%c", value->as.Char) + 1);
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "%c", value->as.Char);
return buf;
}
case GroundType_Bool: {
char* buf = malloc(6); // max(len(true), len(false)) + 1
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, value->as.Bool ? "true" : "false");
return buf;
}
case GroundType_String: {
return Ground.Stringify.String(&value->as.String);
}
case GroundType_List: {
// TODO implement list stringification
}
case GroundType_Function: {
// TODO implement function stringification
}
case GroundType_Struct: {
// TODO implement struct stringification
}
case GroundType_Object: {
// TODO implement object stringification
}
}
Ground.Flags.error = true;
Ground.Log.Error("FIXME implement all cases in Ground.Stringify.Value");
return NULL;
}