1+ #include < iostream>
2+ #include < string>
3+ #include < vector>
4+ #include < sstream>
5+ #include < map>
6+
7+ using namespace std ;
8+
9+ enum TokenType
10+ {
11+ GET,
12+ SET,
13+ GO,
14+ EQUAL,
15+ VARIABLE,
16+ NUMERIC,
17+ END,
18+ INVALID
19+ };
20+
21+ class Token
22+ {
23+ private:
24+ TokenType type;
25+ string value;
26+
27+ public:
28+ Token (TokenType typ, string val) : type(typ), value(val){};
29+
30+ friend void parser (const vector<Token> &input_tokens);
31+ };
32+
33+ bool isAlpha (const string &str)
34+ {
35+ for (char ch : str)
36+ {
37+ if (!isalpha (ch))
38+ {
39+ return false ;
40+ }
41+ }
42+ return true ;
43+ }
44+
45+ bool isNumeric (const string &str)
46+ {
47+ for (char ch : str)
48+ {
49+ if (!isdigit (ch))
50+ {
51+ return false ;
52+ }
53+ }
54+ return true ;
55+ }
56+
57+ vector<Token> lexer (string input)
58+ {
59+ vector<Token> tokens;
60+ istringstream iss (input);
61+ string word;
62+
63+ while (iss >> word)
64+ {
65+ if (word == " GET" )
66+ {
67+ tokens.emplace_back (Token (GET, " GET" ));
68+ }
69+ else if (word == " SET" )
70+ {
71+ tokens.emplace_back (Token (SET, " SET" ));
72+ }
73+ else if (word == " GO" )
74+ {
75+ tokens.emplace_back (Token (GO, " GO" ));
76+ }
77+ else if (isNumeric (word))
78+ {
79+ tokens.emplace_back (Token (NUMERIC, word));
80+ }
81+ else if (word == " END" )
82+ {
83+ tokens.emplace_back (Token (END, " END" ));
84+ }
85+ else if (word == " =" )
86+ {
87+ tokens.emplace_back (Token (EQUAL, " =" ));
88+ }
89+ else if (isAlpha (word))
90+ {
91+ tokens.emplace_back (Token (VARIABLE, word));
92+ }
93+ else
94+ {
95+ tokens.emplace_back (Token (INVALID, word));
96+ }
97+ }
98+
99+ return tokens;
100+ }
101+
102+ map<string, int > variables;
103+ bool interpreter_running = true ;
104+ void parser (const vector<Token> &input_tokens)
105+ {
106+ for (auto itr = input_tokens.begin (); itr != input_tokens.end (); ++itr)
107+ {
108+ switch ((*itr).type )
109+ {
110+ case GET:
111+ if ((*(itr + 1 )).type == VARIABLE)
112+ {
113+ cout << " : " ;
114+ string input_line;
115+ getline (cin, input_line);
116+ stringstream (input_line) >> variables[(*(itr + 1 )).value ];
117+ }
118+
119+ break ;
120+
121+ case SET:
122+ if ((*(itr + 1 )).type == VARIABLE && (*(itr + 2 )).type == EQUAL && (*(itr + 3 )).type == NUMERIC)
123+ {
124+ variables[(*(itr + 1 )).value ] = stoi ((*(itr + 3 )).value );
125+ }
126+
127+ break ;
128+
129+ case GO:
130+ if ((*(itr + 1 )).type == VARIABLE)
131+ {
132+ cout << variables[(*(itr + 1 )).value ] << endl;
133+ }
134+
135+ break ;
136+
137+ case INVALID:
138+ break ;
139+
140+ case END:
141+ interpreter_running = false ;
142+
143+ break ;
144+
145+ default :
146+ break ;
147+ }
148+ }
149+ }
150+
151+ int main ()
152+ {
153+ while (interpreter_running)
154+ {
155+ string input;
156+ char ch;
157+ cout << " >>> " ;
158+
159+ while ((ch = cin.get ()) != ' \n ' )
160+ {
161+ input += ch;
162+ }
163+
164+ parser (lexer (input));
165+ input = " " ;
166+ }
167+
168+ return 0 ;
169+ }
0 commit comments