variable_context.cpp
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <poincare/variable_context.h>
#include <poincare/preferences.h>
#include <assert.h>
#include <cmath>
namespace Poincare {
template<typename T>
VariableContext<T>::VariableContext(char name, Context * parentContext) :
m_name(name),
m_value(Approximation<T>(NAN)),
m_parentContext(parentContext)
{
}
template<typename T>
void VariableContext<T>::setApproximationForVariable(T value) {
m_value = Approximation<T>(value);
}
template<typename T>
void VariableContext<T>::setExpressionForSymbolName(const Expression * expression, const Symbol * symbol, Context & context) {
if (symbol->name() == m_name) {
if (expression == nullptr) {
return;
}
m_value = Approximation<T>(expression->approximateToScalar<T>(context, Preferences::sharedPreferences()->angleUnit()));
} else {
m_parentContext->setExpressionForSymbolName(expression, symbol, context);
}
}
template<typename T>
const Expression * VariableContext<T>::expressionForSymbol(const Symbol * symbol) {
if (symbol->name() == m_name) {
return &m_value;
} else {
return m_parentContext->expressionForSymbol(symbol);
}
}
template class Poincare::VariableContext<float>;
template class Poincare::VariableContext<double>;
}