HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_for_node.hpp
1// Copyright Take Vos 2020.
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
9namespace tt {
10
12 std::unique_ptr<formula_node> name_expression;
13 std::unique_ptr<formula_node> list_expression;
14 bool has_else = false;
15 statement_vector children;
16 statement_vector else_children;
17
18 skeleton_for_node(parse_location location, std::unique_ptr<formula_node> name_expression, std::unique_ptr<formula_node> list_expression) noexcept :
19 skeleton_node(std::move(location)), name_expression(std::move(name_expression)), list_expression(std::move(list_expression)) {}
20
23 bool append(std::unique_ptr<skeleton_node> x) noexcept override {
24 if (has_else) {
25 append_child(else_children, std::move(x));
26 } else {
27 append_child(children, std::move(x));
28 }
29 return true;
30 }
31
32 bool found_else(parse_location _location) noexcept override {
33 if (has_else) {
34 return false;
35 } else {
36 has_else = true;
37 return true;
38 }
39 }
40
41 void post_process(formula_post_process_context &context) override {
42 if (std::ssize(children) > 0) {
43 children.back()->left_align();
44 }
45 if (std::ssize(else_children) > 0) {
46 else_children.back()->left_align();
47 }
48
49 post_process_expression(context, *name_expression, location);
50 post_process_expression(context, *list_expression, location);
51
52 for (ttlet &child: children) {
53 child->post_process(context);
54 }
55 for (ttlet &child: else_children) {
56 child->post_process(context);
57 }
58 }
59
61 auto list_data = evaluate_formula_without_output(context, *list_expression, location);
62
63 if (!list_data.is_vector()) {
64 throw operation_error("{}: Expecting expression returns a vector, got {}", location, list_data);
65 }
66
67 ttlet output_size = context.output_size();
68 if (list_data.size() > 0) {
69 ttlet loop_size = std::ssize(list_data);
70 ssize_t loop_count = 0;
71 for (auto i = list_data.vector_begin(); i != list_data.vector_end(); ++i) {
72 ttlet &item = *i;
73 try {
74 name_expression->assign_without_output(context, item);
75
76 } catch (std::exception const &e) {
77 throw operation_error("{}: Could not evaluate for-loop expression.\n{}", location, e.what());
78 }
79
80 context.loop_push(loop_count++, loop_size);
81 auto tmp = evaluate_children(context, children);
82 context.loop_pop();
83
84 if (tmp.is_break()) {
85 break;
86 } else if (tmp.is_continue()) {
87 continue;
88 } else if (!tmp.is_undefined()) {
89 context.set_output_size(output_size);
90 return tmp;
91 }
92 }
93
94 } else {
95 auto tmp = evaluate_children(context, else_children);
96 if (tmp.is_break() || tmp.is_continue()) {
97 return tmp;
98 } else if (!tmp.is_undefined()) {
99 context.set_output_size(output_size);
100 return tmp;
101 }
102 }
103 return {};
104 }
105
106 std::string string() const noexcept override {
107 std::string s = "<for ";
108 s += to_string(*name_expression);
109 s += ": ";
110 s += to_string(*list_expression);
111 s += join(transform<std::vector<std::string>>(children, [](auto &x) { return to_string(*x); }));
112 if (has_else) {
113 s += "else ";
114 s += join(transform<std::vector<std::string>>(else_children, [](auto &x) { return to_string(*x); }));
115 }
116 s += ">";
117 return s;
118 }
119};
120
121}
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:42
Definition formula_evaluation_context.hpp:16
void set_output_size(ssize_t new_size) noexcept
Set the size of the output.
Definition formula_evaluation_context.hpp:67
ssize_t output_size() const noexcept
Get the size of the output.
Definition formula_evaluation_context.hpp:60
Definition parse_location.hpp:17
Definition skeleton_for_node.hpp:11
bool append(std::unique_ptr< skeleton_node > x) noexcept override
Append a template-piece to the current template.
Definition skeleton_for_node.hpp:23
datum evaluate(formula_evaluation_context &context) override
Evaluate the template.
Definition skeleton_for_node.hpp:60
Definition skeleton_node.hpp:16
T move(T... args)
T what(T... args)