FuncSketch
Loading...
Searching...
No Matches
parsed_expression.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2026 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <type_traits>
23
24#include <fmt/format.h>
25#include <fmt/ranges.h>
26
28
29// NOLINTNEXTLINE(*-static): API of an external library.
30auto fmt::formatter<func_sketch::parser::ParsedLiteral>::format(
32 format_context& context) const -> format_context::iterator {
33 return std::visit(
34 [&context](const auto& actual_value) {
35 using ValueType = std::decay_t<decltype(actual_value)>;
36 if constexpr (std::is_same_v<ValueType, func_sketch::Complex>) {
37 return fmt::format_to(context.out(), "Literal({} + {}i)",
38 actual_value.real(), actual_value.imag());
39 } else if constexpr (std::is_same_v<ValueType, func_sketch::Real>) {
40 // For testing purposes, we want to distinguish between 1.0 and
41 // 1 in the output.
42 return fmt::format_to(
43 context.out(), "Literal({:e})", actual_value);
44 } else {
45 return fmt::format_to(
46 context.out(), "Literal({})", actual_value);
47 }
48 },
49 value.value);
50}
51
52// NOLINTNEXTLINE(*-static): API of an external library.
53auto fmt::formatter<func_sketch::parser::ParsedIdentifier>::format(
55 format_context& context) const -> format_context::iterator {
56 return fmt::format_to(context.out(), "Identifier({})", value.name);
57}
58
59// NOLINTNEXTLINE(*-static): API of an external library.
60auto fmt::formatter<func_sketch::parser::ParsedExpression>::format(
62 format_context& context) const -> format_context::iterator {
63 return boost::apply_visitor(
64 [&context](const auto& concrete_value) {
65 return fmt::format_to(context.out(), "{}", concrete_value);
66 },
67 value);
68}
69
70// NOLINTNEXTLINE(*-static): API of an external library.
71auto fmt::formatter<func_sketch::parser::ParsedFunctionCallExpression>::format(
73 format_context& context) const -> format_context::iterator {
74 return fmt::format_to(context.out(), "FunctionCall({}, {})",
75 value.function_name, fmt::join(value.arguments, ", "));
76}
77
78// NOLINTNEXTLINE(*-static): API of an external library.
79auto fmt::formatter<func_sketch::parser::ParsedUnaryExpression>::format(
81 format_context& context) const -> format_context::iterator {
82 return fmt::format_to(
83 context.out(), "Unary({}, {})", value.operator_str, value.operand);
84}
85
86// NOLINTNEXTLINE(*-static): API of an external library.
87auto fmt::formatter<func_sketch::parser::ParsedBinaryExpression>::format(
89 format_context& context) const -> format_context::iterator {
90 return fmt::format_to(context.out(), "Binary({}, {}, {})",
91 value.operator_str, value.left_operand, value.right_operand);
92}
Definitions of common types.
Definition of ParsedExpression structure.
boost::variant< ParsedLiteral, ParsedIdentifier, boost::recursive_wrapper< ParsedFunctionCallExpression >, boost::recursive_wrapper< ParsedUnaryExpression >, boost::recursive_wrapper< ParsedBinaryExpression > > ParsedExpression
Variant of parsed expressions.
Struct of parsed binary expressions.
Struct of parsed function call expressions.
Struct of parsed identifiers.
Struct of parsed unary expressions.