-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
211 lines (197 loc) · 7.25 KB
/
Copy pathParser.cpp
File metadata and controls
211 lines (197 loc) · 7.25 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
#include "Parser.h"
#include <math.h>
#include <cctype>
#include <cstring>
#define C (m_program_text[idx])
TokenInfo::TokenInfo(int t, int i, std::string n, std::string tt) {
type = t, info = i, name = n, text = tt;
}
TokenInfo::TokenInfo() {
type = 0, info = 0, name = "", text = "";
}
RuaParser::RuaParser() {
m_lineno = 1, m_charno = idx = 0;
for (int i = 0; i < g_n_internal_token && g_internal_tokens[i].info; i++) {
m_tokenInfo.insert(std::make_pair(g_internal_tokens[i].info, g_internal_tokens[i]));
m_name2ID.insert(std::make_pair(g_internal_tokens[i].text, g_internal_tokens[i].info));
//m_tokenInfo[g_internal_tokens[i].info] = g_internal_tokens[i];
//m_name2ID[g_internal_tokens[i].text] = g_internal_tokens[i].info;
}
}
bool& RuaParser::GetLastIsOpe()
{
return last_is_ope;
}
int RuaParser::NewToken(int type, std::string text, std::string name)
{
TokenInfo ti(type, m_nxt_new_idx++, name, text);
m_name2ID.insert(std::make_pair(ti.text, ti.info));
m_tokenInfo.insert(std::make_pair(ti.info, ti));
return m_nxt_new_idx - 1;
}
void RuaParser::PutText(std::string program_text) {
m_program_text = program_text;
idx = m_charno = 0; m_lineno = 1;
}
TokenInfo RuaParser::Parse(RuaData& data) {
std::string token_text;
data = RuaData{0};
int tmp;
while (C != '\0') {
if (C == '\n') m_charno = idx, m_lineno ++, idx++;
else if (isblank(C)) idx++;
else if (isdigit(C) || (last_is_ope && C == '-')) {
if (C == '-') {
token_text = '-';
last_is_ope = true; idx++;
if (!isdigit(C)) return m_tokenInfo[OPE_NEG];
} else last_is_ope = false;
do {
data.i = data.i * 10 - '0' + C, token_text += C;
idx++;
} while (isdigit(C));
if (C == '.'){
data.d = (ruaFloat)data.i, tmp = 0, idx++, token_text += '.';
while (isdigit(C)) {
data.d = data.d * 10.0 - '0' + C, token_text += C;
idx++, tmp++;
}
data.d = data.d / pow(10, tmp) * (last_is_ope ? -1 : 1);
last_is_ope = false;
//printf("DATA: %f ", data.d);
return TokenInfo(TOK_VAR, VAR_FLOAT, "CONST", token_text);
}
else if (C == 'e' || C == 'E') {
data.d = (ruaFloat)data.i, tmp = 0, idx++, token_text += 'e';
data.d *= last_is_ope ? -1 : 1;
last_is_ope = C == '-';
if (last_is_ope) idx++;
while (isdigit(C)) {
tmp = tmp * 10 - '0' + C, token_text += C;
idx++;
}
data.d *= pow(10, tmp * (last_is_ope ? -1 : 1));
last_is_ope = false;
//printf("DATA: %f ", data.d);
return TokenInfo(TOK_VAR, VAR_FLOAT, "CONST", token_text);
}
else {
data.i *= (last_is_ope ? -1 : 1);
last_is_ope = false;
//printf("DATA: %d ", data.i);
return TokenInfo(TOK_VAR, VAR_INTEGER, "CONST", token_text);
}
}
else if (isalpha(C) || C == '_') {
last_is_ope = false;
do
{
token_text += C; idx++;
} while (isalnum(C) || C == '_');
auto iter = m_name2ID.find(token_text);
if (iter == m_name2ID.end()) {
TokenInfo ti = TokenInfo(TOK_VAR, m_nxt_new_idx,
"New Variable", token_text);
m_name2ID[token_text] = m_nxt_new_idx;
m_tokenInfo[m_nxt_new_idx ++] = ti;
return ti;
}
else {
last_is_ope = m_tokenInfo[iter->second].name != "New Variable";
return m_tokenInfo[iter->second];
}
}
else {
if (C == '#') {
do
{
idx++;
} while (C != '\0' && C != '\n');
continue;
}
if (C == '[' && !last_is_ope) {
last_is_ope = true; idx++;
return m_tokenInfo[OPE_INDEX];
}
if (C == '(' && !last_is_ope) {
last_is_ope = true; idx++;
return m_tokenInfo[OPE_CALL];
}
if (C == ')' || C == ']') {
last_is_ope = false;
return m_tokenInfo[m_program_text[idx++]];
}
if (C == '\"') {
idx++;
while (C != '\"')
{
if (C == '\\') {
idx++;
switch (C)
{
case 't':
token_text += '\t'; break;
case 'n':
token_text += '\n'; break;
case '\\':
token_text += '\\'; break;
case '"':
token_text += '"'; break;
default:
EasyLog::Write("Warning (Parser): No " + C +
std::string(" after '\\'."));
break;
}
} else token_text += C;
idx++;
}
idx++;
data.s = new std::string(token_text);
return TokenInfo(TOK_VAR, VAR_STRING, "string", token_text);
}
last_is_ope = true;
std::map<std::string, int>::iterator iter;
do
{
token_text += C; idx++;
} while (!isalnum(C) && !isblank(C) && C);
iter = m_name2ID.find(token_text);
if (iter == m_name2ID.end()) {
do
{
idx--, token_text = token_text.substr(0, token_text.size() - 1);
iter = m_name2ID.find(token_text);
} while (iter == m_name2ID.end() && token_text.size() >= 1);
if (iter == m_name2ID.end()) {
EasyLog::Write("Error (Parser): Unexpexted \" " + token_text +
" \" appears in program text.");
return TokenInfo(-1, 0, "error", token_text);
}
}
return m_tokenInfo[iter->second];
}
}
return m_tokenInfo[TOK_END];
}
std::string RuaParser::GetPosition() {
return "(" + std::to_string(m_lineno) + ", " + std::to_string(idx - m_charno) + ")";
}
TokenInfo* RuaParser::GetTokenInfo(int id) {
auto iter = m_tokenInfo.find(id);
if (iter == m_tokenInfo.end()) {
EasyLog::Write("Warning (Parser): Search token failed : " +
std::to_string(id));
return nullptr;
}
return &(iter->second);
}
TokenInfo* RuaParser::GetTokenInfo(std::string name) {
auto iter = m_name2ID.find(name);
if (iter == m_name2ID.end()) {
EasyLog::Write("Warning (Parser): Search token failed : " + name);
return nullptr;
}
return &m_tokenInfo[iter->second];
}
RuaParser::~RuaParser() {
}