More work on arrays. Fixed some bugs.
Still need to figure out why AsStrDeref isn't working.
This commit is contained in:
parent
999f8dc267
commit
83959b4793
|
@ -87,8 +87,8 @@ enum TokenTypes {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
enum SyntaxOps {
|
enum SyntaxOps {
|
||||||
OP_ASSIGN = 1, // Assign an l-value
|
OP_ASSIGN = 1, // Assign an l-value
|
||||||
OP_ADD, // Add two numbers.
|
OP_ADD, // Add two numbers.
|
||||||
OP_SUBTRACT, // Subtract two numbers.
|
OP_SUBTRACT, // Subtract two numbers.
|
||||||
OP_MULTIPLY, // Multiply two numbers.
|
OP_MULTIPLY, // Multiply two numbers.
|
||||||
OP_DIVIDE, // Divide two numbers.
|
OP_DIVIDE, // Divide two numbers.
|
||||||
|
|
|
@ -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_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_ADDRESS: fprintf(stdout, "OP_ADDRESS %s\n", Symbols[node->Value.ID].Name); return;
|
||||||
case OP_DEREF: fprintf(stdout, "OP_DEREF\n"); 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:
|
default:
|
||||||
DieDecimal("Unknown Dump Operator", node->Operation);
|
DieDecimal("Unknown Dump Operator", node->Operation);
|
||||||
|
|
41
src/Parser.c
41
src/Parser.c
|
@ -165,13 +165,13 @@ struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
|
||||||
LeftNode = PrefixStatement();
|
LeftNode = PrefixStatement();
|
||||||
|
|
||||||
NodeType = CurrentToken.type;
|
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) {
|
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;
|
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)) {
|
while((OperatorPrecedence(NodeType) > PreviousTokenPrecedence) || (IsRightExpr(OpType) && OperatorPrecedence(OpType) == PreviousTokenPrecedence)) {
|
||||||
//printf("inside while\n");
|
//printf("inside while\n");
|
||||||
Tokenise(&CurrentToken);
|
Tokenise(&CurrentToken);
|
||||||
|
@ -190,27 +190,37 @@ struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
|
||||||
OpType = ParseTokenToOperation(NodeType);
|
OpType = ParseTokenToOperation(NodeType);
|
||||||
|
|
||||||
if(OpType == OP_ASSIGN) {
|
if(OpType == OP_ASSIGN) {
|
||||||
|
printf("\tParsePrecedenceASTNode: Assignment statement\r\n");
|
||||||
RightNode->RVal = 1;
|
RightNode->RVal = 1;
|
||||||
|
|
||||||
RightNode = MutateType(RightNode, LeftNode->ExprType, 0);
|
RightNode = MutateType(RightNode, LeftNode->ExprType, 0);
|
||||||
if(LeftNode == NULL)
|
if(LeftNode == NULL)
|
||||||
Die("Incompatible Expression encountered in assignment");
|
Die("Incompatible Expression encountered in assignment");
|
||||||
|
|
||||||
|
|
||||||
printf("\tAssigning variable: %s\n", Symbols[FindSymbol(CurrentIdentifier)].Name);
|
//printf("\tAssigning variable: %s value %d\n", Symbols[FindSymbol(CurrentIdentifier)].Name, RightNode->Value.IntValue);
|
||||||
printf("\tAfter parsing, the identifier name is %s, id %d in the symbol table.\n", CurrentIdentifier, FindSymbol(CurrentIdentifier));
|
|
||||||
|
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;
|
LeftTemp = LeftNode;
|
||||||
LeftNode = RightNode;
|
LeftNode = RightNode;
|
||||||
RightNode = LeftTemp;
|
RightNode = LeftTemp;
|
||||||
|
|
||||||
|
// Clear temps as ensurance
|
||||||
|
RightTemp = NULL;
|
||||||
|
LeftTemp = NULL;
|
||||||
} else {
|
} else {
|
||||||
LeftNode->RVal = 1;
|
LeftNode->RVal = 1;
|
||||||
RightNode->RVal = 1;
|
RightNode->RVal = 1;
|
||||||
|
|
||||||
printf("mutate left\r\n");
|
//printf("mutate left\r\n");
|
||||||
LeftTemp = MutateType(LeftNode, RightNode->ExprType, OpType);
|
LeftTemp = MutateType(LeftNode, RightNode->ExprType, OpType);
|
||||||
printf("mutate right\r\n");
|
//printf("mutate right\r\n");
|
||||||
RightTemp = MutateType(RightNode, LeftNode->ExprType, OpType);
|
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.
|
* If both are null, the types are incompatible.
|
||||||
*/
|
*/
|
||||||
|
@ -242,12 +252,12 @@ struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
|
||||||
/**
|
/**
|
||||||
* Checks over, back to normal parsing.
|
* Checks over, back to normal parsing.
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
if(LeftType)
|
if(LeftTemp != NULL)
|
||||||
LeftNode = ConstructASTBranch(LeftType, RightNode->ExprType, LeftNode, 0);
|
LeftNode = LeftTemp; //ConstructASTBranch(LeftType, RightNode->ExprType, LeftNode, 0);
|
||||||
if(RightType)
|
if(RightTemp != NULL)
|
||||||
RightNode = ConstructASTBranch(RightType, LeftNode->ExprType, RightNode, 0);
|
RightNode = RightTemp; // ConstructASTBranch(RightType, LeftNode->ExprType, RightNode, 0);
|
||||||
*/
|
|
||||||
LeftNode = ConstructASTNode(ParseTokenToOperation(NodeType), LeftNode->ExprType, LeftNode, NULL, RightNode, 0);
|
LeftNode = ConstructASTNode(ParseTokenToOperation(NodeType), LeftNode->ExprType, LeftNode, NULL, RightNode, 0);
|
||||||
NodeType = CurrentToken.type;
|
NodeType = CurrentToken.type;
|
||||||
if(NodeType == LI_SEMIC || NodeType == LI_RPARE || NodeType == LI_RBRAS) {
|
if(NodeType == LI_SEMIC || NodeType == LI_RPARE || NodeType == LI_RBRAS) {
|
||||||
|
@ -441,7 +451,7 @@ struct ASTNode* ParseCompound() {
|
||||||
VerifyToken(LI_SEMIC, ";");
|
VerifyToken(LI_SEMIC, ";");
|
||||||
|
|
||||||
if(Tree) {
|
if(Tree) {
|
||||||
if(!Left)
|
if(Left == NULL)
|
||||||
Left = Tree;
|
Left = Tree;
|
||||||
else
|
else
|
||||||
Left = ConstructASTNode(OP_COMP, RET_NONE, Left, NULL, Tree, 0);
|
Left = ConstructASTNode(OP_COMP, RET_NONE, Left, NULL, Tree, 0);
|
||||||
|
@ -461,6 +471,7 @@ void ParseGlobals() {
|
||||||
printf("Parsing global definitions\n");
|
printf("Parsing global definitions\n");
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
|
printf("New definition incoming..\r\n\n");
|
||||||
Type = ParseOptionalPointer();
|
Type = ParseOptionalPointer();
|
||||||
|
|
||||||
//TODO: converge pathways on this block?
|
//TODO: converge pathways on this block?
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include <Data.h>
|
#include <Data.h>
|
||||||
|
|
||||||
int PointerTo(int Type) {
|
int PointerTo(int Type) {
|
||||||
|
//Type = Type + 1;
|
||||||
printf("\t\tPointerising a %s\n", TypeNames[Type]);
|
printf("\t\tPointerising a %s\n", TypeNames[Type]);
|
||||||
// As it stands, the conversion between
|
// As it stands, the conversion between
|
||||||
// RET and PTR is +4.
|
// RET and PTR is +4.
|
||||||
|
@ -44,8 +44,8 @@ int ParseOptionalPointer() {
|
||||||
// Offset is 20. Rest are in order
|
// Offset is 20. Rest are in order
|
||||||
|
|
||||||
if(CurrentToken.type >= TY_CHAR && CurrentToken.type <= TY_VOID) {
|
if(CurrentToken.type >= TY_CHAR && CurrentToken.type <= TY_VOID) {
|
||||||
Type = CurrentToken.type - 21;
|
Type = CurrentToken.type - 23;
|
||||||
printf("\t\tConverting a %d type to a %d type.\n", CurrentToken.type, Type);
|
printf("\t\tConverting a %s token to a %s type.\n", TokenNames[CurrentToken.type], TypeNames[Type]);
|
||||||
} else {
|
} else {
|
||||||
DieDecimal("Illegal type for pointerisation", CurrentToken.type);
|
DieDecimal("Illegal type for pointerisation", CurrentToken.type);
|
||||||
}
|
}
|
||||||
|
@ -74,9 +74,9 @@ struct ASTNode* AccessArray() {
|
||||||
DieMessage("Accessing undeclared array", CurrentIdentifier);
|
DieMessage("Accessing undeclared array", CurrentIdentifier);
|
||||||
|
|
||||||
LeftNode = ConstructASTLeaf(OP_ADDRESS, Symbols[ID].Type, ID);
|
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);
|
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);
|
RightNode = ParsePrecedenceASTNode(0);
|
||||||
|
|
||||||
|
@ -85,10 +85,12 @@ struct ASTNode* AccessArray() {
|
||||||
if(!TypeIsInt(RightNode->ExprType))
|
if(!TypeIsInt(RightNode->ExprType))
|
||||||
Die("Array index is not integer");
|
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);
|
RightNode = MutateType(RightNode, LeftNode->ExprType, OP_ADD);
|
||||||
|
|
||||||
LeftNode = ConstructASTNode(OP_ADD, Symbols[ID].Type, LeftNode, NULL, RightNode, 0);
|
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);
|
LeftNode = ConstructASTBranch(OP_DEREF, ValueAt(LeftNode->ExprType), LeftNode, 0);
|
||||||
|
printf("\tArray Access constructed\r\n");
|
||||||
return LeftNode;
|
return LeftNode;
|
||||||
}
|
}
|
|
@ -24,11 +24,11 @@
|
||||||
*/
|
*/
|
||||||
void BeginVariableDeclaration(int Type) {
|
void BeginVariableDeclaration(int Type) {
|
||||||
int ID;
|
int ID;
|
||||||
//printf("type: %s\n", Types[Type]);
|
printf("type: %s\n", TypeNames[Type]);
|
||||||
|
|
||||||
if(CurrentToken.type == LI_LBRAS) {
|
if(CurrentToken.type == LI_LBRAS) {
|
||||||
Tokenise(&CurrentToken);
|
Tokenise(&CurrentToken);
|
||||||
Type = Type - 2;
|
//Type = Type - 2;
|
||||||
if(CurrentToken.type == LI_INT) {
|
if(CurrentToken.type == LI_INT) {
|
||||||
printf("Adding array %s that is %d x %s.\r\n", CurrentIdentifier, CurrentToken.value, TypeNames[Type]);
|
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);
|
ID = AddArraySymbol(CurrentIdentifier, PointerTo(Type), ST_ARR, 0, CurrentToken.value);
|
||||||
|
@ -70,7 +70,7 @@ struct ASTNode* ParseFunction(int Type) {
|
||||||
struct ASTNode* FinalStatement;
|
struct ASTNode* FinalStatement;
|
||||||
int SymbolSlot, BreakLabel;
|
int SymbolSlot, BreakLabel;
|
||||||
|
|
||||||
printf("\nIdentified function %s\n", CurrentIdentifier);
|
printf("\nIdentified function %s of return type %s\n", CurrentIdentifier, TypeNames[Type]);
|
||||||
|
|
||||||
BreakLabel = NewLabel();
|
BreakLabel = NewLabel();
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
static int GlobalSymbols = 0;
|
static int GlobalSymbols = 0;
|
||||||
|
|
||||||
static char* Types[4] = { "none", "char", "int", "void" };
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find the position of a symbol in the symbol table.
|
* Find the position of a symbol in the symbol table.
|
||||||
* @Return the index into the symbol table if found,
|
* @Return the index into the symbol table if found,
|
||||||
|
|
|
@ -202,6 +202,7 @@ struct ASTNode* MutateType(struct ASTNode* Tree, int RightType, int Operation) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if(TypeIsInt(LeftType) && TypeIsPtr(RightType)) {
|
if(TypeIsInt(LeftType) && TypeIsPtr(RightType)) {
|
||||||
|
printf("\t\t\tMutateType: Right node needs adjustment\r\n");
|
||||||
RightSize = PrimitiveSize(ValueAt(RightType));
|
RightSize = PrimitiveSize(ValueAt(RightType));
|
||||||
|
|
||||||
if(RightSize > 1)
|
if(RightSize > 1)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user