Bash++
Bash++ compiler internal documentation
bpp_codegen.h
Go to the documentation of this file.
1
6#ifndef SRC_BPP_INCLUDE_BPP_CODEGEN_H_
7#define SRC_BPP_INCLUDE_BPP_CODEGEN_H_
8
9#include <string>
10#include <memory>
11#include <optional>
12#include <deque>
13#include <type_traits>
14#include <antlr4-runtime.h>
15
16#include "bpp.h"
17#include "../antlr/BashppLexer.h"
18#include "../internal_error.h"
19
20namespace bpp {
21// Minimal forward declarations
22class bpp_program;
23class bash_while_condition;
24class bpp_class;
25class bpp_object;
26
41 std::string pre_code;
42 std::string code;
43 std::string post_code;
44
50 std::string full_code() const {
51 return pre_code + (pre_code.empty() ? "" : "\n") + code + (post_code.empty() ? "" : "\n") + post_code;
52 }
53};
54
55code_segment generate_supershell_code(
56 const std::string& code_to_run,
57 bool in_while_condition,
58 std::shared_ptr<bash_while_condition> current_while_condition,
59 std::shared_ptr<bpp::bpp_program> program
60 );
61
63 std::shared_ptr<bpp_object> object,
64 const std::string& object_ref,
65 std::shared_ptr<bpp::bpp_program> program
66 );
67
69 const std::string& reference_code,
70 const std::string& method_name,
71 std::shared_ptr<bpp_class> assumed_class,
72 bool force_static_reference,
73 std::shared_ptr<bpp::bpp_program> program
74 );
75
77 const std::string& reference_code,
78 const std::string& class_name,
79 std::shared_ptr<bpp::bpp_program> program
80 );
81
83 const std::string& reference_code,
84 std::shared_ptr<bpp::bpp_program> program
85 );
86
88 const std::string& new_address,
89 std::shared_ptr<bpp::bpp_class> new_class
90 );
91
92
93
94// Entity reference resolution
95
97 std::shared_ptr<bpp::bpp_entity> entity;
101 std::shared_ptr<bpp::bpp_class> class_containing_the_method;
102
104 std::string message;
105 antlr4::tree::TerminalNode* token = nullptr;
106 };
107
108 std::optional<reference_error> error;
109};
110
112 const std::string& file,
113 std::shared_ptr<bpp::bpp_entity> context,
114 std::deque<antlr4::tree::TerminalNode*>* identifiers,
115 std::deque<std::string>* identifier_texts,
116 bool declare_local,
117 std::shared_ptr<bpp::bpp_program> program
118);
119
121 const std::string& file,
122 std::shared_ptr<bpp::bpp_entity> context,
123 auto identifiers,
124 bool declare_local,
125 std::shared_ptr<bpp::bpp_program> program
126) {
127 // identifiers should be either:
128 // 1. A pointer to a container of strings (eg, std::deque<std::string>*)
129 // 2. A pointer to a container of antlr4::tree::TerminalNode* (eg, std::deque<antlr4::tree::TerminalNode*>*)
130
131 using ident_t = std::remove_cvref_t<decltype(identifiers)>;
132 using container_t = std::remove_pointer_t<ident_t>;
133 using value_t = typename container_t::value_type;
134
135 std::deque<antlr4::tree::TerminalNode*> node_deque;
136 std::deque<std::string> text_deque;
137
138 if constexpr (std::is_convertible_v<value_t, std::string>) {
139 // identifiers: deque<string>*
140 for (const auto& id : *identifiers) {
141 text_deque.push_back(id);
142 }
143 return resolve_reference_impl(file, context, &node_deque, &text_deque, declare_local, program);
144 } else if constexpr (std::is_convertible_v<value_t, antlr4::tree::TerminalNode*>) {
145 // identifiers: deque<TerminalNode*>*
146 for (const auto& node : *identifiers) {
147 node_deque.push_back(node);
148 }
149 for (const auto& node : node_deque) {
150 text_deque.push_back(node->getText());
151 }
152 return resolve_reference_impl(file, context, &node_deque, &text_deque, declare_local, program);
153 } else {
154 static_assert(sizeof(value_t) == 0,
155 "resolve_reference: Identifiers must be either strings or TerminalNode pointers.");
156 }
157
158}
159
160} // namespace bpp
161
162#endif // SRC_BPP_INCLUDE_BPP_CODEGEN_H_
Definition bash_case.cpp:8
entity_reference resolve_reference_impl(const std::string &file, std::shared_ptr< bpp::bpp_entity > context, std::deque< antlr4::tree::TerminalNode * > *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:306
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:120
code_segment generate_typeof_code(const std::string &reference_code, std::shared_ptr< bpp::bpp_program > program)
Definition bpp_codegen.cpp:201
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:185
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:90
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:128
code_segment inline_new(const std::string &new_address, std::shared_ptr< bpp::bpp_class > new_class)
Generates a code segment to INLINE class's "new" function within a method.
Definition bpp_codegen.cpp:241
A struct to hold (compiled) code segments.
Definition bpp_codegen.h:40
std::string full_code() const
Return the full code segment as a single string.
Definition bpp_codegen.h:50
std::string post_code
Definition bpp_codegen.h:43
std::string code
Definition bpp_codegen.h:42
std::string pre_code
Definition bpp_codegen.h:41
Definition bpp_codegen.h:103
std::string message
Definition bpp_codegen.h:104
antlr4::tree::TerminalNode * token
Definition bpp_codegen.h:105
Definition bpp_codegen.h:96
bool created_first_temporary_variable
Definition bpp_codegen.h:99
std::shared_ptr< bpp::bpp_class > class_containing_the_method
Definition bpp_codegen.h:101
std::shared_ptr< bpp::bpp_entity > entity
Definition bpp_codegen.h:97
bool created_second_temporary_variable
Definition bpp_codegen.h:100
code_segment reference_code
Definition bpp_codegen.h:98
std::optional< reference_error > error
Definition bpp_codegen.h:108