Skip to content
Permalink
Browse files

Add min() and max() expression functions.

  • Loading branch information...
Prof9 committed Apr 25, 2019
1 parent d7e3ba4 commit 4355b1255d07d384a900f9c4091181bda1851300
Showing with 102 additions and 0 deletions.
  1. +100 −0 Core/ExpressionFunctions.cpp
  2. +2 −0 Readme.md
@@ -240,7 +240,105 @@ ExpressionValue expFuncFrac(const std::wstring& funcName, const std::vector<Expr
return result;
}

ExpressionValue expFuncMin(const std::wstring& funcName, const std::vector<ExpressionValue>& parameters)
{
ExpressionValue result;
double floatA, floatB;
int64_t intA, intB;

bool isInt = true;

switch (parameters[0].type)
{
case ExpressionValueType::Integer:
intA = parameters[0].intValue;
floatA = (double)parameters[0].intValue;
break;
case ExpressionValueType::Float:
floatA = parameters[0].floatValue;
isInt = false;
break;
default:
return result;
}

switch (parameters[1].type)
{
case ExpressionValueType::Integer:
intB = parameters[1].intValue;
floatB = (double)parameters[1].intValue;
break;
case ExpressionValueType::Float:
floatB = parameters[1].floatValue;
isInt = false;
break;
default:
return result;
}

if (isInt)
{
result.intValue = std::min(intA, intB);
result.type = ExpressionValueType::Integer;
}
else
{
result.floatValue = std::min(floatA, floatB);
result.type = ExpressionValueType::Float;
}

return result;
}

ExpressionValue expFuncMax(const std::wstring& funcName, const std::vector<ExpressionValue>& parameters)
{
ExpressionValue result;
double floatA, floatB;
int64_t intA, intB;

bool isInt = true;

switch (parameters[0].type)
{
case ExpressionValueType::Integer:
intA = parameters[0].intValue;
floatA = (double)parameters[0].intValue;
break;
case ExpressionValueType::Float:
floatA = parameters[0].floatValue;
isInt = false;
break;
default:
return result;
}

switch (parameters[1].type)
{
case ExpressionValueType::Integer:
intB = parameters[1].intValue;
floatB = (double)parameters[1].intValue;
break;
case ExpressionValueType::Float:
floatB = parameters[1].floatValue;
isInt = false;
break;
default:
return result;
}

if (isInt)
{
result.intValue = std::max(intA, intB);
result.type = ExpressionValueType::Integer;
}
else
{
result.floatValue = std::max(floatA, floatB);
result.type = ExpressionValueType::Float;
}

return result;
}

ExpressionValue expFuncAbs(const std::wstring& funcName, const std::vector<ExpressionValue>& parameters)
{
@@ -507,6 +605,8 @@ const ExpressionFunctionMap expressionFunctions = {
{ L"frac", { &expFuncFrac, 1, 1, ExpFuncSafety::Safe } },
{ L"abs", { &expFuncAbs, 1, 1, ExpFuncSafety::Safe } },
{ L"round", { &expFuncRound, 1, 1, ExpFuncSafety::Safe } },
{ L"min", { &expFuncMin, 2, 2, ExpFuncSafety::Safe } },
{ L"max", { &expFuncMax, 2, 2, ExpFuncSafety::Safe } },

{ L"strlen", { &expFuncStrlen, 1, 1, ExpFuncSafety::Safe } },
{ L"substr", { &expFuncSubstr, 3, 3, ExpFuncSafety::Safe } },
@@ -229,6 +229,8 @@ Below is a table of functions built into the assembler that can be used with the
| `abs(val)` | absolute value of int or float `val` |
| `hi(val)` | High half of 32-bit value `val`, adjusted for sign extension of low half (MIPS) |
| `lo(val)` | Sign-extended low half of 32-bit value `val` (MIPS) |
| `min(a, b)` | minimum of int or float `a` and `b`; result type is int if `a` and `b` are both int, float otherwise |
| `max(a, b)` | maximum of int or float `a` and `b`; result type is int if `a` and `b` are both int, float otherwise |
| `strlen(str)` | number of characters in `str` |
| `substr(str, start, count)` | substring of `str` from `start`, length `count` |
| `regex_match(source, regex)` | `1` if `regex` matched entire `source`, `0` otherwise|

0 comments on commit 4355b12

Please sign in to comment.
You can’t perform that action at this time.