|
| 1 | +#include <preprocessor/transpiler.h> |
| 2 | + |
| 3 | +using std::string, std::vector; |
| 4 | + |
| 5 | +std::set<string> get_function_names(const std::string& source) { |
| 6 | + static const std::regex glsl_regex = []() { |
| 7 | + std::vector<std::string> types = { |
| 8 | + "void", "float", "int", "uint", "bool", "double", |
| 9 | + "vec2", "vec3", "vec4", "ivec2", "ivec3", "ivec4", "uvec2", "uvec3", "uvec4", |
| 10 | + "mat2", "mat3", "mat4", "dmat2", "dmat3", "dmat4", "sampler2D", "samplerCube", |
| 11 | + "number", "hp_vec2", "hp_vec3", "hp_vec4" |
| 12 | + }; |
| 13 | + std::string pattern = R"(\b(?:)"; |
| 14 | + for (size_t i = 0; i < types.size(); ++i) { |
| 15 | + pattern += types[i] + (i == types.size() - 1 ? "" : "|"); |
| 16 | + } |
| 17 | + pattern += R"()\s+([\w]+)\s*\()"; |
| 18 | + return std::regex(pattern); |
| 19 | + }(); |
| 20 | + std::set<string> function_names; |
| 21 | + |
| 22 | + auto words_begin = std::sregex_iterator(source.begin(), source.end(), glsl_regex); |
| 23 | + auto words_end = std::sregex_iterator(); |
| 24 | + |
| 25 | + for (std::sregex_iterator it = words_begin; it != words_end; ++it) { |
| 26 | + function_names.insert((*it)[1].str()); |
| 27 | + } |
| 28 | + return function_names; |
| 29 | +} |
| 30 | + |
| 31 | +std::map<string, string> set_to_map(const std::set<string>& word_set, const string& prefix) { |
| 32 | + std::map<string, string> out_map; |
| 33 | + for (const string& s : word_set) { |
| 34 | + if (s.rfind(prefix, 0) == 0) { |
| 35 | + out_map.insert({ s.substr(prefix.size()), s }); |
| 36 | + } |
| 37 | + } |
| 38 | + return out_map; |
| 39 | +} |
| 40 | + |
| 41 | +std::string remove_comments(const std::string& code) { |
| 42 | + std::regex comment_regex(R"(//.*|/\*[\s\S]*?\*/)"); |
| 43 | + return std::regex_replace(code, comment_regex, ""); |
| 44 | +} |
| 45 | + |
| 46 | +std::vector<std::string> tokenize(const std::string& source) { |
| 47 | + std::vector<std::string> tokens; |
| 48 | + std::string current; |
| 49 | + |
| 50 | + for (size_t i = 0; i < source.size(); ++i) { |
| 51 | + char c = source[i]; |
| 52 | + if (std::isspace(c)) { |
| 53 | + if (!current.empty()) { |
| 54 | + tokens.push_back(current); |
| 55 | + current.clear(); |
| 56 | + } |
| 57 | + std::string ws; |
| 58 | + while (i < source.size() && std::isspace(source[i])) { |
| 59 | + ws += source[i++]; |
| 60 | + } |
| 61 | + tokens.push_back(ws); |
| 62 | + i--; |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (std::isalnum(c) || c == '_' || c == '.') { |
| 67 | + if ((c == 'e' || c == 'E') && i + 1 < source.size() && |
| 68 | + (source[i + 1] == '+' || source[i + 1] == '-')) { |
| 69 | + current += c; |
| 70 | + current += source[++i]; |
| 71 | + continue; |
| 72 | + } |
| 73 | + current += c; |
| 74 | + } |
| 75 | + else { |
| 76 | + if (!current.empty()) { |
| 77 | + tokens.push_back(current); |
| 78 | + current.clear(); |
| 79 | + } |
| 80 | + if (std::isspace(c)) continue; |
| 81 | + |
| 82 | + if ((c == '+' || c == '-') && i + 1 < source.size() && source[i + 1] == c) { |
| 83 | + tokens.push_back(std::string(2, c)); |
| 84 | + i++; |
| 85 | + } |
| 86 | + else { |
| 87 | + tokens.push_back(std::string(1, c)); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + if (!current.empty()) tokens.push_back(current); |
| 92 | + return tokens; |
| 93 | +} |
| 94 | + |
| 95 | +void find_and_replace_tokens(std::vector<std::string>& tokens, |
| 96 | + const std::map<std::string, std::string>& rename_map) { |
| 97 | + std::string output; |
| 98 | + output.reserve(tokens.size() * 5); |
| 99 | + for (auto& token : tokens) { |
| 100 | + auto it = rename_map.find(token); |
| 101 | + if (it != rename_map.end()) { |
| 102 | + token = it->second; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +string tokens_to_string(const vector<string>& v) |
| 108 | +{ |
| 109 | + string out = ""; |
| 110 | + for (const string& s : v) { |
| 111 | + out += s; |
| 112 | + } |
| 113 | + return out; |
| 114 | +} |
| 115 | + |
| 116 | +bool is_sig(const std::string& t) { |
| 117 | + return !t.empty() && t.find_first_not_of(" \t\n\r") != std::string::npos; |
| 118 | +} |
| 119 | + |
| 120 | +void handle_ambiguous_types(std::vector<std::string>& tokens) { |
| 121 | + for (size_t i = 0; i < tokens.size(); ++i) { |
| 122 | + if (tokens[i] == "number" || tokens[i] == "hp_vec2") { |
| 123 | + size_t j = i + 1; |
| 124 | + while (j < tokens.size() && !is_sig(tokens[j])) j++; |
| 125 | + if (j < tokens.size() && tokens[j] == "(") { |
| 126 | + tokens[i] = (tokens[i] == "number") ? "float_to_number" : "initialize_hp_vec2"; |
| 127 | + } |
| 128 | + } |
| 129 | + else if (is_sig(tokens[i]) && (std::isdigit(tokens[i][0]) || (tokens[i].size() > 1 && tokens[i][0] == '.' && std::isdigit(tokens[i][1])))) { |
| 130 | + if (tokens[i].find('.') != std::string::npos) { |
| 131 | + std::string num = tokens[i]; |
| 132 | + if (num.back() == 'f' || num.back() == 'F') num.pop_back(); |
| 133 | + tokens[i] = "float_to_number(" + num + ")"; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +int get_left_operand(const std::vector<std::string>& tokens, int op_idx) { |
| 140 | + int i = op_idx - 1; |
| 141 | + while (i >= 0 && !is_sig(tokens[i])) i--; |
| 142 | + if (i < 0) return 0; |
| 143 | + if (tokens[i] == ")") { |
| 144 | + int depth = 0; |
| 145 | + while (i >= 0) { |
| 146 | + if (tokens[i] == ")") depth++; |
| 147 | + else if (tokens[i] == "(") depth--; |
| 148 | + i--; |
| 149 | + if (depth == 0) break; |
| 150 | + } |
| 151 | + while (i >= 0 && !is_sig(tokens[i])) i--; |
| 152 | + if (i >= 0 && (std::isalpha(tokens[i][0]) || tokens[i][0] == '_')) return i; |
| 153 | + return i + 1; |
| 154 | + } |
| 155 | + return i; |
| 156 | +} |
| 157 | + |
| 158 | +int get_right_operand(const std::vector<std::string>& tokens, int op_idx) { |
| 159 | + int i = op_idx + 1; |
| 160 | + while (i < tokens.size() && !is_sig(tokens[i])) i++; |
| 161 | + if (i >= tokens.size()) return tokens.size() - 1; |
| 162 | + if (tokens[i] == "-" || tokens[i] == "+") { |
| 163 | + i++; |
| 164 | + while (i < tokens.size() && !is_sig(tokens[i])) i++; |
| 165 | + } |
| 166 | + if (std::isalpha(tokens[i][0]) || tokens[i][0] == '_') { |
| 167 | + int func_idx = i; |
| 168 | + int next_sig = i + 1; |
| 169 | + while (next_sig < tokens.size() && !is_sig(tokens[next_sig])) next_sig++; |
| 170 | + if (next_sig < tokens.size() && tokens[next_sig] == "(") { |
| 171 | + i = next_sig; |
| 172 | + int depth = 0; |
| 173 | + while (i < tokens.size()) { |
| 174 | + if (tokens[i] == "(") depth++; |
| 175 | + else if (tokens[i] == ")") depth--; |
| 176 | + if (depth == 0) break; |
| 177 | + i++; |
| 178 | + } |
| 179 | + return i; |
| 180 | + } |
| 181 | + return func_idx; |
| 182 | + } |
| 183 | + if (tokens[i] == "(") { |
| 184 | + int depth = 0; |
| 185 | + while (i < tokens.size()) { |
| 186 | + if (tokens[i] == "(") depth++; |
| 187 | + else if (tokens[i] == ")") depth--; |
| 188 | + if (depth == 0) break; |
| 189 | + i++; |
| 190 | + } |
| 191 | + return i; |
| 192 | + } |
| 193 | + return i; |
| 194 | +} |
| 195 | + |
| 196 | +void process_operator_pass(std::vector<std::string>& tokens, const std::vector<std::string>& ops, const std::map<std::string, std::string>& func_map) { |
| 197 | + for (size_t i = 0; i < tokens.size(); ++i) { |
| 198 | + bool match = false; |
| 199 | + for (const auto& op : ops) { |
| 200 | + if (tokens[i] == op) { match = true; break; } |
| 201 | + } |
| 202 | + if (!match) continue; |
| 203 | + |
| 204 | + int left_idx = get_left_operand(tokens, i); |
| 205 | + int right_idx = get_right_operand(tokens, i); |
| 206 | + |
| 207 | + std::string func_name = func_map.at(tokens[i]); |
| 208 | + std::vector<std::string> new_tokens; |
| 209 | + |
| 210 | + for (int j = 0; j < left_idx; ++j) new_tokens.push_back(tokens[j]); |
| 211 | + new_tokens.push_back(func_name); |
| 212 | + new_tokens.push_back("("); |
| 213 | + for (int j = left_idx; j < i; ++j) new_tokens.push_back(tokens[j]); |
| 214 | + new_tokens.push_back(","); |
| 215 | + for (int j = i + 1; j <= right_idx; ++j) new_tokens.push_back(tokens[j]); |
| 216 | + new_tokens.push_back(")"); |
| 217 | + for (size_t j = right_idx + 1; j < tokens.size(); ++j) new_tokens.push_back(tokens[j]); |
| 218 | + |
| 219 | + tokens = new_tokens; |
| 220 | + i = left_idx; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +void handle_operators(std::vector<std::string>& tokens) { |
| 225 | + for (size_t i = 0; i < tokens.size(); ++i) { |
| 226 | + if (tokens[i] == "-") { |
| 227 | + int prev = i - 1; |
| 228 | + while (prev >= 0 && !is_sig(tokens[prev])) prev--; |
| 229 | + if (prev < 0 || tokens[prev] == "=" || tokens[prev] == "return" || tokens[prev] == "(" || tokens[prev] == "," || |
| 230 | + tokens[prev] == "+" || tokens[prev] == "-" || tokens[prev] == "*" || tokens[prev] == "/") { |
| 231 | + int right_idx = get_right_operand(tokens, i); |
| 232 | + std::vector<std::string> new_tokens; |
| 233 | + for (int j = 0; j < i; ++j) new_tokens.push_back(tokens[j]); |
| 234 | + new_tokens.push_back("hp_neg"); |
| 235 | + new_tokens.push_back("("); |
| 236 | + for (int j = i + 1; j <= right_idx; ++j) new_tokens.push_back(tokens[j]); |
| 237 | + new_tokens.push_back(")"); |
| 238 | + for (size_t j = right_idx + 1; j < tokens.size(); ++j) new_tokens.push_back(tokens[j]); |
| 239 | + tokens = new_tokens; |
| 240 | + i = right_idx; |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + std::map<std::string, std::string> mul_div = { {"*", "hp_mult"}, {"/", "hp_div"} }; |
| 246 | + process_operator_pass(tokens, { "*", "/" }, mul_div); |
| 247 | + |
| 248 | + std::map<std::string, std::string> add_sub = { {"+", "hp_add"}, {"-", "hp_sub"} }; |
| 249 | + process_operator_pass(tokens, { "+", "-" }, add_sub); |
| 250 | +} |
| 251 | + |
| 252 | +string transpile_to_highp_glsl(const std::string& lowp_glsl_func_declarations, const std::string& highp_glsl_func_declarations) { |
| 253 | + static const std::map<string, string> type_map = { |
| 254 | + {"float", "number"}, |
| 255 | + {"uint", "number"}, |
| 256 | + {"vec2", "hp_vec2"} |
| 257 | + }; |
| 258 | + const string sanitized_lowp = remove_comments(lowp_glsl_func_declarations); |
| 259 | + std::set<string> highp_function_names = get_function_names(highp_glsl_func_declarations); |
| 260 | + vector<string> tokens = tokenize(sanitized_lowp); |
| 261 | + |
| 262 | + find_and_replace_tokens(tokens, set_to_map(highp_function_names, "hp_")); |
| 263 | + find_and_replace_tokens(tokens, type_map); |
| 264 | + |
| 265 | + handle_ambiguous_types(tokens); |
| 266 | + handle_operators(tokens); |
| 267 | + |
| 268 | + string out = highp_glsl_func_declarations + tokens_to_string(tokens); |
| 269 | + |
| 270 | + return out; |
| 271 | +} |
| 272 | + |
0 commit comments