Skip to content

Commit 493f102

Browse files
committed
implement mod operator
1 parent 1f56c0d commit 493f102

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/compiler.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,44 @@ int compile_to_cpp(std::string filename) {
378378
cpp_file << variable1 << " /= " << variable2 << ";\n";
379379
}
380380
}
381+
} else if (content.find("mod/") == 0) {
382+
content.erase(0, 4);
383+
size_t slash1 = content.find('/');
384+
if (slash1 == std::string::npos) {
385+
std::cerr << "mod missing a slash!" << std::endl;
386+
throw 500;
387+
return 1;
388+
}
389+
std::string variable1 = content.substr(0, slash1);
390+
std::string variable2 = content.substr(slash1 + 1);
391+
if (variable2.empty()) {
392+
std::cerr << "cannot mod by nothing!" << std::endl;
393+
throw 500;
394+
return 1;
395+
} else if (variable2 == "0") {
396+
std::cerr << "cannot mod by zero!" << std::endl;
397+
throw 500;
398+
return 1;
399+
}
400+
auto int_it = std::find(integer_names.begin(), integer_names.end(), variable1);
401+
if (int_it == integer_names.end()) {
402+
std::cerr << "integer " << variable1 << " not found." << std::endl;
403+
throw 500;
404+
return 1;
405+
}
406+
auto int2_it = std::find(integer_names.begin(), integer_names.end(), variable2);
407+
if (int2_it != integer_names.end()) {
408+
cpp_file << variable1 << " %= " << variable2 << ";\n";
409+
} else {
410+
int is_int = all_of(variable2.begin(), variable2.end(), ::isdigit);
411+
if (!is_int) {
412+
std::cerr << "only numbers can mod an integer!" << std::endl;
413+
throw 500;
414+
return 1;
415+
} else {
416+
cpp_file << variable1 << " %= " << variable2 << ";\n";
417+
}
418+
}
381419
} else if (content.find("wait/") == 0) {
382420
content.erase(0, 5);
383421
if (content == "") {

0 commit comments

Comments
 (0)