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
|
|
|
}
|
|
|
|
|
2020-09-13 22:41:46 +00:00
|
|
|
int ParseOptionalPointer() {
|
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;
|
|
|
|
break;
|
|
|
|
case TY_CHAR:
|
|
|
|
Type = RET_CHAR;
|
|
|
|
break;
|
|
|
|
case TY_INT:
|
|
|
|
Type = RET_INT;
|
|
|
|
break;
|
|
|
|
case TY_LONG:
|
|
|
|
Type = RET_LONG;
|
|
|
|
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) {
|
|
|
|
Tokenise(&CurrentToken);
|
|
|
|
printf("\t\t\tType on parsing is %d\n", CurrentToken.type);
|
|
|
|
if(CurrentToken.type != AR_STAR)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Type = PointerTo(Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
2020-11-19 02:31:40 +00:00
|
|
|
Tokenise(&CurrentToken);
|
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;
|
|
|
|
}
|