aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..85ca5ad
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,158 @@
1#include <stdlib.h>
2#include <assert.h>
3#include <errno.h>
4#include <time.h>
5#include <ctype.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include <sys/stat.h>
9#include <sys/mman.h>
10#include "bnf-parser.h"
11#include "utils.h"
12#include "dynamic-array.h"
13#include "string-view.h"
14
15#define I " "
16
17#define TERM_TRESHOLD (1 << 7)
18#define SPEED .25
19
20static struct string_view
21load_file (const char *filename)
22{
23 struct string_view file_contents;
24
25 int fd = open (filename, O_RDONLY);
26 if (fd == -1) DIE ("error: %s: %s", strerror (errno), filename);
27
28 struct stat st;
29 fstat (fd, &st);
30 file_contents.beg = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
31 close (fd);
32 file_contents.end = file_contents.beg + st.st_size;
33
34 return file_contents;
35}
36
37static void
38unload_file (struct string_view sv)
39{
40 munmap ((char *) sv.beg, STRING_VIEW_LEN (sv));
41}
42
43static void
44print_term (FILE *fp, struct bnf_syntax syntax, struct bnf_lexeme term);
45
46static void
47print_random_nonterminal_expansion (FILE *fp,
48 struct bnf_syntax syntax,
49 struct string_view sv)
50{
51 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
52 if (!STRING_VIEW_EQUALS (rule->name.sv, sv)) continue;
53
54 uint32_t total_probability = 0;
55 DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by)
56 total_probability += sequence->probability;
57 uint32_t selection = rand () % total_probability;
58 uint32_t sequence = 0;
59 for (uint32_t probability = rule->is_replaced_by.items[sequence].probability;
60 selection >= probability;
61 probability += rule->is_replaced_by.items[sequence].probability) {
62 sequence += 1;
63 }
64
65 DYNAMIC_ARRAY_FOREACH (term, rule->is_replaced_by.items[sequence])
66 print_term (fp, syntax, *term);
67
68 return;
69 }
70}
71
72static void
73print_terminal (FILE *fp, struct string_view sv)
74{
75 sv.beg += 1;
76 sv.end -= 1;
77 fprintf (fp, "%.*s", (int) STRING_VIEW_LEN (sv), sv.beg);
78}
79
80static int term_count = 0;
81static void
82print_term (FILE *fp, struct bnf_syntax syntax, struct bnf_lexeme term)
83{
84 switch (term.kind) {
85 case BNF_LEXEME_KIND_TERMINAL:
86 print_terminal (fp, term.sv);
87 break;
88 case BNF_LEXEME_KIND_NONTERMINAL:
89 if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) {
90 fprintf (fp, "\n");
91 } else if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) {
92 } else {
93 if (term_count > TERM_TRESHOLD &&
94 STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<expr>"))) {
95 print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<value>"));
96 } else {
97 print_random_nonterminal_expansion (fp, syntax, term.sv);
98 }
99 } break;
100 default: UNREACHABLE ();
101 }
102
103 term_count += 1;
104}
105
106int
107main (int argc, char *argv[])
108{
109 const char *progname = *argv++;
110 if (argc < 2) DIE ("Usage: %s file", progname);
111
112 const char *filename = *argv++;
113 struct string_view file_contents = load_file (filename);
114
115 struct bnf_lexer lexer = bnf_lexer_from_string_view (file_contents, filename);
116 struct bnf_syntax syntax = bnf_parse_syntax (&lexer);
117 assert (bnf_check_syntax (&lexer, syntax)); // check if grammar has rules for all nonterminals
118
119 unsigned int seed = time (NULL);
120 // unsigned int seed = 1742084054;
121 srand (seed);
122
123 FILE *fp = fopen ("/home/cmmm/documents/personal/projects/raylib-shadertoy/res/shaders/glsl330/generated.fs", "w");
124 // FILE *fp = stdout;
125 fprintf (fp, "#version 330 core\n"
126 "// SEED: %u\n"
127 "// TERM_TRESHOLD: %u\n"
128 "\n"
129 "uniform float time;\n"
130 "uniform vec2 resolution;\n"
131 "uniform vec2 mouse;\n"
132 "uniform float zoom;\n"
133 "\n"
134 "out vec4 color;\n"
135 "\n"
136 "float z = zoom;\n"
137 "\n"
138 "void\n"
139 "main ()\n"
140 "{\n"
141 I"vec2 c = gl_FragCoord.xy/resolution*2*z - z;\n"
142 I"vec2 m = mouse/resolution*2*z - z;\n"
143 I"float t = time*%g;\n"
144 "\n"
145 " vec3 g = vec3 (", seed, TERM_TRESHOLD, SPEED);
146 print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<expr>"));
147 fprintf (fp, ", "); term_count = 0;
148 print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<expr>"));
149 fprintf (fp, ", "); term_count = 0;
150 print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<expr>"));
151 fprintf (fp, ");\n"
152 I"color = vec4 ((g + z)/2/z, 1);\n"
153 "}\n");
154 fclose (fp);
155 unload_file (file_contents);
156
157 return EXIT_SUCCESS;
158}