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 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{});
56
57 } catch (...) {
58 auto error_location = location;
59 if (ttlet location_in_function = error_info::peek<parse_location, "parse_location">()) {
60 error_location += *location_in_function;
61 }
62 error_info(true).set<"parse_location">(error_location);
63 throw;
64 }
65
66 if (tmp.is_break()) {
67 tt_error_info().set<"parse_location">(location);
68 throw operation_error("Found #break not inside a loop statement.");
69
70 } else if (tmp.is_continue()) {
71 tt_error_info().set<"parse_location">(location);
72 throw operation_error("Found #continue not inside a loop statement.");
73
74 } else if (tmp.is_undefined()) {
75 return {};
76
77 } else {
78 tt_error_info().set<"parse_location">(location);
79 throw operation_error("Can not use a #return statement inside a #block.");
80 }
81 }
82
83 datum evaluate_call(formula_evaluation_context &context, datum::vector const &arguments) {
84 context.push();
85 auto tmp = evaluate_children(context, children);
86 context.pop();
87
88 if (tmp.is_break()) {
89 tt_error_info().set<"parse_location">(location);
90 throw operation_error("Found #break not inside a loop statement.");
91
92 } else if (tmp.is_continue()) {
93 tt_error_info().set<"parse_location">(location);
94 throw operation_error("Found #continue not inside a loop statement.");
95
96 } else if (tmp.is_undefined()) {
97 return {};
98
99 } else {
100 tt_error_info().set<"parse_location">(location);
101 throw operation_error("Can not use a #return statement inside a #block.");
102 }
103 }
104
105 std::string string() const noexcept override {
106 std::string s = "<block ";
107 s += name;
108 s += join(transform<std::vector<std::string>>(children, [](auto &x) { return to_string(*x); }));
109 s += ">";
110 return s;
111 }
112};
113
114}
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
Definition formula_post_process_context.hpp:18
Definition parse_location.hpp:16
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)