store.cpp
2.41 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
extern "C" {
#include <assert.h>
#include <stdlib.h>
#include <math.h>
}
#include <poincare/store.h>
#include <ion.h>
#include <poincare/complex_matrix.h>
#include <poincare/complex.h>
#include <poincare/context.h>
#include "layout/horizontal_layout.h"
#include "layout/string_layout.h"
namespace Poincare {
Store::Store(Symbol * symbol, Expression * value, bool clone) :
m_symbol(symbol),
m_value(value)
{
if (clone) {
m_symbol = (Symbol *)symbol->clone();
m_value = value->clone();
}
}
Store::~Store() {
delete m_symbol;
delete m_value;
}
bool Store::hasValidNumberOfArguments() const {
return m_value->hasValidNumberOfArguments();
}
Expression::Type Store::type() const {
return Type::Store;
}
const Expression * Store::operand(int i) const {
if (i == 0) {
return m_symbol;
}
return m_value;
}
int Store::numberOfOperands() const {
return 2;
}
Expression * Store::clone() const {
Expression * newOperands[2];
newOperands[0] = m_symbol;
newOperands[1] = m_value;
return this->cloneWithDifferentOperands(newOperands, 2, true);
}
Expression * Store::cloneWithDifferentOperands(Expression ** newOperands, int numberOfOperands, bool cloneOperands) const {
assert(numberOfOperands == 2);
assert(newOperands != nullptr);
assert(newOperands[0]->type() == Type::Symbol);
return new Store((Symbol *)newOperands[0], newOperands[1], cloneOperands);
}
ExpressionLayout * Store::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
assert(floatDisplayMode != FloatDisplayMode::Default);
assert(complexFormat != ComplexFormat::Default);
ExpressionLayout * childrenLayouts[3];
childrenLayouts[0] = m_value->createLayout(floatDisplayMode, complexFormat);
const char stoSymbol[2] = {Ion::Charset::Sto, 0};
childrenLayouts[1] = new StringLayout(stoSymbol, 1);
childrenLayouts[2] = m_symbol->createLayout(floatDisplayMode, complexFormat);
return new HorizontalLayout(childrenLayouts, 3);
}
template<typename T>
Evaluation<T> * Store::templatedEvaluate(Context& context, AngleUnit angleUnit) const {
Evaluation<T> * valueEvaluation = m_value->evaluate<T>(context, angleUnit);
context.setExpressionForSymbolName(valueEvaluation, m_symbol);
delete valueEvaluation;
if (context.expressionForSymbol(m_symbol) != nullptr) {
return context.expressionForSymbol(m_symbol)->evaluate<T>(context, angleUnit);
}
return new Complex<T>(Complex<T>::Float(NAN));
}
}