Add stuff

This commit is contained in:
2026-02-26 11:46:29 +11:00
parent 1bc4b7fa62
commit f5068fe861
5 changed files with 133 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ typedef struct SolsVariable {
typedef struct SolsScope {
SolsVariable* variables;
size_t tmpCounter;
} SolsScope;
// Adds a variable to the SolsScope.

View File

@@ -30,6 +30,40 @@ ResultType(SolsType, charptr) getNodeType(SolsNode* node, SolsScope* scope) {
}
return Success(SolsType, charptr, type.as.success);
}
case SNT_OP_ADD: {
if (node->children.count < 2) {
return Error(SolsType, charptr, "Not enough children to determine type");
}
ResultType(SolsType, charptr) leftType = getNodeType(&node->children.at[0], scope);
if (leftType.error) {
return Error(SolsType, charptr, leftType.as.error);
}
ResultType(SolsType, charptr) rightType = getNodeType(&node->children.at[1], scope);
if (rightType.error) {
return Error(SolsType, charptr, rightType.as.error);
}
if (!(leftType.as.success.type == STT_INT || leftType.as.success.type == STT_DOUBLE || leftType.as.success.type == STT_STRING)) {
return Error(SolsType, charptr, "Cannot add left type");
}
if (!(rightType.as.success.type == STT_INT || rightType.as.success.type == STT_DOUBLE || rightType.as.success.type == STT_STRING)) {
return Error(SolsType, charptr, "Cannot add right type");
}
if (leftType.as.success.type == rightType.as.success.type) {
return Success(SolsType, charptr, leftType.as.success);
}
if ((leftType.as.success.type == STT_INT && rightType.as.success.type == STT_DOUBLE) || (leftType.as.success.type == STT_DOUBLE && rightType.as.success.type == STT_INT)) {
ResultType(SolsType, charptr) type = createSolsType(STT_DOUBLE);
if (type.error) {
return Error(SolsType, charptr, type.as.error);
}
return Success(SolsType, charptr, type.as.success);
}
return Error(SolsType, charptr, "Cannot add these types together");
}
case SNT_LITERAL: {
switch (node->as.literal.type) {
case SLT_INT: {
@@ -115,6 +149,36 @@ static inline ResultType(GroundProgram, charptr) generateSetNode(SolsNode* node,
return Success(GroundProgram, charptr, gp);
}
static inline ResultType(GroundProgram, charptr) generateAddNode(SolsNode* node, SolsScope* scope) {
if (node->children.count < 2) {
return Error(GroundProgram, charptr, "add requires arguments");
}
// Use this function for type checking
ResultType(SolsType, charptr) type = getNodeType(node, scope);
if (type.error) {
return Error(GroundProgram, charptr, type.as.error);
}
GroundProgram gp = groundCreateProgram();
GroundInstruction add = groundCreateInstruction(ADD);
groundAddReferenceToInstruction(&add, node->children.at[0].accessArg);
groundAddReferenceToInstruction(&add, node->children.at[1].accessArg);
char* tmpId = malloc(sizeof(char) * 64);
if (tmpId == NULL) {
return Error(GroundProgram, charptr, "Failed to allocate memory for temporary identifier in add");
}
snprintf(tmpId, 64, "__SOLS_TMP_ADD_%zu", scope->tmpCounter++);
groundAddReferenceToInstruction(&add, groundCreateReference(DIRREF, tmpId));
node->accessArg = groundCreateReference(VALREF, tmpId);
groundAddInstructionToProgram(&gp, add);
return Success(GroundProgram, charptr, gp);
}
ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope) {
GroundProgram program = groundCreateProgram();
@@ -135,6 +199,7 @@ ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope
case SNT_PUTS: generate(Puts);
case SNT_LITERAL: generate(Literal);
case SNT_OP_SET: generate(Set);
case SNT_OP_ADD: generate(Add);
}
return Success(GroundProgram, charptr, program);
}