From 8332da3dddbe564c5cdf56a5b5081e8a21b48760 Mon Sep 17 00:00:00 2001 From: Michael Kohn Date: Thu, 17 Oct 2024 20:14:28 -0500 Subject: [PATCH] Separating out Operator from eval_expression. --- common/Operator.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++ common/Operator.h | 70 +++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 common/Operator.cpp create mode 100644 common/Operator.h diff --git a/common/Operator.cpp b/common/Operator.cpp new file mode 100644 index 00000000..394c6d8c --- /dev/null +++ b/common/Operator.cpp @@ -0,0 +1,124 @@ +/** + * naken_asm assembler. + * Author: Michael Kohn + * Email: mike@mikekohn.net + * Web: https://www.mikekohn.net/ + * License: GPLv3 + * + * Copyright 2010-2024 by Michael Kohn + * + */ + +#include "Operator.h" + +bool Operator::is_math_operator(const char *token) +{ + if (token[1] != 0) { return false; } + + switch (token[0]) + { + case '*': + case '/': + case '%': + case '+': + case '-': + case '&': + case '^': + case '|': + return true; + default: + return false; + } +} + +bool Operator::set_operator(const char *token) +{ + // Single character token. + if (token[1] == 0) + { + switch (token[0]) + { + case '~': + precedence = PREC_NOT; + operation = OPER_NOT; + break; + case '*': + precedence = PREC_MUL; + operation = OPER_MUL; + break; + case '/': + precedence = PREC_MUL; + operation = OPER_DIV; + break; + case '%': + precedence = PREC_MUL; + operation = OPER_MOD; + break; + case '+': + precedence = PREC_ADD; + operation = OPER_PLUS; + break; + case '-': + precedence = PREC_ADD; + operation = OPER_MINUS; + break; + case '&': + precedence = PREC_AND; + operation = OPER_AND; + break; + case '^': + precedence = PREC_XOR; + operation = OPER_XOR; + break; + case '|': + precedence = PREC_OR; + operation = OPER_OR; + break; + default: + return false; + } + + return true; + } + + // If token is more than 2 characters, return false. + if (token[2] != 0) { return false; } + + if (token[0] == '<' && token[1] == '<') + { + precedence = PREC_SHIFT; + operation = OPER_LEFT_SHIFT; + return true; + } + else + if (token[0] == '>' && token[1] == '>') + { + precedence = PREC_SHIFT; + operation = OPER_RIGHT_SHIFT; + return true; + } + + return false; +} + +const char *Operator::to_string() +{ + const char *s[] = + { + "", // OPER_UNSET: + "~", // OPER_NOT: + "*", // OPER_MUL: + "/", // OPER_DIV: + "%", // OPER_MOD: + "+", // OPER_PLUS: + "-", // OPER_MINUS: + "<<", // OPER_LEFT_SHIFT: + ">>", // OPER_RIGHT_SHIFT: + "&", // OPER_AND: + "^", // OPER_XOR: + "|" // OPER_OR:: + }; + + return s[operation]; +} + diff --git a/common/Operator.h b/common/Operator.h new file mode 100644 index 00000000..5b9bc6d4 --- /dev/null +++ b/common/Operator.h @@ -0,0 +1,70 @@ +/** + * naken_asm assembler. + * Author: Michael Kohn + * Email: mike@mikekohn.net + * Web: https://www.mikekohn.net/ + * License: GPLv3 + * + * Copyright 2010-2024 by Michael Kohn + * + */ + +#ifndef NAKEN_ASM_OPERATOR_H +#define NAKEN_ASM_OPERATOR_H + +class Operator +{ +public: + Operator() : + operation (OPER_UNSET), + precedence (PREC_UNSET) + { + } + + bool is_unset() { return operation == OPER_UNSET; } + bool is_set() { return operation != OPER_UNSET; } + + void reset() + { + operation = OPER_UNSET; + precedence = PREC_UNSET; + } + + bool is_math_operator(const char *token); + bool set_operator(const char *token); + const char *to_string(); + + enum + { + PREC_NOT, + PREC_MUL, + PREC_ADD, + PREC_SHIFT, + PREC_AND, + PREC_XOR, + PREC_OR, + PREC_UNSET + }; + + enum + { + OPER_UNSET, + OPER_NOT, + OPER_MUL, + OPER_DIV, + OPER_MOD, + OPER_PLUS, + OPER_MINUS, + OPER_LEFT_SHIFT, + OPER_RIGHT_SHIFT, + OPER_AND, + OPER_XOR, + OPER_OR + }; + + int operation; + int precedence; +}; + +#endif +