'new' keyword

This commit is contained in:
2026-04-09 19:00:40 +10:00
parent 9b55b509f5
commit a2fc138ba1
5 changed files with 129 additions and 16 deletions

View File

@@ -10,10 +10,37 @@ ResultType(SolsType, charptr) createSolsType(SolsTypeType in) {
if (ptr == NULL) {
return Error(SolsType, charptr, "Couldn't allocate memory (in createSolsType() function)");
}
SolsType type = { .type = in, .children.capacity = 32, .children.at = ptr };
SolsType type = {
.type = in,
.identifierType = NULL,
.returnType = NULL,
.children.capacity = 32,
.children.count = 0,
.children.at = ptr,
.typeIsKnown = true,
.needsGroundStruct = false
};
return Success(SolsType, charptr, type);
}
ResultType(SolsType, charptr) createIdentifiedSolsType(char* in) {
char* copy = malloc(strlen(in) + 1);
if (copy == NULL) {
return Error(SolsType, charptr, "Couldn't allocate memory (in createIdentifiedSolsType() function)");
}
strcpy(copy, in);
return Success(SolsType, charptr, ((SolsType) {
.type = STT_UNKNOWN,
.identifierType = copy,
.returnType = NULL,
.children.capacity = 0,
.children.count = 0,
.children.at = NULL,
.typeIsKnown = false,
.needsGroundStruct = false
}));
}
ResultType(SolsType, charptr) copySolsType(SolsType* type) {
SolsType ret = { .type = type->type, .children.count = type->children.count, .children.capacity = type->children.capacity};
@@ -144,7 +171,7 @@ bool compareTypes(SolsType* left, SolsType* right) {
}
}
ResultType(GroundArg, charptr) createGroundArgFromSolsType(SolsType* type) {
ResultType(GroundArg, charptr) createGroundArgFromSolsType(SolsType* type, struct SolsScope* scope) {
switch (type->type) {
case STT_INT: {
return Success(GroundArg, charptr, groundCreateReference(TYPEREF, "int"));
@@ -168,8 +195,20 @@ ResultType(GroundArg, charptr) createGroundArgFromSolsType(SolsType* type) {
return Success(GroundArg, charptr, groundCreateReference(TYPEREF, "struct"));
}
case STT_OBJECT: {
// FIXME Do this later
return Error(GroundArg, charptr, "FIXME");
if (!type->needsGroundStruct) {
return Success(GroundArg, charptr, groundCreateReference(TYPEREF, type->identifierType));
} else {
// FIXME do this later
return Error(GroundArg, charptr, "Anonymous structs are not supported yet");
}
}
case STT_UNKNOWN: {
if (!type->needsGroundStruct) {
return Success(GroundArg, charptr, groundCreateReference(TYPEREF, type->identifierType));
} else {
// FIXME do this later
return Error(GroundArg, charptr, "Anonymous structs are not supported yet");
}
}
}
return Error(GroundArg, charptr, "How did we get here?");

View File

@@ -8,7 +8,7 @@
#include "../include/nothing.h"
typedef enum SolsTypeType {
STT_INT, STT_STRING, STT_DOUBLE, STT_BOOL, STT_CHAR, STT_FUN, STT_TEMPLATE, STT_OBJECT
STT_INT, STT_STRING, STT_DOUBLE, STT_BOOL, STT_CHAR, STT_FUN, STT_TEMPLATE, STT_OBJECT, STT_UNKNOWN
} SolsTypeType;
// Definition of charptr for Result() and ResultType() macros
@@ -53,6 +53,12 @@ typedef struct SolsType {
// For use when type is identified with a name
char* identifierType;
// If type is identified with a name, record whether we have found the actual type
bool typeIsKnown;
// If using anonymous struct, record whether we need to generate a Ground struct
bool needsGroundStruct;
// For use in functions
struct SolsType* returnType;
@@ -80,6 +86,10 @@ Result(SolsType, charptr);
// Failure: char* detailing what went wrong (usually memory failure)
ResultType(SolsType, charptr) createSolsType(SolsTypeType in);
// Creates a SolsType which is identified by a name.
// The type details are not known yet, so the type is marked as unknown.
ResultType(SolsType, charptr) createIdentifiedSolsType(char* in);
Result(Nothing, charptr);
// Adds a child SolsType to a given SolsType.
@@ -93,8 +103,10 @@ ResultType(SolsType, charptr) copySolsType(SolsType* type);
Result(GroundArg, charptr);
struct SolsScope;
// Represents a SolsType as a GroundArg (in typeref form)
ResultType(GroundArg, charptr) createGroundArgFromSolsType(SolsType* type);
ResultType(GroundArg, charptr) createGroundArgFromSolsType(SolsType* type, struct SolsScope* scope);
// Frees a SolsType
void freeSolsType(SolsType* type);

View File

@@ -16,6 +16,7 @@ struct _SolsTokenTypeMap SolsTokenTypeMap[] = {
{"use", STT_KW_USE},
{"struct", STT_KW_STRUCT},
{"ground", STT_KW_GROUND},
{"new", STT_KW_NEW},
{"{", STT_OPEN_CURLY},
{"}", STT_CLOSE_CURLY},
{"(", STT_OPEN_PAREN},