From 5be0e39d0ce97849d7722bd1ac98563c7014faf3 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 11:19:44 +0300 Subject: publish --- src/bnf-lexeme.c | 10 +++ src/bnf-lexeme.h | 36 +++++++++ src/bnf-lexer.c | 101 +++++++++++++++++++++++++ src/bnf-lexer.h | 18 +++++ src/bnf-parser.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/bnf-parser.h | 52 +++++++++++++ src/dynamic-array.h | 59 +++++++++++++++ src/main.c | 158 ++++++++++++++++++++++++++++++++++++++ src/randomart-1.bnf | 10 +++ src/randomart-2.bnf | 10 +++ src/randomart-3.bnf | 10 +++ src/string-view.h | 17 +++++ src/utils.h | 27 +++++++ 13 files changed, 721 insertions(+) create mode 100644 src/bnf-lexeme.c create mode 100644 src/bnf-lexeme.h create mode 100644 src/bnf-lexer.c create mode 100644 src/bnf-lexer.h create mode 100644 src/bnf-parser.c create mode 100644 src/bnf-parser.h create mode 100644 src/dynamic-array.h create mode 100644 src/main.c create mode 100644 src/randomart-1.bnf create mode 100644 src/randomart-2.bnf create mode 100644 src/randomart-3.bnf create mode 100644 src/string-view.h create mode 100644 src/utils.h (limited to 'src') 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 @@ +#include "bnf-lexeme.h" + +#define X(SYM, ...) [BNF_LEXEME_KIND_##SYM] = #SYM, +const char *bnf_lexeme_kind_names[BNF_LEXEME_KIND_COUNT] = { + BNF_LEXEME_KINDS () + [BNF_LEXEME_KIND_END_OF_LINE] = "END_OF_LINE", + [BNF_LEXEME_KIND_END_OF_FILE] = "END_OF_FILE", + [BNF_LEXEME_KIND_INVALID ] = "INVALID", +}; +#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 @@ +#ifndef INCLUDED_BNF_LEXEME_H +#define INCLUDED_BNF_LEXEME_H + +#include +#include +#include "utils.h" +#include "string-view.h" + +#define BNF_LEXEME_KINDS() \ + X (TERMINAL, "\"[^\"]*\"|'[^']*'") \ + X (NONTERMINAL, "<[^>]*>" ) \ + X (PROBABILITY, "[0-9]+" ) \ + X (SYMBOL_ALTERNATIVE, "\\|" ) \ + X (SYMBOL_IS_REPLACED_BY, "::=" ) \ + X (COMMENT, "#[^\n]*" ) + +#define X(SYM, ...) BNF_LEXEME_KIND_##SYM, +enum bnf_lexeme_kind { + BNF_LEXEME_KINDS () + BNF_LEXEME_KIND_END_OF_LINE, + BNF_LEXEME_KIND_END_OF_FILE, + BNF_LEXEME_KIND_INVALID, + BNF_LEXEME_KIND_COUNT, +}; +#undef X + +#define BNF_LEXEME_REGEX_COUNT (BNF_LEXEME_KIND_COUNT - 3) + +struct bnf_lexeme { + struct string_view sv; + enum bnf_lexeme_kind kind; +}; + +extern const char *bnf_lexeme_kind_names[BNF_LEXEME_KIND_COUNT]; + +#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 @@ +#include "bnf-lexer.h" +#include +#include +#include +#include +#include +#include "dynamic-array.h" + +#define X(SYM, REG, ...) [BNF_LEXEME_KIND_##SYM] = "^(" REG ")", +static const char *lexeme_regex_strings[BNF_LEXEME_REGEX_COUNT] = { BNF_LEXEME_KINDS () }; +#undef X + +static regex_t lexeme_regexes[BNF_LEXEME_REGEX_COUNT]; + +static void +print_regex_error (const regex_t *comp, const char *regex, int errcode) +{ + size_t length = regerror (errcode, comp, NULL, 0); + char *buffer = malloc (length); + regerror (errcode, comp, buffer, length); + fprintf (stderr, "error: regcomp (\"%s\"): %s\n", + regex, buffer); + free (buffer); +} + +static void __attribute__((constructor)) +compile_regexes (void) +{ + for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) { + regex_t *compiled = &lexeme_regexes[kind]; + const char *pattern = lexeme_regex_strings[kind]; + int errcode = regcomp (compiled, pattern, REG_EXTENDED); + if (errcode) { + print_regex_error (compiled, pattern, errcode); + exit (1); + } + } +} + +struct bnf_lexer +bnf_lexer_from_string_view (struct string_view sv, const char *filename) +{ + return (struct bnf_lexer) { + .sv = sv, + .cursor = sv.beg, + .filename = filename, + }; +} + +static void +lexer_skip_whitespace (struct bnf_lexer *lexer) +{ + while (lexer->cursor < lexer->sv.end && (*lexer->cursor == ' ' || + *lexer->cursor == '\t')) + lexer->cursor += 1; +} + +struct bnf_lexeme +bnf_lexer_next_lexeme (struct bnf_lexer *lexer) +{ + lexer_skip_whitespace (lexer); + struct bnf_lexeme lexeme = { + .sv.beg = lexer->cursor, + .kind = BNF_LEXEME_KIND_INVALID, + }; + + if (lexer->cursor >= lexer->sv.end) { + lexeme.kind = BNF_LEXEME_KIND_END_OF_FILE; + lexeme.sv.end = lexer->sv.end; + return lexeme; + } + + if (*lexer->cursor == '\n') { + lexeme.kind = BNF_LEXEME_KIND_END_OF_LINE; + lexeme.sv.end = ++lexer->cursor; + return lexeme; + } + + regmatch_t regmatch; + enum bnf_lexeme_kind longest_matching_lexeme_kind = BNF_LEXEME_KIND_INVALID; + int longest_matching_lexeme_kind_length = 0; + for (enum bnf_lexeme_kind kind = 0; kind < BNF_LEXEME_REGEX_COUNT; ++kind) { + if (regexec (&lexeme_regexes[kind], lexer->cursor, 1, ®match, 0)) continue; + if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue; + longest_matching_lexeme_kind_length = regmatch.rm_eo; + longest_matching_lexeme_kind = kind; + } + + if (longest_matching_lexeme_kind != BNF_LEXEME_KIND_INVALID) { + lexeme.kind = longest_matching_lexeme_kind; + lexeme.sv.end = lexer->cursor += longest_matching_lexeme_kind_length; + + switch (lexeme.kind) { + case BNF_LEXEME_KIND_COMMENT: return bnf_lexer_next_lexeme (lexer); + default: return lexeme; + } + } + + lexeme.sv.end = ++lexer->cursor; + return lexeme; +} 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 @@ +#ifndef INCLUDED_BNF_LEXER_H +#define INCLUDED_BNF_LEXER_H + +#include +#include "utils.h" +#include "bnf-lexeme.h" + +struct bnf_lexer { + struct string_view sv; + const char *cursor; + const char *filename; +}; + +extern struct bnf_lexer bnf_lexer_from_string_view (struct string_view, const char *filename); +extern struct bnf_lexer bnf_lexer_from_filename (const char *); +extern struct bnf_lexeme bnf_lexer_next_lexeme (struct bnf_lexer *); + +#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 @@ +#include +#include "bnf-parser.h" +#include "dynamic-array.h" + +void +print_error (struct bnf_lexer *lexer, const char *point, + const char *template, ...) +{ + int row = 1; + int col = 0; + + for (const char *head = lexer->sv.beg; head < point; ++head) { + col += 1; + if (*head == '\n') { + row += 1; + col = 0; + } + }; + + fprintf (stderr, "%s:%d:%d: error: ", lexer->filename, row, col); + + va_list ap; + va_start (ap, template); + vfprintf (stderr, template, ap); + va_end (ap); + + putc ('\n', stderr); +} + +bool +is_bnf_lexeme_valid (struct bnf_lexeme lexeme) +{ + return lexeme.kind != BNF_LEXEME_KIND_INVALID; +} + +bool +bnf_parse_lexeme (struct bnf_lexer *lexer, enum bnf_lexeme_kind kind) +{ + const char *checkpoint = lexer->cursor; + struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); + + if (lexeme.kind != kind) goto revert; + + return true; + +revert: + lexer->cursor = checkpoint; + return false; +} + +struct bnf_lexeme +bnf_parse_nonterminal (struct bnf_lexer *lexer) +{ + const char *checkpoint = lexer->cursor; + struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); + if (lexeme.kind == BNF_LEXEME_KIND_NONTERMINAL) + return lexeme; + +revert: + lexer->cursor = checkpoint; + return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID }; +} + +struct bnf_lexeme +bnf_parse_terminal (struct bnf_lexer *lexer) +{ + const char *checkpoint = lexer->cursor; + + struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); + if (lexeme.kind == BNF_LEXEME_KIND_TERMINAL) + return lexeme; + +revert: + lexer->cursor = checkpoint; + return (struct bnf_lexeme) { .kind = BNF_LEXEME_KIND_INVALID }; +} + +uint32_t +bnf_parse_probability (struct bnf_lexer *lexer) +{ + const char *checkpoint = lexer->cursor; + + struct bnf_lexeme lexeme = bnf_lexer_next_lexeme (lexer); + if (lexeme.kind == BNF_LEXEME_KIND_PROBABILITY) { + uint32_t probability = 0; + while (lexeme.sv.beg != lexeme.sv.end) { + probability *= 10; + probability += *lexeme.sv.beg++ - '0'; + } + return probability; + } + +revert: + lexer->cursor = checkpoint; + return 1; +} + +bool +is_bnf_sequence_valid (struct bnf_sequence sequence) +{ + return sequence.term_count != 0; +} + +struct bnf_sequence +bnf_parse_sequence (struct bnf_lexer *lexer) +{ + struct bnf_sequence sequence = {0}; + const char *checkpoint = lexer->cursor; + + sequence.probability = bnf_parse_probability (lexer); + + for (;;) { + struct bnf_lexeme lexeme = bnf_parse_nonterminal (lexer); + if (!is_bnf_lexeme_valid (lexeme)) + lexeme = bnf_parse_terminal (lexer); + if (!is_bnf_lexeme_valid (lexeme)) + break; + + DYNAMIC_ARRAY_APPEND (sequence, lexeme); + } if (!is_bnf_sequence_valid (sequence)) goto revert; + + if (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_ALTERNATIVE) || + bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE)) + return sequence; + +revert: + DYNAMIC_ARRAY_FREE (sequence); + lexer->cursor = checkpoint; + return (struct bnf_sequence) {0}; +} + +bool +is_bnf_rule_valid (struct bnf_rule rule) +{ + return rule.is_replaced_by.sequence_count != 0; +} + +struct bnf_rule +bnf_parse_rule (struct bnf_lexer *lexer) +{ + struct bnf_rule rule = {0}; + const char *checkpoint = lexer->cursor; + + rule.name = bnf_parse_nonterminal (lexer); + if (!is_bnf_lexeme_valid (rule.name)) + goto revert; + + if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_SYMBOL_IS_REPLACED_BY)) + goto revert; + + for (;;) { + struct bnf_sequence sequence = bnf_parse_sequence (lexer); + if (!is_bnf_sequence_valid (sequence)) break; + DYNAMIC_ARRAY_APPEND (rule.is_replaced_by, sequence); + } if (is_bnf_rule_valid (rule)) return rule; + +revert: + DYNAMIC_ARRAY_FREE (rule.is_replaced_by); + lexer->cursor = checkpoint; + return (struct bnf_rule) {0}; +} + +struct bnf_syntax +bnf_parse_syntax (struct bnf_lexer *lexer) +{ + struct bnf_syntax syntax = {0}; + + for (;;) { + while (bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_LINE)); + struct bnf_rule rule = bnf_parse_rule (lexer); + if (!is_bnf_rule_valid (rule)) { + if (!bnf_parse_lexeme (lexer, BNF_LEXEME_KIND_END_OF_FILE)) { + print_error (lexer, lexer->cursor, "unable to parse rule"); + } + break; + } + + DYNAMIC_ARRAY_APPEND (syntax, rule); + } + + return syntax; +} + +bool +bnf_check_syntax (struct bnf_lexer *lexer, struct bnf_syntax syntax) +{ + bool no_errors = true; + DYNAMIC_ARRAY_FOREACH (rule, syntax) { + DYNAMIC_ARRAY_FOREACH (sequence, rule->is_replaced_by) { + DYNAMIC_ARRAY_FOREACH (term, *sequence) { + if (term->kind != BNF_LEXEME_KIND_NONTERMINAL) continue; + if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR (""))) continue; + if (STRING_VIEW_EQUALS (term->sv, STRING_VIEW_FROM_CSTR (""))) continue; + + bool rule_found = false; + DYNAMIC_ARRAY_FOREACH (rule, syntax) { + if (!STRING_VIEW_EQUALS (rule->name.sv, term->sv)) continue; + rule_found = true; + break; + } + + if (!rule_found) { + no_errors = false; + print_error (lexer, term->sv.beg, + "rule not found: %.*s", + (int) STRING_VIEW_LEN (term->sv), term->sv.beg); + } + } + } + } + + return no_errors; +} 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 @@ +#ifndef INCLUDED_BNF_PARSER_H +#define INCLUDED_BNF_PARSER_H + +#include +#include +#include "bnf-lexer.h" + +struct bnf_sequence { + union { struct bnf_lexeme *items, *terms; }; + union { uint32_t count, term_count; }; + uint32_t probability; + uint32_t capacity; +}; + +struct bnf_rule { + struct bnf_lexeme name; + struct { + union { struct bnf_sequence *items, *sequences; }; + union { uint32_t count, sequence_count; }; + uint32_t capacity; + } is_replaced_by; +}; + +struct bnf_syntax { + union { struct bnf_rule *items, *rules; }; + union { uint32_t count, rule_count; }; + uint32_t capacity; +}; + +// struct term_table { +// union { struct bnf_lexeme *items, *terms; }; +// union { uint32_t count, term_count; }; +// uint32_t capacity; +// }; + +struct bnf_parser { + struct bnf_lexer lexer; +}; + +bool is_bnf_lexeme_valid (struct bnf_lexeme); +bool is_bnf_sequence_valid (struct bnf_sequence); +bool is_bnf_rule_valid (struct bnf_rule); + +bool bnf_check_syntax (struct bnf_lexer *, struct bnf_syntax); +bool bnf_parse_lexeme (struct bnf_lexer *, enum bnf_lexeme_kind); +struct bnf_lexeme bnf_parse_nonterminal (struct bnf_lexer *); +struct bnf_lexeme bnf_parse_terminal (struct bnf_lexer *); +struct bnf_sequence bnf_parse_sequence (struct bnf_lexer *); +struct bnf_rule bnf_parse_rule (struct bnf_lexer *); +struct bnf_syntax bnf_parse_syntax (struct bnf_lexer *); + +#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 @@ +#ifndef INCLUDED_DYNAMIC_ARRAY_H +#define INCLUDED_DYNAMIC_ARRAY_H + +#include "utils.h" + +#ifndef DYNAMIC_ARRAY_INIT_CAP +#define DYNAMIC_ARRAY_INIT_CAP 256 +#endif + +#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ + ({ \ + if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ + if ((DYNAMIC_ARRAY).capacity == 0) \ + (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ + while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ + (DYNAMIC_ARRAY).capacity *= 2; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + } \ + }) + +#define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ + ({ \ + (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + \ + }) + +#define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ + }) + +#define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ + memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ + (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ + (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ + }) + +#define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1] + +#define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) + +#define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) + +#define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ + typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ + ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) + +#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 @@ +#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; +} 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 @@ +# randomart.bnf -- syntax with semantics for generating random art -*- BNF -*- + ::= | 0 "-" | 10 | | 0 "abs(" ")" | "sqrt(abs(" "))" + ::= | 2 |