This commit is contained in:
2026-07-27 20:22:42 +10:00
parent 7a53105eca
commit 8b04bf39b1
10 changed files with 398 additions and 5 deletions

View File

@@ -567,6 +567,7 @@ struct _Ground {
char* (*Value)(GroundValue* value); char* (*Value)(GroundValue* value);
char* (*BytecodeValue)(GroundBytecodeValue* value); char* (*BytecodeValue)(GroundBytecodeValue* value);
char* (*Bytecode)(GroundBytecode* bytecode);
} Stringify; } Stringify;
struct { struct {

View File

@@ -115,6 +115,7 @@ sources = files(
'src/Stringify/Value.c', 'src/Stringify/Value.c',
'src/Stringify/BytecodeValue.c', 'src/Stringify/BytecodeValue.c',
'src/Stringify/Bytecode.c',
'src/Struct/addField.c', 'src/Struct/addField.c',

View File

@@ -1097,6 +1097,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
break; break;
} }
case 3: { case 3: {
printf("fard\n");
GroundBytecodeFunction* function = &gbs->values[offset].as.Function; GroundBytecodeFunction* function = &gbs->values[offset].as.Function;
if (function->closure == NULL) { if (function->closure == NULL) {
function->closure = malloc(sizeof(GroundBytecodeHeap)); function->closure = malloc(sizeof(GroundBytecodeHeap));

316
src/Stringify/Bytecode.c Normal file
View File

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

View File

@@ -51,7 +51,14 @@ char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) {
// TODO implement list stringification // TODO implement list stringification
} }
case GroundType_Function: { case GroundType_Function: {
// TODO implement function stringification char* buf = malloc(sizeof("<Function>"));
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "<Function>");
return buf;
} }
case GroundType_Struct: { case GroundType_Struct: {
Estr str = CREATE_ESTR("<struct fields: { "); Estr str = CREATE_ESTR("<struct fields: { ");
@@ -76,6 +83,26 @@ char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) {
APPEND_ESTR(str, " }>"); APPEND_ESTR(str, " }>");
return str.str; return str.str;
} }
case GroundType_CoreType: {
char* buf = malloc(sizeof("<CoreType>"));
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "<CoreType>");
return buf;
}
case GroundType_Undefined: {
char* buf = malloc(sizeof("<Undefined>"));
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "<Undefined>");
return buf;
}
} }

View File

@@ -291,8 +291,13 @@ char* _GroundStringifyInstruction(GroundInstruction* instruction) {
} }
APPEND_ESTR(estr, arg); APPEND_ESTR(estr, arg);
APPEND_ESTR(estr, " ");
free(arg); free(arg);
} }
if (!estr.shouldBeFreed) {
char* result = strdup(estr.str);
return result;
}
return estr.str; return estr.str;
} }

View File

@@ -16,7 +16,7 @@ char* _GroundStringifyProgram(GroundProgram* program) {
} }
APPEND_ESTR(estr, arg); APPEND_ESTR(estr, arg);
APPEND_ESTR(estr, " "); APPEND_ESTR(estr, "\n");
free(arg); free(arg);
} }

View File

@@ -50,7 +50,14 @@ char* _GroundStringifyValue(GroundValue* value) {
// TODO implement list stringification // TODO implement list stringification
} }
case GroundType_Function: { case GroundType_Function: {
// TODO implement function stringification char* buf = malloc(sizeof("<Function>"));
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "<Function>");
return buf;
} }
case GroundType_Struct: { case GroundType_Struct: {
// TODO implement struct stringification // TODO implement struct stringification
@@ -58,6 +65,26 @@ char* _GroundStringifyValue(GroundValue* value) {
case GroundType_Object: { case GroundType_Object: {
// TODO implement object stringification // TODO implement object stringification
} }
case GroundType_CoreType: {
char* buf = malloc(sizeof("<CoreType>"));
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "<CoreType>");
return buf;
}
case GroundType_Undefined: {
char* buf = malloc(sizeof("<Undefined>"));
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "<Undefined>");
return buf;
}
} }

View File

@@ -81,8 +81,21 @@ int main(int argc, char** argv) {
break; break;
} }
case ARGS_DISASSEMBLE: { case ARGS_DISASSEMBLE: {
fprintf(stderr, "Not yet implemented"); if (args.inputFile == NULL) {
fprintf(stderr, "Please specify a bytecode file\n");
return 1;
}
GroundBytecode bc = Ground.Bytecode.load(args.inputFile);
if (Ground.Flags.error) {
fprintf(stderr, "Failed to load bytecode, printing errors...\n");
Ground.Log.printErrors();
return 1;
}
char* str = Ground.Stringify.Bytecode(&bc);
printf("%s", str);
free(str);
break; break;
} }
case ARGS_HELP: { case ARGS_HELP: {

View File

@@ -121,6 +121,7 @@ char* _GroundStringifyString(GroundString* string);
char* _GroundStringifyValue(GroundValue* value); char* _GroundStringifyValue(GroundValue* value);
char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value); char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value);
char* _GroundStringifyBytecode(GroundBytecode* value);
void* _GroundFFIOpenSharedObject(char* id); void* _GroundFFIOpenSharedObject(char* id);
void* _GroundFFIGetFunction(void* handle, char* id); void* _GroundFFIGetFunction(void* handle, char* id);
@@ -274,6 +275,7 @@ struct _Ground Ground = {
.Value = _GroundStringifyValue, .Value = _GroundStringifyValue,
.BytecodeValue = _GroundStringifyBytecodeValue, .BytecodeValue = _GroundStringifyBytecodeValue,
.Bytecode= _GroundStringifyBytecode,
}, },
.FFI = { .FFI = {