aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bnf-lexeme.c10
-rw-r--r--src/bnf-lexeme.h36
-rw-r--r--src/bnf-lexer.c101
-rw-r--r--src/bnf-lexer.h18
-rw-r--r--src/bnf-parser.c213
-rw-r--r--src/bnf-parser.h52
-rw-r--r--src/dynamic-array.h59
-rw-r--r--src/main.c158
-rw-r--r--src/randomart-1.bnf10
-rw-r--r--src/randomart-2.bnf10
-rw-r--r--src/randomart-3.bnf10
-rw-r--r--src/string-view.h17
-rw-r--r--src/utils.h27
13 files changed, 721 insertions, 0 deletions
diff --git a/src/bnf-lexeme.c b/src/bnf-lexeme.c
new file mode 100644
index 0000000..22ab39d
--- /dev/null
+++ b/src/bnf-lexeme.c
@@ -0,0 +1,10 @@
1#include "bnf-lexeme.h"
2
3#define X(SYM, ...) [BNF_LEXEME_KIND_##SYM] = #SYM,
4const char *bnf_lexeme_kind_names[BNF_LEXEME_KIND_COUNT] = {
5 BNF_LEXEME_KINDS ()
6 [BNF_LEXEME_KIND_END_OF_LINE] = "END_OF_LINE",
7 [BNF_LEXEME_KIND_END_OF_FILE] = "END_OF_FILE",
8 [BNF_LEXEME_KIND_INVALID ] = "INVALID",
9};
10#undef X
diff --git a/src/bnf-lexeme.h b/src/bnf-lexeme.h
new file mode 100644
index 0000000..8b01708
--- /dev/null
+++ b/src/bnf-lexeme.h
@@ -0,0 +1,36 @@
1#ifndef INCLUDED_BNF_LEXEME_H
2#define INCLUDED_BNF_LEXEME_H
3
4#include <stddef.h>
5#include <stdint.h>
6#include "utils.h"
7#include "string-view.h"
8
9#define BNF_LEXEME_KINDS() \
10 X (TERMINAL, "\"[^\"]*\"|'[^']*'") \
11 X (NONTERMINAL, "<[^>]*>" ) \
12 X (PROBABILITY, "[0-9]+" ) \
13 X (SYMBOL_ALTERNATIVE, "\\|" ) \
14 X (SYMBOL_IS_REPLACED_BY, "::=" ) \
15 X (COMMENT, "#[^\n]*" )
16
17#define X(SYM, ...) BNF_LEXEME_KIND_##SYM,
18enum bnf_lexeme_kind {
19 BNF_LEXEME_KINDS ()
20 BNF_LEXEME_KIND_END_OF_LINE,
21 BNF_LEXEME_KIND_END_OF_FILE,
22 BNF_LEXEME_KIND_INVALID,
23 BNF_LEXEME_KIND_COUNT,
24};
25#undef X
26
27#define BNF_LEXEME_REGEX_COUNT (BNF_LEXEME_KIND_COUNT - 3)
28
29struct bnf_lexeme {
30 struct string_view sv;
31 enum bnf_lexeme_kind kind;
32};
33
34extern const char *bnf_lexeme_kind_names[BNF_LEXEME_KIND_COUNT];
35
36#endif // INCLUDED_BNF_LEXEME_H
diff --git a/src/bnf-lexer.c b/src/bnf-lexer.c
new file mode 100644
index 0000000..f859af9
--- /dev/null
+++ b/src/bnf-lexer.c
@@ -0,0 +1,101 @@
1#include "bnf-lexer.h"
2#include <regex.h>
3#include <assert.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <ctype.h>
7#include "dynamic-array.h"
8
9#define X(SYM, REG, ...) [BNF_LEXEME_KIND_##SYM] = "^(" REG ")",
10static const char *lexeme_regex_strings[BNF_LEXEME_REGEX_COUNT] = { BNF_LEXEME_KINDS () };
11#undef X
12
13static regex_t lexeme_regexes[BNF_LEXEME_REGEX_COUNT];
14
15static void
16print_regex_error (const regex_t *comp, const char *regex, int errcode)
17{
18 size_t length = regerror (errcode, comp, NULL, 0);
19 char *buffer = malloc (length);
20 regerror (errcode, comp, buffer, length);
21 fprintf (stderr, "error: regcomp (\"%s\"): %s\n",
22 regex, buffer);
23 free (buffer);
24}
25
26static void __attribute__((constructor))
27compile_regexes (void)
28{
29 for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) {
30 regex_t *compiled = &lexeme_regexes[kind];
31 const char *pattern = lexeme_regex_strings[kind];
32 int errcode = regcomp (compiled, pattern, REG_EXTENDED);
33 if (errcode) {
34 print_regex_error (compiled, pattern, errcode);
35 exit (1);
36 }
37 }
38}
39
40struct bnf_lexer
41bnf_lexer_from_string_view (struct string_view sv, const char *filename)
42{
43 return (struct bnf_lexer) {
44 .sv = sv,
45 .cursor = sv.beg,
46 .filename = filename,
47 };
48}
49
50static void
51lexer_skip_whitespace (struct bnf_lexer *lexer)
52{
53 while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' ||
54 *lexer->cursor == '\t'))
55 lexer->cursor += 1;
56}
57
58struct bnf_lexeme
59bnf_lexer_next_lexeme (struct bnf_lexer *lexer)
60{
61 lexer_skip_whitespace (lexer);
62 struct bnf_lexeme lexeme = {
63 .sv.beg = lexer->cursor,
64 .kind = BNF_LEXEME_KIND_INVALID,
65 };
66
67 if (lexer->cursor >= lexer->sv.end) {
68 lexeme.kind = BNF_LEXEME_KIND_END_OF_FILE;
69 lexeme.sv.end = lexer->sv.end;
70 return lexeme;
71 }
72
73 if (*lexer->cursor == '\n') {
74 lexeme.kind = BNF_LEXEME_KIND_END_OF_LINE;
75 lexeme.sv.end = ++lexer->cursor;
76 return lexeme;
77 }
78
79 regmatch_t regmatch;
80 enum bnf_lexeme_kind longest_matching_lexeme_kind = BNF_LEXEME_KIND_INVALID;
81 int longest_matching_lexeme_kind_length = 0;
82 for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) {
83 if (regexec (&lexeme_regexes[kind], lexer->cursor, 1, &regmatch, 0)) continue;
84 if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue;
85 longest_matching_lexeme_kind_length = regmatch.rm_eo;
86 longest_matching_lexeme_kind = kind;
87 }
88
89 if (longest_matching_lexeme_kind != BNF_LEXEME_KIND_INVALID) {
90 lexeme.kind = longest_matching_lexeme_kind;
91 lexeme.sv.end = lexer->cursor += longest_matching_lexeme_kind_length;
92
93 switch (lexeme.kind) {
94 case BNF_LEXEME_KIND_COMMENT: return bnf_lexer_next_lexeme (lexer);
95 default: return lexeme;
96 }
97 }
98
99 lexeme.sv.end = ++lexer->cursor;
100 return lexeme;
101}
diff --git a/src/bnf-lexer.h b/src/bnf-lexer.h
new file mode 100644
index 0000000..5863219
--- /dev/null
+++ b/src/bnf-lexer.h
@@ -0,0 +1,18 @@
1#ifndef INCLUDED_BNF_LEXER_H
2#define INCLUDED_BNF_LEXER_H
3
4#include <stddef.h>
5#include "utils.h"
6#include "bnf-lexeme.h"
7
8struct bnf_lexer {
9 struct string_view sv;
10 const char *cursor;
11 const char *filename;
12};
13
14extern struct bnf_lexer bnf_lexer_from_string_view (struct string_view, const char *filename);
15extern struct bnf_lexer bnf_lexer_from_filename (const char *);
16extern struct bnf_lexeme bnf_lexer_next_lexeme (struct bnf_lexer *);
17
18#endif // INCLUDED_BNF_LEXER_H
diff --git a/src/bnf-parser.c b/src/bnf-parser.c
new file mode 100644
index 0000000..ce14e43
--- /dev/null
+++ b/src/bnf-parser.c
@@ -0,0 +1,213 @@
1#include <stdarg.h>
2#include "bnf-parser.h"
3#include "dynamic-array.h"
4
5void
6print_error (struct bnf_lexer *lexer, const char *point,
7 const char *template, ...)
8{
9 int row = 1;
10 int col = 0;
11
12 for (const char *head = lexer->sv.beg; head < point; ++head) {
13 col += 1;
14 if (*head == '\n') {
15 row += 1;
16 col = 0;
17 }
18 };
19
20 fprintf (stderr, "%s:%d:%d: error: ", lexer->filename, row, col);
21
22 va_list ap;
23 va_start (ap, template);
24 vfprintf (stderr, template, ap);
25 va_end (ap);
26
27 putc ('\n', stderr);
28}
29
30bool
31is_bnf_lexeme_valid (struct bnf_lexeme lexeme)
32{
33 return lexeme.kind != BNF_LEXEME_KIND_INVALID;
34}
35
36bool
37bnf_parse_lexeme (struct bnf_lexer *lexer, enum bnf_lexeme_kind kind)
38{
39 const char *checkpoint = lexer->cursor;
40 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
41
42 if (lexeme.kind != kind) goto revert;
43
44 return true;
45
46revert:
47 lexer->cursor = checkpoint;
48 return false;
49}
50
51struct bnf_lexeme
52bnf_parse_nonterminal (struct bnf_lexer *lexer)
53{
54 const char *checkpoint = lexer->cursor;
55 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
56 if (lexeme.kind == BNF_LEXEME_KIND_NONTERMINAL)
57 return lexeme;
58
59revert:
60 lexer->cursor = checkpoint;
61 return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID };
62}
63
64struct bnf_lexeme
65bnf_parse_terminal (struct bnf_lexer *lexer)
66{
67 const char *checkpoint = lexer->cursor;
68
69 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
70 if (lexeme.kind == BNF_LEXEME_KIND_TERMINAL)
71 return lexeme;
72
73revert:
74 lexer->cursor = checkpoint;
75 return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID };
76}
77
78uint32_t
79bnf_parse_probability (struct bnf_lexer *lexer)
80{
81 const char *checkpoint = lexer->cursor;
82
83 struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer);
84 if (lexeme.kind == BNF_LEXEME_KIND_PROBABILITY) {
85 uint32_t probability = 0;
86 while (lexeme.sv.beg != lexeme.sv.end) {
87 probability *= 10;
88 probability += *lexeme.sv.beg++ - '0';
89 }
90 return probability;
91 }
92
93revert:
94 lexer->cursor = checkpoint;
95 return 1;
96}
97
98bool
99is_bnf_sequence_valid (struct bnf_sequence sequence)
100{
101 return sequence.term_count != 0;
102}
103
104struct bnf_sequence
105bnf_parse_sequence (struct bnf_lexer *lexer)
106{
107 struct bnf_sequence sequence = {0};
108 const char *checkpoint = lexer->cursor;
109
110 sequence.probability = bnf_parse_probability (lexer);
111
112 for (;;) {
113 struct bnf_lexeme lexeme = bnf_parse_nonterminal (lexer);
114 if (!is_bnf_lexeme_valid (lexeme))
115 lexeme = bnf_parse_terminal (lexer);
116 if (!is_bnf_lexeme_valid (lexeme))
117 break;
118
119 DYNAMIC_ARRAY_APPEND (sequence, lexeme);
120 } if (!is_bnf_sequence_valid (sequence)) goto revert;
121
122 if (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_ALTERNATIVE) ||
123 bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE))
124 return sequence;
125
126revert:
127 DYNAMIC_ARRAY_FREE (sequence);
128 lexer->cursor = checkpoint;
129 return (struct bnf_sequence) {0};
130}
131
132bool
133is_bnf_rule_valid (struct bnf_rule rule)
134{
135 return rule.is_replaced_by.sequence_count != 0;
136}
137
138struct bnf_rule
139bnf_parse_rule (struct bnf_lexer *lexer)
140{
141 struct bnf_rule rule = {0};
142 const char *checkpoint = lexer->cursor;
143
144 rule.name = bnf_parse_nonterminal (lexer);
145 if (!is_bnf_lexeme_valid (rule.name))
146 goto revert;
147
148 if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_IS_REPLACED_BY))
149 goto revert;
150
151 for (;;) {
152 struct bnf_sequence sequence = bnf_parse_sequence (lexer);
153 if (!is_bnf_sequence_valid (sequence)) break;
154 DYNAMIC_ARRAY_APPEND (rule.is_replaced_by, sequence);
155 } if (is_bnf_rule_valid (rule)) return rule;
156
157revert:
158 DYNAMIC_ARRAY_FREE (rule.is_replaced_by);
159 lexer->cursor = checkpoint;
160 return (struct bnf_rule) {0};
161}
162
163struct bnf_syntax
164bnf_parse_syntax (struct bnf_lexer *lexer)
165{
166 struct bnf_syntax syntax = {0};
167
168 for (;;) {
169 while (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE));
170 struct bnf_rule rule = bnf_parse_rule (lexer);
171 if (!is_bnf_rule_valid (rule)) {
172 if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_FILE)) {
173 print_error (lexer, lexer->cursor, "unable to parse rule");
174 }
175 break;
176 }
177
178 DYNAMIC_ARRAY_APPEND (syntax, rule);
179 }
180
181 return syntax;
182}
183
184bool
185bnf_check_syntax (struct bnf_lexer *lexer, struct bnf_syntax syntax)
186{
187 bool no_errors = true;
188 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
189 DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) {
190 DYNAMIC_ARRAY_FOREACH (term, *sequence) {
191 if (term->kind != BNF_LEXEME_KIND_NONTERMINAL) continue;
192 if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOL>"))) continue;
193 if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) continue;
194
195 bool rule_found = false;
196 DYNAMIC_ARRAY_FOREACH (rule, syntax) {
197 if (!STRING_VIEW_EQUALS (rule->name.sv, term->sv)) continue;
198 rule_found = true;
199 break;
200 }
201
202 if (!rule_found) {
203 no_errors = false;
204 print_error (lexer, term->sv.beg,
205 "rule not found: %.*s",
206 (int) STRING_VIEW_LEN (term->sv), term->sv.beg);
207 }
208 }
209 }
210 }
211
212 return no_errors;
213}
diff --git a/src/bnf-parser.h b/src/bnf-parser.h
new file mode 100644
index 0000000..8887a66
--- /dev/null
+++ b/src/bnf-parser.h
@@ -0,0 +1,52 @@
1#ifndef INCLUDED_BNF_PARSER_H
2#define INCLUDED_BNF_PARSER_H
3
4#include <stdint.h>
5#include <stdbool.h>
6#include "bnf-lexer.h"
7
8struct bnf_sequence {
9 union { struct bnf_lexeme *items, *terms; };
10 union { uint32_t count, term_count; };
11 uint32_t probability;
12 uint32_t capacity;
13};
14
15struct bnf_rule {
16 struct bnf_lexeme name;
17 struct {
18 union { struct bnf_sequence *items, *sequences; };
19 union { uint32_t count, sequence_count; };
20 uint32_t capacity;
21 } is_replaced_by;
22};
23
24struct bnf_syntax {
25 union { struct bnf_rule *items, *rules; };
26 union { uint32_t count, rule_count; };
27 uint32_t capacity;
28};
29
30// struct term_table {
31// union { struct bnf_lexeme *items, *terms; };
32// union { uint32_t count, term_count; };
33// uint32_t capacity;
34// };
35
36struct bnf_parser {
37 struct bnf_lexer lexer;
38};
39
40bool is_bnf_lexeme_valid (struct bnf_lexeme);
41bool is_bnf_sequence_valid (struct bnf_sequence);
42bool is_bnf_rule_valid (struct bnf_rule);
43
44bool bnf_check_syntax (struct bnf_lexer *, struct bnf_syntax);
45bool bnf_parse_lexeme (struct bnf_lexer *, enum bnf_lexeme_kind);
46struct bnf_lexeme bnf_parse_nonterminal (struct bnf_lexer *);
47struct bnf_lexeme bnf_parse_terminal (struct bnf_lexer *);
48struct bnf_sequence bnf_parse_sequence (struct bnf_lexer *);
49struct bnf_rule bnf_parse_rule (struct bnf_lexer *);
50struct bnf_syntax bnf_parse_syntax (struct bnf_lexer *);
51
52#endif // INCLUDED_BNF_PARSER_H
diff --git a/src/dynamic-array.h b/src/dynamic-array.h
new file mode 100644
index 0000000..726fcce
--- /dev/null
+++ b/src/dynamic-array.h
@@ -0,0 +1,59 @@
1#ifndef INCLUDED_DYNAMIC_ARRAY_H
2#define INCLUDED_DYNAMIC_ARRAY_H
3
4#include "utils.h"
5
6#ifndef DYNAMIC_ARRAY_INIT_CAP
7#define DYNAMIC_ARRAY_INIT_CAP 256
8#endif
9
10#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \
11 ({ \
12 if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \
13 if ((DYNAMIC_ARRAY).capacity == 0) \
14 (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \
15 while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \
16 (DYNAMIC_ARRAY).capacity *= 2; \
17 (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \
18 (DYNAMIC_ARRAY).capacity * \
19 sizeof (*(DYNAMIC_ARRAY).items)); \
20 ASSERT((DYNAMIC_ARRAY).items); \
21 } \
22 })
23
24#define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \
25 ({ \
26 (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \
27 (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \
28 (DYNAMIC_ARRAY).capacity * \
29 sizeof (*(DYNAMIC_ARRAY).items)); \
30 ASSERT((DYNAMIC_ARRAY).items); \
31 \
32 })
33
34#define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \
35 ({ \
36 DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \
37 (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \
38 })
39
40#define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \
41 ({ \
42 DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \
43 memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \
44 (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \
45 (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \
46 })
47
48#define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \
49 (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1]
50
51#define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; })
52
53#define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items)
54
55#define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \
56 typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \
57 ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM)
58
59#endif
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}
diff --git a/src/randomart-1.bnf b/src/randomart-1.bnf
new file mode 100644
index 0000000..b7f6fd5
--- /dev/null
+++ b/src/randomart-1.bnf
@@ -0,0 +1,10 @@
1# randomart.bnf -- syntax with semantics for generating random art -*- BNF -*-
2 <expr> ::= <value> | 0 "-" <expr> | 10 <binop> | <trig> | 0 "abs(" <expr> ")" | "sqrt(abs(" <expr> "))"
3<value> ::= <coord> | 2 <mouse> | <time> | <const>
4<coord> ::= 4 "c.x" | 3 "c.y"
5<mouse> ::= 4 "m.x" | 3 "m.y"
6 <time> ::= "sin(t)" | 0 "cos(t)"
7<const> ::= "." <digit> <digit> <digit> <digit> <digit> <digit>
8<digit> ::= 2 "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
9<binop> ::= 10 "(" <expr> " + " <expr> ")" | 5 "(" <expr> " - " <expr> ")" | 3 <expr> "*" <expr> | 0 "(" <expr> "/" <expr> ")"
10 <trig> ::= "sin(" <expr> ")" | "cos(" <expr> ")" | "tan(" <expr> ")"
diff --git a/src/randomart-2.bnf b/src/randomart-2.bnf
new file mode 100644
index 0000000..42507dd
--- /dev/null
+++ b/src/randomart-2.bnf
@@ -0,0 +1,10 @@
1# randomart.bnf -- syntax with semantics for generating random art -*- BNF -*-
2 <expr> ::= <value> | 0 "-" <expr> | 10 <binop> | 3 <trig> | 1 "abs(" <expr> ")" | 5 "sqrt(abs(" <expr> "))"
3<value> ::= 3 <coord> | <mouse> | <time> | <const>
4<coord> ::= 4 "c.x" | 3 "c.y"
5<mouse> ::= 4 "m.x" | 3 "m.y"
6 <time> ::= "sin(t)" | "cos(t)"
7<const> ::= "." <digit> <digit> <digit> <digit> <digit> <digit>
8<digit> ::= 10 "0" | 9 "1" | 8 "2" | 7 "3" | 6 "4" | 5 "5" | 4 "6" | 3 "7" | 2 "8" | 1 "9"
9<binop> ::= 1 "(" <expr> " + " <expr> ")" | 30 "(" <expr> " - " <expr> ")" | 5 <expr> "*" <expr> | 7 "(" <expr> "/" <expr> ")"
10 <trig> ::= "sin(" <expr> ")" | "cos(" <expr> ")" | 0 "tan(" <expr> ")"
diff --git a/src/randomart-3.bnf b/src/randomart-3.bnf
new file mode 100644
index 0000000..a8c075f
--- /dev/null
+++ b/src/randomart-3.bnf
@@ -0,0 +1,10 @@
1# randomart.bnf -- syntax with semantics for generating random art -*- BNF -*-
2 <expr> ::= 1 <value> | 0 "(-" <expr> ")" | 10 <binop> | 3 <trig>
3<value> ::= 1 <coord> | 2 <mouse> | <time> | <const>
4<coord> ::= "(4*abs(c.x) + abs(c.y))/2"
5<mouse> ::= "sqrt((m.x - c.x)*(m.x - c.x) + (m.y - c.y)*(m.y - c.y))"
6 <time> ::= "sin(t)" | "cos(t)"
7<const> ::= "." <digit> <digit> <digit> <digit> <digit> <digit>
8<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | 3 "9"
9<binop> ::= 1 "(" <expr> " + " <expr> ")" | 0 "(" <expr> " - " <expr> ")" | 1 <expr> "*" <expr> | 0 "(" <expr> "/" <expr> ")"
10 <trig> ::= "sin(" <expr> ")" | "cos(" <expr> ")"
diff --git a/src/string-view.h b/src/string-view.h
new file mode 100644
index 0000000..0b6034a
--- /dev/null
+++ b/src/string-view.h
@@ -0,0 +1,17 @@
1#ifndef INCLUDED_STRING_VIEW_H
2#define INCLUDED_STRING_VIEW_H
3
4#include <string.h>
5
6struct string_view {
7 const char *beg;
8 const char *end;
9};
10
11#define STRING_VIEW_FROM_CSTR(CSTR) ((struct string_view) { .beg = (CSTR), .end = (CSTR) + strlen (CSTR) })
12#define STRING_VIEW_LEN(SV) ((SV).end - (SV).beg)
13#define STRING_VIEW_EQUALS(A, B) \
14 (STRING_VIEW_LEN (A) == STRING_VIEW_LEN (B) && \
15 !memcmp ((A).beg, (B).beg, STRING_VIEW_LEN (A)))
16
17#endif // INCLUDED_STRING_VIEW_H
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..fa8b3ee
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,27 @@
1#ifndef INCLUDED_UTILS_H
2#define INCLUDED_UTILS_H
3
4#include <assert.h>
5#include <stdlib.h>
6#include <stdio.h>
7
8#define UNUSED(X) ((void) (X))
9#define UNREACHABLE() ASSERT (!"reached the unreachable...")
10#define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__)
11// #define TODO(...)
12#define REACHED(FMT, ...) fprintf (stderr, "%s:%d: info: %s: reached: "FMT"\n", __FILE__, __LINE__, __func__ __VA_OPT__(,) __VA_ARGS__)
13
14#define MALLOC malloc
15#define REALLOC realloc
16#define FREE free
17#define ASSERT assert
18
19#define PRINT_EXP(EXP, FMT) printf (#EXP" = "FMT"\n", EXP)
20
21#define DIE(FMT, ...) \
22 ({ \
23 fprintf (stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__); \
24 exit (EXIT_FAILURE); \
25 })
26
27#endif