/*************/ /*GEMWIRE */ /* ERYTHRO*/ /*************/ #include #include int PointerTo(int Type) { //Type = Type + 1; 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; } int ParseOptionalPointer() { int Type; // TODO: THIS IS WRONG AND SHOULD NOT EXIST // TY_CHAR is 22, RET_CHAR is 1. // Offset is 20. Rest are in order if(CurrentToken.type >= TY_CHAR && CurrentToken.type <= TY_VOID) { Type = CurrentToken.type - 23; printf("\t\tConverting a %s token to a %s type.\n", TokenNames[CurrentToken.type], TypeNames[Type]); } 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; } 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"); printf("\t\tPreparing types - RightNode of type %s must be mutated to LeftNode type %s\r\n", TypeNames[RightNode->ExprType], TypeNames[LeftNode->ExprType]); RightNode = MutateType(RightNode, LeftNode->ExprType, OP_ADD); LeftNode = ConstructASTNode(OP_ADD, Symbols[ID].Type, LeftNode, NULL, RightNode, 0); printf("\t\t\tAccessArray: Preparing LeftNode for dereference.\r\n"); LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, 0); printf("\tArray Access constructed\r\n"); return LeftNode; }