tan  0.0.1
expr.cpp
1 #include "ast/expr.h"
2 #include "ast/type.h"
3 #include "ast/decl.h"
4 #include <utility>
5 #include <algorithm>
6 
7 using namespace tanlang;
8 
9 Expr::Expr(ASTNodeType type, TokenizedSourceFile *src, int bp) : ASTBase(type, src, bp) {}
10 
11 vector<ASTBase *> Expr::get_children() const { return {}; }
12 
13 /// \section Literals
14 
15 Literal::Literal(ASTNodeType type, TokenizedSourceFile *src, int bp) : Expr(type, src, bp) {}
16 
17 BoolLiteral *BoolLiteral::Create(TokenizedSourceFile *src, bool val) {
18  auto ret = new BoolLiteral(src);
19  ret->_value = val;
20  return ret;
21 }
22 
23 bool BoolLiteral::get_value() const { return _value; }
24 
25 BoolLiteral::BoolLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::BOOL_LITERAL, src, 0) {}
26 
27 IntegerLiteral::IntegerLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::INTEGER_LITERAL, src, 0) {}
28 
29 IntegerLiteral *IntegerLiteral::Create(TokenizedSourceFile *src, uint64_t val, bool is_unsigned) {
30  auto ret = new IntegerLiteral(src);
31  ret->_value = val;
32  ret->_is_unsigned = is_unsigned;
33  return ret;
34 }
35 
36 FloatLiteral *FloatLiteral::Create(TokenizedSourceFile *src, double val) {
37  auto ret = new FloatLiteral(src);
38  ret->_value = val;
39  return ret;
40 }
41 
42 double FloatLiteral::get_value() const { return _value; }
43 
44 void FloatLiteral::set_value(double value) { _value = value; }
45 
46 FloatLiteral::FloatLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::FLOAT_LITERAL, src, 0) {}
47 
48 StringLiteral *StringLiteral::Create(TokenizedSourceFile *src, const str &val) {
49  auto ret = new StringLiteral(src);
50  ret->_value = val;
51  return ret;
52 }
53 
54 str StringLiteral::get_value() const { return _value; }
55 
56 StringLiteral::StringLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::STRING_LITERAL, src, 0) {}
57 
58 CharLiteral *CharLiteral::Create(TokenizedSourceFile *src, uint8_t val) {
59  auto ret = new CharLiteral(src);
60  ret->_value = val;
61  return ret;
62 }
63 
64 void CharLiteral::set_value(uint8_t val) { _value = val; }
65 
66 uint8_t CharLiteral::get_value() const { return _value; }
67 
68 CharLiteral::CharLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::CHAR_LITERAL, src, 0) {}
69 
70 ArrayLiteral *ArrayLiteral::Create(TokenizedSourceFile *src, vector<Literal *> val) {
71  auto ret = new ArrayLiteral(src);
72  ret->_elements = std::move(val);
73  return ret;
74 }
75 
76 ArrayLiteral *ArrayLiteral::Create(TokenizedSourceFile *src) { return new ArrayLiteral(src); }
77 
78 void ArrayLiteral::set_elements(const vector<Literal *> &elements) { _elements = elements; }
79 
80 vector<Literal *> ArrayLiteral::get_elements() const { return _elements; }
81 
82 ArrayLiteral::ArrayLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::ARRAY_LITERAL, src, 0) {}
83 
84 NullPointerLiteral::NullPointerLiteral(TokenizedSourceFile *src) : Literal(ASTNodeType::NULLPTR_LITERAL, src, 0) {}
85 
86 NullPointerLiteral *NullPointerLiteral::Create(TokenizedSourceFile *src) { return new NullPointerLiteral(src); }
87 
88 /// \section Identifier
89 
90 VarRef *VarRef::Create(TokenizedSourceFile *src, const str &name, Decl *referred) {
91  auto ret = new VarRef(src);
92  ret->set_name(name);
93  ret->_referred = referred;
94  return ret;
95 }
96 
97 VarRef::VarRef(TokenizedSourceFile *src) : Expr(ASTNodeType::VAR_REF, src, 0) { _is_lvalue = true; }
98 
99 Decl *VarRef::get_referred() const { return _referred; }
100 
101 Type *VarRef::get_type() const {
102  TAN_ASSERT(_referred);
103  return _referred->get_type();
104 }
105 
106 void VarRef::set_type(Type *) { TAN_ASSERT(false); }
107 
108 Identifier::Identifier(TokenizedSourceFile *src) : Expr(ASTNodeType::ID, src, 0) {}
109 
110 Identifier *Identifier::Create(TokenizedSourceFile *src, const str &name) {
111  auto ret = new Identifier(src);
112  ret->set_name(name);
113  return ret;
114 }
115 
116 IdentifierType Identifier::get_id_type() const { return _id_type; }
117 
118 VarRef *Identifier::get_var_ref() const {
119  TAN_ASSERT(_id_type == IdentifierType::ID_VAR_REF);
120  return _var_ref;
121 }
122 
123 void Identifier::set_var_ref(VarRef *var_ref) {
124  _id_type = IdentifierType::ID_VAR_REF;
125  _var_ref = var_ref;
126 }
127 
128 void Identifier::set_type_ref(Type *type_ref) {
129  _id_type = IdentifierType::ID_TYPE_REF;
130  set_type(type_ref);
131 }
132 
133 bool Identifier::is_lvalue() {
134  TAN_ASSERT(_id_type != IdentifierType::INVALID);
135  if (_id_type == IdentifierType::ID_VAR_REF) {
136  return true;
137  }
138  return false;
139 }
140 
141 void Identifier::set_lvalue(bool) { TAN_ASSERT(false); }
142 
143 /// \section Binary operators
144 
146  : Expr(ASTNodeType::BOP, src, BinaryOperator::BOPPrecedence[op]), _op(op) {}
147 
148 umap<BinaryOpKind, int> BinaryOperator::BOPPrecedence = {
149  {BinaryOpKind::SUM, PREC_TERM },
150  {BinaryOpKind::SUBTRACT, PREC_TERM },
151  {BinaryOpKind::BOR, PREC_TERM },
152  {BinaryOpKind::XOR, PREC_TERM },
153  {BinaryOpKind::MULTIPLY, PREC_FACTOR },
154  {BinaryOpKind::DIVIDE, PREC_FACTOR },
155  {BinaryOpKind::MOD, PREC_FACTOR },
156  {BinaryOpKind::BAND, PREC_FACTOR },
157  {BinaryOpKind::GT, PREC_COMPARISON },
158  {BinaryOpKind::GE, PREC_COMPARISON },
159  {BinaryOpKind::NE, PREC_COMPARISON },
160  {BinaryOpKind::LT, PREC_COMPARISON },
161  {BinaryOpKind::LE, PREC_COMPARISON },
162  {BinaryOpKind::EQ, PREC_COMPARISON },
163  {BinaryOpKind::LAND, PREC_LOGICAL_AND},
164  {BinaryOpKind::LOR, PREC_LOGICAL_OR },
165  {BinaryOpKind::MEMBER_ACCESS, PREC_HIGHEST }
166 };
167 
168 BinaryOperator *BinaryOperator::Create(BinaryOpKind op, TokenizedSourceFile *src) {
169  return new BinaryOperator(op, src);
170 }
171 
172 BinaryOperator *BinaryOperator::Create(BinaryOpKind op, TokenizedSourceFile *src, Expr *lhs, Expr *rhs) {
173  auto ret = new BinaryOperator(op, src);
174  ret->_lhs = lhs;
175  ret->_rhs = rhs;
176  return ret;
177 }
178 
179 void BinaryOperator::set_lhs(Expr *lhs) { _lhs = lhs; }
180 
181 void BinaryOperator::set_rhs(Expr *rhs) { _rhs = rhs; }
182 
183 Expr *BinaryOperator::get_lhs() const { return _lhs; }
184 
185 Expr *BinaryOperator::get_rhs() const { return _rhs; }
186 
187 BinaryOpKind BinaryOperator::get_op() const { return _op; }
188 
189 vector<ASTBase *> BinaryOperator::get_children() const { return {_lhs, _rhs}; }
190 
191 /// \section Unary operators
192 
193 umap<UnaryOpKind, int> UnaryOperator::UOPPrecedence = {
194  {UnaryOpKind::BNOT, PREC_UNARY },
195  {UnaryOpKind::LNOT, PREC_UNARY },
196  {UnaryOpKind::ADDRESS_OF, PREC_UNARY },
197  {UnaryOpKind::PLUS, PREC_UNARY },
198  {UnaryOpKind::MINUS, PREC_UNARY },
199  {UnaryOpKind::PTR_DEREF, PREC_HIGHEST}
200 };
201 
202 UnaryOperator::UnaryOperator(UnaryOpKind op, TokenizedSourceFile *src)
203  : Expr(ASTNodeType::UOP, src, UnaryOperator::UOPPrecedence[op]), _op(op) {}
204 
205 void UnaryOperator::set_rhs(Expr *rhs) { _rhs = rhs; }
206 
207 UnaryOperator *UnaryOperator::Create(UnaryOpKind op, TokenizedSourceFile *src) { return new UnaryOperator(op, src); }
208 
209 UnaryOperator *UnaryOperator::Create(UnaryOpKind op, TokenizedSourceFile *src, Expr *rhs) {
210  auto ret = new UnaryOperator(op, src);
211  ret->_rhs = rhs;
212  return ret;
213 }
214 
215 UnaryOpKind UnaryOperator::get_op() const { return _op; }
216 
217 Expr *UnaryOperator::get_rhs() const { return _rhs; }
218 
219 vector<ASTBase *> UnaryOperator::get_children() const { return {_rhs}; }
220 
221 /// \section Parenthesis
222 
224 
225 Parenthesis::Parenthesis(TokenizedSourceFile *src)
226  : Expr(ASTNodeType::PARENTHESIS, src, ASTBase::OpPrecedence[ASTNodeType::PARENTHESIS]) {}
227 
228 void Parenthesis::set_sub(Expr *sub) { _sub = sub; }
229 
230 Expr *Parenthesis::get_sub() const { return _sub; }
231 
232 vector<ASTBase *> Parenthesis::get_children() const { return {_sub}; }
233 
234 void Parenthesis::set_lvalue(bool) { TAN_ASSERT(false); }
235 
236 bool Parenthesis::is_lvalue() { return _sub->is_lvalue(); }
237 
238 /// \section MEMBER_ACCESS operator
239 
241 
242 MemberAccess::MemberAccess(TokenizedSourceFile *src) : BinaryOperator(BinaryOpKind::MEMBER_ACCESS, src) {}
243 
244 void MemberAccess::set_lvalue(bool) { TAN_ASSERT(false); }
245 
246 bool MemberAccess::is_lvalue() {
247  TAN_ASSERT(_lhs);
248  return _lhs->is_lvalue();
249 }
250 
251 /// \section Function call
252 
254 
255 FunctionCall::FunctionCall(TokenizedSourceFile *src) : Expr(ASTNodeType::FUNC_CALL, src, PREC_LOWEST) {}
256 
257 size_t FunctionCall::get_n_args() const { return _args.size(); }
258 
259 Expr *FunctionCall::get_arg(size_t i) const {
260  TAN_ASSERT(i < _args.size());
261  return _args[i];
262 }
263 
264 vector<ASTBase *> FunctionCall::get_children() const {
265  vector<ASTBase *> ret = {(ASTBase *)_callee};
266  std::for_each(_args.begin(), _args.end(), [&](Expr *e) { ret.push_back(e); });
267  return ret;
268 }
269 
270 /// \section Assignment
271 
272 Expr *Assignment::get_rhs() const { return _rhs; }
273 
274 void Assignment::set_rhs(Expr *rhs) { _rhs = rhs; }
275 
276 Assignment *Assignment::Create(TokenizedSourceFile *src) { return new Assignment(src); }
277 
278 Assignment::Assignment(TokenizedSourceFile *src)
279  : Expr(ASTNodeType::ASSIGN, src, ASTBase::OpPrecedence[ASTNodeType::ASSIGN]) {}
280 
281 ASTBase *Assignment::get_lhs() const { return _lhs; }
282 
283 void Assignment::set_lhs(ASTBase *lhs) { _lhs = lhs; }
284 
285 vector<ASTBase *> Assignment::get_children() const { return {_lhs, _rhs}; }
286 
287 /// \section Cast
288 
289 Expr *Cast::get_lhs() const { return _lhs; }
290 
291 void Cast::set_lhs(Expr *lhs) { _lhs = lhs; }
292 
293 bool Cast::is_lvalue() { return _lhs->is_lvalue(); }
294 
295 void Cast::set_lvalue(bool) { TAN_ASSERT(false); }
296 
297 Cast *Cast::Create(TokenizedSourceFile *src) { return new Cast(src); }
298 
299 Cast::Cast(TokenizedSourceFile *src) : Expr(ASTNodeType::CAST, src, ASTBase::OpPrecedence[ASTNodeType::CAST]) {}
300 
301 vector<ASTBase *> Cast::get_children() const { return {_lhs}; }
302 
303 bool Cast::is_comptime_known() { return _lhs->is_comptime_known(); }
304 
305 BinaryOrUnary::BinaryOrUnary(TokenizedSourceFile *src, int bp) : Expr(ASTNodeType::BOP_OR_UOP, src, bp) {}
306 
307 BinaryOrUnary *BinaryOrUnary::Create(TokenizedSourceFile *src, int bp) { return new BinaryOrUnary(src, bp); }
308 
309 void BinaryOrUnary::set_bop(BinaryOperator *bop) {
310  TAN_ASSERT(_kind == UNKNOWN); /// prevent setting this twice
311  _kind = BINARY;
312  _bop = bop;
313 }
314 
315 void BinaryOrUnary::set_uop(UnaryOperator *uop) {
316  TAN_ASSERT(_kind == UNKNOWN); /// prevent setting this twice
317  _kind = UNARY;
318  _uop = uop;
319 }
320 
321 Expr *BinaryOrUnary::get_expr_ptr() const {
322  switch (_kind) {
323  case BINARY:
324  return _bop;
325  case UNARY:
326  return _uop;
327  default:
328  TAN_ASSERT(false);
329  }
330 }
331 
332 ASTBase *BinaryOrUnary::get() const { return get_expr_ptr(); }
333 
334 Type *BinaryOrUnary::get_type() const { return get_expr_ptr()->get_type(); }
335 
336 void BinaryOrUnary::set_type(Type *type) { get_expr_ptr()->set_type(type); }
337 
338 vector<ASTBase *> BinaryOrUnary::get_children() const { return get_expr_ptr()->get_children(); }
339 
340 bool BinaryOrUnary::is_lvalue() { return get_expr_ptr()->is_lvalue(); }
341 
342 void BinaryOrUnary::set_lvalue(bool is_lvalue) { get_expr_ptr()->set_lvalue(is_lvalue); }
343 
345 Literal::CreateIntegerLiteral(TokenizedSourceFile *src, uint64_t val, size_t bit_size, bool is_unsigned) {
346  auto *ret = IntegerLiteral::Create(src, val, is_unsigned);
347  ret->set_type(Type::GetIntegerType(bit_size, is_unsigned));
348  return ret;
349 }
350 
351 BoolLiteral *Literal::CreateBoolLiteral(TokenizedSourceFile *src, bool val) {
352  auto *ret = BoolLiteral::Create(src, val);
353  ret->set_type(Type::GetBoolType());
354  return ret;
355 }
356 
357 FloatLiteral *Literal::CreateFloatLiteral(TokenizedSourceFile *src, double val, size_t bit_size) {
358  auto *ret = FloatLiteral::Create(src, val);
359  ret->set_type(Type::GetFloatType(bit_size));
360  return ret;
361 }
362 
363 StringLiteral *Literal::CreateStringLiteral(TokenizedSourceFile *src, str val) {
364  auto *ret = StringLiteral::Create(src, val);
365  ret->set_type(Type::GetStringType());
366  return ret;
367 }
368 
369 CharLiteral *Literal::CreateCharLiteral(TokenizedSourceFile *src, uint8_t val) {
370  auto *ret = CharLiteral::Create(src, val);
371  ret->set_type(Type::GetCharType());
372  return ret;
373 }
374 
375 ArrayLiteral *Literal::CreateArrayLiteral(TokenizedSourceFile *src, Type *element_type, vector<Literal *> elements) {
376  auto *ret = ArrayLiteral::Create(src, elements);
377 
378  vector<Type *> sub_types{};
379  ret->set_type(Type::GetArrayType(element_type, (int)elements.size()));
380 
381  return ret;
382 }
383 
384 NullPointerLiteral *Literal::CreateNullPointerLiteral(TokenizedSourceFile *src, Type *element_type) {
385  auto *ret = NullPointerLiteral::Create(src);
386  ret->set_type(Type::GetPointerType(element_type));
387  return ret;
388 }
Expr * _rhs
lhs can be decl or expr (identifier)
Definition: expr.h:364
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:285
Expr * get_rhs() const
Definition: expr.cpp:272
BinaryOperator(BinaryOpKind op, TokenizedSourceFile *src)
Definition: expr.cpp:145
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:189
static umap< BinaryOpKind, int > BOPPrecedence
binary operator precedence
Definition: expr.h:209
ASTBase * get() const override
Get the "actual" this. Used for implementing proxy classes.
Definition: expr.cpp:332
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:338
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:301
Expr * get_lhs() const
Definition: expr.cpp:289
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:11
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:264
static FunctionCall * Create(TokenizedSourceFile *src)
Definition: expr.cpp:253
Literal(ASTNodeType type, TokenizedSourceFile *src, int bp)
Definition: expr.cpp:15
static MemberAccess * Create(TokenizedSourceFile *src)
Definition: expr.cpp:240
static Parenthesis * Create(TokenizedSourceFile *src)
Definition: expr.cpp:223
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:232
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...
Definition: type.h:22
static umap< UnaryOpKind, int > UOPPrecedence
binary operator precedence
Definition: expr.h:263
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: expr.cpp:219
static VarRef * Create(TokenizedSourceFile *src, const str &name, Decl *referred)
Definition: expr.cpp:90