7 using namespace tanlang;
11 vector<ASTBase *> Expr::get_children()
const {
return {}; }
23 bool BoolLiteral::get_value()
const {
return _value; }
32 ret->_is_unsigned = is_unsigned;
42 double FloatLiteral::get_value()
const {
return _value; }
44 void FloatLiteral::set_value(
double value) { _value = value; }
54 str StringLiteral::get_value()
const {
return _value; }
64 void CharLiteral::set_value(uint8_t val) { _value = val; }
66 uint8_t CharLiteral::get_value()
const {
return _value; }
72 ret->_elements = std::move(val);
78 void ArrayLiteral::set_elements(
const vector<Literal *> &elements) { _elements = elements; }
80 vector<Literal *> ArrayLiteral::get_elements()
const {
return _elements; }
91 auto ret =
new VarRef(src);
93 ret->_referred = referred;
99 Decl *VarRef::get_referred()
const {
return _referred; }
101 Type *VarRef::get_type()
const {
102 TAN_ASSERT(_referred);
103 return _referred->get_type();
106 void VarRef::set_type(
Type *) { TAN_ASSERT(
false); }
116 IdentifierType Identifier::get_id_type()
const {
return _id_type; }
118 VarRef *Identifier::get_var_ref()
const {
119 TAN_ASSERT(_id_type == IdentifierType::ID_VAR_REF);
123 void Identifier::set_var_ref(
VarRef *var_ref) {
124 _id_type = IdentifierType::ID_VAR_REF;
128 void Identifier::set_type_ref(
Type *type_ref) {
129 _id_type = IdentifierType::ID_TYPE_REF;
133 bool Identifier::is_lvalue() {
134 TAN_ASSERT(_id_type != IdentifierType::INVALID);
135 if (_id_type == IdentifierType::ID_VAR_REF) {
141 void Identifier::set_lvalue(
bool) { TAN_ASSERT(
false); }
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 }
179 void BinaryOperator::set_lhs(
Expr *lhs) { _lhs = lhs; }
181 void BinaryOperator::set_rhs(
Expr *rhs) { _rhs = rhs; }
183 Expr *BinaryOperator::get_lhs()
const {
return _lhs; }
185 Expr *BinaryOperator::get_rhs()
const {
return _rhs; }
187 BinaryOpKind BinaryOperator::get_op()
const {
return _op; }
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}
205 void UnaryOperator::set_rhs(
Expr *rhs) { _rhs = rhs; }
215 UnaryOpKind UnaryOperator::get_op()
const {
return _op; }
217 Expr *UnaryOperator::get_rhs()
const {
return _rhs; }
226 :
Expr(ASTNodeType::PARENTHESIS, src,
ASTBase::OpPrecedence[ASTNodeType::PARENTHESIS]) {}
228 void Parenthesis::set_sub(
Expr *sub) { _sub = sub; }
230 Expr *Parenthesis::get_sub()
const {
return _sub; }
234 void Parenthesis::set_lvalue(
bool) { TAN_ASSERT(
false); }
236 bool Parenthesis::is_lvalue() {
return _sub->is_lvalue(); }
244 void MemberAccess::set_lvalue(
bool) { TAN_ASSERT(
false); }
246 bool MemberAccess::is_lvalue() {
248 return _lhs->is_lvalue();
257 size_t FunctionCall::get_n_args()
const {
return _args.size(); }
259 Expr *FunctionCall::get_arg(
size_t i)
const {
260 TAN_ASSERT(i < _args.size());
265 vector<ASTBase *> ret = {(
ASTBase *)_callee};
266 std::for_each(_args.begin(), _args.end(), [&](
Expr *e) { ret.push_back(e); });
274 void Assignment::set_rhs(
Expr *rhs) {
_rhs = rhs; }
279 :
Expr(ASTNodeType::ASSIGN, src,
ASTBase::OpPrecedence[ASTNodeType::ASSIGN]) {}
281 ASTBase *Assignment::get_lhs()
const {
return _lhs; }
283 void Assignment::set_lhs(
ASTBase *lhs) { _lhs = lhs; }
291 void Cast::set_lhs(
Expr *lhs) { _lhs = lhs; }
293 bool Cast::is_lvalue() {
return _lhs->is_lvalue(); }
295 void Cast::set_lvalue(
bool) { TAN_ASSERT(
false); }
303 bool Cast::is_comptime_known() {
return _lhs->is_comptime_known(); }
310 TAN_ASSERT(_kind == UNKNOWN);
316 TAN_ASSERT(_kind == UNKNOWN);
321 Expr *BinaryOrUnary::get_expr_ptr()
const {
334 Type *BinaryOrUnary::get_type()
const {
return get_expr_ptr()->get_type(); }
336 void BinaryOrUnary::set_type(
Type *type) { get_expr_ptr()->set_type(type); }
340 bool BinaryOrUnary::is_lvalue() {
return get_expr_ptr()->is_lvalue(); }
342 void BinaryOrUnary::set_lvalue(
bool is_lvalue) { get_expr_ptr()->set_lvalue(is_lvalue); }
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));
352 auto *ret = BoolLiteral::Create(src, val);
353 ret->set_type(Type::GetBoolType());
358 auto *ret = FloatLiteral::Create(src, val);
359 ret->set_type(Type::GetFloatType(bit_size));
364 auto *ret = StringLiteral::Create(src, val);
365 ret->set_type(Type::GetStringType());
370 auto *ret = CharLiteral::Create(src, val);
371 ret->set_type(Type::GetCharType());
376 auto *ret = ArrayLiteral::Create(src, elements);
378 vector<Type *> sub_types{};
379 ret->set_type(Type::GetArrayType(element_type, (
int)elements.size()));
385 auto *ret = NullPointerLiteral::Create(src);
386 ret->set_type(Type::GetPointerType(element_type));
Expr * _rhs
lhs can be decl or expr (identifier)
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
BinaryOperator(BinaryOpKind op, TokenizedSourceFile *src)
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
static umap< BinaryOpKind, int > BOPPrecedence
binary operator precedence
ASTBase * get() const override
Get the "actual" this. Used for implementing proxy classes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
static FunctionCall * Create(TokenizedSourceFile *src)
Literal(ASTNodeType type, TokenizedSourceFile *src, int bp)
static MemberAccess * Create(TokenizedSourceFile *src)
static Parenthesis * Create(TokenizedSourceFile *src)
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
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...
static umap< UnaryOpKind, int > UOPPrecedence
binary operator precedence
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
static VarRef * Create(TokenizedSourceFile *src, const str &name, Decl *referred)