Add syntax files for expression and directive preprocessor.
[mesa.git] / src / mesa / shader / slang / library / gc_to_bin.c
1 #include "../../grammar/grammar_crt.h"
2 #include "../../grammar/grammar_crt.c"
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 static const char *slang_shader_syn =
7 #include "slang_shader_syn.h"
8 ;
9
10 static int gc_to_bin (grammar id, const char *in, const char *out)
11 {
12 FILE *f;
13 byte *source, *prod;
14 unsigned int size, i, line = 0;
15
16 printf ("Precompiling %s\n", in);
17
18 f = fopen (in, "r");
19 if (f == NULL)
20 return 1;
21 fseek (f, 0, SEEK_END);
22 size = ftell (f);
23 fseek (f, 0, SEEK_SET);
24 source = (byte *) grammar_alloc_malloc (size + 1);
25 source[fread (source, 1, size, f)] = '\0';
26 fclose (f);
27
28 if (!grammar_fast_check (id, source, &prod, &size, 65536))
29 {
30 grammar_alloc_free (source);
31 return 1;
32 }
33
34 f = fopen (out, "w");
35 fprintf (f, "\n");
36 fprintf (f, "/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */\n");
37 fprintf (f, "/* %s */\n", in);
38 fprintf (f, "\n");
39 for (i = 0; i < size; i++)
40 {
41 unsigned int a;
42 if (prod[i] < 10)
43 a = 1;
44 else if (prod[i] < 100)
45 a = 2;
46 else
47 a = 3;
48 if (i < size - 1)
49 a++;
50 if (line + a >= 100)
51 {
52 fprintf (f, "\n");
53 line = 0;
54 }
55 line += a;
56 fprintf (f, "%d", prod[i]);
57 if (i < size - 1)
58 fprintf (f, ",");
59 }
60 fprintf (f, "\n");
61 fclose (f);
62 grammar_alloc_free (prod);
63 return 0;
64 }
65
66 int main (int argc, char *argv[])
67 {
68 grammar id;
69
70 id = grammar_load_from_text ((const byte *) slang_shader_syn);
71 if (id == 0)
72 return 1;
73 grammar_set_reg8 (id, (const byte *) "parsing_builtin", 1);
74 grammar_set_reg8 (id, (const byte *) "shader_type", atoi (argv[1]));
75 if (gc_to_bin (id, argv[2], argv[3])) {
76 grammar_destroy (id);
77 return 1;
78 }
79 grammar_destroy (id);
80 return 0;
81 }
82