2020-09-13 01:26:49 +00:00
|
|
|
|
|
|
|
/*************/
|
|
|
|
/*GEMWIRE */
|
|
|
|
/* ERYTHRO*/
|
|
|
|
/*************/
|
|
|
|
|
|
|
|
#include <Defs.h>
|
|
|
|
#include <Data.h>
|
|
|
|
|
|
|
|
int PointerTo(int Type) {
|
|
|
|
|
|
|
|
printf("\t\tPointerising a %s\n", TypeNames[Type]);
|
|
|
|
// As it stands, the conversion between
|
|
|
|
// RET and PTR is +4.
|
|
|
|
// TODO: if we add types, increase this number
|
|
|
|
// TODO: Make this a proper translation table
|
|
|
|
// TODO: More checks! This can go wrong easily!
|
|
|
|
if(Type >= RET_CHAR && Type <= RET_VOID) {
|
|
|
|
return Type + 4;
|
|
|
|
} else {
|
|
|
|
DieDecimal("Unable to create a pointer to the desired type", Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ValueAt(int Type) {
|
|
|
|
|
|
|
|
printf("\t\tDereferencing a %s\n", TypeNames[Type]);
|
|
|
|
//TODO: this is still bullshittery!
|
|
|
|
if(Type >= PTR_CHAR && Type <= PTR_VOID) {
|
|
|
|
return Type - 4;
|
|
|
|
} else {
|
|
|
|
DieDecimal("Unable to dereference type", Type);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:41:46 +00:00
|
|
|
int ParseOptionalPointer() {
|
2020-09-13 01:26:49 +00:00
|
|
|
|
|
|
|
int Type;
|
|
|
|
// TODO: THIS IS WRONG AND SHOULD NOT EXIST
|
2020-09-13 22:41:46 +00:00
|
|
|
// TY_CHAR is 22, RET_CHAR is 1.
|
2020-09-13 01:26:49 +00:00
|
|
|
// Offset is 20. Rest are in order
|
|
|
|
|
|
|
|
if(CurrentToken.type >= TY_CHAR && CurrentToken.type <= TY_VOID) {
|
2020-09-13 22:41:46 +00:00
|
|
|
Type = CurrentToken.type - 21;
|
|
|
|
printf("\t\tConverting a %d type to a %d type.\n", CurrentToken.type, Type);
|
2020-09-13 01:26:49 +00:00
|
|
|
} else {
|
|
|
|
DieDecimal("Illegal type for pointerisation", CurrentToken.type);
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
int ID;
|
|
|
|
|
|
|
|
printf("\tAccessing array %s as requested\r\n", CurrentIdentifier);
|
|
|
|
if ((ID = FindSymbol(CurrentIdentifier)) == -1 || Symbols[ID].Structure != ST_ARR)
|
|
|
|
DieMessage("Accessing undeclared array", CurrentIdentifier);
|
|
|
|
|
|
|
|
LeftNode = ConstructASTLeaf(OP_ADDRESS, Symbols[ID].Type, ID);
|
|
|
|
printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
|
|
|
|
Tokenise(&CurrentToken);
|
|
|
|
printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
|
|
|
|
|
|
|
|
RightNode = ParsePrecedenceASTNode(0);
|
|
|
|
|
|
|
|
VerifyToken(LI_RBRAS, "]");
|
|
|
|
|
|
|
|
if(!TypeIsInt(RightNode->ExprType))
|
|
|
|
Die("Array index is not integer");
|
|
|
|
|
|
|
|
RightNode = MutateType(RightNode, LeftNode->ExprType, OP_ADD);
|
|
|
|
|
|
|
|
LeftNode = ConstructASTNode(OP_ADD, Symbols[ID].Type, LeftNode, NULL, RightNode, 0);
|
|
|
|
LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, 0);
|
|
|
|
|
|
|
|
return LeftNode;
|
|
|
|
}
|