-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
195 lines (164 loc) · 5.6 KB
/
main.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
190
191
192
193
194
195
#include <map>
#include <string>
#include <vector>
#include <iostream>
bool Throw(const std::string & s) { throw std::runtime_error(s); }
#define assert(cond) (cond ? true : Throw("Assertion failed; line " + std::to_string(lex_pos.l) + " column " + std::to_string(lex_pos.c)))
#define match(s, r) (assert((r ? read() : t) == s) ? s : "")
#define assert_do(c, d) ((assert(c) ? d : nullptr))
#define loop_exprs(d) for (auto & e : expr.exprs) d
/// Structure used for all expressions
/// This is the stuff the AST is made of
struct Expr {
bool nameless;
std::string x;
std::vector<Expr> exprs;
bool operator==(const Expr & o) const { return nameless == o.nameless and x == o.x and exprs == o.exprs; }
bool operator!=(const Expr & o) const { return !(this->operator==(o)); }
};
typedef std::function<std::string(Expr)> overruling;
/// Everything that causes the lexer to end a token
std::string stoppers = "() \n;";
/// (Lexer internal) current char
char c = ' ';
/// Current token
std::string t;
/// Generated functions (strings of c code)
std::vector<std::string> functions;
/// Overwrites for certain expressions
std::map<std::string, overruling> overrulings;
/// Total count of functions
int function_counter = 0;
struct {
int l;
int c;
} lex_pos;
// -------------------- Lexer --------------------
/// Read character from input file
char readChar() {
char buffer[1];
std::cin.read(buffer, 1);
lex_pos.c++;
if (buffer[0] == '\n') {
lex_pos.c = 0;
lex_pos.l++;
}
return c = (std::cin.gcount() < 1 ? (char) -1 : buffer[0]);
}
/// Read token from input file
std::string read() {
std::string token;
while (c == ' ' or c == '\t' or c == '\n' or c == ';') {
if (c == ';')
while (c != '\n') readChar();
readChar();
}
if (c < 0) return "";
else if (c == '"') {
token += c;
do {
token += readChar();
} while (c != '"');
readChar();
} else
do {
if (stoppers.find(c) == std::string::npos)
token += c;
else {
if (token.empty()) {
token = {c};
readChar();
}
break;
}
} while ((readChar()) >= 0);
return t = token;
}
// -------------------- Parser --------------------
Expr read_instr(bool);
/// Read a value [primitives, strings or (do stuff)]
Expr read_val(bool r) {
if (r) read();
if (t == "(") return read_instr(false);
return Expr {true, t, {}};
}
/// Read an instruction [looks like this: (do stuff)]
Expr read_instr(bool r) {
match("(", r);
Expr expr = {.nameless = false, .x = read()};
while (true) {
read();
if (t != ")") expr.exprs.push_back(read_val(false));
else break;
}
return expr;
}
// -------------------- Code Generator --------------------
/// Generate c code for an expression
std::string gen_expr(Expr expr) {
if (!expr.nameless) {
if (overrulings.count(expr.x))
return overrulings[expr.x](expr);
std::string call_l = expr.x + "(";
loop_exprs(call_l += gen_expr(e) + (e != expr.exprs.back() ? ", " : ""));
return call_l + ")";
} else
return expr.x;
}
#define type_overruling(type) overrulings.emplace(#type, [](Expr expr){ \
return assert_do(expr.exprs.size() == 1, #type" " + expr.exprs[0].x); \
})
#define scope_overruling(name) overrulings.emplace(#name, [](Expr expr){ \
std::string res = #name" (" + gen_expr(expr.exprs[0]) + "){"; \
expr.exprs.erase(expr.exprs.begin()); \
loop_exprs(res += gen_expr(e) + ";\n"); \
return res + "}"; \
})
#define operator_overruling(name, op) overrulings.emplace(#name, [](Expr expr) { \
std::string res; \
loop_exprs(res += e.x + (e != expr.exprs.back() ? #op : "")); \
return res; \
})
int main(int argc, const char ** argv) {
overrulings.emplace("return", [](const Expr & expr) {
std::string res = "return " + gen_expr(assert(expr.exprs.size() == 1) ? expr.exprs[0] : Expr());
return "return " + gen_expr(expr.exprs[0]);
});
overrulings.emplace("set", [](Expr expr) {
return assert_do(expr.exprs.size() == 2, expr.exprs[0].x + "=" + gen_expr(expr.exprs[1]));
});
overrulings.emplace("include", [](Expr expr) {
functions.push_back("#include " + expr.exprs[0].x + "\n");
return assert_do(expr.exprs.size() == 1, "");
});
overrulings.emplace("do", [](Expr expr) {
std::string function_id = std::to_string(++function_counter);
std::string function = assert_do(expr.exprs.size() > 1, expr.exprs[0].x) + " f" + function_id + "() {\n";
expr.exprs.erase(expr.exprs.begin());
loop_exprs(function += gen_expr(e) + ";\n");
functions.push_back(function + "}\n");
return "f" + function_id + "()";
});
operator_overruling(add, +);
operator_overruling(sub, -);
operator_overruling(mul, *);
operator_overruling(div, /);
operator_overruling(mod, %);
scope_overruling(if);
scope_overruling(while);
type_overruling(int);
type_overruling(float);
/// Open input and input file and pipe to stdin/out
freopen(assert(argc >= 2) ? argv[1] : nullptr, "r", stdin);
freopen("out.c", "w", stdout);
/// Read and parse
Expr root = read_instr(true);
/// Write all the functions
std::string e = gen_expr(root);
std::string o;
for (const auto & func : functions)
o += func + "\n";
/// Make sure everything is called and print the code
std::cout << o + "int main(){" + e + ";}" << std::endl;
return 0;
}