-
Notifications
You must be signed in to change notification settings - Fork 11
/
rpn_stack.h
75 lines (58 loc) · 1.97 KB
/
rpn_stack.h
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
#ifndef RPN_STACK_H
#define RPN_STACK_H
#include <escher.h>
#include <poincare/expression.h>
#include <apps/i18n.h>
#include <stddef.h>
namespace Rpn {
class Stack {
public:
constexpr static size_t k_stackSize = 8;
constexpr static int k_expressionSize = TextField::maxBufferSize();
enum StackOperation {
DUP,
SWAP,
ROT,
OVER,
POP,
CLEAR
} ;
enum SpecialOperation {
Exp,
CommonLogarithm,
Square
} ;
private:
struct Element {
Element();
Element(Poincare::Expression &exp, Poincare::Context &context);
char expression[k_expressionSize];
char approximate[k_expressionSize];
KDCoordinate expressionHeight;
KDCoordinate approximateHeight;
};
public:
Stack() = default;
const char* operator[](size_t idx) const { return approximate ? m_stack[idx].approximate : m_stack[idx].expression; }
Poincare::Expression operator()(size_t idx, Poincare::Context* context = nullptr) const { return Poincare::Expression::Parse(m_stack[idx].expression, context); }
KDCoordinate height(size_t idx) const { return approximate ? m_stack[idx].approximateHeight : m_stack[idx].expressionHeight; }
I18n::Message operator()(const char* text, Poincare::Context *context);
I18n::Message operator()(StackOperation op);
I18n::Message operator()(SpecialOperation op, Poincare::Context *context);
I18n::Message operator()(Poincare::ExpressionNode::Type op, Poincare::Context *context);
I18n::Message operator()(I18n::Message op, Poincare::Context *context);
void dropNth(size_t index);
size_t length() const { return m_length; }
bool full() const { return m_length == k_stackSize; }
bool empty() const { return m_length == 0; }
bool approximate;
private:
I18n::Message doOperation(Poincare::Expression e, Poincare::Context &context, int nargs);
I18n::Message push(Poincare::Expression e, Poincare::Context &context);
I18n::Message push(Element exp);
void pop();
Element m_stack[k_stackSize];
size_t m_length;
};
}
#endif