-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.cpp
150 lines (138 loc) · 8.2 KB
/
compile.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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "header.hpp"
using namespace std;
bool canAdvance(int i, int s) {
if (i+1 == s) {
return false;
} else return true;
}
void compile(vector<Token> tokens, string fileOutputName) {
string start = "SYS_WRITE equ 4 ; write text to stdout\nSYS_READ equ 3 ; read text from stdin\nSYS_EXIT equ 1 ; terminate the program\nSYS_FORK equ 2\n";
string data = "\nsection .data\n";
string bss = "\nsection .bss\n";
string text = "\n\nsection .text\n global _start\n\n_start:\n";
string end = " mov eax, SYS_EXIT\n mov ebx, 0\n int 0x80\n";
int strIdx, bssIdx = 0;
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i].tType == "func") {
if (tokens[i].tVal == "yap") {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "string") {
text += " mov edx, len" + to_string(strIdx)+ "\n mov ecx, jklZmmVariableIndex" + to_string(strIdx) + "\n mov ebx, 1\n mov eax, SYS_WRITE\n int 0x80\n\n";
data += " jklZmmVariableIndex" + to_string(strIdx) + " db '" + tokens[i].tVal + "', 0xa\n len" + to_string(strIdx) + " equ $ - jklZmmVariableIndex" + to_string(strIdx) + "\n";
strIdx++;
continue;
} else if (tokens[i].tType == "int") {
double sum = stoi(tokens[i].tVal);
while (true) {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "math") {
if (tokens[i].tVal == "plus") {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "int") {
sum += stoi(tokens[i].tVal);
} else {
cout << "holup [4] - error while compiling: erm wtf cant put those two together..." << endl;
exit(4);
}
} else {
cout << "holup [3] - error while compiling: bro tried to yap math" << endl;
exit(3);
}
} else if (tokens[i].tVal == "minus") {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "int") {
sum -= stoi(tokens[i].tVal);
} else {
cout << "holup [4] - error while compiling: erm wtf cant put those two together..." << endl;
exit(4);
}
} else {
cout << "holup [3] - error while compiling: bro tried to yap math" << endl;
exit(3);
}
} else if (tokens[i].tVal == "multi") {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "int") {
sum *= stoi(tokens[i].tVal);
} else {
cout << "holup [4] - error while compiling: erm wtf cant put those two together..." << endl;
exit(4);
}
} else {
cout << "holup [3] - error while compiling: bro tried to yap math" << endl;
exit(3);
}
} else if (tokens[i].tVal == "divi") {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "int") {
sum = sum / stoi(tokens[i].tVal);
cout << 4 / 8 << endl;
} else {
cout << "holup [4] - error while compiling: erm wtf cant put those two together..." << endl;
exit(4);
}
} else {
cout << "holup [3] - error while compiling: bro tried to yap math" << endl;
exit(3);
}
} else {
break;
}
} else {
cout << "holup [2] - error while compiling: number and what bro?" << endl;
exit(2);
}
} else {
break;
}
}
text += " mov edx, len" + to_string(strIdx)+ "\n mov ecx, jklZmmVariableIndex" + to_string(strIdx) + "\n mov ebx, 1\n mov eax, SYS_WRITE\n int 0x80\n\n";
data += " jklZmmVariableIndex" + to_string(strIdx) + " db '" + to_string(sum) + "', 0xa\n len" + to_string(strIdx) + " equ $ - jklZmmVariableIndex" + to_string(strIdx) + "\n";
strIdx++;
}
} else {
cout << "holup [1] - error while compiling: can't yap allat" << endl;
exit(1);
}
} else if (tokens[i].tVal == "erm") {
if (canAdvance(i, tokens.size())) {
i++;
if (tokens[i].tType == "string") {
// length of question
bss += " jklZmmVariableIndexBss" + to_string(bssIdx) + " resb 1024\n";
bss += " jklZmmVariableIndexBss" + to_string(bssIdx) + "len equ $ - jklZmmVariableIndexBss" + to_string(bssIdx);
// message to output
data += " jklZmmVariableIndex" + to_string(strIdx) + " db '" + tokens[i].tVal + "'\n len" + to_string(strIdx) + " equ $ - jklZmmVariableIndex" + to_string(strIdx) + "\n";
// output message
text += " mov edx, len" + to_string(strIdx)+ "\n mov ecx, jklZmmVariableIndex" + to_string(strIdx) + "\n mov ebx, 1\n mov eax, SYS_WRITE\n int 0x80\n\n";
strIdx++;
// store input
text += " mov eax, SYS_READ\n mov ebx, SYS_FORK\n mov ecx, jklZmmVariableIndexBss" + to_string(bssIdx) + "\n mov edx, 1024\n int 0x80\n\n";
bssIdx++;
} else {
cout << "holup [1] - error while compiling: can't yap allat" << endl;
exit(1);
}
continue;
}
}
}
}
ofstream zmmOut("zmmOut.asm");
zmmOut << start + data + bss + text + end;
zmmOut.close();
system("nasm -felf64 zmmOut.asm -o zmmOut.o");
system(("ld zmmOut.o -o \"" + fileOutputName + "\"").c_str());
// remove("zmmOut.asm");
remove("zmmOut.o");
}