FuncSketch
Loading...
Searching...
No Matches
convert_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
34
35namespace func_sketch::parser {
36
46[[nodiscard]] expressions::Expression* convert_expression(
47 const ParsedExpression& parsed_expression,
48 const math::MathFunctionList& math_function_list,
49 const math::ConstantList& constant_list,
50 expressions::ExpressionMemoryPool& pool);
51
60 const ParsedLiteral& parsed_expression,
61 const math::MathFunctionList& /*math_function_list*/,
62 const math::ConstantList& /*constant_list*/,
65 parsed_expression.value);
66}
67
77 const ParsedIdentifier& parsed_expression,
78 const math::MathFunctionList& /*math_function_list*/,
79 const math::ConstantList& constant_list,
81 if (parsed_expression.name == "x") {
83 }
84 const auto constant = constant_list.get(parsed_expression.name);
85 if (constant) {
86 return pool.create<expressions::ConstantExpression>(*constant);
87 }
89 "Unknown identifier: " + parsed_expression.name);
90}
91
102 const ParsedFunctionCallExpression& parsed_expression,
103 const math::MathFunctionList& math_function_list,
104 const math::ConstantList& constant_list,
106 auto function =
107 math_function_list.get(parsed_expression.function_name.name);
108 if (!function) {
110 "Unknown function: " + parsed_expression.function_name.name);
111 }
112 std::vector<expressions::Expression*> arguments;
113 arguments.reserve(parsed_expression.arguments.size());
114 for (const auto& argument : parsed_expression.arguments) {
115 arguments.push_back(convert_expression(
116 argument, math_function_list, constant_list, pool));
117 }
119 std::move(arguments), std::move(*function));
120}
121
129 const std::string& operator_str) {
130 if (operator_str == "+") {
132 }
133 if (operator_str == "-") {
135 }
136 throw InvalidExpressionException("Unknown unary operator: " + operator_str);
137}
138
149 const ParsedUnaryExpression& parsed_expression,
150 const math::MathFunctionList& math_function_list,
151 const math::ConstantList& constant_list,
155 parsed_expression.operand, math_function_list, constant_list, pool),
156 get_unary_operator(parsed_expression.operator_str));
157}
158
166 const std::string& operator_str) {
167 if (operator_str == "+") {
169 }
170 if (operator_str == "-") {
172 }
173 if (operator_str == "*") {
175 }
176 if (operator_str == "/") {
178 }
179 if (operator_str == "**") {
181 }
183 "Unknown binary operator: " + operator_str);
184}
185
196 const ParsedBinaryExpression& parsed_expression,
197 const math::MathFunctionList& math_function_list,
198 const math::ConstantList& constant_list,
201 convert_expression(parsed_expression.left_operand, math_function_list,
202 constant_list, pool),
203 convert_expression(parsed_expression.right_operand, math_function_list,
204 constant_list, pool),
205 get_binary_operator(parsed_expression.operator_str));
206}
207
209 const ParsedExpression& parsed_expression,
210 const math::MathFunctionList& math_function_list,
211 const math::ConstantList& constant_list,
213 return boost::apply_visitor(
214 [&pool, &math_function_list, &constant_list](
215 const auto& concrete_expression) -> expressions::Expression* {
216 return convert_expression(
217 concrete_expression, math_function_list, constant_list, pool);
218 },
219 parsed_expression);
220}
221
223 const ParsedExpression& parsed_expression,
224 const math::MathFunctionList& math_function_list,
225 const math::ConstantList& constant_list) {
226 auto pool = std::make_unique<expressions::ExpressionMemoryPool>();
227 auto* expression = convert_expression(
228 parsed_expression, math_function_list, constant_list, *pool);
229 return expressions::ExpressionPtr(expression, std::move(pool));
230}
231
232} // namespace func_sketch::parser
Definition of BinaryExpression structure.
Definition of BinaryOperator class.
Definition of binary operators.
Class of exceptions of invalid expressions.
Definition exceptions.h:45
Class of memory pools for expressions.
Expression * create(Args &&... args)
Create an expression.
Class of pointers to expressions with RAII.
Class of addition operator.
Class of binary operators.
Class of lists of constants.
std::optional< Number > get(const std::string &name) const
Get a constant from the list.
Class of division operator.
Class of lists of mathematical functions.
std::optional< MathFunction > get(const std::string &name) const
Get a mathematical function from the list.
Class of multiplication operator.
Class of subtraction operator.
Class of unary minus operator.
Class of unary operators.
Class of unary plus operator.
Definition of ConstantExpression structure.
math::UnaryOperator get_unary_operator(const std::string &operator_str)
Get a unary operator by name.
math::BinaryOperator get_binary_operator(const std::string &operator_str)
Get a binary operator by name.
Declaration of convert_expression function.
expressions::ExpressionPtr convert_expression(const ParsedExpression &parsed_expression, const math::MathFunctionList &math_function_list, const math::ConstantList &constant_list)
Convert parsed expression.
Definition of exception classes.
Definition of Expression class.
Definition of ExpressionMemoryPool class.
Definition of MathFunctionList class.
Definition of ParameterExpression structure.
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.
ParsedExpression left_operand
Left operand.
ParsedExpression right_operand
Right operand.
Struct of parsed function call expressions.
std::vector< ParsedExpression > arguments
Arguments.
Struct of parsed identifiers.
Struct of parsed unary expressions.
Definition of UnaryExpression structure.
Definition of unary operators.