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