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. * KeyWords are prefixed KW.
* TYpes are prefixed TY. * TYpes are prefixed TY.
* CoMParisons are prefixed CMP. * 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! * NOTE: Tokens are different from Syntax Operations!
@ -29,10 +33,12 @@ enum TokenTypes {
LI_EOF, LI_EOF,
LI_EQUAL, // = LI_EQUAL, // =
AR_PLUS, // Arithmetic + BOOL_OR, // Boolean OR (||)
AR_MINUS, // Arithmetic - BOOL_AND, // Boolean AND (&&)
AR_STAR, // Arithmetic *
AR_SLASH, // Arithmetic / BIT_OR, // Bitwise OR (|)
BIT_XOR, // Bitwise XOR (^)
BIT_AND, // Bitwise AND (&)
CMP_EQUAL, // =? CMP_EQUAL, // =?
CMP_INEQ, // != CMP_INEQ, // !=
@ -41,6 +47,21 @@ enum TokenTypes {
CMP_LTE, // <= CMP_LTE, // <=
CMP_GTE, // => 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_INT, // Integer literal
LI_STR, // String literal LI_STR, // String literal
LI_SEMIC, // ; LI_SEMIC, // ;
@ -89,11 +110,13 @@ 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_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_EQUAL, // Compare equality
OP_INEQ, // Compare inequality OP_INEQ, // Compare inequality
OP_LESS, // Less than? OP_LESS, // Less than?
@ -101,7 +124,20 @@ 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_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_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

View File

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

View File

@ -17,18 +17,23 @@
* }; * };
* *
*/ */
static int Precedence[] = static int Precedence[] = {
{ 0, 10, // EOF, ASSIGN 0, 10, // EOF, ASSIGN
20, 20, // + - 20, 30, // || &&
30, 30, // * / 40, 50, // | ^
40, 40, // =? != 60, 70, // & =?
50, 50, // < > 70, 80, // != <
50, 50}; // <= => 80, 80, // > <=
80, 90, // => <<
90, 100, // >> +
100, 110, // - *
110 // /
};
static int OperatorPrecedence(int Token) { static int OperatorPrecedence(int Token) {
int Prec = Precedence[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]); DieMessage("Attempting to determine operator precedence of an EOF or INT literal", TokenNames[Token]);
} }
@ -475,7 +480,7 @@ void ParseGlobals() {
struct ASTNode* Tree; struct ASTNode* Tree;
int Type, FunctionComing; int Type, FunctionComing;
printf("Parsing global definitions\n"); printf("Parsing global definitions\r\n");
while(1) { while(1) {
printf("New definition incoming..\r\n\n"); 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: Make this a proper translation table
// TODO: More checks! This can go wrong easily! // TODO: More checks! This can go wrong easily!
if(Type >= RET_CHAR && Type <= RET_VOID) { if(Type >= RET_CHAR && Type <= RET_VOID) {
return Type + 4; return Type + (PTR_CHAR - RET_CHAR);
} else { } else {
DieDecimal("Unable to create a pointer to the desired type", Type); DieDecimal("Unable to create a pointer to the desired type", Type);
} }
@ -41,11 +41,9 @@ int ParseOptionalPointer() {
int Type; int Type;
// TODO: THIS IS WRONG AND SHOULD NOT EXIST // 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) { 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]); 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);