aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 85ca5ad2e2cfbc63b2048fdc74c3b3532a98337b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#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 ("<EOL>"))) {
        fprintf (fp, "\n");
      } else if (STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<EOF>"))) {
      } else {
        if (term_count > TERM_TRESHOLD &&
            STRING_VIEW_EQUALS (term.sv, STRING_VIEW_FROM_CSTR ("<expr>"))) {
          print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<value>"));
        } 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 ("<expr>"));
  fprintf (fp, ", "); term_count = 0;
  print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<expr>"));
  fprintf (fp, ", "); term_count = 0;
  print_random_nonterminal_expansion (fp, syntax, STRING_VIEW_FROM_CSTR ("<expr>"));
  fprintf (fp, ");\n"
               I"color = vec4 ((g + z)/2/z, 1);\n"
               "}\n");
  fclose (fp);
  unload_file (file_contents);

  return EXIT_SUCCESS;
}