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 (!holds_alternative<datum::vector_type>(list_data)) {
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 (ttlet &item : list_data) {
72 try {
73 name_expression->assign_without_output(context, item);
74
75 } catch (std::exception const &e) {
76 throw operation_error("{}: Could not evaluate for-loop expression.\n{}", location, e.what());
77 }
78
79 context.loop_push(loop_count++, loop_size);
80 auto tmp = evaluate_children(context, children);
81 context.loop_pop();
82
83 if (tmp.is_break()) {
84 break;
85 } else if (tmp.is_continue()) {
86 continue;
87 } else if (!tmp.is_undefined()) {
88 context.set_output_size(output_size);
89 return tmp;
90 }
91 }
92
93 } else {
94 auto tmp = evaluate_children(context, else_children);
95 if (tmp.is_break() || tmp.is_continue()) {
96 return tmp;
97 } else if (!tmp.is_undefined()) {
98 context.set_output_size(output_size);
99 return tmp;
100 }
101 }
102 return {};
103 }
104
105 std::string string() const noexcept override {
106 std::string s = "<for ";
107 s += to_string(*name_expression);
108 s += ": ";
109 s += to_string(*list_expression);
110 s += join(transform<std::vector<std::string>>(children, [](auto &x) { return to_string(*x); }));
111 if (has_else) {
112 s += "else ";
113 s += join(transform<std::vector<std::string>>(else_children, [](auto &x) { return to_string(*x); }));
114 }
115 s += ">";
116 return s;
117 }
118};
119
120}
A dynamic data type.
Definition datum.hpp:213
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)