From e44158f3b711387de8a5f0db598e573b72b2a50a Mon Sep 17 00:00:00 2001 From: Curle Date: Mon, 23 Nov 2020 20:01:36 +0000 Subject: [PATCH] Prepare for bitwise, boolean and PPMM operators --- include/Defs.h | 52 ++++++++++++++++++++++++++++++++++++++++++-------- src/Main.c | 19 ++++++++++++++---- src/Parser.c | 23 +++++++++++++--------- src/Pointers.c | 6 ++---- 4 files changed, 75 insertions(+), 25 deletions(-) diff --git a/include/Defs.h b/include/Defs.h index 8f96c6f..9e440cc 100644 --- a/include/Defs.h +++ b/include/Defs.h @@ -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,11 +110,13 @@ 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 OP_LESS, // Less than? @@ -101,7 +124,20 @@ 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 diff --git a/src/Main.c b/src/Main.c index effd54e..2296a24 100644 --- a/src/Main.c +++ b/src/Main.c @@ -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", diff --git a/src/Parser.c b/src/Parser.c index 41e039b..d790a99 100644 --- a/src/Parser.c +++ b/src/Parser.c @@ -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"); diff --git a/src/Pointers.c b/src/Pointers.c index ed067e4..f7a2666 100644 --- a/src/Pointers.c +++ b/src/Pointers.c @@ -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);