More work on arrays. Fixed some bugs.

Still need to figure out why AsStrDeref isn't working.
This commit is contained in:
Curle 2020-11-21 02:07:44 +00:00
parent 999f8dc267
commit 83959b4793
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
7 changed files with 41 additions and 29 deletions

View File

@ -86,7 +86,7 @@ void DumpTree(struct ASTNode* node, int level) {
case OP_CALL: fprintf(stdout, "OP_CALL %s\n", Symbols[node->Value.ID].Name); return;
case OP_ADDRESS: fprintf(stdout, "OP_ADDRESS %s\n", Symbols[node->Value.ID].Name); return;
case OP_DEREF: fprintf(stdout, "OP_DEREF\n"); return;
case OP_SCALE: fprintf(stdout, "OP_SCALE %d\n", Symbols[node->Value.Size]); return;
case OP_SCALE: fprintf(stdout, "OP_SCALE %s\n", TypeNames[node->Value.Size]); return;
default:
DieDecimal("Unknown Dump Operator", node->Operation);

View File

@ -165,13 +165,13 @@ struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
LeftNode = PrefixStatement();
NodeType = CurrentToken.type;
printf("%d\r\n", CurrentToken.type);
//printf("%d\r\n", CurrentToken.type);
if(NodeType == LI_SEMIC || NodeType == LI_RPARE || NodeType == LI_RBRAS) {
printf("Current token matches ; ) ]\r\n");
//printf("Current token matches ; ) ]\r\n");
LeftNode->RVal = 1; return LeftNode;
}
printf("Current token has value %d, type %s\n", CurrentToken.value, TokenNames[CurrentToken.type]);
//printf("Current token has value %d, type %s\n", CurrentToken.value, TokenNames[CurrentToken.type]);
while((OperatorPrecedence(NodeType) > PreviousTokenPrecedence) || (IsRightExpr(OpType) && OperatorPrecedence(OpType) == PreviousTokenPrecedence)) {
//printf("inside while\n");
Tokenise(&CurrentToken);
@ -190,27 +190,37 @@ struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
OpType = ParseTokenToOperation(NodeType);
if(OpType == OP_ASSIGN) {
printf("\tParsePrecedenceASTNode: Assignment statement\r\n");
RightNode->RVal = 1;
RightNode = MutateType(RightNode, LeftNode->ExprType, 0);
if(LeftNode == NULL)
Die("Incompatible Expression encountered in assignment");
printf("\tAssigning variable: %s\n", Symbols[FindSymbol(CurrentIdentifier)].Name);
printf("\tAfter parsing, the identifier name is %s, id %d in the symbol table.\n", CurrentIdentifier, FindSymbol(CurrentIdentifier));
//printf("\tAssigning variable: %s value %d\n", Symbols[FindSymbol(CurrentIdentifier)].Name, RightNode->Value.IntValue);
int currSymbolID = FindSymbol(CurrentIdentifier);
printf("\t\tAssigning variable: %s\n", Symbols[currSymbolID].Name);
printf("\t\tAfter parsing, the identifier name is %s, id %d in the symbol table.\n", CurrentIdentifier, FindSymbol(CurrentIdentifier));
LeftTemp = LeftNode;
LeftNode = RightNode;
RightNode = LeftTemp;
// Clear temps as ensurance
RightTemp = NULL;
LeftTemp = NULL;
} else {
LeftNode->RVal = 1;
RightNode->RVal = 1;
printf("mutate left\r\n");
//printf("mutate left\r\n");
LeftTemp = MutateType(LeftNode, RightNode->ExprType, OpType);
printf("mutate right\r\n");
//printf("mutate right\r\n");
RightTemp = MutateType(RightNode, LeftNode->ExprType, OpType);
printf("mutate right over\r\n");
//printf("mutate right over\r\n");
/**
* If both are null, the types are incompatible.
*/
@ -242,12 +252,12 @@ struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
/**
* Checks over, back to normal parsing.
*/
/*
if(LeftType)
LeftNode = ConstructASTBranch(LeftType, RightNode->ExprType, LeftNode, 0);
if(RightType)
RightNode = ConstructASTBranch(RightType, LeftNode->ExprType, RightNode, 0);
*/
if(LeftTemp != NULL)
LeftNode = LeftTemp; //ConstructASTBranch(LeftType, RightNode->ExprType, LeftNode, 0);
if(RightTemp != NULL)
RightNode = RightTemp; // ConstructASTBranch(RightType, LeftNode->ExprType, RightNode, 0);
LeftNode = ConstructASTNode(ParseTokenToOperation(NodeType), LeftNode->ExprType, LeftNode, NULL, RightNode, 0);
NodeType = CurrentToken.type;
if(NodeType == LI_SEMIC || NodeType == LI_RPARE || NodeType == LI_RBRAS) {
@ -441,7 +451,7 @@ struct ASTNode* ParseCompound() {
VerifyToken(LI_SEMIC, ";");
if(Tree) {
if(!Left)
if(Left == NULL)
Left = Tree;
else
Left = ConstructASTNode(OP_COMP, RET_NONE, Left, NULL, Tree, 0);
@ -461,6 +471,7 @@ void ParseGlobals() {
printf("Parsing global definitions\n");
while(1) {
printf("New definition incoming..\r\n\n");
Type = ParseOptionalPointer();
//TODO: converge pathways on this block?

View File

@ -8,7 +8,7 @@
#include <Data.h>
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.
@ -44,8 +44,8 @@ int ParseOptionalPointer() {
// Offset is 20. Rest are in order
if(CurrentToken.type >= TY_CHAR && CurrentToken.type <= TY_VOID) {
Type = CurrentToken.type - 21;
printf("\t\tConverting a %d type to a %d type.\n", CurrentToken.type, Type);
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);
}
@ -74,9 +74,9 @@ struct ASTNode* AccessArray() {
DieMessage("Accessing undeclared array", CurrentIdentifier);
LeftNode = ConstructASTLeaf(OP_ADDRESS, Symbols[ID].Type, ID);
printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
//printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
Tokenise(&CurrentToken);
printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
//printf("\t\tCurrent token: %s\r\n", TokenNames[CurrentToken.type]);
RightNode = ParsePrecedenceASTNode(0);
@ -85,10 +85,12 @@ struct ASTNode* AccessArray() {
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;
}

View File

@ -24,11 +24,11 @@
*/
void BeginVariableDeclaration(int Type) {
int ID;
//printf("type: %s\n", Types[Type]);
printf("type: %s\n", TypeNames[Type]);
if(CurrentToken.type == LI_LBRAS) {
Tokenise(&CurrentToken);
Type = Type - 2;
//Type = Type - 2;
if(CurrentToken.type == LI_INT) {
printf("Adding array %s that is %d x %s.\r\n", CurrentIdentifier, CurrentToken.value, TypeNames[Type]);
ID = AddArraySymbol(CurrentIdentifier, PointerTo(Type), ST_ARR, 0, CurrentToken.value);
@ -70,7 +70,7 @@ struct ASTNode* ParseFunction(int Type) {
struct ASTNode* FinalStatement;
int SymbolSlot, BreakLabel;
printf("\nIdentified function %s\n", CurrentIdentifier);
printf("\nIdentified function %s of return type %s\n", CurrentIdentifier, TypeNames[Type]);
BreakLabel = NewLabel();

View File

@ -9,8 +9,6 @@
static int GlobalSymbols = 0;
static char* Types[4] = { "none", "char", "int", "void" };
/*
* Find the position of a symbol in the symbol table.
* @Return the index into the symbol table if found,

View File

@ -202,6 +202,7 @@ struct ASTNode* MutateType(struct ASTNode* Tree, int RightType, int Operation) {
*/
if(TypeIsInt(LeftType) && TypeIsPtr(RightType)) {
printf("\t\t\tMutateType: Right node needs adjustment\r\n");
RightSize = PrimitiveSize(ValueAt(RightType));
if(RightSize > 1)