LMCAS
Lamina Computer Algebra System
载入中...
搜索中...
未找到
expr_parser_construction.cpp
浏览该文件的文档.
3
4#include <exception>
5#include <utility>
6
7namespace LMCAS::expr_detail {
8namespace {
9
10ExprResult invalid_construction(std::string message) {
12 CasErrc::InvalidArgument, std::move(message), kParseOperation);
13}
14
15template <typename Factory>
16ExprResult construct_expression(std::string allocation_message,
17 Factory&& factory) {
18 try {
19 auto node = std::forward<Factory>(factory)();
21 LMCAS::detail::make_expression_ptr(std::move(node)));
22 } catch (const std::bad_alloc&) {
24 CasErrc::ResourceLimit, std::move(allocation_message),
26 } catch (const std::exception& error) {
29 }
30}
31
32}
33
34
36 const ExprPtr& lhs, const ExprPtr& rhs, RelationOp op) {
37 if (!lhs || !rhs) {
38 return invalid_construction("relational operands cannot be null");
39 }
40 return construct_expression(
41 "relational expression allocation failed", [&] {
44 });
45}
46
48 const ExprPtr& lhs, const ExprPtr& rhs, LogicalNode::Op op) {
49 if (!lhs || (op != LogicalNode::Op::Not && !rhs)) {
50 return invalid_construction("logical operands cannot be null");
51 }
52 return construct_expression(
53 "logical expression allocation failed", [&] {
56 rhs ? LMCAS::detail::node(rhs) : nullptr, op);
57 });
58}
59
60ExprResult ExprParser::finite_set(const std::vector<ExprPtr>& elements) {
61 for (const auto& element : elements) {
63 return invalid_construction("set elements cannot be null");
64 }
65 }
66 return construct_expression(
67 "set expression allocation failed", [&] {
68 std::vector<std::shared_ptr<const SymbolicNode>> nodes;
69 nodes.reserve(elements.size());
70 for (const auto& element : elements) {
71 nodes.push_back(LMCAS::detail::node(element));
72 }
73 return LMCAS::detail::make_node<FiniteSetNode>(std::move(nodes));
74 });
75}
76
78 const ExprPtr& lower, const ExprPtr& upper,
79 bool lower_closed, bool upper_closed) {
80 if (!lower || !upper) {
81 return invalid_construction("interval bounds cannot be null");
82 }
83 return construct_expression(
84 "interval expression allocation failed", [&] {
88 });
89}
90
92 const ExprPtr& element, const ExprPtr& set, bool negated) {
93 if (!element || !set) {
94 return invalid_construction("membership operands cannot be null");
95 }
96 return construct_expression(
97 "membership expression allocation failed", [&] {
98 std::shared_ptr<const SymbolicNode> node =
101 if (negated) {
103 std::move(node), nullptr, LogicalNode::Op::Not);
104 }
105 return node;
106 });
107}
108
110 const std::string& name, const std::vector<ExprPtr>& arguments,
112 for (const auto& argument : arguments) {
113 if (!argument || !LMCAS::detail::node(argument)) {
114 return invalid_construction("function arguments cannot be null");
115 }
116 }
117 return construct_expression(
118 name + " expression allocation failed", [&] {
119 std::vector<std::shared_ptr<const SymbolicNode>> nodes;
120 nodes.reserve(arguments.size());
121 for (const auto& argument : arguments) {
122 nodes.push_back(LMCAS::detail::node(argument));
123 }
125 type, std::move(nodes));
126 });
127}
128
129}
@ Not
一元逻辑非,right 为 nullptr。
static Result success(ExprPtr value)
ExprResult function_node(const std::string &name, const std::vector< ExprPtr > &arguments, FunctionNode::FuncType type)
ExprResult interval(const ExprPtr &lower, const ExprPtr &upper, bool lower_closed, bool upper_closed)
ExprResult logical(const ExprPtr &lhs, const ExprPtr &rhs, LogicalNode::Op op)
ExprResult finite_set(const std::vector< ExprPtr > &elements)
ExprResult relational(const ExprPtr &lhs, const ExprPtr &rhs, RelationOp op)
ExprResult membership(const ExprPtr &element, const ExprPtr &set, bool negated)
std::shared_ptr<::LMCAS::SymbolicExpr > make_expression_ptr(SymbolicNodePtr root)
const SymbolicNodePtr & node(const ::LMCAS::SymbolicExpr &expression) noexcept
std::shared_ptr< const Node > make_node(Args &&... args)
ExprResult construct_expression(std::string allocation_message, Factory &&factory)
ExprResult expression_failure(CasErrc code, std::string message, const char *operation)
constexpr const char * kParseOperation
const ExprPtr & element
__attribute__((visibility("default"))) ExprResult function(const std __attribute__((visibility("default"))) ExprResult finite_set(std ExprPtr upper
const ExprPtr & lower
const ExprPtr & set
Result< ExprPtr > ExprResult
RelationOp
Stable relational operator used by public APIs.
const ExprPtr RelationOp op
__attribute__((visibility("default"))) ExprResult function(const std __attribute__((visibility("default"))) ExprResult finite_set(std ExprPtr bool lower_closed
std::shared_ptr< SymbolicExpr > ExprPtr
const ExprPtr & rhs
__attribute__((visibility("default"))) ExprResult function(const std __attribute__((visibility("default"))) ExprResult finite_set(std ExprPtr bool bool upper_closed
const ExprPtr bool negated
__attribute__((visibility("default"))) Result< EqvProfile > eqv_profile_from_name(const std const std::string & name