symbol.cpp
6.61 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <poincare/symbol.h>
#include <poincare/context.h>
#include <poincare/complex.h>
#include <poincare/division.h>
#include <poincare/layout_engine.h>
#include <poincare/parenthesis.h>
#include <poincare/power.h>
#include <poincare/multiplication.h>
#include "layout/baseline_relative_layout.h"
#include "layout/string_layout.h"
#include <ion.h>
extern "C" {
#include <assert.h>
}
#include <cmath>
namespace Poincare {
const char * Symbol::textForSpecialSymbols(char name) const {
switch (name) {
case SpecialSymbols::Ans:
return "ans";
case SpecialSymbols::un:
return "u(n)";
case SpecialSymbols::un1:
return "u(n+1)";
case SpecialSymbols::vn:
return "v(n)";
case SpecialSymbols::vn1:
return "v(n+1)";
case SpecialSymbols::M0:
return "M0";
case SpecialSymbols::M1:
return "M1";
case SpecialSymbols::M2:
return "M2";
case SpecialSymbols::M3:
return "M3";
case SpecialSymbols::M4:
return "M4";
case SpecialSymbols::M5:
return "M5";
case SpecialSymbols::M6:
return "M6";
case SpecialSymbols::M7:
return "M7";
case SpecialSymbols::M8:
return "M8";
case SpecialSymbols::M9:
return "M9";
default:
assert(false);
return nullptr;
}
}
Symbol::SpecialSymbols Symbol::matrixSymbol(char index) {
switch (index - '0') {
case 0:
return SpecialSymbols::M0;
case 1:
return SpecialSymbols::M1;
case 2:
return SpecialSymbols::M2;
case 3:
return SpecialSymbols::M3;
case 4:
return SpecialSymbols::M4;
case 5:
return SpecialSymbols::M5;
case 6:
return SpecialSymbols::M6;
case 7:
return SpecialSymbols::M7;
case 8:
return SpecialSymbols::M8;
case 9:
return SpecialSymbols::M9;
default:
assert(false);
return SpecialSymbols::M0;
}
}
Symbol::Symbol(char name) :
m_name(name)
{
}
Symbol::Symbol(Symbol&& other) :
m_name(other.m_name)
{
}
Symbol::Symbol(const Symbol& other) :
m_name(other.m_name)
{
}
Expression * Symbol::clone() const {
return new Symbol(m_name);
}
Expression * Symbol::replaceSymbolWithExpression(char symbol, Expression * expression) {
if (m_name == symbol) {
Expression * value = expression->clone();
if (parent() && value->needParenthesisWithParent(parent())) {
value = new Parenthesis(value, false);
}
return replaceWith(value, true);
}
return this;
}
Expression::Sign Symbol::sign() const {
/* TODO: Maybe, we will want to know that from a context given in parameter:
if (context.expressionForSymbol(this) != nullptr) {
return context.expressionForSymbol(this)->sign(context);
}*/
if (m_name == Ion::Charset::SmallPi) {
return Sign::Positive;
}
if (m_name == Ion::Charset::Exponential) {
return Sign::Positive;
}
return Sign::Unknown;
}
bool Symbol::isApproximate(Context & context) const {
// TODO: so far, all symbols A to Z, M0->M9 hold an approximate values. But they should be able to hold exact values later.
if (isScalarSymbol() || isMatrixSymbol()) {
return true;
}
return false;
}
bool Symbol::hasAnExactRepresentation(Context & context) const {
if (m_name == Ion::Charset::IComplex) {
return true;
}
// TODO: so far, no symbols can be exact but A, ..Z should be able to hold exact values later.
return false;
}
Expression * Symbol::shallowReduce(Context& context, AngleUnit angleUnit) {
// Do not replace symbols in expression of type: 3->A
if (parent()->type() == Type::Store && parent()->operand(1) == this) {
return this;
}
const Expression * e = context.expressionForSymbol(this);
if (e != nullptr && hasAnExactRepresentation(context)) { // TODO: later A...Z should be replaced.
/* The stored expression had been beautified which forces to call deepReduce. */
return replaceWith(e->clone(), true)->deepReduce(context, angleUnit);
}
return this;
}
template<typename T>
Expression * Symbol::templatedApproximate(Context& context, AngleUnit angleUnit) const {
if (context.expressionForSymbol(this) != nullptr) {
return context.expressionForSymbol(this)->approximate<T>(context, angleUnit);
}
return new Complex<T>(Complex<T>::Float(NAN));
}
Expression::Type Symbol::type() const {
return Expression::Type::Symbol;
}
char Symbol::name() const {
return m_name;
}
ExpressionLayout * Symbol::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
assert(floatDisplayMode != FloatDisplayMode::Default);
assert(complexFormat != ComplexFormat::Default);
if (m_name == SpecialSymbols::Ans) {
return new StringLayout("ans", 3);
}
if (m_name == SpecialSymbols::un) {
return new BaselineRelativeLayout(new StringLayout("u", 1), new StringLayout("n",1, KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
}
if (m_name == SpecialSymbols::un1) {
return new BaselineRelativeLayout(new StringLayout("u", 1), new StringLayout("n+1",3, KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
}
if (m_name == SpecialSymbols::vn) {
return new BaselineRelativeLayout(new StringLayout("v", 1), new StringLayout("n",1, KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
}
if (m_name == SpecialSymbols::vn1) {
return new BaselineRelativeLayout(new StringLayout("v", 1), new StringLayout("n+1",3, KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
}
if (isMatrixSymbol()) {
const char mi[] = { 'M', (char)(m_name-(char)SpecialSymbols::M0+'0') };
return new StringLayout(mi, sizeof(mi));
}
return new StringLayout(&m_name, 1);
}
int Symbol::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const {
if (bufferSize == 0) {
return -1;
}
if (bufferSize == 1) {
buffer[bufferSize-1] = 0;
return 0;
}
/* Special cases for all special symbols */
if (m_name >0 && m_name < 32) {
return strlcpy(buffer, textForSpecialSymbols(m_name), bufferSize);
}
buffer[0] = m_name;
buffer[1] = 0;
return 1;
}
bool Symbol::isMatrixSymbol() const {
if (m_name >= (char)SpecialSymbols::M0 && m_name <= (char)SpecialSymbols::M9) {
return true;
}
return false;
}
bool Symbol::isScalarSymbol() const {
if (m_name >= 'A' && m_name <= 'Z') {
return true;
}
return false;
}
int Symbol::simplificationOrderSameType(const Expression * e, bool canBeInterrupted) const {
assert(e->type() == Expression::Type::Symbol);
if ((uint8_t)m_name == ((uint8_t)static_cast<const Symbol *>(e)->name())) {
return 0;
}
if ((uint8_t)m_name > ((uint8_t)static_cast<const Symbol *>(e)->name())) {
return 1;
}
return -1;
}
}