Struct field access (slightly buggy)

This commit is contained in:
2026-04-10 10:14:23 +10:00
parent 5841a7a999
commit fd08b7cdb7
3 changed files with 41 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
#include <stdlib.h>
#include "../lexer/lexer.h"
#include "../lexer/SolsType.h"
#include "../parser/parser.h"
#include "../parser/SolsNode.h"
#include "../include/estr.h"
@@ -249,6 +250,16 @@ ResultType(SolsType, charptr) getNodeType(SolsNode* node, SolsScope* scope) {
type.type = STT_OBJECT;
return Success(SolsType, charptr, type);
}
case SNT_DOT: {
ResultType(SolsType, charptr) type = getNodeType(&node->children.at[0], scope);
if (type.error) {
return Error(SolsType, charptr, type.as.error);
}
if (type.as.success.type != STT_OBJECT) {
return Error(SolsType, charptr, "Cannot use dot operator on non-object");
}
return findStructMemberType(&type.as.success, node->children.at[1].as.idName);
}
}
return Error(SolsType, charptr, "Not yet implemented");
}
@@ -1074,6 +1085,23 @@ ResultType(GroundProgram, charptr) generateNewNode(SolsNode* node, SolsScope* sc
return Success(GroundProgram, charptr, program);
}
ResultType(GroundProgram, charptr) generateDotNode(SolsNode* node, SolsScope* scope) {
GroundProgram program = groundCreateProgram();
GroundInstruction inst = groundCreateInstruction(GETFIELD);
groundAddReferenceToInstruction(&inst, node->children.at[0].accessArg);
groundAddReferenceToInstruction(&inst, groundCreateReference(DIRREF, node->children.at[1].as.idName));
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_DOT_%zu", scope->tmpCounter++);
node->accessArg = groundCreateReference(VALREF, tmpId);
groundAddReferenceToInstruction(&inst, groundCreateReference(DIRREF, tmpId));
groundAddInstructionToProgram(&program, inst);
return Success(GroundProgram, charptr, program);
}
ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope) {
@@ -1131,6 +1159,7 @@ ResultType(GroundProgram, charptr) generateCode(SolsNode* node, SolsScope* scope
case SNT_GROUND: generate(InlineGround);
case SNT_STRUCT: generate(Struct);
case SNT_NEW: generate(New);
case SNT_DOT: generate(Dot);
}
return Success(GroundProgram, charptr, program);
}