Compare commits

5 Commits

Author SHA1 Message Date
1570177de1 Fix type conversions 2026-01-24 16:36:37 +11:00
2da11f854d Struct and object printing 2026-01-24 16:23:01 +11:00
337c6ada24 Actually fix the bug 2026-01-24 15:46:09 +11:00
bb0baba167 Fix bug when defining functions and structs 2026-01-24 15:05:05 +11:00
ca0b87aacd Update docs 2026-01-23 15:30:12 +11:00
5 changed files with 84 additions and 22 deletions

View File

@@ -75,7 +75,7 @@ build
- [x] String operations
- [x] Maths
- [x] Comparisions
- [ ] Type conversions
- [x] Type conversions
- [x] Functions
- [x] Define functions
- [x] Call functions
@@ -83,7 +83,7 @@ build
- [x] Arguments for functions
- [x] Jumping within functions
- [x] Custom data structures
- [ ] Working with external libraries
- [x] Working with external libraries
## Debugger

View File

@@ -362,3 +362,23 @@ Looks in the path $GROUND_LIBS/`$libraryName`.grnd for the library. ($GROUND_LIB
Attempts to import a shared library written in a compiled language like C or C++ for usage within the current program.
Looks in the path $GROUND_LIBS/`$libraryName`.so for the library. ($GROUND_LIBS is a system environment variable.)
### Data Structures
#### struct -structName
Creates a new struct which can be initialised. Until the endstruct keyword, the only valid instructions are init, set, fun, endfun, struct, and endstruct.
Any value created inside the struct will be added to the struct.
#### endstruct
Ends the creation of a struct.
#### getfield $object &fieldName &outputVar
Gets a field from an initialised object. fieldName must be a valid name of a field in the object. Errors if the field does not exist.
#### setfield &object &fieldName $value
Sets a field to a new value in the object. The value must be of the same type as the field's old value.

View File

@@ -443,6 +443,7 @@ GroundStruct parseStruct(GroundProgram* in, GroundScope* scope, size_t errorOffs
addInstructionToProgram(&gp, in->instructions[i]);
i++;
}
i--;
GroundFunction* function = parseFunction(&gp, errorOffset);
function->startLine = i;
@@ -509,6 +510,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
addInstructionToProgram(&gp, in->instructions[i]);
i++;
}
i--;
GroundFunction* function = parseFunction(&gp, errorOffset);
function->startLine = i;
@@ -546,6 +548,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
addInstructionToProgram(&gp, in->instructions[i]);
i++;
}
i--;
GroundValue gv = {
.type = STRUCTVAL,
@@ -1104,7 +1107,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.args[1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
}
addVariable(scope->variables, in->args.args[2].value.refName, createIntGroundValue(atoll(in->args.args[0].value.value.data.stringVal)));
addVariable(scope->variables, in->args.args[1].value.refName, createIntGroundValue(atoll(in->args.args[0].value.value.data.stringVal)));
break;
}
case STOD: {
@@ -1120,7 +1123,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (in->args.args[1].type != DIRREF) {
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
}
addVariable(scope->variables, in->args.args[1].value.refName, createIntGroundValue(atof(in->args.args[0].value.value.data.stringVal)));
addVariable(scope->variables, in->args.args[1].value.refName, createDoubleGroundValue(atof(in->args.args[0].value.value.data.stringVal)));
break;
}
case TOSTRING: {
@@ -1137,57 +1140,53 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
runtimeError(ARG_TYPE_MISMATCH, "Expecting a DirectRef for arg 2", in, currentInstruction);
}
GroundValue* value = &in->args.args[0].value.value;
char buf[256];
switch (value->type) {
case INT: {
char* buf = malloc(sizeof(char) * 256);
snprintf(buf, sizeof(char) * 256, "%" PRId64, value->data.intVal);
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
snprintf(buf, sizeof(buf) * 256, "%" PRId64, value->data.intVal);
break;
}
case DOUBLE: {
char* buf = malloc(sizeof(char) * 256);
snprintf(buf, sizeof(char) * 256, "%f", value->data.doubleVal);
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
snprintf(buf, sizeof(buf) * 256, "%f", value->data.doubleVal);
break;
}
case STRING: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(value->data.stringVal));
snprintf(buf, sizeof(buf), "%s", value->data.stringVal);
break;
}
case CHAR: {
char* buf = malloc(sizeof(char) * 2);
buf[0] = value->data.charVal;
buf[1] = '\0';
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
break;
}
case BOOL: {
if (value->data.boolVal) {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("true"));
snprintf(buf, sizeof(buf), "true");
} else {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("false"));
snprintf(buf, sizeof(buf), "false");
}
break;
}
case LIST: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<list>"));
snprintf(buf, sizeof(buf), "<list>");
break;
}
case FUNCTION: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<function>"));
snprintf(buf, sizeof(buf), "<function>");
break;
}
case CUSTOM: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<custom>"));
snprintf(buf, sizeof(buf), "<custom>");
break;
}
case NONE:
default: {
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("<default>"));
snprintf(buf, sizeof(buf), "<default>");
break;
}
}
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue(buf));
break;
}

View File

@@ -149,8 +149,47 @@ void printGroundValue(GroundValue* gv) {
printf("<function>");
break;
}
default: {
printf("FIXME");
case STRUCTVAL: {
printf("<struct fields: { ");
for (size_t i = 0; i < gv->data.structVal->size; i++) {
if (i != 0) {
printf(", ");
}
printf("%s: ", gv->data.structVal->fields[i].id);
if (gv->data.structVal->fields[i].value.type == STRING) {
printf("\"");
printGroundValue(&gv->data.structVal->fields[i].value);
printf("\"");
} else {
printGroundValue(&gv->data.structVal->fields[i].value);
}
}
printf(" }>");
break;
}
case CUSTOM: {
printf("<object fields: { ");
for (size_t i = 0; i < gv->customType->size; i++) {
if (i != 0) {
printf(", ");
}
printf("%s: ", gv->customType->fields[i].id);
GroundObjectField* field = findField(*gv->data.customVal, gv->customType->fields[i].id);
if (field == NULL) {
printf("<missing>");
} else {
printGroundValue(&field->value);
}
}
printf(" }>");
break;
}
case ERROR: {
printf("<error type: %s, what: %s>", gv->data.errorVal.type, gv->data.errorVal.what);
break;
}
case NONE: {
printf("<none>");
break;
}
}

View File

@@ -1,3 +1,7 @@
tostring 32 &int
tostring 32 &str
stoi "12" &int
stod "3.14" &dou
println $str
println $int
println $dou