-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
189 lines (173 loc) · 5.48 KB
/
engine.cpp
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
#include <iostream>
#include <string>
#include <sstream>
#include "engine.h"
#include "utils.h"
#include "libs/stringadd.h"
#include "libs/stringop.h"
#include "libs/stringcond.h"
using namespace std;
// This cpp file contains the whole engine to run code.
// Version of the software
string VERSION = "0.1.1-alpha";
string* stringvars = new string[128];
int* intvars = new int[128];
//Boot screen. Displays useless info
void BootScreen() {
cout<<"MicrowaveBASIC " << VERSION << " by h34ting4ppliance \nCopyright (C) Microwave Micro-computers Co."<<endl;
//Display shit lol
}
//Will execute an entire code.
void ExecuteCode(std::string* code) {
int o = 0;
for (int i = 0; i < 99999; ++i) {
if (code[i] != "")
o = ExecuteLine(code[i], i);
if (o == -1) {
cout << "!STOP AT " << to_string(i) << endl;
break;
}
if (o >= 1)
i = o - 1;
o = 0;
}
}
//Will execute only a single line.
int ExecuteLine(std::string line, int pptr) {
if (starts_with(line, "P ")) { // " PRINT command
string o = line.erase(0, 2);
if (o == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
else
cout << evalStringAddition(o, stringvars, intvars) << endl;
return 0;
} else if (starts_with(line, "//")) // Comment
return 0;
else if (starts_with(line, "p ")) { // ' PRINT command
string o = line.erase(0, 2);
if (o == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
else
cout << evalStringAddition(o, stringvars, intvars);
return 0;
} else if (starts_with(line, "?* ")) {
string o = line.erase(0, 3);
string inp = "";
if (o == "" || !isalpha(o.at(0))) {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
else {
getline(cin, inp);
if (!is_number(inp)) {
cout << "!INPUT ERROR" << endl;
return -1;
}
intvars[toupper(o.at(0)) - 'A'] = evalString(inp, intvars, false);
}
return 0;
} else if (starts_with(line, "?$ ")) {
string o = line.erase(0, 3);
string inp = "";
if (o == "" || !isalpha(o.at(0))) {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
getline(cin, inp);
stringvars[toupper(o.at(0)) - 'A'] = inp;
}
return 0;
} else if (starts_with(line, "* ")) {
string o = line.erase(0, 2);
if (o == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
cout << to_string(evalString(o, intvars, false));
return 0;
}
} else if (starts_with(line, "=* ")) {
string o = line.erase(0, 3);
string oa = line.erase(0, 1);
if (o == "" || !isalpha(o.at(0)) || oa == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
intvars[toupper(o.at(0)) - 'A'] = evalString(oa, intvars, false);
return 0;
}
} else if (starts_with(line, "=$ ")) {
string o = line.erase(0, 3);
string oa = line.erase(0, 1);
if (o == "" || !isalpha(o.at(0)) || oa == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
stringvars[toupper(o.at(0)) - 'A'] = evalStringAddition(oa, stringvars, intvars);
return 0;
}
} else if (starts_with(line, "G ")) {
if (pptr == -1) {
cout << "!PROGRAM ERROR" << endl;
return -1;
}
string o = line.erase(0, 2);
if (!is_number(o)) {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
pptr = evalString(o, intvars, false) - 1;
if (pptr <= 0) {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
return pptr;
} else if (starts_with(line, "I* ")) {
string o = line.erase(0, 3);
int sepPos = searchChar(':', o);
if (sepPos == -1) { // If sepPos = -1, that means not any separator has been detected
cout << "!SYNTAX ERROR" << endl;
return -1;
}
string argA = trimString(o, 0, sepPos - 1);
string argB = trimString(o, sepPos + 1, line.size() - 1);
bool condi = evalIntCondition(argA, intvars);
if (condi) {
int a = ExecuteLine(argB, pptr);
if (a == -1)
return -1;
else
return a;
} else {
return 0;
}
} else if (starts_with(line, "I$ ")) {
string o = line.erase(0, 3);
int sepPos = searchChar(':', o);
if (sepPos == -1) { // If sepPos = -1, that means not any separator has been detected
cout << "!SYNTAX ERROR" << endl;
return -1;
}
string argA = trimString(o, 0, sepPos - 1);
string argB = trimString(o, sepPos + 1, line.size() - 1);
bool condi = evalStrCondition(argA, stringvars, intvars);
if (condi) {
int a = ExecuteLine(argB, pptr);
if (a == -1)
return -1;
else
return a;
} else {
return 0;
}
}
else if (line != "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
return 0;
}