ExpressionParser: Add multiplication and division operators. (division by zero evaluates as zero). Don't clamp result of addition operator. Clamping will be done later.

This commit is contained in:
Jordan Woyak 2018-12-30 11:51:12 -06:00
parent f3192ca06d
commit bf63f85d73

View File

@ -28,6 +28,8 @@ enum TokenType
TOK_OR, TOK_OR,
TOK_NOT, TOK_NOT,
TOK_ADD, TOK_ADD,
TOK_MUL,
TOK_DIV,
TOK_CONTROL, TOK_CONTROL,
TOK_LITERAL, TOK_LITERAL,
}; };
@ -44,6 +46,10 @@ inline std::string OpName(TokenType op)
return "Not"; return "Not";
case TOK_ADD: case TOK_ADD:
return "Add"; return "Add";
case TOK_MUL:
return "Mul";
case TOK_DIV:
return "Div";
default: default:
assert(false); assert(false);
return ""; return "";
@ -78,6 +84,10 @@ public:
return "!"; return "!";
case TOK_ADD: case TOK_ADD:
return "+"; return "+";
case TOK_MUL:
return "*";
case TOK_DIV:
return "/";
case TOK_CONTROL: case TOK_CONTROL:
return "Device(" + data + ")"; return "Device(" + data + ")";
case TOK_LITERAL: case TOK_LITERAL:
@ -170,6 +180,10 @@ public:
return Token(TOK_NOT); return Token(TOK_NOT);
case '+': case '+':
return Token(TOK_ADD); return Token(TOK_ADD);
case '*':
return Token(TOK_MUL);
case '/':
return Token(TOK_DIV);
case '\'': case '\'':
return GetLiteral(); return GetLiteral();
case '`': case '`':
@ -266,7 +280,14 @@ public:
case TOK_OR: case TOK_OR:
return std::max(lhsValue, rhsValue); return std::max(lhsValue, rhsValue);
case TOK_ADD: case TOK_ADD:
return std::min(lhsValue + rhsValue, 1.0); return lhsValue + rhsValue;
case TOK_MUL:
return lhsValue * rhsValue;
case TOK_DIV:
{
const ControlState result = lhsValue / rhsValue;
return std::isinf(result) ? 0.0 : result;
}
default: default:
assert(false); assert(false);
return 0; return 0;
@ -508,6 +529,8 @@ private:
case TOK_AND: case TOK_AND:
case TOK_OR: case TOK_OR:
case TOK_ADD: case TOK_ADD:
case TOK_MUL:
case TOK_DIV:
return true; return true;
default: default:
return false; return false;