-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreterCommandLine.cpp
More file actions
200 lines (165 loc) · 8.28 KB
/
interpreterCommandLine.cpp
File metadata and controls
200 lines (165 loc) · 8.28 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
/*
Created by
Ethan Roberts
on 09/21/2019
This program is a custom interpreter meant to read commands from a command line and then
execute the commands.
This program implements a tree along with a vector. When values are being "pulled" from tree, they are inserted
into the command vector.
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
class interp {
public:
string commandAry;
string readCommand();
void build(interp*, string, int);
interp *rootSave;
~interp();
interp();
private:
interp * newNode();
void evaluate();
interp * down;
interp* right;
char token;
char value;
};
interp *root = nullptr;
vector<char>vect;
vector<char>::iterator it;
bool FOUNDCONS = false;
interp::interp() {
}
interp::~interp() {
root = nullptr;
rootSave = nullptr;
this->down = nullptr;
this->right = nullptr;
}
interp* interp::newNode() {
interp *ptr = new interp;
ptr->down = nullptr;
ptr->right = nullptr;
ptr->value = NULL;
return ptr;
}
string interp::readCommand() {
string command;
cout << "> ";
getline(cin, this->commandAry); //read-in command
command = this->commandAry;
return command;
}
void interp::build(interp *ptr, string command, int index) {
if (command[index] == '@' || command[index] == '!' || command[index] == '$') {
ptr = newNode();
if (command[index] == '@') {
ptr->token = '@';
}
else if (command[index] == '!') {
ptr->token = '!';
}
else if (command[index] == '$') {
ptr->token = '$';
FOUNDCONS = true;
}
++index;
build(ptr->down, command, index);
ptr->evaluate();
return;
}
if (root == NULL) { //base case
ptr = this->newNode();
root = ptr;
build(root, command, ++index);
return;
}
if (index == command.length() - 1) { // all data as been processed into tree
return;
}
if (command[index] == '(') {
ptr = newNode();
ptr->value = '(';
++index;
build(ptr->down, command, index);
if (FOUNDCONS == true && vect.size() > 0 && vect.front() == '(') {
it = vect.begin() + 1;
vect.insert(it, ptr->value);
}
if (vect.back() != ')' && ptr->token != '@' ) { vect.push_back(')'); }
it = vect.begin();
if (*it != '(' && ptr->token != '@') { vect.insert(it, ptr->value);; }
}
else if (command[index] >= 97 && command[index] <= 122 || (command[index] >= 48 && command[index] <= 57)) { //it's a value
ptr = newNode();
ptr->value = command[index];
++index;
build(ptr->right, command, index);
if (FOUNDCONS == true && vect.size() > 0 && vect.front() == '(') {
it = vect.begin() + 1;
vect.insert(it, ptr->value);
}
else{
it = vect.begin();
vect.insert(it, ptr->value);
}
}
else if (command[index] == ')') {
ptr = newNode();
ptr->value = command[index];
++index;
build(ptr->down, command, index);
if (FOUNDCONS == true && vect.size() > 0 && vect.front() == '(') {
it = vect.begin() + 1;
vect.insert(it, ptr->value);
}
if (ptr->value != ')') { //actual data is incoming
it = vect.begin();
vect.insert(it, ptr->value);
}
else { ++index; }
}
else if (command[index] == ' ') { //handles cases for if there are blanks in the command entered
++index;
build(ptr, command, index);
}
}
void interp::evaluate() {
char element = '\0';
if (this->token == '@') { // if "car" command (get first element from list)
it = vect.begin() + 1;
element = *it;
vect.clear();
vect.push_back(element);
}
else if (this->token == '!') { // if "cdr" command (delete first element and return list)
vect.erase(vect.begin() + 1);
it = vect.begin();
}
return;
}
int main()
{
interp interp;
vector<char>::iterator it;
string command = "";
while (command != "exit") {
vect.clear();
root = nullptr;
FOUNDCONS = false;
command = interp.readCommand();
interp.build(root, command, 0);
it = vect.begin(); // returning pointer from tree
cout << " ";
for (auto x : vect) {
cout << x;
}
cout << endl << endl;
}
return 0;
}