-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
172 lines (147 loc) · 3.25 KB
/
main.cpp
File metadata and controls
172 lines (147 loc) · 3.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
//Author: Saiyam Jain
//License: MIT License
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<map>
using namespace std;
enum TokenType
{
GET,
SET,
GO,
EQUAL,
VARIABLE,
NUMERIC,
END,
INVALID
};
class Token
{
private:
TokenType type;
string value;
public:
Token(TokenType typ, string val) : type(typ), value(val){};
friend void parser(const vector<Token> &input_tokens);
};
bool isAlpha(const string &str)
{
for (char ch : str)
{
if (!isalpha(ch))
{
return false;
}
}
return true;
}
bool isNumeric(const string &str)
{
for (char ch : str)
{
if (!isdigit(ch))
{
return false;
}
}
return true;
}
vector<Token> lexer(string input)
{
vector<Token> tokens;
istringstream iss(input);
string word;
while(iss >> word)
{
if(word == "GET")
{
tokens.emplace_back(Token(GET, "GET"));
}
else if(word == "SET")
{
tokens.emplace_back(Token(SET, "SET"));
}
else if(word == "GO")
{
tokens.emplace_back(Token(GO, "GO"));
}
else if(isNumeric(word))
{
tokens.emplace_back(Token(NUMERIC, word));
}
else if(word == "END")
{
tokens.emplace_back(Token(END, "END"));
}
else if(word == "=")
{
tokens.emplace_back(Token(EQUAL, "="));
}
else if(isAlpha(word))
{
tokens.emplace_back(Token(VARIABLE, word));
}
else
{
tokens.emplace_back(Token(INVALID, word));
}
}
return tokens;
}
map<string, int> variables;
bool interpreter_running = true;
void parser(const vector<Token> &input_tokens)
{
for(auto itr = input_tokens.begin(); itr != input_tokens.end(); ++itr)
{
switch ((*itr).type)
{
case GET:
if((*(itr + 1)).type == VARIABLE)
{
cout << ": ";
string input_line;
getline(cin, input_line);
stringstream(input_line) >> variables[(*(itr + 1)).value];
}
break;
case SET:
if((*(itr + 1)).type == VARIABLE && (*(itr + 2)).type == EQUAL && (*(itr + 3)).type == NUMERIC)
{
variables[(*(itr + 1)).value] = stoi((*(itr + 3)).value);
}
break;
case GO:
if((*(itr + 1)).type == VARIABLE)
{
cout << variables[(*(itr + 1)).value] << endl;
}
break;
case INVALID:
break;
case END:
interpreter_running = false;
break;
default:
break;
}
}
}
int main()
{
while(interpreter_running)
{
string input;
char ch;
cout << ">>> ";
while((ch = cin.get()) != '\n')
{
input += ch;
}
parser(lexer(input));
input = "";
}
return 0;
}