-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separating out Operator from eval_expression.
- Loading branch information
Showing
2 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|