Skip to content
Permalink
Browse files

Added rounding FOCS functions.

  • Loading branch information...
geoffthemedio committed Jun 27, 2018
1 parent e3e8108 commit ac8cc3e6c17da7042f26e0196e329d89fe977aa8
Showing with 48 additions and 5 deletions.
  1. +3 −0 parse/ArithmeticRules.cpp
  2. +3 −0 parse/Tokens.h
  3. +25 −4 universe/ValueRef.cpp
  4. +13 −0 universe/ValueRef.h
  5. +4 −1 universe/ValueRefFwd.h
@@ -41,6 +41,9 @@ namespace parse { namespace detail {
| tok.Cos_ [ _c = ValueRef::COSINE ]
| tok.Log_ [ _c = ValueRef::LOGARITHM ]
| tok.Abs_ [ _c = ValueRef::ABS ]
| tok.Round_ [ _c = ValueRef::ROUND_NEAREST ]
| tok.Ceil_ [ _c = ValueRef::ROUND_UP ]
| tok.Floor_ [ _c = ValueRef::ROUND_DOWN ]
)
>> ('(' > expr > ')') [ _val = construct_movable_(new_<ValueRef::Operation<T>>(_c, deconstruct_movable_(_1, _pass))) ]
)
@@ -52,6 +52,7 @@
(Capture) \
(CaptureResult) \
(Category) \
(Ceil) \
(Class) \
(ClockwiseNextPlanetType) \
(Colony) \
@@ -128,6 +129,7 @@
(Fleet) \
(FleetID) \
(FleetSupplyableByEmpire) \
(Floor) \
(Foci) \
(Focus) \
(FocusType) \
@@ -351,6 +353,7 @@
(Retain) \
(RMS) \
(RootCandidate) \
(Round) \
(Scope)

#define TOKEN_SEQ_11 \
@@ -3027,7 +3027,7 @@ double Operation<double>::EvalImpl(const ScriptingContext& context) const
}

case NEGATE:
return -(LHS()->Eval(context)); break;
return -(LHS()->Eval(context)); break;

case EXPONENTIATE: {
double op2 = RHS()->Eval(context);
@@ -3057,10 +3057,10 @@ double Operation<double>::EvalImpl(const ScriptingContext& context) const
}

case SINE:
return std::sin(LHS()->Eval(context)); break;
return std::sin(LHS()->Eval(context)); break;

case COSINE:
return std::cos(LHS()->Eval(context)); break;
return std::cos(LHS()->Eval(context)); break;

case MINIMUM:
case MAXIMUM: {
@@ -3130,6 +3130,19 @@ double Operation<double>::EvalImpl(const ScriptingContext& context) const
}
}

case ROUND_NEAREST: {
return std::round(LHS()->Eval(context));
break;
}
case ROUND_UP: {
return std::ceil(LHS()->Eval(context));
break;
}
case ROUND_DOWN: {
return std::floor(LHS()->Eval(context));
break;
}

default: break;
}

@@ -3164,7 +3177,7 @@ int Operation<int>::EvalImpl(const ScriptingContext& context) const
}

case NEGATE:
return -LHS()->Eval(context); break;
return -LHS()->Eval(context); break;

case EXPONENTIATE: {
double op2 = RHS()->Eval(context);
@@ -3240,6 +3253,14 @@ int Operation<int>::EvalImpl(const ScriptingContext& context) const
break;
}

case ROUND_NEAREST:
case ROUND_UP:
case ROUND_DOWN: {
// integers don't need to be rounded...
return LHS()->Eval(context);
break;
}

case COMPARE_EQUAL:
case COMPARE_GREATER_THAN:
case COMPARE_GREATER_THAN_OR_EQUAL:
@@ -2133,6 +2133,13 @@ std::string Operation<T>::Description() const
return retval;
}

if (m_op_type == ROUND_NEAREST)
return "round(" + LHS()->Description() + ")";
if (m_op_type == ROUND_UP)
return "ceil(" + LHS()->Description() + ")";
if (m_op_type == ROUND_DOWN)
return "floor(" + LHS()->Description() + ")";

bool parenthesize_lhs = false;
bool parenthesize_rhs = false;
if (auto lhs = dynamic_cast<const Operation<T>*>(LHS())) {
@@ -2242,6 +2249,12 @@ std::string Operation<T>::Dump(unsigned short ntabs) const
return retval;
}

if (m_op_type == ROUND_NEAREST)
return "round(" + LHS()->Dump(ntabs) + ")";
if (m_op_type == ROUND_UP)
return "ceil(" + LHS()->Dump(ntabs) + ")";
if (m_op_type == ROUND_DOWN)
return "floor(" + LHS()->Dump(ntabs) + ")";

bool parenthesize_lhs = false;
bool parenthesize_rhs = false;
@@ -60,7 +60,10 @@ namespace ValueRef {
COMPARE_GREATER_THAN_OR_EQUAL,
COMPARE_LESS_THAN,
COMPARE_LESS_THAN_OR_EQUAL,
COMPARE_NOT_EQUAL
COMPARE_NOT_EQUAL,
ROUND_NEAREST,
ROUND_UP,
ROUND_DOWN
};
}

0 comments on commit ac8cc3e

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