Compare commits
No commits in common. "bc1bac8a63122d4de6b20d486ed04909515dad86" and "764f89bb88912045d4db3d6619097e01770f483c" have entirely different histories.
bc1bac8a63
...
764f89bb88
|
@ -27,7 +27,6 @@
|
||||||
|
|
||||||
enum TokenTypes {
|
enum TokenTypes {
|
||||||
LI_EOF,
|
LI_EOF,
|
||||||
LI_EQUAL, // =
|
|
||||||
|
|
||||||
AR_PLUS, // Arithmetic +
|
AR_PLUS, // Arithmetic +
|
||||||
AR_MINUS, // Arithmetic -
|
AR_MINUS, // Arithmetic -
|
||||||
|
@ -41,6 +40,7 @@ enum TokenTypes {
|
||||||
CMP_LTE, // <=
|
CMP_LTE, // <=
|
||||||
CMP_GTE, // =>
|
CMP_GTE, // =>
|
||||||
|
|
||||||
|
LI_EQUAL, // =
|
||||||
LI_INT, // Integer literal
|
LI_INT, // Integer literal
|
||||||
LI_SEMIC, // ;
|
LI_SEMIC, // ;
|
||||||
|
|
||||||
|
@ -84,8 +84,7 @@ enum TokenTypes {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
enum SyntaxOps {
|
enum SyntaxOps {
|
||||||
OP_ASSIGN = 1, // Assign an l-value
|
OP_ADD = 1, // 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.
|
||||||
|
@ -97,6 +96,7 @@ enum SyntaxOps {
|
||||||
OP_LESSE, // Less than or Equal to?
|
OP_LESSE, // Less than or Equal to?
|
||||||
OP_GREATE, // Greater than or Equal to?
|
OP_GREATE, // Greater than or Equal to?
|
||||||
|
|
||||||
|
OP_ASSIGN, // Assign an l-value
|
||||||
|
|
||||||
OP_ADDRESS, // Fetch the address of a var
|
OP_ADDRESS, // Fetch the address of a var
|
||||||
OP_DEREF, // Get the value of the address in a pointer
|
OP_DEREF, // Get the value of the address in a pointer
|
||||||
|
@ -104,6 +104,7 @@ enum SyntaxOps {
|
||||||
TERM_INTLITERAL, // Integer Literal. This is a virtual operation, so it's a terminal.
|
TERM_INTLITERAL, // Integer Literal. This is a virtual operation, so it's a terminal.
|
||||||
|
|
||||||
REF_IDENT, // Reference (read) an identifier (variable).
|
REF_IDENT, // Reference (read) an identifier (variable).
|
||||||
|
LV_IDENT, // Write an identifier in the form of an l-value.
|
||||||
|
|
||||||
OP_WIDEN, // Something contains a type that needs to be casted up
|
OP_WIDEN, // Something contains a type that needs to be casted up
|
||||||
OP_SCALE, // We have a pointer that needs to be scaled!
|
OP_SCALE, // We have a pointer that needs to be scaled!
|
||||||
|
@ -124,7 +125,6 @@ enum SyntaxOps {
|
||||||
struct ASTNode {
|
struct ASTNode {
|
||||||
int Operation; // SyntaxOps Index
|
int Operation; // SyntaxOps Index
|
||||||
int ExprType; // Value->IntValue's DataType
|
int ExprType; // Value->IntValue's DataType
|
||||||
int RVal; // True if this node is an Rval, false if Lval
|
|
||||||
struct ASTNode* Left;
|
struct ASTNode* Left;
|
||||||
struct ASTNode* Middle;
|
struct ASTNode* Middle;
|
||||||
struct ASTNode* Right;
|
struct ASTNode* Right;
|
||||||
|
@ -307,9 +307,7 @@ int AsDiv(int Left, int Right);
|
||||||
int AsLdVar(int ID);
|
int AsLdVar(int ID);
|
||||||
int AsStrVar(int Register, int ID);
|
int AsStrVar(int Register, int ID);
|
||||||
|
|
||||||
|
|
||||||
int AsDeref(int Reg, int Type);
|
int AsDeref(int Reg, int Type);
|
||||||
int AsStrDeref(int Register1, int Register2, int Type);
|
|
||||||
int AsAddr(int ID);
|
int AsAddr(int ID);
|
||||||
|
|
||||||
void AsNewSymb(int ID);
|
void AsNewSymb(int ID);
|
||||||
|
@ -353,6 +351,3 @@ struct ASTNode* ParseIdentifier(void);
|
||||||
struct ASTNode* IfStatement();
|
struct ASTNode* IfStatement();
|
||||||
struct ASTNode* WhileStatement();
|
struct ASTNode* WhileStatement();
|
||||||
struct ASTNode* ForStatement();
|
struct ASTNode* ForStatement();
|
||||||
|
|
||||||
|
|
||||||
void DumpTree(struct ASTNode* node, int level);
|
|
|
@ -30,15 +30,10 @@ static char* InvComparisons[6] = { "jne", "je", "jge", "jle", "jg", "j
|
||||||
* * * * R O O T O F A S S E M B L E R * * * *
|
* * * * R O O T O F A S S E M B L E R * * * *
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
static int Started = 0;
|
|
||||||
|
|
||||||
int AssembleTree(struct ASTNode* Node, int Register, int ParentOp) {
|
int AssembleTree(struct ASTNode* Node, int Register, int ParentOp) {
|
||||||
int LeftVal, RightVal;
|
int LeftVal, RightVal;
|
||||||
if(!Started)
|
|
||||||
DumpTree(Node, 0);
|
|
||||||
Started = 1;
|
|
||||||
|
|
||||||
printf("Current operation: %d\r\n", Node->Operation);
|
|
||||||
switch(Node->Operation) {
|
switch(Node->Operation) {
|
||||||
case OP_IF:
|
case OP_IF:
|
||||||
return AsIf(Node);
|
return AsIf(Node);
|
||||||
|
@ -107,18 +102,10 @@ int AssembleTree(struct ASTNode* Node, int Register, int ParentOp) {
|
||||||
return AsDeref(LeftVal, Node->Left->ExprType);
|
return AsDeref(LeftVal, Node->Left->ExprType);
|
||||||
|
|
||||||
case OP_ASSIGN:
|
case OP_ASSIGN:
|
||||||
printf("Calculating for assignment..\r\n");
|
return RightVal;
|
||||||
if(Node->Right == NULL)
|
|
||||||
Die("Fault in assigning a null rvalue");
|
|
||||||
switch(Node->Right->Operation) {
|
|
||||||
case REF_IDENT: return AsStrVar(LeftVal, Node->Right->Value.ID);
|
|
||||||
case OP_DEREF: return AsStrDeref(LeftVal, RightVal, Node->Right->ExprType);
|
|
||||||
default: DieDecimal("Can't ASSIGN in AssembleTree: ", Node->Operation);
|
|
||||||
}
|
|
||||||
|
|
||||||
case OP_WIDEN:
|
case OP_WIDEN:
|
||||||
printf("\tWidening types..\r\n");
|
return LeftVal;
|
||||||
return LeftVal; //AsWiden(LeftVal, Node->Left->ExprType, Node->ExprType);
|
|
||||||
|
|
||||||
case OP_RET:
|
case OP_RET:
|
||||||
AsReturn(LeftVal, CurrentFunction);
|
AsReturn(LeftVal, CurrentFunction);
|
||||||
|
@ -159,13 +146,14 @@ int AssembleTree(struct ASTNode* Node, int Register, int ParentOp) {
|
||||||
|
|
||||||
|
|
||||||
case REF_IDENT:
|
case REF_IDENT:
|
||||||
if(Node->RVal || ParentOp == OP_DEREF)
|
return AsLdVar(Node->Value.ID);
|
||||||
return AsLdVar(Node->Value.ID);
|
|
||||||
else
|
case LV_IDENT:
|
||||||
return -1;
|
return AsStrVar(Register, Node->Value.ID);
|
||||||
|
|
||||||
case TERM_INTLITERAL:
|
case TERM_INTLITERAL:
|
||||||
return AsLoad(Node->Value.IntValue);
|
return AsLoad(Node->Value.IntValue);
|
||||||
|
break;
|
||||||
|
|
||||||
case OP_PRINT:
|
case OP_PRINT:
|
||||||
AssemblerPrint(LeftVal);
|
AssemblerPrint(LeftVal);
|
||||||
|
@ -222,7 +210,6 @@ int NewLabel(void) {
|
||||||
return id++;
|
return id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int AsIf(struct ASTNode* Node) {
|
int AsIf(struct ASTNode* Node) {
|
||||||
int FalseLabel, EndLabel;
|
int FalseLabel, EndLabel;
|
||||||
|
|
||||||
|
@ -452,33 +439,11 @@ int AsDeref(int Reg, int Type) {
|
||||||
case PTR_LONG:
|
case PTR_LONG:
|
||||||
fprintf(OutputFile, "\tmovq\t(%s), %s\n", Registers[Reg], Registers[Reg]);
|
fprintf(OutputFile, "\tmovq\t(%s), %s\n", Registers[Reg], Registers[Reg]);
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
DieDecimal("Can't generate dereference for type", Type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Reg;
|
return Reg;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AsStrDeref(int Register1, int Register2, int Type) {
|
|
||||||
printf("\tStoring contents of %s into %s through a dereference, type %d\n", Registers[Register1], Registers[Register2], Type);
|
|
||||||
|
|
||||||
switch(Type) {
|
|
||||||
case PTR_CHAR:
|
|
||||||
fprintf(OutputFile, "\tmovb\t%s, (%s)\n", ByteRegisters[Register1], Registers[Register2]);
|
|
||||||
break;
|
|
||||||
case PTR_INT:
|
|
||||||
fprintf(OutputFile, "\tmovq\t%s, (%s)\n", Registers[Register1], Registers[Register2]);
|
|
||||||
break;
|
|
||||||
case PTR_LONG:
|
|
||||||
fprintf(OutputFile, "\tmovq\t%s, (%s)\n", Registers[Register1], Registers[Register2]);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
DieDecimal("Can't generate store-into-deref of type", Type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Register1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AsNewSymb(int ID) {
|
void AsNewSymb(int ID) {
|
||||||
int TypeSize;
|
int TypeSize;
|
||||||
|
|
||||||
|
|
95
src/Dump.c
95
src/Dump.c
|
@ -1,95 +0,0 @@
|
||||||
|
|
||||||
/*************/
|
|
||||||
/*GEMWIRE */
|
|
||||||
/* ERYTHRO*/
|
|
||||||
/*************/
|
|
||||||
|
|
||||||
#include <Defs.h>
|
|
||||||
#include <Data.h>
|
|
||||||
|
|
||||||
static int GenerateSrg() {
|
|
||||||
static int srgId = 1;
|
|
||||||
return srgId++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DumpTree(struct ASTNode* node, int level) {
|
|
||||||
int Lfalse, Lstart, Lend;
|
|
||||||
|
|
||||||
// Handle weirdo loops and conditions first.
|
|
||||||
switch(node->Operation) {
|
|
||||||
case OP_IF:
|
|
||||||
Lfalse = GenerateSrg();
|
|
||||||
for(int i = 0; i < level; i++)
|
|
||||||
fprintf(stdout, " ");
|
|
||||||
fprintf(stdout, "IF");
|
|
||||||
if(node->Right) {
|
|
||||||
Lend = GenerateSrg();
|
|
||||||
fprintf(stdout, ", end label %d", Lend);
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stdout, "\n");
|
|
||||||
DumpTree(node->Left, level + 2);
|
|
||||||
DumpTree(node->Middle, level + 2);
|
|
||||||
|
|
||||||
if(node->Right)
|
|
||||||
DumpTree(node->Right, level + 2);
|
|
||||||
|
|
||||||
return;
|
|
||||||
case OP_LOOP:
|
|
||||||
Lstart = GenerateSrg();
|
|
||||||
for(int i = 0; i < level; i++)
|
|
||||||
fprintf(stdout, " ");
|
|
||||||
fprintf(stdout, "LOOP starts at %d\n", Lstart);
|
|
||||||
Lend = GenerateSrg();
|
|
||||||
DumpTree(node->Left, level + 2);
|
|
||||||
DumpTree(node->Right, level + 2);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If current node is a compound, we treat it as if we didn't just enter a loop.
|
|
||||||
if(node->Operation == OP_COMP)
|
|
||||||
level = -2;
|
|
||||||
|
|
||||||
if(node->Left)
|
|
||||||
DumpTree(node->Left, level + 2);
|
|
||||||
|
|
||||||
if(node->Right)
|
|
||||||
DumpTree(node->Right, level + 2);
|
|
||||||
|
|
||||||
// The meat of this operation!
|
|
||||||
for(int i = 0; i < level; i++)
|
|
||||||
fprintf(stdout, " ");
|
|
||||||
|
|
||||||
switch (node->Operation){
|
|
||||||
case OP_COMP: fprintf(stdout, "\n\n"); return;
|
|
||||||
case OP_FUNC: fprintf(stdout, "OP_FUNC %s\n", Symbols[node->Value.ID].Name); return;
|
|
||||||
case OP_ADD: fprintf(stdout, "OP_ADD\n"); return;
|
|
||||||
case OP_SUBTRACT: fprintf(stdout, "OP_SUBTRACT\n"); return;
|
|
||||||
case OP_MULTIPLY: fprintf(stdout, "OP_MULTIPLY\n"); return;
|
|
||||||
case OP_DIVIDE: fprintf(stdout, "OP_DIVIDE\n"); return;
|
|
||||||
case OP_EQUAL: fprintf(stdout, "OP_EQUAL\n"); return;
|
|
||||||
case OP_INEQ: fprintf(stdout, "OP_INEQ\n"); return;
|
|
||||||
case OP_LESS: fprintf(stdout, "OP_LESS\n"); return;
|
|
||||||
case OP_GREAT: fprintf(stdout, "OP_GREAT\n"); return;
|
|
||||||
case OP_LESSE: fprintf(stdout, "OP_LESSE\n"); return;
|
|
||||||
case OP_GREATE: fprintf(stdout, "OP_GREATE\n"); return;
|
|
||||||
case TERM_INTLITERAL: fprintf(stdout, "TERM_INTLITERAL %d\n", node->Value.IntValue); return;
|
|
||||||
case REF_IDENT:
|
|
||||||
if(node->Right)
|
|
||||||
fprintf(stdout, "REF_IDENT rval %s\n", Symbols[node->Value.ID].Name);
|
|
||||||
else
|
|
||||||
fprintf(stdout, "REF_IDENT %s\n", Symbols[node->Value.ID].Name);
|
|
||||||
return;
|
|
||||||
case OP_ASSIGN: fprintf(stdout, "OP_ASSIGN\n"); return;
|
|
||||||
case OP_WIDEN: fprintf(stdout, "OP_WIDEN\n"); return;
|
|
||||||
case OP_RET: fprintf(stdout, "OP_RET\n"); 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_DEREF: fprintf(stdout, "OP_DEREF\n"); return;
|
|
||||||
case OP_SCALE: fprintf(stdout, "OP_SCALE %d\n", Symbols[node->Value.Size]); return;
|
|
||||||
|
|
||||||
default:
|
|
||||||
DieDecimal("Unknown Dump Operator", node->Operation);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -77,7 +77,6 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
AddFunctionSymbol("PrintInteger", RET_CHAR, ST_FUNC, 0);
|
AddFunctionSymbol("PrintInteger", RET_CHAR, ST_FUNC, 0);
|
||||||
//AddSymbol("forgecord", PTR_CHAR, ST_VAR);
|
|
||||||
|
|
||||||
Tokenise(&CurrentToken);
|
Tokenise(&CurrentToken);
|
||||||
|
|
||||||
|
|
128
src/Parser.c
128
src/Parser.c
|
@ -18,12 +18,12 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static int Precedence[] =
|
static int Precedence[] =
|
||||||
{ 0, 10, // EOF, ASSIGN
|
{ 0, // EOF
|
||||||
20, 20, // + -
|
10, 10, // + -
|
||||||
30, 30, // * /
|
20, 20, // * /
|
||||||
40, 40, // =? !=
|
30, 30, // =? !=
|
||||||
50, 50, // < >
|
40, 40, // < >
|
||||||
50, 50}; // <= =>
|
40, 40}; // <= =>
|
||||||
|
|
||||||
static int OperatorPrecedence(int Token) {
|
static int OperatorPrecedence(int Token) {
|
||||||
int Prec = Precedence[Token];
|
int Prec = Precedence[Token];
|
||||||
|
@ -35,10 +35,6 @@ static int OperatorPrecedence(int Token) {
|
||||||
return Prec;
|
return Prec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int IsRightExpr(int Token) {
|
|
||||||
return (Token == LI_EQUAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* * * * * * * * * * * * * * * * * * * * * * * *
|
/* * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* * * N O D E C O N S T R U C T I O N * * *
|
* * * N O D E C O N S T R U C T I O N * * *
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * */
|
* * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
@ -139,7 +135,7 @@ struct ASTNode* ParsePrimary(void) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
DieDecimal("Unable to parse primary type", CurrentToken.type);
|
DieDecimal("Syntax Error", CurrentToken.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
Tokenise(&CurrentToken);
|
Tokenise(&CurrentToken);
|
||||||
|
@ -151,102 +147,77 @@ struct ASTNode* ParsePrimary(void) {
|
||||||
struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
|
struct ASTNode* ParsePrecedenceASTNode(int PreviousTokenPrecedence) {
|
||||||
struct ASTNode* LeftNode, *RightNode;
|
struct ASTNode* LeftNode, *RightNode;
|
||||||
struct ASTNode* LeftTemp, *RightTemp;
|
struct ASTNode* LeftTemp, *RightTemp;
|
||||||
// int LeftType, RightType;
|
int LeftType, RightType;
|
||||||
int NodeType, OpType;
|
int NodeType, OpType;
|
||||||
|
|
||||||
LeftNode = PrefixStatement();
|
LeftNode = PrefixStatement();
|
||||||
|
|
||||||
NodeType = CurrentToken.type;
|
NodeType = CurrentToken.type;
|
||||||
if(NodeType == LI_SEMIC || NodeType == LI_RPARE) {
|
if(NodeType == LI_SEMIC || NodeType == LI_RPARE)
|
||||||
LeftNode->RVal = 1; return LeftNode;
|
return LeftNode;
|
||||||
}
|
|
||||||
|
|
||||||
printf("Current token has value %d, type %d\n", CurrentToken.value, CurrentToken.type);
|
//printf("Current token has value %d, type %d\n", CurrentToken.value, CurrentToken.type);
|
||||||
while((OperatorPrecedence(NodeType) > PreviousTokenPrecedence) || (IsRightExpr(OpType) && OperatorPrecedence(OpType) == PreviousTokenPrecedence)) {
|
while(OperatorPrecedence(NodeType) > PreviousTokenPrecedence) {
|
||||||
//printf("inside while\n");
|
//printf("inside while\n");
|
||||||
Tokenise(&CurrentToken);
|
Tokenise(&CurrentToken);
|
||||||
|
|
||||||
RightNode = ParsePrecedenceASTNode(Precedence[NodeType]);
|
RightNode = ParsePrecedenceASTNode(Precedence[NodeType]);
|
||||||
|
|
||||||
/*
|
|
||||||
LeftType = LeftNode->ExprType;
|
LeftType = LeftNode->ExprType;
|
||||||
RightType = RightNode->ExprType;
|
RightType = RightNode->ExprType;
|
||||||
*/
|
|
||||||
/**
|
/**
|
||||||
* While parsing this node, we may need to widen some types.
|
* While parsing this node, we may need to widen some types.
|
||||||
* This requires a few functions and checks.
|
* This requires a few functions and checks.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
OpType = ParseTokenToOperation(NodeType);
|
OpType = ParseTokenToOperation(NodeType);
|
||||||
|
LeftTemp = MutateType(LeftNode, RightType, OpType);
|
||||||
|
RightTemp = MutateType(RightNode, LeftType, OpType);
|
||||||
|
|
||||||
if(OpType == OP_ASSIGN) {
|
/**
|
||||||
RightNode->RVal = 1;
|
* If both are null, the types are incompatible.
|
||||||
|
*/
|
||||||
|
|
||||||
RightNode = MutateType(RightNode, LeftNode->ExprType, 0);
|
if(LeftTemp == NULL && RightTemp == NULL)
|
||||||
if(LeftNode == NULL)
|
Die("Incompatible types in parsing nodes");
|
||||||
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));
|
* If the left was valid, or valid for
|
||||||
|
* expansion, then it will be non-null.
|
||||||
|
*
|
||||||
|
* If it was valid, then this will be
|
||||||
|
* equivalent to LeftNode = LeftNode
|
||||||
|
*/
|
||||||
|
|
||||||
LeftTemp = LeftNode;
|
if(LeftTemp)
|
||||||
LeftNode = RightNode;
|
LeftNode = LeftTemp;
|
||||||
RightNode = LeftTemp;
|
|
||||||
} else {
|
|
||||||
LeftNode->RVal = 1;
|
|
||||||
RightNode->RVal = 1;
|
|
||||||
|
|
||||||
printf("mutate left\r\n");
|
/**
|
||||||
LeftTemp = MutateType(LeftNode, RightNode->ExprType, OpType);
|
* Same here, but there is a higher chance
|
||||||
printf("mutate right\r\n");
|
* for the right node to be incompatible due
|
||||||
RightTemp = MutateType(RightNode, LeftNode->ExprType, OpType);
|
* to the nature of widening types.
|
||||||
printf("mutate right over\r\n");
|
*/
|
||||||
/**
|
|
||||||
* If both are null, the types are incompatible.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if(LeftTemp == NULL && RightTemp == NULL)
|
if(RightTemp)
|
||||||
Die("Incompatible types in parsing nodes");
|
RightNode = RightTemp;
|
||||||
|
|
||||||
/**
|
|
||||||
* If the left was valid, or valid for
|
|
||||||
* expansion, then it will be non-null.
|
|
||||||
*
|
|
||||||
* If it was valid, then this will be
|
|
||||||
* equivalent to LeftNode = LeftNode
|
|
||||||
*/
|
|
||||||
|
|
||||||
if(LeftTemp)
|
|
||||||
LeftNode = LeftTemp;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Same here, but there is a higher chance
|
|
||||||
* for the right node to be incompatible due
|
|
||||||
* to the nature of widening types.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if(RightTemp)
|
|
||||||
RightNode = RightTemp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks over, back to normal parsing.
|
* Checks over, back to normal parsing.
|
||||||
*/
|
*/
|
||||||
/*
|
|
||||||
if(LeftType)
|
if(LeftType)
|
||||||
LeftNode = ConstructASTBranch(LeftType, RightNode->ExprType, LeftNode, 0);
|
LeftNode = ConstructASTBranch(LeftType, RightNode->ExprType, LeftNode, 0);
|
||||||
if(RightType)
|
if(RightType)
|
||||||
RightNode = ConstructASTBranch(RightType, LeftNode->ExprType, RightNode, 0);
|
RightNode = 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) {
|
|
||||||
LeftNode->RVal = 1;
|
|
||||||
return LeftNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
LeftNode = ConstructASTNode(ParseTokenToOperation(NodeType), LeftNode->ExprType, LeftNode, NULL, RightNode, 0);
|
||||||
|
|
||||||
|
NodeType = CurrentToken.type;
|
||||||
|
if(NodeType == LI_SEMIC || NodeType == LI_RPARE)
|
||||||
|
return LeftNode;
|
||||||
}
|
}
|
||||||
LeftNode->RVal = 1;
|
|
||||||
return LeftNode;
|
return LeftNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,23 +349,27 @@ struct ASTNode* ParseStatement(void) {
|
||||||
int Type;
|
int Type;
|
||||||
|
|
||||||
switch(CurrentToken.type) {
|
switch(CurrentToken.type) {
|
||||||
|
|
||||||
|
case KW_PRINT:
|
||||||
|
return PrintStatement();
|
||||||
|
|
||||||
case TY_CHAR:
|
case TY_CHAR:
|
||||||
case TY_LONG:
|
case TY_LONG:
|
||||||
case TY_INT:
|
case TY_INT:
|
||||||
printf("\t\tNew Variable: %s\n", CurrentIdentifier);
|
printf("\t\tNew Variable: %s\n", CurrentIdentifier);
|
||||||
Type = ParseOptionalPointer();
|
Type = ParseOptionalPointer(CurrentToken.type);
|
||||||
VerifyToken(TY_IDENTIFIER, "ident");
|
VerifyToken(TY_IDENTIFIER, "ident");
|
||||||
BeginVariableDeclaration(Type);
|
BeginVariableDeclaration(Type);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
/*case TY_IDENTIFIER:
|
case TY_IDENTIFIER:
|
||||||
if(Symbols[FindSymbol(CurrentIdentifier)].Structure == ST_FUNC)
|
if(Symbols[FindSymbol(CurrentIdentifier)].Structure == ST_FUNC)
|
||||||
printf("\t\tCalling Function: %s\n", Symbols[FindSymbol(CurrentIdentifier)].Name);
|
printf("\t\tCalling Function: %s\n", Symbols[FindSymbol(CurrentIdentifier)].Name);
|
||||||
else
|
else
|
||||||
printf("\t\tAssigning variable: %s\n", Symbols[FindSymbol(CurrentIdentifier)].Name);
|
printf("\t\tAssigning variable: %s\n", Symbols[FindSymbol(CurrentIdentifier)].Name);
|
||||||
|
|
||||||
return ParseIdentifier();
|
return ParseIdentifier();
|
||||||
*/
|
|
||||||
case KW_IF:
|
case KW_IF:
|
||||||
return IfStatement();
|
return IfStatement();
|
||||||
|
|
||||||
|
@ -408,8 +383,7 @@ struct ASTNode* ParseStatement(void) {
|
||||||
return ReturnStatement();
|
return ReturnStatement();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
ParsePrecedenceASTNode(0);
|
DieDecimal("Syntax Error in single-statement parsing. Token:", CurrentToken.type);
|
||||||
//DieDecimal("Syntax Error in single-statement parsing. Token:", CurrentToken.type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -101,19 +101,16 @@ struct ASTNode* ReturnStatement() {
|
||||||
|
|
||||||
Tree = ParsePrecedenceASTNode(0);
|
Tree = ParsePrecedenceASTNode(0);
|
||||||
|
|
||||||
/*
|
|
||||||
ReturnType = Tree->ExprType;
|
ReturnType = Tree->ExprType;
|
||||||
FunctionType = Symbols[CurrentFunction].Type;
|
FunctionType = Symbols[CurrentFunction].Type;
|
||||||
*/
|
|
||||||
|
|
||||||
Tree = MutateType(Tree, Symbols[CurrentFunction].Type, 0);
|
Tree = MutateType(Tree, FunctionType, 0);
|
||||||
if(Tree == NULL)
|
if(!Tree)
|
||||||
Die("Returning a value of incorrect type for function");
|
Die("Returning a value of incorrect type for function");
|
||||||
|
|
||||||
/*
|
|
||||||
if(ReturnType)
|
if(ReturnType)
|
||||||
Tree = ConstructASTBranch(ReturnType, FunctionType, Tree, 0);
|
Tree = ConstructASTBranch(ReturnType, FunctionType, Tree, 0);
|
||||||
*/
|
|
||||||
|
|
||||||
Tree = ConstructASTBranch(OP_RET, RET_NONE, Tree, 0);
|
Tree = ConstructASTBranch(OP_RET, RET_NONE, Tree, 0);
|
||||||
|
|
||||||
|
@ -139,7 +136,6 @@ struct ASTNode* ReturnStatement() {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
struct ASTNode* ParseIdentifier() {
|
struct ASTNode* ParseIdentifier() {
|
||||||
struct ASTNode* Left, *Right, *Tree;
|
struct ASTNode* Left, *Right, *Tree;
|
||||||
int LeftType, RightType;
|
int LeftType, RightType;
|
||||||
|
@ -175,7 +171,7 @@ struct ASTNode* ParseIdentifier() {
|
||||||
Tree = ConstructASTNode(OP_ASSIGN, RET_INT, Left, NULL, Right, 0);
|
Tree = ConstructASTNode(OP_ASSIGN, RET_INT, Left, NULL, Right, 0);
|
||||||
|
|
||||||
return Tree;
|
return Tree;
|
||||||
}*/
|
}
|
||||||
|
|
||||||
struct ASTNode* IfStatement() {
|
struct ASTNode* IfStatement() {
|
||||||
struct ASTNode* Condition, *True, *False = NULL;
|
struct ASTNode* Condition, *True, *False = NULL;
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
int c;
|
|
||||||
int d;
|
|
||||||
int *e;
|
|
||||||
int f;
|
|
||||||
|
|
||||||
int :: main() {
|
|
||||||
c= 12;
|
|
||||||
d=18;
|
|
||||||
PrintInteger(c);
|
|
||||||
e= &c + 1;
|
|
||||||
f= *e;
|
|
||||||
PrintInteger(f);
|
|
||||||
return(0);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user