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