2020-09-13 01:26:49 +00:00
/*************/
/*GEMWIRE */
/* ERYTHRO*/
/*************/
# include <Defs.h>
# include <Data.h>
int PointerTo ( int Type ) {
2020-11-21 02:07:44 +00:00
//Type = Type + 1;
2020-09-13 01:26:49 +00:00
printf ( " \t \t Pointerising 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 ) {
2020-11-23 20:01:36 +00:00
return Type + ( PTR_CHAR - RET_CHAR ) ;
2020-09-13 01:26:49 +00:00
} else {
DieDecimal ( " Unable to create a pointer to the desired type " , Type ) ;
}
return - 1 ;
}
int ValueAt ( int Type ) {
printf ( " \t \t Dereferencing a %s \n " , TypeNames [ Type ] ) ;
//TODO: this is still bullshittery!
if ( Type > = PTR_CHAR & & Type < = PTR_VOID ) {
2020-11-21 02:32:46 +00:00
printf ( " \t \t \t Dereference of %s is %s. \r \n " , TypeNames [ Type ] , TypeNames [ Type - 4 ] ) ;
2020-09-13 01:26:49 +00:00
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
if ( CurrentToken . type > = TY_CHAR & & CurrentToken . type < = TY_VOID ) {
2020-11-23 20:01:36 +00:00
Type = CurrentToken . type - ( TY_CHAR - RET_CHAR ) ;
2020-11-21 02:07:44 +00:00
printf ( " \t \t Converting a %s token to a %s type. \n " , TokenNames [ CurrentToken . type ] , TypeNames [ 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 \t Type 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 ( " \t Accessing 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 ) ;
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 " ) ;
2020-11-21 02:07:44 +00:00
printf ( " \t \t Preparing types - RightNode of type %s must be mutated to LeftNode type %s \r \n " , TypeNames [ RightNode - > ExprType ] , TypeNames [ LeftNode - > ExprType ] ) ;
2020-11-19 02:31:40 +00:00
RightNode = MutateType ( RightNode , LeftNode - > ExprType , OP_ADD ) ;
LeftNode = ConstructASTNode ( OP_ADD , Symbols [ ID ] . Type , LeftNode , NULL , RightNode , 0 ) ;
2020-11-21 02:32:46 +00:00
printf ( " \t AccessArray: Preparing LeftNode for dereference. \r \n " ) ;
2020-11-19 02:31:40 +00:00
LeftNode = ConstructASTBranch ( OP_DEREF , ValueAt ( LeftNode - > ExprType ) , LeftNode , 0 ) ;
2020-11-21 02:07:44 +00:00
printf ( " \t Array Access constructed \r \n " ) ;
2020-11-19 02:31:40 +00:00
return LeftNode ;
}