2020-09-13 01:26:49 +00:00
|
|
|
|
|
|
|
/*************/
|
|
|
|
/*GEMWIRE */
|
|
|
|
/* ERYTHRO*/
|
|
|
|
/*************/
|
|
|
|
|
|
|
|
#include <Defs.h>
|
|
|
|
#include <Data.h>
|
|
|
|
|
|
|
|
int PointerTo(int Type) {
|
2021-01-20 01:05:41 +00:00
|
|
|
if((Type & 0xf) == 0xf)
|
|
|
|
DieDecimal("Unrecognized type in pointerisation", Type);
|
|
|
|
printf("\t\tPointerising a %s\n", TypeNames(Type));
|
|
|
|
return (Type + 1);
|
2020-09-13 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ValueAt(int Type) {
|
2021-01-20 01:05:41 +00:00
|
|
|
printf("\t\tDereferencing a %s\n", TypeNames(Type));
|
2020-09-13 01:26:49 +00:00
|
|
|
//TODO: this is still bullshittery!
|
2021-01-20 01:05:41 +00:00
|
|
|
if((Type & 0xf) == 0x0)
|
|
|
|
DieDecimal("Unrecognized type in defererencing", Type);
|
|
|
|
return (Type - 1);
|
2020-09-13 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 19:22:15 +00:00
|
|
|
int ParseOptionalPointer(struct SymbolTableEntry** Composite) {
|
2020-09-13 01:26:49 +00:00
|
|
|
|
|
|
|
int Type;
|
2021-01-20 01:05:41 +00:00
|
|
|
|
|
|
|
switch(CurrentToken.type) {
|
|
|
|
case TY_VOID:
|
|
|
|
Type = RET_VOID;
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2021-01-20 01:05:41 +00:00
|
|
|
break;
|
|
|
|
case TY_CHAR:
|
|
|
|
Type = RET_CHAR;
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2021-01-20 01:05:41 +00:00
|
|
|
break;
|
|
|
|
case TY_INT:
|
|
|
|
Type = RET_INT;
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2021-01-20 01:05:41 +00:00
|
|
|
break;
|
|
|
|
case TY_LONG:
|
|
|
|
Type = RET_LONG;
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
|
|
|
break;
|
|
|
|
case KW_STRUCT:
|
|
|
|
Type = DAT_STRUCT;
|
|
|
|
*Composite = BeginStructDeclaration();
|
2021-01-20 01:05:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DieDecimal("Illegal type for pointerisation", CurrentToken.type);
|
2020-09-13 01:26:49 +00:00
|
|
|
}
|
|
|
|
// Recursively scan more *s
|
|
|
|
// This makes things like:
|
|
|
|
// x = **y;
|
|
|
|
// possible.
|
|
|
|
while(1) {
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2020-09-13 01:26:49 +00:00
|
|
|
printf("\t\t\tType on parsing is %d\n", CurrentToken.type);
|
|
|
|
if(CurrentToken.type != AR_STAR)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Type = PointerTo(Type);
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2020-09-13 01:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
2020-11-19 02:31:40 +00:00
|
|
|
struct ASTNode* AccessArray() {
|
|
|
|
struct ASTNode* LeftNode, *RightNode;
|
2021-01-20 01:05:41 +00:00
|
|
|
struct SymbolTableEntry* Entry;
|
2020-11-19 02:31:40 +00:00
|
|
|
|
|
|
|
printf("\tAccessing array %s as requested\r\n", CurrentIdentifier);
|
2021-01-20 01:05:41 +00:00
|
|
|
if ((Entry = FindSymbol(CurrentIdentifier)) == NULL || Entry->Structure != ST_ARR)
|
2020-11-19 02:31:40 +00:00
|
|
|
DieMessage("Accessing undeclared array", CurrentIdentifier);
|
|
|
|
|
2021-01-20 01:05:41 +00:00
|
|
|
LeftNode = ConstructASTLeaf(OP_ADDRESS, Entry->Type, Entry, 0);
|
2020-11-21 02:07:44 +00:00
|
|
|
//printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
|
2021-01-20 19:22:15 +00:00
|
|
|
Tokenise();
|
2020-11-21 02:07:44 +00:00
|
|
|
//printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
|
2020-11-19 02:31:40 +00:00
|
|
|
|
|
|
|
RightNode = ParsePrecedenceASTNode(0);
|
|
|
|
|
|
|
|
VerifyToken(LI_RBRAS, "]");
|
|
|
|
|
|
|
|
if(!TypeIsInt(RightNode->ExprType))
|
|
|
|
Die("Array index is not integer");
|
|
|
|
|
2021-01-20 01:05:41 +00:00
|
|
|
printf("\t\tPreparing types - RightNode of type %s must be mutated to LeftNode type %s\r\n", (RightNode->ExprType), TypeNames(LeftNode->ExprType));
|
2020-11-19 02:31:40 +00:00
|
|
|
RightNode = MutateType(RightNode, LeftNode->ExprType, OP_ADD);
|
|
|
|
|
2021-01-20 01:05:41 +00:00
|
|
|
LeftNode = ConstructASTNode(OP_ADD, Entry->Type, LeftNode, NULL, RightNode, NULL, 0);
|
2020-11-21 02:32:46 +00:00
|
|
|
printf("\tAccessArray: Preparing LeftNode for dereference.\r\n");
|
2021-01-20 01:05:41 +00:00
|
|
|
LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, NULL, 0);
|
2020-11-21 02:07:44 +00:00
|
|
|
printf("\tArray Access constructed\r\n");
|
2020-11-19 02:31:40 +00:00
|
|
|
return LeftNode;
|
|
|
|
}
|