11#include < preprocessor/preprocessor.h>
22#include < types/type_mapper.h>
3+ #include < array>
4+ #include < stdexcept>
35
46string get_source (const string& filename) {
57 std::ifstream file (filename);
@@ -11,6 +13,7 @@ string get_source(const string& filename) {
1113 return buffer.str ();
1214}
1315
16+
1417void preprocess (const string& filename, const vector<TokenOperator>& operators) {
1518 string preprocessed_string = get_preprocessor_string (operators);
1619 initialize_map_id ();
@@ -40,3 +43,80 @@ void preprocess(const string& filename, const vector<TokenOperator>& operators)
4043 out << raw_source;
4144 out.close ();
4245}
46+
47+
48+ string build_shader_string (const string& new_shader, const string& origin) {
49+ const vector<string> tags = { " INTERPRETER_SPECIFIC_FUNCTIONS" , " FUNCTION_DEFINITIONS" , " CONSTANT_DEFINITIONS" };
50+ string out = new_shader;
51+ for (const string& tag : tags) {
52+ const string& block = get_block (origin, tag);
53+ inject_at (out, tag, block);
54+ }
55+ return out;
56+ }
57+
58+ static bool found_bounds (const string& source, const string& tag, size_t & out_start, size_t & out_length) {
59+ string start_marker = " #define " + tag;
60+ string end_marker = " #define END_" + tag;
61+
62+ size_t start = source.find (start_marker);
63+ if (start == string::npos) return false ;
64+
65+ size_t end_tag_start = source.find (end_marker, start);
66+ if (end_tag_start == string::npos) return false ;
67+
68+ size_t end_of_line = source.find (' \n ' , end_tag_start);
69+ if (end_of_line == string::npos) end_of_line = source.size ();
70+
71+ out_start = start;
72+ out_length = end_of_line - start;
73+ return true ;
74+ }
75+
76+ string get_block (const string& source, const string& tag) {
77+ size_t start, length;
78+ if (!found_bounds (source, tag, start, length)) return " " ;
79+
80+ string start_str = " #define " + tag;
81+ size_t content_start = start + start_str.length ();
82+
83+ string end_str = " #define END_" + tag;
84+ size_t content_end = source.find (end_str, start);
85+
86+ return source.substr (content_start, content_end - content_start);
87+ }
88+
89+ string erase_block (const string& source, const string& tag) {
90+ string copy = source;
91+ size_t start, length;
92+ while (found_bounds (copy, tag, start, length)) {
93+ copy.erase (start, length);
94+ }
95+ return copy;
96+ }
97+
98+ string inject_at (const string& source, const string& tag, const string& payload) {
99+ string copy = source;
100+ const string marker = " #define " + tag;
101+ size_t pos = source.find (marker);
102+ if (pos == string::npos) {
103+ throw std::runtime_error (" Injection marker '" + tag + " ' not found" );
104+ }
105+ size_t end = source.find (' \n ' , pos);
106+ if (end == string::npos) end = pos + marker.length ();
107+
108+ copy.insert (end + 1 , " \n " + payload + " \n " );
109+ return copy;
110+ }
111+
112+ void inject_at (string& source, const string& tag, const string& payload) {
113+ const string marker = " #define " + tag;
114+ size_t pos = source.find (marker);
115+ if (pos == string::npos) {
116+ throw std::runtime_error (" Injection marker '" + tag + " ' not found" );
117+ }
118+ size_t end = source.find (' \n ' , pos);
119+ if (end == string::npos) end = pos + marker.length ();
120+
121+ source.insert (end + 1 , " \n " + payload + " \n " );
122+ }
0 commit comments