'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

@@ -41,7 +41,8 @@ ResultType(SolsType, charptr) getNodeType(SolsNode* node, SolsScope* scope) {
case SNT_PUTS:
case SNT_IF:
case SNT_WHILE:
case SNT_CODE_BLOCK:
case SNT_CODE_BLOCK:
case SNT_STRUCT:
case SNT_RETURN: {
return Error(SolsType, charptr, "Specified node does not return data");
}
@@ -229,6 +230,25 @@ ResultType(SolsType, charptr) getNodeType(SolsNode* node, SolsScope* scope) {
case SNT_EXPR_IN_PAREN: {
return getNodeType(&node->children.at[node->children.count - 1], scope);
}
case SNT_NEW: {
SolsType type;
if (node->as.type.typeIsKnown) {
type = node->as.type;
} else {
SolsVariable* var = findSolsVariable(scope, node->as.type.identifierType);
if (var == NULL) {
Estr estr = CREATE_ESTR("Unable to find variable ");
APPEND_ESTR(estr, node->as.type.identifierType);
return Error(SolsType, charptr, estr.str);
}
type = var->typeinfo;
}
if (type.type == STT_OBJECT) {
return Error(SolsType, charptr, "Cannot use initialized type on new");
}
type.type = STT_OBJECT;
return Success(SolsType, charptr, type);
}
}
return Error(SolsType, charptr, "Not yet implemented");
}
@@ -724,14 +744,14 @@ ResultType(GroundProgram, charptr) generateLambdaNode(SolsNode* node, SolsScope*
node->accessArg = groundCreateReference(VALREF, lambdaId);
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, lambdaId));
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType);
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType, scope);
if (arg.error) {
return Error(GroundProgram, charptr, arg.as.error);
}
groundAddReferenceToInstruction(&signature, arg.as.success);
for (size_t i = 0; i < node->as.type.children.count; i++) {
// Add type
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(&node->as.type.children.at[i].type);
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(&node->as.type.children.at[i].type, scope);
if (arg.error) {
return Error(GroundProgram, charptr, arg.as.error);
}
@@ -793,14 +813,14 @@ ResultType(GroundProgram, charptr) generateDefNode(SolsNode* node, SolsScope* sc
node->accessArg = groundCreateReference(VALREF, fnName);
groundAddReferenceToInstruction(&signature, groundCreateReference(FNREF, fnName));
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType);
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(node->as.type.returnType, scope);
if (arg.error) {
return Error(GroundProgram, charptr, arg.as.error);
}
groundAddReferenceToInstruction(&signature, arg.as.success);
for (size_t i = 0; i < node->as.type.children.count; i++) {
// Add type
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(&node->as.type.children.at[i].type);
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(&node->as.type.children.at[i].type, scope);
if (arg.error) {
return Error(GroundProgram, charptr, arg.as.error);
}
@@ -1035,13 +1055,34 @@ ResultType(GroundProgram, charptr) generateStructNode(SolsNode* node, SolsScope*
return Success(GroundProgram, charptr, constants);
}
ResultType(GroundProgram, charptr) generateNewNode(SolsNode* node, SolsScope* scope) {
GroundProgram program = groundCreateProgram();
GroundInstruction inst = groundCreateInstruction(INIT);
ResultType(GroundArg, charptr) arg = createGroundArgFromSolsType(&node->as.type, scope);
if (arg.error) {
return Error(GroundProgram, charptr, arg.as.error);
}
char* tmpId = malloc(sizeof(char) * 64);
if (tmpId == NULL) {
return Error(GroundProgram, charptr, "Failed to allocate memory for tmp identifier");
}
snprintf(tmpId, 64, "__SOLS_TMP_NEW_%zu", scope->tmpCounter++);
node->accessArg = groundCreateReference(VALREF, tmpId);
groundAddReferenceToInstruction(&inst, groundCreateReference(DIRREF, tmpId));
groundAddReferenceToInstruction(&inst, arg.as.success);
groundAddInstructionToProgram(&program, inst);
return Success(GroundProgram, charptr, program);
}
ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope) {
GroundProgram program = groundCreateProgram();
SolsScope backupScope = {NULL, 0};
if (node->type != SNT_IF && node->type != SNT_WHILE && node->type != SNT_LAMBDA && node->type != SNT_DEF && node->type != SNT_STRUCT) {
if (node->type != SNT_IF && node->type != SNT_WHILE && node->type != SNT_LAMBDA &&
node->type != SNT_DEF && node->type != SNT_STRUCT && node->type != SNT_NEW) {
if (node->type == SNT_CODE_BLOCK) {
backupScope = *scope;
SolsScope newScope = copySolsScope(scope);
@@ -1089,6 +1130,7 @@ ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope
case SNT_USE: generate(Use);
case SNT_GROUND: generate(InlineGround);
case SNT_STRUCT: generate(Struct);
case SNT_NEW: generate(New);
}
return Success(GroundProgram, charptr, program);
}