HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_for_node.hpp
1// Copyright Take Vos 2020-2021.
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 "skeleton_node.hpp"
8#include "../macros.hpp"
9
10namespace hi::inline v1 {
11
13 std::unique_ptr<formula_node> name_expression;
14 std::unique_ptr<formula_node> list_expression;
15 bool has_else = false;
16 statement_vector children;
17 statement_vector else_children;
18
20 parse_location location,
21 std::unique_ptr<formula_node> name_expression,
22 std::unique_ptr<formula_node> list_expression) noexcept :
23 skeleton_node(std::move(location)),
24 name_expression(std::move(name_expression)),
25 list_expression(std::move(list_expression))
26 {
27 }
28
31 bool append(std::unique_ptr<skeleton_node> x) noexcept override
32 {
33 if (has_else) {
34 append_child(else_children, std::move(x));
35 } else {
36 append_child(children, std::move(x));
37 }
38 return true;
39 }
40
41 bool found_else(parse_location _location) noexcept override
42 {
43 if (has_else) {
44 return false;
45 } else {
46 has_else = true;
47 return true;
48 }
49 }
50
51 void post_process(formula_post_process_context &context) override
52 {
53 if (ssize(children) > 0) {
54 children.back()->left_align();
55 }
56 if (ssize(else_children) > 0) {
57 else_children.back()->left_align();
58 }
59
60 post_process_expression(context, *name_expression, location);
61 post_process_expression(context, *list_expression, location);
62
63 for (hilet &child : children) {
64 child->post_process(context);
65 }
66 for (hilet &child : else_children) {
67 child->post_process(context);
68 }
69 }
70
71 datum evaluate(formula_evaluation_context &context) override
72 {
73 auto list_data = evaluate_formula_without_output(context, *list_expression, location);
74
75 if (!holds_alternative<datum::vector_type>(list_data)) {
76 throw operation_error(std::format("{}: Expecting expression returns a vector, got {}", location, list_data));
77 }
78
79 hilet output_size = context.output_size();
80 if (hilet loop_size = list_data.size()) {
81 ssize_t loop_count = 0;
82 for (hilet &item : list_data) {
83 try {
84 name_expression->assign_without_output(context, item);
85
86 } catch (std::exception const &e) {
87 throw operation_error(std::format("{}: Could not evaluate for-loop expression.\n{}", location, e.what()));
88 }
89
90 context.loop_push(loop_count++, loop_size);
91 auto tmp = evaluate_children(context, children);
92 context.loop_pop();
93
94 if (tmp.is_break()) {
95 break;
96 } else if (tmp.is_continue()) {
97 continue;
98 } else if (!tmp.is_undefined()) {
99 context.set_output_size(output_size);
100 return tmp;
101 }
102 }
103
104 } else {
105 auto tmp = evaluate_children(context, else_children);
106 if (tmp.is_break() || tmp.is_continue()) {
107 return tmp;
108 } else if (!tmp.is_undefined()) {
109 context.set_output_size(output_size);
110 return tmp;
111 }
112 }
113 return {};
114 }
115
116 std::string string() const noexcept override
117 {
118 std::string s = "<for ";
119 s += to_string(*name_expression);
120 s += ": ";
121 s += to_string(*list_expression);
122 s += join(transform<std::vector<std::string>>(children, [](auto &x) {
123 return to_string(*x);
124 }));
125 if (has_else) {
126 s += "else ";
127 s += join(transform<std::vector<std::string>>(else_children, [](auto &x) {
128 return to_string(*x);
129 }));
130 }
131 s += ">";
132 return s;
133 }
134};
135
136} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition parse_location.hpp:18
Definition skeleton_for_node.hpp:12
bool append(std::unique_ptr< skeleton_node > x) noexcept override
Append a template-piece to the current template.
Definition skeleton_for_node.hpp:31
datum evaluate(formula_evaluation_context &context) override
Evaluate the template.
Definition skeleton_for_node.hpp:71
Definition skeleton_node.hpp:16
T move(T... args)
T what(T... args)