HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
formula_node.hpp
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "formula_post_process_context.hpp"
8#include "formula_evaluation_context.hpp"
9#include "../utility/utility.hpp"
10#include "../parser/parser.hpp"
11#include "../codec/codec.hpp"
12#include "../macros.hpp"
13#include <vector>
14#include <memory>
15#include <string>
16
17hi_export_module(hikogui.formula.formula_node);
18
19namespace hi { inline namespace v1 {
20
22 using formula_vector = std::vector<std::unique_ptr<formula_node>>;
23
24 size_t line_nr;
25 size_t column_nr;
26
27 virtual ~formula_node() {}
28 formula_node() = delete;
29 formula_node(formula_node const&) = delete;
30 formula_node(formula_node&&) = delete;
31 formula_node& operator=(formula_node const&) = delete;
32 formula_node& operator=(formula_node&&) = delete;
33
34 formula_node(size_t line_nr, size_t column_nr) : line_nr(line_nr), column_nr(column_nr) {}
35
40
45
49
50 datum evaluate_without_output(formula_evaluation_context& context) const
51 {
52 context.disable_output();
53 auto r = evaluate(context);
54 context.enable_output();
55 return r;
56 }
57
61 {
62 throw operation_error(std::format("{}:{}: Expression is not a modifiable value.", line_nr, column_nr));
63 }
64
65 virtual bool has_evaluate_xvalue() const
66 {
67 return false;
68 }
69
73 {
74 throw operation_error(std::format("{}:{}: Expression is not a xvalue.", line_nr, column_nr));
75 }
76
80 {
81 return evaluate_lvalue(context) = rhs;
82 }
83
84 datum& assign_without_output(formula_evaluation_context& context, datum const& rhs) const
85 {
86 context.disable_output();
87 auto& r = assign(context, rhs);
88 context.enable_output();
89 return r;
90 }
91
95 {
96 throw operation_error(std::format("{}:{}: Expression is not callable.", line_nr, column_nr));
97 }
98
101 virtual std::string get_name() const
102 {
103 throw parse_error(std::format("{}:{}: Expect a name, got {})", line_nr, column_nr, *this));
104 }
105
110 {
111 throw parse_error(std::format("{}:{}: Expect a function definition, got {})", line_nr, column_nr, *this));
112 }
113
114 virtual std::string string() const noexcept = 0;
115
116 friend std::string to_string(formula_node const& rhs) noexcept
117 {
118 return rhs.string();
119 }
120};
121
122}} // namespace hi::v1
123
124template<class CharT>
125struct std::formatter<hi::formula_node, CharT> : std::formatter<std::string_view, CharT> {
126 auto format(hi::formula_node const& t, auto& fc) const
127 {
128 return std::formatter<std::string_view, CharT>::format(to_string(t), fc);
129 }
130};
131
132namespace hi { inline namespace v1 {
133
134inline std::ostream& operator<<(std::ostream& lhs, formula_node const& rhs) noexcept
135{
136 return lhs << std::format("{}", rhs);
137}
138
139}} // namespace hi::v1
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A dynamic data type.
Definition datum.hpp:212
Definition formula_evaluation_context.hpp:18
Definition formula_node.hpp:21
virtual datum & assign(formula_evaluation_context &context, datum const &rhs) const
Assign to a non-existing or existing lvalue.
Definition formula_node.hpp:79
virtual std::string get_name() const
Get the name of a formula_name_node.
Definition formula_node.hpp:101
virtual void post_process(formula_post_process_context &context)
Resolve function and method pointers.
Definition formula_node.hpp:39
virtual datum call(formula_evaluation_context &context, datum::vector_type const &arguments) const
Call a function with a datum::vector as arguments.
Definition formula_node.hpp:94
virtual datum const & evaluate_xvalue(formula_evaluation_context const &context) const
Evaluate an existing xvalue.
Definition formula_node.hpp:72
virtual datum & evaluate_lvalue(formula_evaluation_context &context) const
Evaluate an existing lvalue.
Definition formula_node.hpp:60
virtual std::vector< std::string > get_name_and_argument_names() const
Get name and argument names from a function declaration.
Definition formula_node.hpp:109
virtual void resolve_function_pointer(formula_post_process_context &context)
Resolve function and method pointers.
Definition formula_node.hpp:44
virtual datum evaluate(formula_evaluation_context &context) const =0
Evaluate an rvalue.
Definition formula_post_process_context.hpp:237
Exception thrown during parsing on an error.
Definition exception_intf.hpp:47
Exception thrown during execution of a dynamic operation.
Definition exception_intf.hpp:160
T to_string(T... args)