Skip to content

Commit f8d7c83

Browse files
Add files via upload
1 parent e2fd6ea commit f8d7c83

File tree

1 file changed

+140
-4
lines changed

1 file changed

+140
-4
lines changed

JunLang.cpp

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
#include <sstream>
77
#include <condition_variable>
88
using namespace std;
9-
#define _VERSION_ "1.10"
9+
#define _VERSION_ "1.11"
1010
#define _DEFAULT_BUFFER_SIZE_ 1024
1111
string identifiers[] = {
1212
"output",
1313
"wrap",
1414
"new",
15-
"set"
15+
"set",
16+
"input",
17+
"addition",
18+
"subtraction",
19+
"multiplication",
20+
"division"
1621
};
1722
struct Variable {
18-
int value;
23+
long double value;
1924
string name;
2025
};
2126

@@ -40,6 +45,7 @@ struct UndeclaredVariable : public exception {
4045
return "Undeclared variable.";
4146
}
4247
};
48+
4349
vector<string> split(string str, char delim) {
4450
stringstream ss(str);
4551
string item;
@@ -51,7 +57,17 @@ vector<string> split(string str, char delim) {
5157
}
5258
return result;
5359
}
54-
int main (int argc, char* argv[]) {
60+
bool isInteger(string x) {
61+
bool integer = true;
62+
for (int i = 0; i < strlen(x.c_str()); i++) {
63+
if (x[i] < '0' || x[i] > '9') {
64+
integer = false;
65+
break;
66+
}
67+
}
68+
return integer;
69+
}
70+
int main(int argc, char* argv[]) {
5571
try {
5672
string fileName = string(argv[1]);
5773
int bufferSize;
@@ -128,6 +144,126 @@ int main (int argc, char* argv[]) {
128144
}
129145
break;
130146
}
147+
if (*it == identifiers[4]) {
148+
bool undeclared = true;
149+
for (int i = 0; i <= variableCount; i++) {
150+
if (variables[i].name == *(it + 1)) {
151+
int temp;
152+
cin >> temp;
153+
variables[i].value = temp;
154+
undeclared = false;
155+
break;
156+
}
157+
}
158+
if (undeclared) {
159+
throw UndeclaredVariable();
160+
}
161+
break;
162+
}
163+
if (*it == identifiers[5]) {
164+
bool undeclared = true;
165+
for (int i = 0; i <= variableCount; i++) {
166+
if (variables[i].name == *(it + 1)) {
167+
if (isInteger(*(it + 2))) {
168+
variables[i].value += atoi((*(it + 2)).c_str());
169+
undeclared = false;
170+
break;
171+
}
172+
else {
173+
undeclared = true;
174+
for (int j = 0; j <= variableCount; j++) {
175+
if (variables[j].name == *(it + 2)) {
176+
variables[i].value += variables[j].value;
177+
undeclared = false;
178+
break;
179+
}
180+
}
181+
}
182+
}
183+
}
184+
if (undeclared) {
185+
throw UndeclaredVariable();
186+
}
187+
break;
188+
}
189+
if (*it == identifiers[6]) {
190+
bool undeclared = true;
191+
for (int i = 0; i <= variableCount; i++) {
192+
if (variables[i].name == *(it + 1)) {
193+
if (isInteger(*(it + 2))) {
194+
variables[i].value -= atoi((*(it + 2)).c_str());
195+
undeclared = false;
196+
break;
197+
}
198+
else {
199+
undeclared = true;
200+
for (int j = 0; j <= variableCount; j++) {
201+
if (variables[j].name == *(it + 2)) {
202+
variables[i].value -= variables[j].value;
203+
undeclared = false;
204+
break;
205+
}
206+
}
207+
}
208+
}
209+
}
210+
if (undeclared) {
211+
throw UndeclaredVariable();
212+
}
213+
break;
214+
}
215+
if (*it == identifiers[7]) {
216+
bool undeclared = true;
217+
for (int i = 0; i <= variableCount; i++) {
218+
if (variables[i].name == *(it + 1)) {
219+
if (isInteger(*(it + 2))) {
220+
variables[i].value *= atoi((*(it + 2)).c_str());
221+
undeclared = false;
222+
break;
223+
}
224+
else {
225+
undeclared = true;
226+
for (int j = 0; j <= variableCount; j++) {
227+
if (variables[j].name == *(it + 2)) {
228+
variables[i].value *= variables[j].value;
229+
undeclared = false;
230+
break;
231+
}
232+
}
233+
}
234+
}
235+
}
236+
if (undeclared) {
237+
throw UndeclaredVariable();
238+
}
239+
break;
240+
}
241+
if (*it == identifiers[8]) {
242+
bool undeclared = true;
243+
for (int i = 0; i <= variableCount; i++) {
244+
if (variables[i].name == *(it + 1)) {
245+
if (isInteger(*(it + 2))) {
246+
variables[i].value /= atoi((*(it + 2)).c_str());
247+
undeclared = false;
248+
break;
249+
}
250+
else {
251+
undeclared = true;
252+
for (int j = 0; j <= variableCount; j++) {
253+
if (variables[j].name == *(it + 2)) {
254+
variables[i].value /= variables[j].value;
255+
undeclared = false;
256+
break;
257+
}
258+
}
259+
}
260+
}
261+
}
262+
if (undeclared) {
263+
throw UndeclaredVariable();
264+
}
265+
break;
266+
}
131267
}
132268
else {
133269
throw UnknowIdentifier();

0 commit comments

Comments
 (0)