#include #include #include #include #include #include #include #include #include #include "bnf-parser.h" #include "utils.h" #include "dynamic-array.h" #include "string-view.h" #define I " " #define TERM_TRESHOLD (1 << 7) #define SPEED .25 static struct string_view load_file (const char *filename) { struct string_view file_contents; int fd = open (filename, O_RDONLY); if (fd == -1) DIE ("error: %s: %s", strerror (errno), filename); struct stat st; fstat (fd, &st); file_contents.beg = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); close (fd); file_contents.end = file_contents.beg + st.st_size; return file_contents; } static void unload_file (struct string_view sv) { munmap ((char *) sv.beg, STRING_VIEW_LEN (sv)); } static void print_term (FILE *fp, struct bnf_syntax syntax, struct bnf_lexeme term); static void print_random_nonterminal_expansion (FILE *fp, struct bnf_syntax syntax, struct string_view sv) { DYNAMIC_ARRAY_FOREACH (rule, syntax) { if (!STRING_VIEW_EQUALS (rule->name.sv, sv)) continue; uint32_t total_probability = 0; DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) total_probability += sequence->probability; uint32_t selection = rand () % total_probability; uint32_t sequence = 0; for (uint32_t probability = rule->is_replaced_by.items[sequence].probability; selection >= probability; probability += rule->is_replaced_by.items[sequence].probability) { sequence += 1; } DYNAMIC_ARRAY_FOREACH (term, rule->is_replaced_by.items[sequence]) print_term (fp, syntax, *term); return; } } static void print_terminal (FILE *fp, struct string_view sv) { sv.beg += 1; sv.end -= 1; fprintf (fp, "%.*s", (int) STRING_VIEW_LEN (sv), sv.beg); } static int term_count = 0; static void print_term (FILE *fp, struct bnf_syntax syntax, struct bnf_lexeme term) { switch (term.kind) { case BNF_LEXEME_KIND_TERMINAL: print_terminal (fp, term.sv); break; case BNF_LEXEME_KIND_NONTERMINAL: if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR (""))) { fprintf (fp, "\n"); } else if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR (""))) { } else { if (term_count > TERM_TRESHOLD && STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR (""))) { print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("")); } else { print_random_nonterminal_expansion (fp, syntax, term.sv); } } break; default: UNREACHABLE (); } term_count += 1; } int main (int argc, char *argv[]) { const char *progname = *argv++; if (argc < 2) DIE ("Usage: %s file", progname); const char *filename = *argv++; struct string_view file_contents = load_file (filename); struct bnf_lexer lexer = bnf_lexer_from_string_view (file_contents, filename); struct bnf_syntax syntax = bnf_parse_syntax (&lexer); assert (bnf_check_syntax (&lexer, syntax)); // check if grammar has rules for all nonterminals unsigned int seed = time (NULL); // unsigned int seed = 1742084054; srand (seed); FILE *fp = fopen ("/home/cmmm/documents/personal/projects/raylib-shadertoy/res/shaders/glsl330/generated.fs", "w"); // FILE *fp = stdout; fprintf (fp, "#version 330 core\n" "// SEED: %u\n" "// TERM_TRESHOLD: %u\n" "\n" "uniform float time;\n" "uniform vec2 resolution;\n" "uniform vec2 mouse;\n" "uniform float zoom;\n" "\n" "out vec4 color;\n" "\n" "float z = zoom;\n" "\n" "void\n" "main ()\n" "{\n" I"vec2 c = gl_FragCoord.xy/resolution*2*z - z;\n" I"vec2 m = mouse/resolution*2*z - z;\n" I"float t = time*%g;\n" "\n" " vec3 g = vec3 (", seed, TERM_TRESHOLD, SPEED); print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("")); fprintf (fp, ", "); term_count = 0; print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("")); fprintf (fp, ", "); term_count = 0; print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("")); fprintf (fp, ");\n" I"color = vec4 ((g + z)/2/z, 1);\n" "}\n"); fclose (fp); unload_file (file_contents); return EXIT_SUCCESS; }