Implement (broken) struct member access
This commit is contained in:
parent
2bdbe6e6c0
commit
8e45ea5eef
|
@ -77,6 +77,8 @@ enum TokenTypes {
|
|||
LI_RPARE, // )
|
||||
|
||||
LI_COM, // ,
|
||||
LI_DOT, // .
|
||||
LI_ARROW, // ->
|
||||
|
||||
TY_IDENTIFIER, // Identifier name. Variable, function, etc.
|
||||
TY_NONE, // No return type. Literal void.
|
||||
|
@ -254,9 +256,9 @@ enum DataTypes {
|
|||
enum StructureType {
|
||||
ST_VAR, // This is variable
|
||||
ST_FUNC, // This is a function
|
||||
ST_ARR // This is an array
|
||||
ST_ARR, // This is an array
|
||||
ST_RUCT // This is a struct
|
||||
// This is an enum
|
||||
// This is a struct
|
||||
// This is a typedef
|
||||
};
|
||||
|
||||
|
@ -342,6 +344,7 @@ int ValueAt(int Type);
|
|||
int PointerTo(int Type);
|
||||
|
||||
struct ASTNode* AccessArray();
|
||||
struct ASTNode* AccessMember(bool Deref);
|
||||
|
||||
int ParseTokenToOperation(int Token);
|
||||
|
||||
|
|
|
@ -377,6 +377,10 @@ void Tokenise() {
|
|||
Token->type = LI_EOF;
|
||||
return;
|
||||
|
||||
case '.':
|
||||
Token->type = LI_DOT;
|
||||
return;
|
||||
|
||||
case '+':
|
||||
// + can be either "+" or "++".
|
||||
Char = NextChar();
|
||||
|
@ -389,10 +393,12 @@ void Tokenise() {
|
|||
break;
|
||||
|
||||
case '-':
|
||||
// - can be either "-" or "--"
|
||||
// - can be either "-" or "--" or "->"
|
||||
Char = NextChar();
|
||||
if(Char == '-') {
|
||||
Token->type = PPMM_MINUS;
|
||||
} else if(Char == '>') {
|
||||
Token->type = LI_ARROW;
|
||||
} else {
|
||||
Token->type = AR_MINUS;
|
||||
ReturnCharToStream(Char);
|
||||
|
|
|
@ -58,6 +58,8 @@ char* TokenNames[] = {
|
|||
"Logical Block End",
|
||||
|
||||
"Comma",
|
||||
"Dot",
|
||||
"Arrow",
|
||||
|
||||
"Identifier",
|
||||
"None Type",
|
||||
|
|
|
@ -153,5 +153,57 @@ struct ASTNode* AccessArray() {
|
|||
printf("\tAccessArray: Preparing LeftNode for dereference.\r\n");
|
||||
LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, NULL, 0);
|
||||
printf("\tArray Access constructed\r\n");
|
||||
return LeftNode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Members of enums and structs are accessed with x.y or *x->y
|
||||
*
|
||||
* x must be enum, struct, or pointer to struct.
|
||||
* y must be a valid member of any of the above.
|
||||
*
|
||||
* It is a wrapper around *((imax*)x + xType.ordinal(y))
|
||||
*
|
||||
* @return the AST Node representing this statement.
|
||||
*/
|
||||
struct ASTNode* AccessMember(bool Deref) {
|
||||
struct ASTNode* LeftNode, *RightNode;
|
||||
struct SymbolTableEntry* CompositeVar, *TypePtr, *Member;
|
||||
|
||||
|
||||
if((CompositeVar = FindSymbol(CurrentIdentifier)) == NULL)
|
||||
DieMessage("Undecalred variable", CurrentIdentifier);
|
||||
if(Deref && CompositeVar->Type != PointerTo(DAT_STRUCT))
|
||||
DieMessage("Undeclared struct", CurrentIdentifier);
|
||||
if(!Deref && CompositeVar->Type != DAT_STRUCT)
|
||||
DieMessage("Undeclared struct", CurrentIdentifier);
|
||||
|
||||
if(Deref)
|
||||
LeftNode = ConstructASTLeaf(REF_IDENT, PointerTo(DAT_STRUCT), CompositeVar, 0);
|
||||
else
|
||||
LeftNode = ConstructASTLeaf(OP_ADDRESS, CompositeVar->Type, CompositeVar, 0);
|
||||
|
||||
LeftNode->RVal = true;
|
||||
|
||||
TypePtr = CompositeVar->CompositeType;
|
||||
|
||||
Tokenise();
|
||||
VerifyToken(TY_IDENTIFIER, "identifier");
|
||||
|
||||
for(Member = TypePtr->Start; Member != NULL, Member = Member->NextSymbol;) {
|
||||
printf("\tComparing struct entry %s with the wanted %s. Index %d.\r\n", Member->Name, CurrentIdentifier, Member->SinkOffset);
|
||||
if(!strcmp(Member->Name, CurrentIdentifier))
|
||||
break;
|
||||
}
|
||||
|
||||
if(Member == NULL)
|
||||
DieMessage("Invalid struct member", CurrentIdentifier);
|
||||
|
||||
RightNode = ConstructASTLeaf(TERM_INTLITERAL, RET_INT, NULL, Member->SinkOffset);
|
||||
|
||||
LeftNode = ConstructASTNode(OP_ADD, PointerTo(Member->Type), LeftNode, NULL, RightNode, NULL, 0);
|
||||
LeftNode = ConstructASTBranch(OP_DEREF, Member->Type, LeftNode, Member, 0);
|
||||
|
||||
return LeftNode;
|
||||
}
|
|
@ -103,6 +103,7 @@ struct SymbolTableEntry* BeginStructDeclaration() {
|
|||
StructMembers = StructMembersEnd = NULL;
|
||||
|
||||
Member = Composite->Start;
|
||||
printf("\tSetting first entry in struct to %s\r\n", Member->Name);
|
||||
Member->SinkOffset = 0;
|
||||
Offset = TypeSize(Member->Type, Member->CompositeType);
|
||||
|
||||
|
@ -516,6 +517,10 @@ struct ASTNode* PostfixStatement() {
|
|||
// Here we check for postincrement and postdecrement.
|
||||
|
||||
switch(CurrentToken.type) {
|
||||
case LI_DOT:
|
||||
return AccessMember(false);
|
||||
case LI_ARROW:
|
||||
return AccessMember(true);
|
||||
case PPMM_PLUS:
|
||||
Tokenise();
|
||||
Tree = ConstructASTLeaf(OP_POSTINC, Entry->Type, Entry, 0);
|
||||
|
|
Loading…
Reference in New Issue
Block a user