HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_function_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 std::vector<std::string> argument_names;
14 statement_vector children;
15
17
18 skeleton_function_node(parse_location location, formula_post_process_context &context, std::unique_ptr<formula_node> function_declaration_expression) noexcept :
19 skeleton_node(std::move(location))
20 {
21 auto name_and_arguments = function_declaration_expression->get_name_and_argument_names();
22 tt_assert(name_and_arguments.size() >= 1);
23
24 name = name_and_arguments[0];
25 name_and_arguments.erase(name_and_arguments.begin());
26 argument_names = std::move(name_and_arguments);
27
28 super_function = context.set_function(name,
29 [this,&location](formula_evaluation_context &context, datum::vector const &arguments) {
30 try {
31 return this->evaluate_call(context, arguments);
32
33 } catch (std::exception const &e) {
34 error_info(true).set<"parse_location">(location);
35 throw operation_error("Failed during handling of function call.\n{}", tt::to_string(e, false));
36 }
37 }
38 );
39 }
40
43 bool append(std::unique_ptr<skeleton_node> x) noexcept override {
44 append_child(children, std::move(x));
45 return true;
46 }
47
48 void post_process(formula_post_process_context &context) override {
49 if (std::ssize(children) > 0) {
50 children.back()->left_align();
51 }
52
53 context.push_super(super_function);
54 for (ttlet &child: children) {
55 child->post_process(context);
56 }
57 context.pop_super();
58 }
59
61 return {};
62 }
63
64 datum evaluate_call(formula_evaluation_context &context, datum::vector const &arguments) {
65 context.push();
66 if (std::ssize(argument_names) != std::ssize(arguments)) {
67 tt_error_info().set<"parse_location">(location);
68 throw operation_error("Invalid number of arguments to function {}() expecting {} got {}.", name, argument_names.size(), arguments.size());
69 }
70
71 for (ssize_t i = 0; i != std::ssize(argument_names); ++i) {
72 context.set(argument_names[i], arguments[i]);
73 }
74
75 ttlet output_size = context.output_size();
76 auto tmp = evaluate_children(context, children);
77 context.pop();
78
79 if (tmp.is_break()) {
80 tt_error_info().set<"parse_location">(location);
81 throw operation_error("Found #break not inside a loop statement.");
82
83 } else if (tmp.is_continue()) {
84 tt_error_info().set<"parse_location">(location);
85 throw operation_error("Found #continue not inside a loop statement.");
86
87 } else if (tmp.is_undefined()) {
88 return {};
89
90 } else {
91 // When a function returns, it should not have written data to the output.
92 context.set_output_size(output_size);
93 return tmp;
94 }
95 }
96
97 std::string string() const noexcept override {
98 std::string s = "<function ";
99 s += name;
100 s += "(";
101 s += join(argument_names, ",");
102 s += ")";
103 s += join(transform<std::vector<std::string>>(children, [](auto &x) { return to_string(*x); }));
104 s += ">";
105 return s;
106 }
107};
108
109}
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 formula_post_process_context.hpp:18
Definition parse_location.hpp:16
Definition skeleton_function_node.hpp:11
bool append(std::unique_ptr< skeleton_node > x) noexcept override
Append a template-piece to the current template.
Definition skeleton_function_node.hpp:43
datum evaluate(formula_evaluation_context &context) override
Evaluate the template.
Definition skeleton_function_node.hpp:60
Definition skeleton_node.hpp:16
T move(T... args)
T size(T... args)
T to_string(T... args)