<commit_before>#include "tzsl.hpp"
#include "core/assert.hpp"
#include "source_transform.hpp"
#include "gl/api/shader.hpp"

#include "stdlib.hpp"

namespace tzslc
{
	std::string default_defines();
	tz::gl::ShaderStage try_get_stage(const std::string&);
	void evaluate_imports_new(std::string&);
	void evaluate_keywords(std::string&, tz::gl::ShaderStage);
	void evaluate_inout_blocks(std::string&, tz::gl::ShaderStage);
	void evaluate_language_level_functions(std::string&);
	void collapse_namespaces(std::string&);

	void rename_user_main(std::string& shader_source);
	void add_main_definition(std::string& shader_source);

//--------------------------------------------------------------------------------------------------

	void preprocess(std::string& shader_source)
	{
		shader_source = default_defines() + shader_source;
		evaluate_imports_new(shader_source);
		tz::gl::ShaderStage stage = try_get_stage(shader_source);
		tz_assert(stage != tz::gl::ShaderStage::Count, "Detected invalid shader stage. Internal tzslc error. Please submit a bug report.");
		evaluate_keywords(shader_source, stage);
		evaluate_inout_blocks(shader_source, stage);
		evaluate_language_level_functions(shader_source);
		collapse_namespaces(shader_source);

		if(stage == tz::gl::ShaderStage::Vertex)
		{
			rename_user_main(shader_source);
			add_main_definition(shader_source);
		}
	}

//--------------------------------------------------------------------------------------------------

	std::string default_defines()
	{
		/*
		 * tzslc header info
		 * #version 450 core
		 * #define TZ_OGL ?
		 * #define TZ_VULKAN ?
		 * #define TZ_DEBUG ?
		 *
		 * 5 lines total
		 */
		std::string ret = "/*tzslc header info*/\n#version 450 core\n";
		#if TZ_VULKAN
			ret += "#define TZ_OGL 0\n#define TZ_VULKAN 1\n#extension GL_EXT_debug_printf : enable\n";
		#elif TZ_OGL
			ret += "#define TZ_OGL 1\n#define TZ_VULKAN 0\n#extension GL_ARB_bindless_texture : enable\n";
		#endif

		#if TZ_DEBUG
			ret += "#define TZ_DEBUG 1;\n";
		#else
			ret += "#define TZ_DEBUG 0;\n"
		#endif
		return ret;
	}

//--------------------------------------------------------------------------------------------------

	void evaluate_imports_new(std::string& shader_source)
	{
		constexpr char import_regex[] = "import <([a-zA-Z0-9\\s]+)>";
		tzslc::transform(shader_source, std::regex{import_regex},
		[](auto beg, auto end)
		{
			const std::string& m = *beg;
			if(m == "space")
			{<commit_msg>* Fix missing semicolon lmao, which broke release builds<commit_after>#include "tzsl.hpp"
#include "core/assert.hpp"
#include "source_transform.hpp"
#include "gl/api/shader.hpp"

#include "stdlib.hpp"

namespace tzslc
{
	std::string default_defines();
	tz::gl::ShaderStage try_get_stage(const std::string&);
	void evaluate_imports_new(std::string&);
	void evaluate_keywords(std::string&, tz::gl::ShaderStage);
	void evaluate_inout_blocks(std::string&, tz::gl::ShaderStage);
	void evaluate_language_level_functions(std::string&);
	void collapse_namespaces(std::string&);

	void rename_user_main(std::string& shader_source);
	void add_main_definition(std::string& shader_source);

//--------------------------------------------------------------------------------------------------

	void preprocess(std::string& shader_source)
	{
		shader_source = default_defines() + shader_source;
		evaluate_imports_new(shader_source);
		tz::gl::ShaderStage stage = try_get_stage(shader_source);
		tz_assert(stage != tz::gl::ShaderStage::Count, "Detected invalid shader stage. Internal tzslc error. Please submit a bug report.");
		evaluate_keywords(shader_source, stage);
		evaluate_inout_blocks(shader_source, stage);
		evaluate_language_level_functions(shader_source);
		collapse_namespaces(shader_source);

		if(stage == tz::gl::ShaderStage::Vertex)
		{
			rename_user_main(shader_source);
			add_main_definition(shader_source);
		}
	}

//--------------------------------------------------------------------------------------------------

	std::string default_defines()
	{
		/*
		 * tzslc header info
		 * #version 450 core
		 * #define TZ_OGL ?
		 * #define TZ_VULKAN ?
		 * #define TZ_DEBUG ?
		 *
		 * 5 lines total
		 */
		std::string ret = "/*tzslc header info*/\n#version 450 core\n";
		#if TZ_VULKAN
			ret += "#define TZ_OGL 0\n#define TZ_VULKAN 1\n#extension GL_EXT_debug_printf : enable\n";
		#elif TZ_OGL
			ret += "#define TZ_OGL 1\n#define TZ_VULKAN 0\n#extension GL_ARB_bindless_texture : enable\n";
		#endif

		#if TZ_DEBUG
			ret += "#define TZ_DEBUG 1;\n";
		#else
			ret += "#define TZ_DEBUG 0;\n";
		#endif
		return ret;
	}

//--------------------------------------------------------------------------------------------------

	void evaluate_imports_new(std::string& shader_source)
	{
		constexpr char import_regex[] = "import <([a-zA-Z0-9\\s]+)>";
		tzslc::transform(shader_source, std::regex{import_regex},
		[](auto beg, auto end)
		{
			const std::string& m = *beg;
			if(m == "space")
			{
{'fraction_non_alphanumeric': 0.24294354838709678, 'fraction_numerical': 0.006048387096774193, 'mean_word_length': 2.824980724749422, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3702212', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1605, 'n_words': 304}