HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_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 "../formula/formula.hpp"
8#include "../strings.hpp"
9#include "../algorithm.hpp"
10#include <memory>
11#include <string_view>
12#include <optional>
13
14namespace tt {
15
17 using statement_vector = typename std::vector<std::unique_ptr<skeleton_node>>;
18
19 parse_location location;
20
22 location(std::move(location)) {}
23
24 virtual ~skeleton_node() {}
25
28 [[nodiscard]] virtual bool append(std::unique_ptr<skeleton_node> x) noexcept { return false; }
29
32 [[nodiscard]] virtual bool should_left_align() const noexcept { return true; }
33
36 virtual void left_align() noexcept {}
37
38 [[nodiscard]] virtual bool found_elif(parse_location _location, std::unique_ptr<formula_node> expression) noexcept { return false; }
39 [[nodiscard]] virtual bool found_else(parse_location _location) noexcept { return false;}
40 [[nodiscard]] virtual bool found_while(parse_location _location, std::unique_ptr<formula_node> expression) noexcept { return false; }
41
42 virtual void post_process(formula_post_process_context &context) {}
43
52 [[nodiscard]] virtual datum evaluate(formula_evaluation_context &context) {
53 tt_no_default();
54 }
55
56 [[nodiscard]] std::string evaluate_output(formula_evaluation_context &context) {
57 auto tmp = evaluate(context);
58 if (tmp.is_break()) {
59 throw operation_error("{}: Found #break not inside a loop statement.", location);
60
61 } else if (tmp.is_continue()) {
62 throw operation_error("{}: Found #continue not inside a loop statement.", location);
63
64 } else if (tmp.is_undefined()) {
65 return std::move(context.output);
66
67 } else {
68 throw operation_error("{}: Found #return not inside a function.", location);
69 }
70 }
71
72 [[nodiscard]] std::string evaluate_output() {
73 auto context = formula_evaluation_context{};
74 return evaluate_output(context);
75 }
76
77 [[nodiscard]] virtual std::string string() const noexcept {
78 return "<skeleton_node>";
79 }
80
81 [[nodiscard]] friend std::string to_string(skeleton_node const &lhs) noexcept {
82 return lhs.string();
83 }
84
85 friend std::ostream &operator<<(std::ostream &lhs, skeleton_node const &rhs) {
86 return lhs << to_string(rhs);
87 }
88
89 static void append_child(statement_vector &children, std::unique_ptr<skeleton_node> new_child) {
90 if (std::ssize(children) > 0 && new_child->should_left_align()) {
91 children.back()->left_align();
92 }
93 children.push_back(std::move(new_child));
94 }
95
96 [[nodiscard]] static datum evaluate_formula_without_output(formula_evaluation_context &context, formula_node const &expression, parse_location const &location) {
97 try {
98 return expression.evaluate_without_output(context);
99
100 } catch (std::exception const &e) {
101 throw operation_error("{}: Could not evaluate.\n{}", location, e.what());
102 }
103 }
104
105 [[nodiscard]] static datum evaluate_expression(formula_evaluation_context &context, formula_node const &expression, parse_location const &location) {
106 try {
107 return expression.evaluate(context);
108
109 } catch (std::exception const &e) {
110 throw operation_error("{}: Could not evaluate expression.\n{}", location, e.what());
111 }
112 }
113
114 static void post_process_expression(formula_post_process_context &context, formula_node &expression, parse_location const &location) {
115 try {
116 return expression.post_process(context);
117
118 } catch (std::exception const &e) {
119 throw operation_error("{}: Could not post-process expression.\n{}", location, e.what());
120 }
121 }
122
123 [[nodiscard]] static datum evaluate_children(formula_evaluation_context &context, statement_vector const &children) {
124 for (ttlet &child: children) {
125 ttlet tmp = child->evaluate(context);
126 if (!tmp.is_undefined()) {
127 return tmp;
128 }
129 }
130 return {};
131 }
132};
133
134}
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:42
Definition formula_evaluation_context.hpp:16
Definition parse_location.hpp:16
Definition skeleton_node.hpp:16
virtual datum evaluate(formula_evaluation_context &context)
Evaluate the template.
Definition skeleton_node.hpp:52
virtual void left_align() noexcept
Remove any trailing spaces or tabs after a new-line.
Definition skeleton_node.hpp:36
virtual bool append(std::unique_ptr< skeleton_node > x) noexcept
Append a template-piece to the current template.
Definition skeleton_node.hpp:28
virtual bool should_left_align() const noexcept
Should any spaces on the left side of a statement be removed?
Definition skeleton_node.hpp:32
T move(T... args)
T what(T... args)