HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_block_node.hpp
1// Copyright Take Vos 2020-2022.
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 hi::inline v1 {
10
12 std::string name;
13 statement_vector children;
14
17
19 parse_location location,
21 formula_node const &name_expression) noexcept :
22 skeleton_node(std::move(location)), name(name_expression.get_name())
23 {
24 name = name_expression.get_name();
25
26 super_function =
27 context.set_function(name, [&](formula_evaluation_context &context, datum::vector_type const &arguments) {
28 return this->evaluate_call(context, arguments);
29 });
30 }
31
34 bool append(std::unique_ptr<skeleton_node> x) noexcept override
35 {
36 append_child(children, std::move(x));
37 return true;
38 }
39
40 void post_process(formula_post_process_context &context) override
41 {
42 if (ssize(children) > 0) {
43 children.back()->left_align();
44 }
45
46 function = context.get_function(name);
47 hi_assert(function);
48
49 context.push_super(super_function);
50 for (hilet &child : children) {
51 child->post_process(context);
52 }
53 context.pop_super();
54 }
55
57 {
58 datum tmp;
59 try {
60 tmp = function(context, datum::vector_type{});
61
62 } catch (std::exception const &e) {
63 throw operation_error(std::format("{}: Could not evaluate block.\n{}", location, e.what()));
64 }
65
66 if (tmp.is_break()) {
67 throw operation_error(std::format("{}: Found #break not inside a loop statement.", location));
68
69 } else if (tmp.is_continue()) {
70 throw operation_error(std::format("{}: Found #continue not inside a loop statement.", location));
71
72 } else if (tmp.is_undefined()) {
73 return {};
74
75 } else {
76 throw operation_error(std::format("{}: Can not use a #return statement inside a #block.", location));
77 }
78 }
79
80 datum evaluate_call(formula_evaluation_context &context, datum::vector_type const &arguments)
81 {
82 context.push();
83 auto tmp = evaluate_children(context, children);
84 context.pop();
85
86 if (tmp.is_break()) {
87 throw operation_error(std::format("{}: Found #break not inside a loop statement.", location));
88
89 } else if (tmp.is_continue()) {
90 throw operation_error(std::format("{}: Found #continue not inside a loop statement.", location));
91
92 } else if (tmp.is_undefined()) {
93 return {};
94
95 } else {
96 throw operation_error(std::format("{}: Can not use a #return statement inside a #block.", location));
97 }
98 }
99
100 std::string string() const noexcept override
101 {
102 std::string s = "<block ";
103 s += name;
104 s += join(transform<std::vector<std::string>>(children, [](auto &x) {
105 return to_string(*x);
106 }));
107 s += ">";
108 return s;
109 }
110};
111
112} // namespace hi::inline v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
A dynamic data type.
Definition datum.hpp:219
constexpr bool is_break() const noexcept
Check if the result of a expression was a break flow control statement.
Definition datum.hpp:695
constexpr bool is_continue() const noexcept
Check if the result of a expression was a continue flow control statement.
Definition datum.hpp:703
constexpr bool is_undefined() const noexcept
Check if the datum has an undefined value.
Definition datum.hpp:687
Definition formula_evaluation_context.hpp:15
Definition formula_node.hpp:28
virtual std::string get_name() const
Get the name of a formula_name_node.
Definition formula_node.hpp:107
Definition formula_post_process_context.hpp:19
Definition functional.hpp:14
Definition parse_location.hpp:18
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:34
datum evaluate(formula_evaluation_context &context) override
Evaluate the template.
Definition skeleton_block_node.hpp:56
Definition skeleton_node.hpp:16
T move(T... args)
T to_string(T... args)
T what(T... args)