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 tt_error_info().set<"parse_location">(location);
65 throw operation_error("Expecting expression returns a vector, got {}", list_data);
66 }
67
68 ttlet output_size = context.output_size();
69 if (list_data.size() > 0) {
70 ttlet loop_size = std::ssize(list_data);
71 ssize_t loop_count = 0;
72 for (auto i = list_data.vector_begin(); i != list_data.vector_end(); ++i) {
73 ttlet &item = *i;
74 try {
75 name_expression->assign_without_output(context, item);
76
77 } catch (...) {
78 auto error_location = location;
79 if (ttlet expression_location = error_info::peek<parse_location, "parse_location">()) {
80 error_location += *expression_location;
81 }
82 error_info(true).set<"parse_location">(error_location);
83 throw;
84 }
85
86 context.loop_push(loop_count++, loop_size);
87 auto tmp = evaluate_children(context, children);
88 context.loop_pop();
89
90 if (tmp.is_break()) {
91 break;
92 } else if (tmp.is_continue()) {
93 continue;
94 } else if (!tmp.is_undefined()) {
95 context.set_output_size(output_size);
96 return tmp;
97 }
98 }
99
100 } else {
101 auto tmp = evaluate_children(context, else_children);
102 if (tmp.is_break() || tmp.is_continue()) {
103 return tmp;
104 } else if (!tmp.is_undefined()) {
105 context.set_output_size(output_size);
106 return tmp;
107 }
108 }
109 return {};
110 }
111
112 std::string string() const noexcept override {
113 std::string s = "<for ";
114 s += to_string(*name_expression);
115 s += ": ";
116 s += to_string(*list_expression);
117 s += join(transform<std::vector<std::string>>(children, [](auto &x) { return to_string(*x); }));
118 if (has_else) {
119 s += "else ";
120 s += join(transform<std::vector<std::string>>(else_children, [](auto &x) { return to_string(*x); }));
121 }
122 s += ">";
123 return s;
124 }
125};
126
127}
Error information passed alongside an error code or exception.
Definition error_info.hpp:81
error_info & set(Arg &&value) noexcept
Set an information for a given tag.
Definition error_info.hpp:144
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:37
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:16
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)