7#include "formula_evaluation_context.hpp"
8#include "../utility/utility.hpp"
9#include "../codec/codec.hpp"
10#include "../path/path.hpp"
11#include "../macros.hpp"
13#include <unordered_map>
18hi_export_module(hikogui.formula.formula_post_process_context);
20namespace hi {
inline namespace v1 {
23[[
nodiscard]]
constexpr datum function_float(formula_evaluation_context&
context, datum::vector_type
const& args)
25 if (args.size() != 1) {
26 throw operation_error(std::format(
"Expecting 1 argument for float() function, got {}", args.size()));
29 return datum{
static_cast<double>(args[0])};
32[[
nodiscard]]
constexpr datum function_integer(formula_evaluation_context&
context, datum::vector_type
const& args)
34 if (args.size() != 1) {
35 throw operation_error(std::format(
"Expecting 1 argument for integer() function, got {}", args.size()));
38 return datum{
static_cast<long long int>(args[0])};
41[[
nodiscard]]
constexpr datum function_decimal(formula_evaluation_context&
context, datum::vector_type
const& args)
43 if (args.size() != 1) {
44 throw operation_error(std::format(
"Expecting 1 argument for decimal() function, got {}", args.size()));
47 return datum{
static_cast<decimal
>(args[0])};
50[[
nodiscard]]
inline datum function_string(formula_evaluation_context&
context, datum::vector_type
const& args)
52 if (args.size() != 1) {
53 throw operation_error(std::format(
"Expecting 1 argument for string() function, got {}", args.size()));
59[[
nodiscard]]
constexpr datum function_boolean(formula_evaluation_context&
context, datum::vector_type
const& args)
61 if (args.size() != 1) {
62 throw operation_error(std::format(
"Expecting 1 argument for boolean() function, got {}", args.size()));
65 return datum{to_bool(args[0])};
68[[
nodiscard]]
constexpr datum function_size(formula_evaluation_context&
context, datum::vector_type
const& args)
70 if (args.size() != 1) {
71 throw operation_error(std::format(
"Expecting 1 argument for size() function, got {}", args.size()));
74 return datum{args[0].size()};
77[[
nodiscard]]
inline datum function_keys(formula_evaluation_context&
context, datum::vector_type
const& args)
79 if (args.size() != 1) {
80 throw operation_error(std::format(
"Expecting 1 argument for keys() function, got {}", args.size()));
83 return datum{args[0].keys()};
86[[
nodiscard]]
inline datum function_values(formula_evaluation_context&
context, datum::vector_type
const& args)
88 if (args.size() != 1) {
89 throw operation_error(std::format(
"Expecting 1 argument for values() function, got {}", args.size()));
92 return datum{args[0].values()};
95[[
nodiscard]]
inline datum function_items(formula_evaluation_context&
context, datum::vector_type
const& args)
97 if (args.size() != 1) {
98 throw operation_error(std::format(
"Expecting 1 argument for items() function, got {}", args.size()));
101 return datum{args[0].items()};
104[[
nodiscard]]
constexpr datum function_sort(formula_evaluation_context&
context, datum::vector_type
const& args)
106 if (args.size() != 1) {
107 throw operation_error(std::format(
"Expecting 1 argument for sort() function, got {}", args.size()));
116 throw operation_error(std::format(
"Expecting vector argument for sort() function, got {}", args[0].type_name()));
121method_contains(formula_evaluation_context&
context, datum
const&
self, datum::vector_type
const& args)
123 if (args.size() != 1) {
124 throw operation_error(std::format(
"Expecting 1 argument for .contains() method, got {}", args.size()));
128 return datum{
self.contains(args[0])};
131 throw operation_error(
132 std::format(
"Expecting vector or map on left hand side for .contains() method, got {}",
self.type_name()));
136[[
nodiscard]]
constexpr datum method_append(formula_evaluation_context&
context, datum&
self, datum::vector_type
const& args)
138 if (args.size() != 1) {
139 throw operation_error(std::format(
"Expecting 1 argument for .append() method, got {}", args.size()));
143 self.push_back(args[0]);
147 throw operation_error(std::format(
"Expecting vector on left hand side for .append() method, got {}",
self.type_name()));
151[[
nodiscard]]
constexpr datum method_pop(formula_evaluation_context&
context, datum&
self, datum::vector_type
const& args)
153 if (args.size() != 0) {
154 throw operation_error(std::format(
"Expecting 0 arguments for .pop() method, got {}", args.size()));
158 auto r =
self.back();
163 throw operation_error(std::format(
"Expecting vector on left hand side for .pop() method, got {}",
self.type_name()));
167[[
nodiscard]]
constexpr datum method_year(formula_evaluation_context&
context, datum&
self, datum::vector_type
const& args)
169 if (args.size() != 0) {
170 throw operation_error(std::format(
"Expecting 0 arguments for .year() method, got {}", args.size()));
174 return datum{
static_cast<int>(
ymd->year())};
176 throw operation_error(std::format(
"Expecting date type for .year() method, got {}",
self.type_name()));
180[[
nodiscard]]
constexpr datum method_quarter(formula_evaluation_context&
context, datum&
self, datum::vector_type
const& args)
182 if (args.size() != 0) {
183 throw operation_error(std::format(
"Expecting 0 arguments for .quarter() method, got {}", args.size()));
187 auto month =
static_cast<unsigned>(
ymd->month());
197 throw operation_error(std::format(
"Month {} outside of range 1-12",
month));
200 throw operation_error(std::format(
"Expecting date type for .month() method, got {}",
self.type_name()));
204[[
nodiscard]]
constexpr datum method_month(formula_evaluation_context&
context, datum&
self, datum::vector_type
const& args)
206 if (args.size() != 0) {
207 throw operation_error(std::format(
"Expecting 0 arguments for .month() method, got {}", args.size()));
211 return datum{
static_cast<unsigned>(
ymd->month())};
213 throw operation_error(std::format(
"Expecting date type for .month() method, got {}",
self.type_name()));
217[[
nodiscard]]
constexpr datum method_day(formula_evaluation_context&
context, datum&
self, datum::vector_type
const& args)
219 if (args.size() != 0) {
220 throw operation_error(std::format(
"Expecting 0 arguments for .day() method, got {}", args.size()));
224 return datum{
static_cast<unsigned>(
ymd->day())};
226 throw operation_error(std::format(
"Expecting date type for .day() method, got {}",
self.type_name()));
246 static inline auto global_functions = function_table{
247 {
"float", detail::function_float},
248 {
"integer", detail::function_integer},
249 {
"decimal", detail::function_decimal},
250 {
"string", detail::function_string},
251 {
"boolean", detail::function_boolean},
252 {
"size", detail::function_size},
253 {
"keys", detail::function_keys},
254 {
"values", detail::function_values},
255 {
"items", detail::function_items},
256 {
"sort", detail::function_sort}};
258 static inline auto global_methods = method_table{
259 {
"append", detail::method_append},
260 {
"contains", detail::method_contains},
261 {
"push", detail::method_append},
262 {
"pop", detail::method_pop},
263 {
"year", detail::method_year},
264 {
"quarter", detail::method_quarter},
265 {
"month", detail::method_month},
266 {
"day", detail::method_day}};
268 static inline auto global_filters = filter_table{{
"id", make_identifier}, {
"url", detail::url_encode}};
270 function_table functions = {};
271 function_stack super_stack = {};
275 if (name ==
"super") {
276 if (super_stack.size() > 0) {
277 return super_stack.
back();
283 hilet i = functions.find(name);
284 if (i != functions.end()) {
288 hilet
j = global_functions.find(name);
289 if (
j != global_functions.end()) {
300 swap(functions[name],
func);
304 void push_super(function_type
func)
noexcept
306 super_stack.push_back(
func);
309 void pop_super(
void)
noexcept
311 super_stack.pop_back();
316 hilet i = global_filters.find(name);
317 if (i != global_filters.end()) {
326 hilet i = global_methods.find(name);
327 if (i != global_methods.end()) {
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_post_process_context.hpp:237
static constexpr std::string encode(It first, ItEnd last) noexcept
URI encode a component.
Definition URI.hpp:758