@@ -122,6 +122,18 @@ struct ASTInterpreter::NewPlacementResult {};
122
122
123
123
struct ASTInterpreter ::NestedNamespaceSpecifierResult {};
124
124
125
+ struct ASTInterpreter ::ToBool {
126
+ ASTInterpreter& interp;
127
+
128
+ auto operator ()(const StringLiteral*) const -> std::optional<bool> {
129
+ return true ;
130
+ }
131
+
132
+ auto operator ()(const auto & value) const -> std::optional<bool> {
133
+ return bool (value);
134
+ }
135
+ };
136
+
125
137
struct ASTInterpreter ::UnitVisitor {
126
138
ASTInterpreter& accept;
127
139
@@ -2193,10 +2205,17 @@ auto ASTInterpreter::ExpressionVisitor::operator()(BinaryExpressionAST* ast)
2193
2205
auto ASTInterpreter::ExpressionVisitor::operator ()(
2194
2206
ConditionalExpressionAST* ast) -> ExpressionResult {
2195
2207
auto conditionResult = accept (ast->condition );
2196
- auto iftrueExpressionResult = accept (ast->iftrueExpression );
2197
- auto iffalseExpressionResult = accept (ast->iffalseExpression );
2198
2208
2199
- return ExpressionResult{std::nullopt};
2209
+ if (!conditionResult.has_value ()) return std::nullopt;
2210
+
2211
+ if (accept.toBool (conditionResult.value ())) {
2212
+ auto result = accept (ast->iftrueExpression );
2213
+ return result;
2214
+ }
2215
+
2216
+ auto result = accept (ast->iffalseExpression );
2217
+
2218
+ return result;
2200
2219
}
2201
2220
2202
2221
auto ASTInterpreter::ExpressionVisitor::operator ()(YieldExpressionAST* ast)
@@ -3176,4 +3195,8 @@ auto ASTInterpreter::evaluate(ExpressionAST* ast) -> std::optional<ConstValue> {
3176
3195
return result;
3177
3196
}
3178
3197
3198
+ auto ASTInterpreter::toBool (const ConstValue& value) -> std::optional<bool> {
3199
+ return std::visit (ToBool{*this }, value);
3200
+ }
3201
+
3179
3202
} // namespace cxx
0 commit comments