/
recreator.h
137 lines (122 loc) · 2.93 KB
/
recreator.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
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
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: recreator.h - recreator class header file
// Copyright (C) 2013-2015 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2013-11-02 initial version
#ifndef RECREATOR_H
#define RECREATOR_H
#include <stack>
#include <string>
#include "ibcp.h"
class RpnList;
class Table;
class Recreator
{
struct StackItem
{
StackItem(std::string string, int precedence = HighestPrecedence,
bool unaryOperator = false) : m_string {string},
m_precedence {precedence}, m_unaryOperator {unaryOperator} {}
std::string m_string; // string of stack item
int m_precedence; // precedence of stack item
bool m_unaryOperator; // stack item is a unary operator
};
public:
explicit Recreator();
std::string operator()(const RpnList &rpnList, bool exprMode = false);
// holding stack access functions
template <typename... Args>
void emplace(Args&&... args)
{
m_stack.emplace(std::forward<Args>(args)...);
}
std::string popString()
{
std::string string = std::move(m_stack.top().m_string);
m_stack.pop();
return string;
}
int topPrecedence() const
{
return m_stack.top().m_precedence;
}
bool topUnaryOperator() const
{
return m_stack.top().m_unaryOperator;
}
void topAppend(std::string &&string)
{
m_stack.top().m_string += string;
}
void topAppend(char c)
{
m_stack.top().m_string += c;
}
void topAddParens()
{
m_stack.top().m_string = '(' + std::move(m_stack.top().m_string) + ')';
}
std::string popWithParens(bool addParens);
void pushWithOperands(std::string &&name, int count);
bool empty() const
{
return m_stack.empty();
}
// output string access functions
void append(std::string &&string)
{
m_output += string;
}
void append(char c)
{
m_output += c;
}
bool backIsNotSpace()
{
return !m_output.empty() && m_output.back() != ' ';
}
// separator access functions
char separator() const
{
return m_separator;
}
bool separatorIsSet() const
{
return m_separator != '\0';
}
bool separatorIsSet(char separator) const
{
return m_separator == separator;
}
void clearSeparator()
{
m_separator = '\0';
}
void setSeparator(char separator)
{
m_separator = separator;
}
private:
std::stack<StackItem> m_stack; // holding string stack
char m_separator; // current separator character
std::string m_output; // output string
};
#endif // RECREATOR_H