nixd
Loading...
Searching...
No Matches
Expr.h
Go to the documentation of this file.
1#pragma once
2
3#include "Attrs.h"
4
5namespace nixf {
6
7class ExprSelect : public Expr {
8 const std::shared_ptr<Expr> E;
9 const std::shared_ptr<Dot> Do;
10 const std::shared_ptr<AttrPath> Path;
11 const std::shared_ptr<Expr> Default;
12
13public:
14 ExprSelect(LexerCursorRange Range, std::shared_ptr<Expr> E,
15 std::shared_ptr<Dot> Do, std::shared_ptr<AttrPath> Path,
16 std::shared_ptr<Expr> Default)
17 : Expr(NK_ExprSelect, Range), E(std::move(E)), Do(std::move(Do)),
18 Path(std::move(Path)), Default(std::move(Default)) {
19 assert(this->E && "E must not be null");
20 }
21
22 [[nodiscard]] Expr &expr() const {
23 assert(E && "E must not be null");
24 return *E;
25 }
26
27 [[nodiscard]] Dot *dot() const { return Do.get(); }
28
29 [[nodiscard]] Expr *defaultExpr() const { return Default.get(); }
30
31 [[nodiscard]] AttrPath *path() const { return Path.get(); }
32
33 [[nodiscard]] ChildVector children() const override {
34 return {E.get(), Path.get(), Default.get()};
35 }
36};
37
38/// A call/apply to some function.
39class ExprCall : public Expr {
40 const std::shared_ptr<Expr> Fn;
41 const std::vector<std::shared_ptr<Expr>> Args;
42
43public:
44 ExprCall(LexerCursorRange Range, std::shared_ptr<Expr> Fn,
45 std::vector<std::shared_ptr<Expr>> Args)
46 : Expr(NK_ExprCall, Range), Fn(std::move(Fn)), Args(std::move(Args)) {
47 assert(this->Fn && "Fn must not be null");
48 }
49
50 [[nodiscard]] Expr &fn() const {
51 assert(Fn && "Fn must not be null");
52 return *Fn;
53 }
54
55 [[nodiscard]] const std::vector<std::shared_ptr<Expr>> &args() const {
56 return Args;
57 }
58
59 [[nodiscard]] ChildVector children() const override {
60 ChildVector Children;
61 Children.reserve(Args.size() + 1);
62 Children.emplace_back(Fn.get());
63 for (const auto &Member : Args) {
64 Children.emplace_back(Member.get());
65 }
66 return Children;
67 }
68};
69
70class ExprList : public Expr {
71 const std::vector<std::shared_ptr<Expr>> Elements;
72
73public:
74 ExprList(LexerCursorRange Range, std::vector<std::shared_ptr<Expr>> Elements)
75 : Expr(NK_ExprList, Range), Elements(std::move(Elements)) {}
76
77 [[nodiscard]] const std::vector<std::shared_ptr<Expr>> &elements() const {
78 return Elements;
79 }
80
81 [[nodiscard]] ChildVector children() const override {
82 ChildVector Children;
83 Children.reserve(Elements.size());
84 for (const auto &Element : Elements) {
85 Children.emplace_back(Element.get());
86 }
87 return Children;
88 }
89};
90
91class ExprIf : public Expr {
92 const std::shared_ptr<Expr> Cond;
93 const std::shared_ptr<Expr> Then;
94 const std::shared_ptr<Expr> Else;
95
96public:
97 ExprIf(LexerCursorRange Range, std::shared_ptr<Expr> Cond,
98 std::shared_ptr<Expr> Then, std::shared_ptr<Expr> Else)
99 : Expr(NK_ExprIf, Range), Cond(std::move(Cond)), Then(std::move(Then)),
100 Else(std::move(Else)) {}
101
102 [[nodiscard]] Expr *cond() const { return Cond.get(); }
103 [[nodiscard]] Expr *then() const { return Then.get(); }
104 [[nodiscard]] Expr *elseExpr() const { return Else.get(); }
105
106 [[nodiscard]] ChildVector children() const override {
107 return {Cond.get(), Then.get(), Else.get()};
108 }
109};
110
111class ExprAssert : public Expr {
112 const std::shared_ptr<Expr> Cond;
113 const std::shared_ptr<Expr>
114 Value; // If "cond" is true, then "value" is returned.
115
116public:
117 ExprAssert(LexerCursorRange Range, std::shared_ptr<Expr> Cond,
118 std::shared_ptr<Expr> Value)
119 : Expr(NK_ExprAssert, Range), Cond(std::move(Cond)),
120 Value(std::move(Value)) {}
121
122 [[nodiscard]] Expr *cond() const { return Cond.get(); }
123 [[nodiscard]] Expr *value() const { return Value.get(); }
124
125 [[nodiscard]] ChildVector children() const override {
126 return {Cond.get(), Value.get()};
127 }
128};
129
130class ExprLet : public Expr {
131 // 'let' binds 'in' expr
132
133 const std::shared_ptr<Misc> KwLet; // 'let', not null
134 const std::shared_ptr<Misc> KwIn;
135 const std::shared_ptr<Expr> E;
136
137 const std::shared_ptr<ExprAttrs> Attrs;
138
139public:
140 ExprLet(LexerCursorRange Range, std::shared_ptr<Misc> KwLet,
141 std::shared_ptr<Misc> KwIn, std::shared_ptr<Expr> E,
142 std::shared_ptr<ExprAttrs> Attrs)
143 : Expr(NK_ExprLet, Range), KwLet(std::move(KwLet)), KwIn(std::move(KwIn)),
144 E(std::move(E)), Attrs(std::move(Attrs)) {
145 assert(this->KwLet && "KwLet should not be empty!");
146 }
147
148 [[nodiscard]] const Binds *binds() const {
149 return Attrs ? Attrs->binds() : nullptr;
150 }
151 [[nodiscard]] const ExprAttrs *attrs() const { return Attrs.get(); }
152 [[nodiscard]] const Expr *expr() const { return E.get(); }
153 [[nodiscard]] const Misc &let() const { return *KwLet; }
154 [[nodiscard]] const Misc *in() const { return KwIn.get(); }
155
156 [[nodiscard]] ChildVector children() const override {
157 return {KwLet.get(), Attrs.get(), KwIn.get(), E.get()};
158 }
159};
160
161class ExprWith : public Expr {
162 const std::shared_ptr<Misc> KwWith;
163 const std::shared_ptr<Misc> TokSemi;
164 const std::shared_ptr<Expr> With;
165 const std::shared_ptr<Expr> E;
166
167public:
168 ExprWith(LexerCursorRange Range, std::shared_ptr<Misc> KwWith,
169 std::shared_ptr<Misc> TokSemi, std::shared_ptr<Expr> With,
170 std::shared_ptr<Expr> E)
171 : Expr(NK_ExprWith, Range), KwWith(std::move(KwWith)),
172 TokSemi(std::move(TokSemi)), With(std::move(With)), E(std::move(E)) {}
173
174 [[nodiscard]] const Misc &kwWith() const { return *KwWith; }
175 [[nodiscard]] const Misc *tokSemi() const { return TokSemi.get(); }
176 [[nodiscard]] Expr *with() const { return With.get(); }
177 [[nodiscard]] Expr *expr() const { return E.get(); }
178
179 [[nodiscard]] ChildVector children() const override {
180 return {KwWith.get(), TokSemi.get(), With.get(), E.get()};
181 }
182};
183
184} // namespace nixf
Holds a "." in the language.
Definition Basic.h:126
ExprAssert(LexerCursorRange Range, std::shared_ptr< Expr > Cond, std::shared_ptr< Expr > Value)
Definition Expr.h:117
Expr * value() const
Definition Expr.h:123
ChildVector children() const override
Definition Expr.h:125
Expr * cond() const
Definition Expr.h:122
Expr & fn() const
Definition Expr.h:50
ExprCall(LexerCursorRange Range, std::shared_ptr< Expr > Fn, std::vector< std::shared_ptr< Expr > > Args)
Definition Expr.h:44
ChildVector children() const override
Definition Expr.h:59
const std::vector< std::shared_ptr< Expr > > & args() const
Definition Expr.h:55
Expr * then() const
Definition Expr.h:103
Expr * cond() const
Definition Expr.h:102
Expr * elseExpr() const
Definition Expr.h:104
ExprIf(LexerCursorRange Range, std::shared_ptr< Expr > Cond, std::shared_ptr< Expr > Then, std::shared_ptr< Expr > Else)
Definition Expr.h:97
ChildVector children() const override
Definition Expr.h:106
const Misc & let() const
Definition Expr.h:153
ExprLet(LexerCursorRange Range, std::shared_ptr< Misc > KwLet, std::shared_ptr< Misc > KwIn, std::shared_ptr< Expr > E, std::shared_ptr< ExprAttrs > Attrs)
Definition Expr.h:140
const Binds * binds() const
Definition Expr.h:148
const ExprAttrs * attrs() const
Definition Expr.h:151
const Expr * expr() const
Definition Expr.h:152
ChildVector children() const override
Definition Expr.h:156
const Misc * in() const
Definition Expr.h:154
const std::vector< std::shared_ptr< Expr > > & elements() const
Definition Expr.h:77
ChildVector children() const override
Definition Expr.h:81
ExprList(LexerCursorRange Range, std::vector< std::shared_ptr< Expr > > Elements)
Definition Expr.h:74
ChildVector children() const override
Definition Expr.h:33
ExprSelect(LexerCursorRange Range, std::shared_ptr< Expr > E, std::shared_ptr< Dot > Do, std::shared_ptr< AttrPath > Path, std::shared_ptr< Expr > Default)
Definition Expr.h:14
Expr * defaultExpr() const
Definition Expr.h:29
Expr & expr() const
Definition Expr.h:22
AttrPath * path() const
Definition Expr.h:31
Dot * dot() const
Definition Expr.h:27
const Misc & kwWith() const
Definition Expr.h:174
Expr * with() const
Definition Expr.h:176
const Misc * tokSemi() const
Definition Expr.h:175
ExprWith(LexerCursorRange Range, std::shared_ptr< Misc > KwWith, std::shared_ptr< Misc > TokSemi, std::shared_ptr< Expr > With, std::shared_ptr< Expr > E)
Definition Expr.h:168
ChildVector children() const override
Definition Expr.h:179
Expr * expr() const
Definition Expr.h:177
Expr(NodeKind Kind, LexerCursorRange Range)
Definition Basic.h:72
Misc node, used for parentheses, keywords, etc.
Definition Basic.h:106
boost::container::small_vector< Node *, 8 > ChildVector
Definition Basic.h:42