Prepare for bitwise, boolean and PPMM operators

This commit is contained in:
Curle 2020-11-23 20:01:36 +00:00
parent 245daeb6f9
commit e44158f3b7
Signed by: TheCurle
GPG Key ID: 5942F13718443F79
4 changed files with 75 additions and 25 deletions

View File

@ -15,6 +15,10 @@
* KeyWords are prefixed KW.
* TYpes are prefixed TY.
* CoMParisons are prefixed CMP.
* BOOLean maths is prefixed BOOL.
* BITwise maths is prefixed BIT.
* Arithmetic SHifts are prefixed SH.
* PlusPlusMinusMinus operators are prefixed PPMM.
*
*
* NOTE: Tokens are different from Syntax Operations!
@ -29,10 +33,12 @@ enum TokenTypes {
LI_EOF,
LI_EQUAL, // =
AR_PLUS, // Arithmetic +
AR_MINUS, // Arithmetic -
AR_STAR, // Arithmetic *
AR_SLASH, // Arithmetic /
BOOL_OR, // Boolean OR (||)
BOOL_AND, // Boolean AND (&&)
BIT_OR, // Bitwise OR (|)
BIT_XOR, // Bitwise XOR (^)
BIT_AND, // Bitwise AND (&)
CMP_EQUAL, // =?
CMP_INEQ, // !=
@ -41,6 +47,21 @@ enum TokenTypes {
CMP_LTE, // <=
CMP_GTE, // =>
SH_LEFT, // Left Shift (<<)
SH_RIGHT, // Right Shift (>>)
AR_PLUS, // Arithmetic +
AR_MINUS, // Arithmetic -
AR_STAR, // Arithmetic *
AR_SLASH, // Arithmetic /
PPMM_PLUS, // PPMM Increment (++)
PPMM_MINUS, // PPMM Decrement (--)
BOOL_INVERT, // Boolean Invert (!)
BIT_NOT, // Bitwise NOT (¬)
LI_INT, // Integer literal
LI_STR, // String literal
LI_SEMIC, // ;
@ -89,10 +110,12 @@ enum TokenTypes {
enum SyntaxOps {
OP_ASSIGN = 1, // Assign an l-value
OP_ADD, // Add two numbers.
OP_SUBTRACT, // Subtract two numbers.
OP_MULTIPLY, // Multiply two numbers.
OP_DIVIDE, // Divide two numbers.
OP_BOOLOR, // Boolean OR two statements
OP_BOOLAND, // Boolean AND two statements
OP_BITOR, // Bitwise OR a number
OP_BITXOR, // Bitwise XOR a number
OP_BITAND, // Bitwise AND a number
OP_EQUAL, // Compare equality
OP_INEQ, // Compare inequality
@ -101,6 +124,19 @@ enum SyntaxOps {
OP_LESSE, // Less than or Equal to?
OP_GREATE, // Greater than or Equal to?
OP_SHIFTL, // Arithmetic Shift Left (Multiply by 2)
OP_SHIFTR, // Arithmetic Shift Right (Divide by 2)
OP_ADD, // Add two numbers.
OP_SUBTRACT, // Subtract two numbers.
OP_MULTIPLY, // Multiply two numbers.
OP_DIVIDE, // Divide two numbers.
OP_INCREMENT, // Increment a number (pre or postfix)
OP_DECREMENT, // Decrement a number (pre or postfix)
OP_BITNOT, // Invert a number bitwise
OP_BOOLNOT, // Invert a statement
OP_ADDRESS, // Fetch the address of a var
OP_DEREF, // Get the value of the address in a pointer

View File

@ -17,10 +17,12 @@ char* TokenNames[] = {
"End of file",
"Equivalency",
"Addition",
"Subtraction",
"Multiplication",
"Division",
"Boolean Logic OR",
"Boolean Logic AND",
"Bitwise OR",
"Bitwise XOR",
"Bitwise AND",
"Equality Check",
"Inequality Check",
@ -29,6 +31,15 @@ char* TokenNames[] = {
"Less Than or Equal",
"Greater Than or Equal",
"Left Shift",
"Right Shift",
"Addition",
"Subtraction",
"Multiplication",
"Division",
"Integer literal",
"Statement End",

View File

@ -17,18 +17,23 @@
* };
*
*/
static int Precedence[] =
{ 0, 10, // EOF, ASSIGN
20, 20, // + -
30, 30, // * /
40, 40, // =? !=
50, 50, // < >
50, 50}; // <= =>
static int Precedence[] = {
0, 10, // EOF, ASSIGN
20, 30, // || &&
40, 50, // | ^
60, 70, // & =?
70, 80, // != <
80, 80, // > <=
80, 90, // => <<
90, 100, // >> +
100, 110, // - *
110 // /
};
static int OperatorPrecedence(int Token) {
int Prec = Precedence[Token];
if(Prec == 0 || Token >= LI_INT) {
if(Prec == 0 || Token >= PPMM_PLUS) {
DieMessage("Attempting to determine operator precedence of an EOF or INT literal", TokenNames[Token]);
}
@ -475,7 +480,7 @@ void ParseGlobals() {
struct ASTNode* Tree;
int Type, FunctionComing;
printf("Parsing global definitions\n");
printf("Parsing global definitions\r\n");
while(1) {
printf("New definition incoming..\r\n\n");

View File

@ -16,7 +16,7 @@ int PointerTo(int Type) {
// 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;
return Type + (PTR_CHAR - RET_CHAR);
} else {
DieDecimal("Unable to create a pointer to the desired type", Type);
}
@ -41,11 +41,9 @@ int ParseOptionalPointer() {
int Type;
// TODO: THIS IS WRONG AND SHOULD NOT EXIST
// TY_CHAR is 25, RET_CHAR is 1.
// Offset is 20. Rest are in order
if(CurrentToken.type >= TY_CHAR && CurrentToken.type <= TY_VOID) {
Type = CurrentToken.type - 24;
Type = CurrentToken.type - (TY_CHAR - RET_CHAR);
printf("\t\tConverting a %s token to a %s type.\n", TokenNames[CurrentToken.type], TypeNames[Type]);
} else {
DieDecimal("Illegal type for pointerisation", CurrentToken.type);