HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_block_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::string name;
13 statement_vector children;
14
17
19 skeleton_node(std::move(location)), name(name_expression->get_name())
20 {
21 name = name_expression->get_name();
22
23 super_function = context.set_function(name,
24 [&](formula_evaluation_context &context, datum::vector_type const &arguments) {
25 return this->evaluate_call(context, arguments);
26 }
27 );
28 }
29
32 bool append(std::unique_ptr<skeleton_node> x) noexcept override {
33 append_child(children, std::move(x));
34 return true;
35 }
36
37 void post_process(formula_post_process_context &context) override {
38 if (std::ssize(children) > 0) {
39 children.back()->left_align();
40 }
41
42 function = context.get_function(name);
43 tt_assert(function);
44
45 context.push_super(super_function);
46 for (ttlet &child: children) {
47 child->post_process(context);
48 }
49 context.pop_super();
50 }
51
53 datum tmp;
54 try {
55 tmp = function(context, datum::vector_type{});
56
57 } catch (std::exception const &e) {
58 throw operation_error("{}: Could not evaluate block.\n{}", location, e.what());
59 }
60
61 if (tmp.is_break()) {
62 throw operation_error("{}: Found #break not inside a loop statement.", location);
63
64 } else if (tmp.is_continue()) {
65 throw operation_error("{}: Found #continue not inside a loop statement.", location);
66
67 } else if (tmp.is_undefined()) {
68 return {};
69
70 } else {
71 throw operation_error("{}: Can not use a #return statement inside a #block.", location);
72 }
73 }
74
75 datum evaluate_call(formula_evaluation_context &context, datum::vector_type const &arguments) {
76 context.push();
77 auto tmp = evaluate_children(context, children);
78 context.pop();
79
80 if (tmp.is_break()) {
81 throw operation_error("{}: Found #break not inside a loop statement.", location);
82
83 } else if (tmp.is_continue()) {
84 throw operation_error("{}: Found #continue not inside a loop statement.", location);
85
86 } else if (tmp.is_undefined()) {
87 return {};
88
89 } else {
90 throw operation_error("{}: Can not use a #return statement inside a #block.", location);
91 }
92 }
93
94 std::string string() const noexcept override {
95 std::string s = "<block ";
96 s += name;
97 s += join(transform<std::vector<std::string>>(children, [](auto &x) { return to_string(*x); }));
98 s += ">";
99 return s;
100 }
101};
102
103}
A dynamic data type.
Definition datum.hpp:213
constexpr bool is_continue() const noexcept
Check if the result of a expression was a continue flow control statement.
Definition datum.hpp:638
constexpr bool is_break() const noexcept
Check if the result of a expression was a break flow control statement.
Definition datum.hpp:630
constexpr bool is_undefined() const noexcept
Check if the datum has an undefined value.
Definition datum.hpp:622
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:42
Definition formula_evaluation_context.hpp:16
Definition formula_post_process_context.hpp:19
Definition parse_location.hpp:17
Definition skeleton_block_node.hpp:11
bool append(std::unique_ptr< skeleton_node > x) noexcept override
Append a template-piece to the current template.
Definition skeleton_block_node.hpp:32
datum evaluate(formula_evaluation_context &context) override
Evaluate the template.
Definition skeleton_block_node.hpp:52
Definition skeleton_node.hpp:16
T move(T... args)
T to_string(T... args)
T what(T... args)