Bash++
Bash++ compiler internal documentation
bpp_codegen.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <string>
10#include <memory>
11#include <optional>
12#include <deque>
13#include <type_traits>
14
15#include "bpp.h"
16#include <error/InternalError.h>
17#include <AST/ASTNode.h>
18
19namespace bpp {
20// Minimal forward declarations
21class bpp_program;
22class bash_while_or_until_condition;
23class bpp_class;
24class bpp_object;
25
40 std::string pre_code;
41 std::string code;
42 std::string post_code;
43
49 std::string full_code() const {
50 return pre_code + (pre_code.empty() ? "" : "\n") + code + (post_code.empty() ? "" : "\n") + post_code;
51 }
52};
53
54code_segment generate_supershell_code(
55 const std::string& code_to_run,
56 std::shared_ptr<bpp::bpp_program> program
57 );
58
60 std::shared_ptr<bpp_object> object,
61 const std::string& object_ref,
62 std::shared_ptr<bpp::bpp_program> program
63 );
64
66 const std::string& reference_code,
67 std::shared_ptr<bpp_class> assumed_class
68 );
69
71 const std::string& reference_code,
72 std::shared_ptr<bpp_class> assumed_class,
73 bool force_static_reference,
74 std::shared_ptr<bpp::bpp_program> program
75 );
76
78 const std::string& reference_code,
79 const std::string& method_name,
80 std::shared_ptr<bpp_class> assumed_class,
81 bool force_static_reference,
82 std::shared_ptr<bpp::bpp_program> program
83 );
84
86 const std::string& reference_code,
87 const std::string& class_name,
88 std::shared_ptr<bpp::bpp_program> program
89 );
90
92 const std::string& reference_code,
93 std::shared_ptr<bpp::bpp_program> program
94 );
95
97 const std::string& new_address,
98 std::shared_ptr<bpp_class> new_class,
99 bool inline_new,
100 bool silent
101 );
102
103// Generators for standardized system methods
104std::shared_ptr<bpp::bpp_method> generate_copy_method(
105 std::shared_ptr<bpp::bpp_class> containing_class,
106 std::shared_ptr<bpp::bpp_program> program
107 );
108
110 const std::string& new_address,
111 std::shared_ptr<bpp::bpp_class> new_class,
112 bool inline_new
113 );
114
115std::shared_ptr<bpp::bpp_method> generate_new_method(
116 std::shared_ptr<bpp::bpp_class> containing_class
117 );
118
119std::shared_ptr<bpp::bpp_method> generate_delete_method(
120 std::shared_ptr<bpp::bpp_class> containing_class
121 );
122
123std::string get_encased_ref(const std::string& ref, uint8_t indirection_level);
124
125
126// Entity reference resolution
127
138 std::shared_ptr<bpp::bpp_entity> entity;
142 std::shared_ptr<bpp::bpp_class> class_containing_the_method;
143
148
149 std::optional<reference_error> error;
150};
151
153 const std::string& file,
154 std::shared_ptr<bpp::bpp_entity> context,
155 std::deque<AST::Token<std::string>>* nodes,
156 std::deque<std::string>* identifiers,
157 bool declare_local,
158 std::shared_ptr<bpp::bpp_program> program
159);
160
162 const std::string& file,
163 std::shared_ptr<bpp::bpp_entity> context,
164 auto identifiers,
165 bool declare_local,
166 std::shared_ptr<bpp::bpp_program> program
167) {
168 // identifiers should be either:
169 // 1. A pointer to a container of strings (eg, std::deque<std::string>*)
170 // 2. A pointer to a container of AST::Token<std::string> (eg, std::deque<AST::Token<std::string>>*)
171
172 using ident_t = std::remove_cvref_t<decltype(identifiers)>;
173 using container_t = std::remove_pointer_t<ident_t>;
174 using value_t = typename container_t::value_type;
175
176 std::deque<AST::Token<std::string>> node_deque;
177 std::deque<std::string> text_deque;
178
179
180 if constexpr (std::is_convertible_v<value_t, AST::Token<std::string>>) {
181 // identifiers: deque<TerminalNode*>*
182 for (const auto& node : *identifiers) {
183 node_deque.push_back(node);
184 }
185 for (const auto& node : node_deque) {
186 text_deque.push_back(node.getValue());
187 }
188 return resolve_reference_impl(file, context, &node_deque, &text_deque, declare_local, program);
189 } else if constexpr (std::is_convertible_v<value_t, std::string>) {
190 // identifiers: deque<string>*
191 for (const auto& id : *identifiers) {
192 text_deque.push_back(id);
193 }
194 return resolve_reference_impl(file, context, &node_deque, &text_deque, declare_local, program);
195 } else {
196 static_assert(sizeof(value_t) == 0,
197 "resolve_reference: Identifiers must be either strings or TerminalNode pointers.");
198 }
199
200}
201
202} // namespace bpp
A class representing a token in the Bash++ AST. Tokens store their value along with line and column i...
Definition Token.h:22
Definition bash_case.cpp:9
code_segment generate_supershell_code(const std::string &code_to_run, std::shared_ptr< bpp::bpp_program > program)
Generates a supershell code segment for executing a bash command.
Definition bpp_codegen.cpp:28
entity_reference resolve_reference(const std::string &file, std::shared_ptr< bpp::bpp_entity > context, auto identifiers, bool declare_local, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.h:161
code_segment generate_typeof_code(const std::string &reference_code, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.cpp:252
code_segment generate_destructor_call_code(const std::string &reference_code, std::shared_ptr< bpp_class > assumed_class, bool force_static_reference, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.cpp:196
code_segment generate_dynamic_cast_code(const std::string &reference_code, const std::string &class_name, std::shared_ptr< bpp::bpp_program > program)
Generates a code segment for performing a dynamic cast.
Definition bpp_codegen.cpp:236
code_segment generate_delete_code(std::shared_ptr< bpp::bpp_object > object, const std::string &object_ref, std::shared_ptr< bpp::bpp_program > program)
Generates a code segment for deleting an object.
Definition bpp_codegen.cpp:84
std::string get_encased_ref(const std::string &ref, uint8_t indirection_level)
Encases a temporary variable reference with the appropriate level of indirection.
Definition bpp_codegen.cpp:526
code_segment generate_new_code(const std::string &new_address, std::shared_ptr< bpp_class > new_class, bool inline_new, bool silent)
Generates a code segment for creating a new object of a given class.
Definition bpp_codegen.cpp:279
std::shared_ptr< bpp::bpp_method > generate_delete_method(std::shared_ptr< bpp::bpp_class > containing_class)
Definition bpp_codegen.cpp:476
code_segment generate_method_call_code(const std::string &reference_code, const std::string &method_name, std::shared_ptr< bpp::bpp_class > assumed_class, bool force_static_reference, std::shared_ptr< bpp::bpp_program > program)
Generates a code segment for calling a method.
Definition bpp_codegen.cpp:134
code_segment generate_constructor_call_code(const std::string &reference_code, std::shared_ptr< bpp_class > assumed_class)
Definition bpp_codegen.cpp:177
code_segment generate_code_for_new_method(const std::string &new_address, std::shared_ptr< bpp::bpp_class > new_class, bool inline_new)
Generate the assignments necessary to create a new object of a given class.
Definition bpp_codegen.cpp:309
std::shared_ptr< bpp::bpp_method > generate_new_method(std::shared_ptr< bpp::bpp_class > containing_class)
Definition bpp_codegen.cpp:448
entity_reference resolve_reference_impl(const std::string &file, std::shared_ptr< bpp::bpp_entity > context, std::deque< AST::Token< std::string > > *nodes, std::deque< std::string > *identifiers, bool declare_local, std::shared_ptr< bpp::bpp_program > program)
Resolves a reference to an entity in a particular context.
Definition bpp_codegen.cpp:575
std::shared_ptr< bpp::bpp_method > generate_copy_method(std::shared_ptr< bpp::bpp_class > containing_class, std::shared_ptr< bpp::bpp_program > program)
Generates a copy method for a class.
Definition bpp_codegen.cpp:380
A struct to hold (compiled) code segments.
Definition bpp_codegen.h:39
std::string full_code() const
Return the full code segment as a single string.
Definition bpp_codegen.h:49
std::string post_code
Definition bpp_codegen.h:42
std::string code
Definition bpp_codegen.h:41
std::string pre_code
Definition bpp_codegen.h:40
Definition bpp_codegen.h:144
AST::Token< std::string > token
Definition bpp_codegen.h:146
std::string message
Definition bpp_codegen.h:145
Represents the result of resolving a reference to an entity.
Definition bpp_codegen.h:137
bool created_first_temporary_variable
Definition bpp_codegen.h:140
std::shared_ptr< bpp::bpp_class > class_containing_the_method
Definition bpp_codegen.h:142
std::shared_ptr< bpp::bpp_entity > entity
Definition bpp_codegen.h:138
bool created_second_temporary_variable
Definition bpp_codegen.h:141
code_segment reference_code
Definition bpp_codegen.h:139
std::optional< reference_error > error
Definition bpp_codegen.h:149