-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
191 lines (178 loc) · 6.3 KB
/
parser.cpp
File metadata and controls
191 lines (178 loc) · 6.3 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
#include <parser.hpp>
#include <sstream>
#include <error.hpp>
#include <special.hpp>
// TokenList functions
TokenList::TokenList(const vector<Token> &token) : token(token) {}
// returns the next token and increase the pointer
Token TokenList::next()
{
return token[pointer++];
}
bool TokenList::isEmpty()
{
return pointer >= (int)token.size();
}
// conver string to memseg
memseg convert(const string &num)
{
memseg m{0}, tens{1};
for (int i = num.length() - 1; i >= 0; i--)
{
m = m + (tens * (num[i] - '0'));
tens = (tens * 10);
}
return m;
}
// foreward declaration of recursive function
void recursiveParsing(vector<unique_ptr<Statement>> &list, TokenList &tList, map<string, unique_ptr<StackBase>> &stack);
// recusive calls the function when the start of bracket is found
template <class T>
void handleBracket(vector<unique_ptr<Statement>> &list, TokenList &tList, map<string, unique_ptr<StackBase>> &stack)
{
unique_ptr<T> b = make_unique<T>();
recursiveParsing(b->statements, tList, stack);
list.push_back(move(b));
}
// merges all the consecutive operator tokens with target stack
void handleOperator(Token tt, vector<unique_ptr<Statement>> &list, TokenList &tList, map<string, unique_ptr<StackBase>> &stack)
{
vector<Operation> opList;
opList.push_back(tt.type == Type::Move ? Operation::Move : Operation::Copy);
while (!tList.isEmpty())
{
Token t = tList.next();
switch (t.type)
{
// keep adding the next operator to opList
case Type::Move:
opList.push_back(Operation::Move);
break;
case Type::Copy:
opList.push_back(Operation::Copy);
break;
// when the next token is stack then terminate the function
case Type::Stack:
list.push_back(make_unique<Operator>(opList, stack.find(t.data)->second));
return;
default:
// next token is neither an operator nor a stack, so error
stringstream msg;
msg << "unexpected token " << t << " after operator " << tt;
error(msg.str());
return;
}
}
// tokenlist ran out of tokens without the terminating stack token, so error
stringstream msg;
msg << "no stack specified after operator " << tt;
error(msg.str());
}
// recursive call to parse the tokens
void recursiveParsing(vector<unique_ptr<Statement>> &list, TokenList &tList, map<string, unique_ptr<StackBase>> &stack)
{
while (!tList.isEmpty())
{
Token t = tList.next();
switch (t.type)
{
// exit condition if next tokens are closing brackets
case Type::ZeroEnd:
case Type::EmptyEnd:
return;
// recursive call if next tokens are starting brackets
case Type::ZeroStart:
handleBracket<ZeroBlock>(list, tList, stack);
break;
case Type::EmptyStart:
handleBracket<EmptyBlock>(list, tList, stack);
break;
// special function call for operator
case Type::Move:
case Type::Copy:
handleOperator(t, list, tList, stack);
break;
// setstack case
case Type::Stack:
list.push_back(make_unique<SetStack>(stack.find(t.data)->second));
break;
// setnumber case
case Type::Number:
list.push_back(make_unique<SetNumber>(convert(t.data)));
break;
}
}
}
// reports about unmatched brackets
void checkBrackets(const vector<Token> &token)
{
vector<Token> b;
for (Token t : token)
{
if (t.type == Type::EmptyStart || t.type == Type::EmptyEnd || t.type == Type::ZeroStart || t.type == Type::ZeroEnd)
{
// if b has opening bracket of same type as closing bracket of t then pop b
if ((b.size() > 0) && ((t.type == Type::ZeroEnd && b[b.size() - 1].type == Type::ZeroStart) || (t.type == Type::EmptyEnd && b[b.size() - 1].type == Type::EmptyStart)))
b.pop_back();
else
b.push_back(t);
}
}
// now b contains the unmatched brackets tokens, so error
if (b.size() > 0)
{
std::stringstream stream;
stream << "parser error";
for (Token t : b)
{
stream << "\nunmatched bracket '" << t;
}
error(stream.str());
}
}
// reports about non-literal and non-stack at start of tokenlist
void checkStart(const std::vector<Token> &tokens)
{
if (tokens.size() > 0 && (tokens[0].type != Type::Stack && tokens[0].type != Type::Number))
error("Unexpected starting token.It should be either a stack or a number");
}
// parse the tokenlist
vector<unique_ptr<Statement>> parse(const vector<Token> &token, map<string, unique_ptr<StackBase>> &stack)
{
// check for error
checkBrackets(token);
checkStart(token);
TokenList list(token);
vector<unique_ptr<Statement>> graph;
recursiveParsing(graph, list, stack);
return graph;
}
// creates a map of stack which will be later used in parsing
map<string, unique_ptr<StackBase>> generateStackMap(const vector<Token> &token)
{
// map should contain all the special stacks of the program
map<string, unique_ptr<StackBase>> m;
m.insert(pair<string, unique_ptr<StackBase>>("io", make_unique<Console>()));
m.insert(pair<string, unique_ptr<StackBase>>("add", make_unique<Add>()));
m.insert(pair<string, unique_ptr<StackBase>>("and", make_unique<And>()));
m.insert(pair<string, unique_ptr<StackBase>>("or", make_unique<Or>()));
m.insert(pair<string, unique_ptr<StackBase>>("rsft", make_unique<RightShift>()));
m.insert(pair<string, unique_ptr<StackBase>>("lsft", make_unique<LeftShift>()));
m.insert(pair<string, unique_ptr<StackBase>>("inv", make_unique<Invert>()));
m.insert(pair<string, unique_ptr<StackBase>>("int", make_unique<Integer>()));
m.insert(pair<string, unique_ptr<StackBase>>("bin", make_unique<Bin>()));
// inserting user defined stacks to the map
for (auto t : token)
{
if (t.type == Type::Stack)
{
auto iter = m.find(t.data);
if (iter == m.end())
{
m.insert(pair<string, unique_ptr<Stack>>(t.data, make_unique<Stack>()));
}
}
}
// returning the user generated map
return m;
}