-
Notifications
You must be signed in to change notification settings - Fork 1
/
xos.h
292 lines (240 loc) · 6.64 KB
/
xos.h
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#pragma once
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include <iostream>
#include <istream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wconversion"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Verifier.h"
#pragma GCC diagnostic pop
using namespace llvm;
namespace xos {
// Just in case we ever decide to parse a file with more than 2^32 rows/cols,
// let's reserve the type here so we can change it easily.
using pos_t = uint32_t;
struct SourceLocation {
pos_t row, col;
bool operator==(const SourceLocation &other) const {
return row == other.row && col == other.col;
}
};
class Token {
public:
enum Kind {
eof,
out,
lparen,
rparen,
arrow,
string,
identifier,
number,
};
Token() = default;
Token(Kind kind, pos_t row, pos_t col, std::string val)
: kind_(kind), start_{row, col}, val_(val) {}
Token(Kind kind, pos_t row, pos_t col) : kind_(kind), start_{row, col} {}
Kind getKind() const { return kind_; }
pos_t getRow() const { return start_.row; }
pos_t getCol() const { return start_.col; }
std::string getVal() const { return val_; }
bool operator==(const Token &other) const {
return kind_ == other.kind_ && start_ == other.start_ && val_ == other.val_;
}
private:
Kind kind_;
SourceLocation start_;
std::string val_;
};
template <typename T>
class Result {
public:
template <typename... ArgsTy>
Result(ArgsTy... args) : result_(new T(std::forward<ArgsTy>(args)...)) {}
Result(const T &result) : result_(new T(result)) {}
Result(T &&result) : result_(new T(std::move(result))) {}
template <typename U>
Result(const Result<U> &other) : err_(other.getError()) {}
class BuildError;
Result(const BuildError &builder) : err_(builder.get()) {}
static Result Error(const std::string &msg) {
Result result;
result.err_ = msg;
return result;
}
bool hasError() const { return !result_; }
operator bool() const { return !hasError(); }
const T &operator*() const { return get(); }
T &operator*() { return get(); }
const T *operator->() const { return &get(); }
T *operator->() { return &get(); }
const T &get() const {
assert(!hasError() &&
"Getting from an invalid result. Don't forget to check if a result "
"is valid with `res.hasError()` before getting from it.");
return *result_;
}
T &get() {
assert(!hasError() &&
"Getting from an invalid result. Don't forget to check if a result "
"is valid with `res.hasError()` before getting from it.");
return *result_;
}
const auto &getError() const {
assert(hasError() && "Expected an error");
return err_;
}
T *Release() { return result_.release(); }
// Example usage:
//
// Result<Token> getTok() {
// ...
// if (err)
// return Result<Token>::BuildError() << "Error on row " << row;
// ...
// }
//
class BuildError {
public:
BuildError() {}
template <typename U>
BuildError &operator<<(const U &u) {
buffer_ << u;
return *this;
}
std::string get() const { return buffer_.str(); }
private:
std::stringstream buffer_;
};
private:
Result() = default;
std::string err_;
std::unique_ptr<T> result_;
};
class Lexer {
public:
Lexer(std::istream &input);
Result<Token> Lex() {
if (has_lookahead_token_) {
has_lookahead_token_ = false;
return lookahead_token_;
}
return LexImpl();
}
Result<Token> Peek() {
if (!has_lookahead_token_) {
auto next = LexImpl();
if (next.hasError()) return next;
has_lookahead_token_ = true;
lookahead_token_ = next.get();
}
return lookahead_token_;
}
private:
int getNextChar();
Result<Token> LexImpl();
std::istream &input_;
pos_t row_ = 1, col_ = 0;
bool has_lookahead_ = false;
int lookahead_;
bool has_lookahead_token_ = false;
Token lookahead_token_;
};
namespace ast {
class Expr {
public:
virtual ~Expr() = default;
bool operator==(
const Expr &other) const // TODO(PiJoules): add rtti and implement the
// comparison operator here.
{
const auto l = other;
return true;
};
};
class Prototype {
public:
Prototype(const std::string &name, std::vector<std::string> args,
std::string return_type)
: name_(name), args_(args), return_type_(return_type) {}
bool operator==(const Prototype &other) const {
return name_ == other.name_ && args_ == other.args_ &&
return_type_ == other.return_type_;
}
std::string getName() const { return name_; }
private:
std::string name_;
std::vector<std::string> args_;
std::string return_type_;
};
class Str : public Expr {
public:
Str(const std::string &str) : str_(str) {}
std::string getStr() const { return str_; }
bool operator==(const Str &other) const { return str_ == other.str_; }
private:
std::string str_;
};
class Out : public Expr {
public:
Out(std::unique_ptr<Str> &&expr) : expr_(std::move(expr)) {}
const Str &getExpr() const { return *expr_; }
private:
std::unique_ptr<Str> expr_;
};
class Func {
public:
Func(std::unique_ptr<Prototype> proto, std::unique_ptr<Out> body)
: proto_(std::move(proto)), body_(std::move(body)) {}
bool operator==(const Func &other) const {
return *proto_ == *other.proto_ && *body_ == *other.body_;
}
const Prototype &getProto() const { return *proto_; }
const Out &getBody() const { return *body_; }
private:
std::unique_ptr<Prototype> proto_;
std::unique_ptr<Out> body_;
};
} // namespace ast
class Parser {
public:
Parser(Lexer &lexer) : lexer_(lexer) {}
Result<ast::Str> parseStr();
Result<ast::Out> parseOut();
Result<ast::Func> parseFunc();
Result<ast::Out> parseBody();
Result<ast::Prototype> parsePrototype(std::string name);
private:
Lexer &lexer_;
};
class Compiler {
std::unique_ptr<LLVMContext> TheContext;
std::unique_ptr<IRBuilder<>> Builder;
std::map<std::string, Value *> NamedValues;
public:
std::unique_ptr<Module> TheModule;
Compiler();
Value *LogErrorV(const char *str);
// Value *NumberExprAST();
// Value *VariableExprAST();
// Value *BinaryExprAST();
// Value *CallExprAST();
Value *StringExprAST(const ast::Str &str);
Value *OutExprAST(const ast::Out &out);
Function *PrototypeAST(const ast::Prototype &);
Function *FunctionAST(const ast::Func &func);
void compile(const ast::Func &funcAST);
};
} // namespace xos