-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlolcode.c
215 lines (174 loc) · 4.65 KB
/
lolcode.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "lolcode.h"
#include <iostream>
#include <utility>
#include <stdio.h>
#include <map>
int _indent = 1;
struct Variable {
VariableType type;
std::string value;
};
typedef std::map<std::string, Variable> VariablesMap;
VariablesMap _variables;
int yyerror(const char *s) {
std::cout << "ERR> " << s << "\n";
}
void dumpVars()
{
for(VariablesMap::const_iterator itor = _variables.begin(); itor != _variables.end(); ++ itor){
std::cout << (*itor).first << " - (value: " << (*itor).second.value << ") | type: " << (*itor).second.type << "\n";
}
}
void gen_programStart() {
std::cout << "#include <iostream>\n"
<< "#include <string>\n"
<< "using namespace std;\n"
<< "int main() {\n";
}
std::string indent()
{
std::string res;
for(int i = 0; i < _indent; ++i) {
res += "\t";
}
return res;
}
void gen_programEnd() {
std::cout << indent() << "return 0;" << "\n}\n";
}
void gen_print(const std::string& s) {
std::cout << indent() << "cout << " << s << ";\n";
}
void gen_input(const std::string& variable) {
VariablesMap::iterator itor = _variables.find(std::string(variable));
if (itor == _variables.end()) {
std::cerr << "Trying to assign an undeclared variable: " << variable << " !!\n";
return;
}
Variable &var = (*itor).second;
if (var.type == NONE){
std::cout << indent() << "string " << variable << ";\n";
var.type = STRING;
}
std::cout << indent() << "cin >> " << variable << ";\n";
}
void gen_declareVar(const std::string& name, const std::string& value, VariableType type, int opt) {
VariablesMap::iterator itor = _variables.find(std::string(name));
if (itor != _variables.end()) {
if ((*itor).second.type != NONE && (*itor).second.type != type) {
std::cerr << "Variable " << name << " is already defined using another type!\n";
}
return;
}
Variable var;
var.type = type;
var.value = value;
_variables.insert(make_pair(std::string(name), var));
// declare the variable
if (type != NONE) {
if ((opt & FORMAT_INDENT) == FORMAT_INDENT) {
std::cout << indent();
}
if (type == INT) {
std::cout << "int ";
}
else if (type == STRING) {
std::cout << "string ";
}
std::cout << name << " = " << value;
if ((opt & FORMAT_EOL) == FORMAT_EOL) {
std::cout << ";\n";
}
}
}
void gen_assignVar(const std::string& name, const std::string& value, VariableType type) {
VariablesMap::iterator itor = _variables.find(std::string(name));
if (itor == _variables.end()) {
std::cerr << "Trying to assign an undeclared variable: " << name << " !!\n";
return;
}
Variable &var = (*itor).second;
if (var.type != NONE && var.type != type){
std::cerr << "Trying to assign a value to an invalid type\n";
return;
}
// need to declare it
if (var.type == NONE) {
std::cout << indent();
if (type == STRING) {
std::cout << "string ";
}
else if (type == INT) {
std::cout << "int ";
}
std::cout << name << " = " << value << ";\n";
}
else {
std::cout << indent() << name << " = " << value << ";\n";
}
var.type = type;
var.value = value;
}
std::string gen_op(const std::string& op, const std::string& a, const std::string& b) {
return "(" + a + " " + op + " " + b + ")";
}
std::string gen_func(const std::string& funcName, const std::string& a, const std::string& b) {
return "(" + funcName + "(" + a + ", " + b + "))";
}
void gen_forStart(const std::string& variable, const std::string& condition, const std::string& mode) {
std::cout << indent() << "for (";
gen_declareVar(variable, "0", INT, FORMAT_NONE);
std::cout << "; " << condition << "; " << ( mode == "UPPIN" ? "++ " : "--") << variable << ")\n" << indent() << "{\n";
++ _indent;
}
void gen_forEnd()
{
-- _indent;
std::cout << indent() << "}\n";
}
void gen_ifStart(const std::string& cond) {
std::cout << indent() << "if (" << cond << ") {\n";
++ _indent;
}
void gen_ifElse() {
-- _indent;
std::cout << indent() << "} else {\n";
++ _indent;
}
void gen_condEnd() {
-- _indent;
std::cout << indent() << "}\n";
}
void gen_ifElseIf(const std::string& cond) {
-- _indent;
std::cout << indent() << "} else if (" << cond << ") {\n";
++ _indent;
}
std::string _swCond;
bool swFirst = true;
void gen_swStart(const std::string& cond) {
_swCond = cond;
std::cout << indent() << "{\n";
++ _indent;
swFirst = true;
}
void gen_swCaseStart(const std::string& option) {
std::cout << indent();
if (!swFirst){
std::cout << "else ";
}
std::cout << "if (" << _swCond << " == " << option << ") {\n";
++ _indent;
swFirst = false;
}
void gen_swDefaultStart() {
std::cout << indent() << "else {\n";
++ _indent;
}
void gen_break() {
//std::cout << indent() << "break;\n";
}
void gen_swCaseEnd() {
std::cout << indent() << "}\n";
-- _indent;
}