#include <iostream>
#include <cstring>
using namespace std;
void c_to_mips(const char* c_code);
int main() {
const char* c_code = "int main() { int a = 5; int b = 10; int sum = a + b; return 0; }";
c_to_mips(c_code);
return 0;
}
void c_to_mips(const char* c_code) {
char token = strtok(const_cast<char>(c_code), " \t\n\r\f(),;");
while (token != nullptr) {
if (strcmp(token, "int") == 0) {
token = strtok(nullptr, " \t\n\r\f(),;");
while (token != nullptr) {
if (strcmp(token, "(") == 0) {
cout << "addi $sp, $sp, -4" << endl;
} else if (strcmp(token, ")") == 0) {
cout << "addi $sp, $sp, 4" << endl;
} else if (strcmp(token, "{") == 0) {
cout << "main:" << endl;
} else if (strcmp(token, "}") == 0) {
cout << "li $v0, 10" << endl;
cout << "syscall" << endl;
} else {
cout << "addi $t0, $zero, " << atoi(token) << endl;
}
token = strtok(nullptr, " \t\n\r\f(),;");
}
}
token = strtok(nullptr, " \t\n\r\f(),;");
}
}