Skip to content
Permalink
Browse files

Merge pull request #149 from Prof9/master

Add min() and max() expression functions.
  • Loading branch information...
Kingcom committed May 3, 2019
2 parents b4340ac + 26a011b commit 8de69782dab8a0f2e05aa89d9186f0cff1bb5f31
@@ -240,7 +240,97 @@ 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 floatMin, floatCur;
int64_t intMin, intCur;

floatCur = floatMin = std::numeric_limits<double>::max();
intCur = intMin = std::numeric_limits<int64_t>::max();
bool isInt = true;

for (size_t i = 0; i < parameters.size(); i++)
{
switch (parameters[i].type)
{
case ExpressionValueType::Integer:
intCur = parameters[i].intValue;
floatCur = (double)parameters[i].intValue;
break;
case ExpressionValueType::Float:
floatCur = parameters[i].floatValue;
isInt = false;
break;
default:
return result;
}

if (intCur < intMin)
intMin = intCur;
if (floatCur < floatMin)
floatMin = floatCur;
}

if (isInt)
{
result.intValue = intMin;
result.type = ExpressionValueType::Integer;
}
else
{
result.floatValue = floatMin;
result.type = ExpressionValueType::Float;
}

return result;
}

ExpressionValue expFuncMax(const std::wstring& funcName, const std::vector<ExpressionValue>& parameters)
{
ExpressionValue result;
double floatMax, floatCur;
int64_t intMax, intCur;

floatCur = floatMax = std::numeric_limits<double>::min();
intCur = intMax = std::numeric_limits<int64_t>::min();
bool isInt = true;

for (size_t i = 0; i < parameters.size(); i++)
{
switch (parameters[i].type)
{
case ExpressionValueType::Integer:
intCur = parameters[i].intValue;
floatCur = (double)parameters[i].intValue;
break;
case ExpressionValueType::Float:
floatCur = parameters[i].floatValue;
isInt = false;
break;
default:
return result;
}

if (intCur > intMax)
intMax = intCur;
if (floatCur > floatMax)
floatMax = floatCur;
}

if (isInt)
{
result.intValue = intMax;
result.type = ExpressionValueType::Integer;
}
else
{
result.floatValue = floatMax;
result.type = ExpressionValueType::Float;
}

return result;
}

ExpressionValue expFuncAbs(const std::wstring& funcName, const std::vector<ExpressionValue>& parameters)
{
@@ -507,6 +597,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, 1, std::numeric_limits<size_t>::max(), ExpFuncSafety::Safe } },
{ L"max", { &expFuncMax, 1, std::numeric_limits<size_t>::max(), 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 parameters `a`, `b`, ...; result type is int if all parameters are int, float otherwise |
| `max(a, b, ...)` | maximum of int or float parameters `a`, `b`, ...; result type is int if all parameters are 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|
@@ -86,4 +86,17 @@ test1 defined,label1

test3 readascii,fileA,4,4

test1 max,1
test2 max,1,2
test3 max,1,2,3
test3 max,1,2.5,4
test1 min,1
test2 min,1,2
test3 min,1,2,3
test3 min,1,2.5,4
test2 max,-3,3
test2 min,-3,3
test2 max,-3.0,3.0
test2 min,-3.0,3.0

.close
@@ -34,3 +34,15 @@ ExpressionFunctions.asm(81) notice: regex_extract("test123test","[0-9]+"): 123
ExpressionFunctions.asm(84) notice: defined(label): 1
ExpressionFunctions.asm(85) notice: defined(label1): 0
ExpressionFunctions.asm(87) notice: readascii("file.bin",4,4): test
ExpressionFunctions.asm(89) notice: max(1): 1
ExpressionFunctions.asm(90) notice: max(1,2): 2
ExpressionFunctions.asm(91) notice: max(1,2,3): 3
ExpressionFunctions.asm(92) notice: max(1,2.5,4): 4.0000000000000000
ExpressionFunctions.asm(93) notice: min(1): 1
ExpressionFunctions.asm(94) notice: min(1,2): 1
ExpressionFunctions.asm(95) notice: min(1,2,3): 1
ExpressionFunctions.asm(96) notice: min(1,2.5,4): 1.0000000000000000
ExpressionFunctions.asm(97) notice: max((-3),3): 3
ExpressionFunctions.asm(98) notice: min((-3),3): -3
ExpressionFunctions.asm(99) notice: max((-3),3): 3.0000000000000000
ExpressionFunctions.asm(100) notice: min((-3),3): -3.0000000000000000

0 comments on commit 8de6978

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