Skip to content

Commit

Permalink
Separating out Operator from eval_expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeakohn committed Oct 18, 2024
1 parent ea01c05 commit 8332da3
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 0 deletions.
124 changes: 124 additions & 0 deletions common/Operator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* naken_asm assembler.
* Author: Michael Kohn
* Email: [email protected]
* 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];
}

70 changes: 70 additions & 0 deletions common/Operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* naken_asm assembler.
* Author: Michael Kohn
* Email: [email protected]
* 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

0 comments on commit 8332da3

Please sign in to comment.