nixd
Loading...
Searching...
No Matches
Op.h
Go to the documentation of this file.
1#pragma once
2
3#include "Basic.h"
4
7
8#include <memory>
9
10namespace nixf {
11
12class Op : public Node {
13 const tok::TokenKind OpKind;
14
15public:
17 : Node(NK_Op, Range), OpKind(OpKind) {}
18
19 [[nodiscard]] tok::TokenKind op() const { return OpKind; }
20
21 [[nodiscard]] ChildVector children() const override { return {}; }
22};
23
24/// \brief Abstract class for binary operators and unary operators.
25class ExprOp : public Expr {
26 const std::shared_ptr<Op> O;
27
28public:
29 ExprOp(NodeKind Kind, LexerCursorRange Range, std::shared_ptr<Op> O)
30 : Expr(Kind, Range), O(std::move(O)) {
31 assert(this->O && "O must not be null");
32 }
33
34 [[nodiscard]] Op &op() const { return *O; }
35
36 [[nodiscard]] ChildVector children() const override { return {O.get()}; }
37};
38
39class ExprBinOp : public ExprOp {
40 const std::shared_ptr<Expr> LHS;
41 const std::shared_ptr<Expr> RHS;
42
43public:
44 ExprBinOp(LexerCursorRange Range, std::shared_ptr<Op> O,
45 std::shared_ptr<Expr> LHS, std::shared_ptr<Expr> RHS)
46 : ExprOp(NK_ExprBinOp, Range, std::move(O)), LHS(std::move(LHS)),
47 RHS(std::move(RHS)) {}
48
49 [[nodiscard]] Expr *lhs() const { return LHS.get(); }
50 [[nodiscard]] Expr *rhs() const { return RHS.get(); }
51
52 [[nodiscard]] ChildVector children() const override {
53 return {&op(), LHS.get(), RHS.get()};
54 }
55};
56
57class ExprOpHasAttr : public ExprOp {
58 const std::shared_ptr<Expr> E;
59 const std::shared_ptr<AttrPath> Path;
60
61public:
62 ExprOpHasAttr(LexerCursorRange Range, std::shared_ptr<Op> O,
63 std::shared_ptr<Expr> E, std::shared_ptr<AttrPath> Path)
64 : ExprOp(NK_ExprOpHasAttr, Range, std::move(O)), E(std::move(E)),
65 Path(std::move(Path)) {}
66
67 [[nodiscard]] Expr *expr() const { return E.get(); }
68 [[nodiscard]] AttrPath *attrpath() const { return Path.get(); }
69
70 [[nodiscard]] ChildVector children() const override {
71 return {E.get(), Path.get()};
72 }
73};
74
75class ExprUnaryOp : public ExprOp {
76 const std::shared_ptr<Expr> E;
77
78public:
79 ExprUnaryOp(LexerCursorRange Range, std::shared_ptr<Op> O,
80 std::shared_ptr<Expr> E)
81 : ExprOp(NK_ExprUnaryOp, Range, std::move(O)), E(std::move(E)) {}
82
83 [[nodiscard]] Expr *expr() const { return E.get(); }
84
85 [[nodiscard]] ChildVector children() const override {
86 return {&op(), E.get()};
87 }
88};
89
90} // namespace nixf
ExprBinOp(LexerCursorRange Range, std::shared_ptr< Op > O, std::shared_ptr< Expr > LHS, std::shared_ptr< Expr > RHS)
Definition Op.h:44
Expr * rhs() const
Definition Op.h:50
Expr * lhs() const
Definition Op.h:49
ChildVector children() const override
Definition Op.h:52
Expr * expr() const
Definition Op.h:67
ChildVector children() const override
Definition Op.h:70
AttrPath * attrpath() const
Definition Op.h:68
ExprOpHasAttr(LexerCursorRange Range, std::shared_ptr< Op > O, std::shared_ptr< Expr > E, std::shared_ptr< AttrPath > Path)
Definition Op.h:62
Abstract class for binary operators and unary operators.
Definition Op.h:25
ChildVector children() const override
Definition Op.h:36
Op & op() const
Definition Op.h:34
ExprOp(NodeKind Kind, LexerCursorRange Range, std::shared_ptr< Op > O)
Definition Op.h:29
ExprUnaryOp(LexerCursorRange Range, std::shared_ptr< Op > O, std::shared_ptr< Expr > E)
Definition Op.h:79
ChildVector children() const override
Definition Op.h:85
Expr * expr() const
Definition Op.h:83
boost::container::small_vector< Node *, 8 > ChildVector
Definition Basic.h:42
Definition Op.h:12
ChildVector children() const override
Definition Op.h:21
tok::TokenKind op() const
Definition Op.h:19
Op(LexerCursorRange Range, tok::TokenKind OpKind)
Definition Op.h:16