-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
157 lines (136 loc) · 3.96 KB
/
parser.y
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
/* PROLOGUE */
%{
#include <stdio.h>
#include <string.h>
extern int yyparse();
extern void yyerror(char *s);
extern int yylex (void);
FILE *file;
%}
%union {
char *string;
char delim;
int token;
}
%start sentence
/* TERMINALS */
%token <string> STR TID
%token <token> TINT
%token <delim> TCOMMA TQTE LPAR RPAR
%token <string> WRITE DRAW
// /* NON TERMINALS */
%type <string> sentence print write draw str color screens
%type <token> time x y size
%left TCOMMA
%left TQTE
%left LPAR RPAR WRITE DRAW
%%
/* GRAMMAR RULES */
sentence:
sentence sentence {
;
}
| LPAR screens TCOMMA print TCOMMA time RPAR { // with time specified (in seconds)
fprintf(file, "offset = %s;\n", $2);
fprintf(file, "delay(%d);\n", $6);
fprintf(file, "matrix.fillScreen(0);\nmatrix.show();\n");
}
| LPAR screens TCOMMA print RPAR { // without time specified
fprintf(file, "offset = %s\n;", $2); // TODO: allow for indefinite looping
}
| LPAR screens TCOMMA sentence TCOMMA time RPAR { // with time specified, nested
fprintf(file, "offset = %s\n;", $2); // TODO: make sure nesting works
fprintf(file, "delay(%d)\n;", $6);
fprintf(file, "matrix.fillScreen(0);\nmatrix.show();\n");
}
| LPAR screens TCOMMA sentence RPAR { // without time specified, nested
fprintf(file, "offset = %s\n", $2); // TODO: make sure nesting works
} // TODO: allow for indefinite looping
;
screens: // TODO: allow for multiple screens
LPAR screens RPAR {
fprintf(file, "(%s)\n", $2);
}
| screens TCOMMA TINT {
char *tmp;
sprintf(tmp, "%s, %d", $1, ($3-1)*64);
$$ = tmp;
}
| TINT {
char tmp[60];
sprintf(tmp, "%d", ($1-1)*64);
$$ = strdup(tmp);
}
;
time:
TINT {
$$ = $1 * 1000; // convert seconds to milliseconds
}
;
print:
LPAR print RPAR { ; }
| LPAR print RPAR LPAR draw RPAR { ; }
| LPAR print RPAR LPAR write RPAR { ; }
| draw { ; }
| write { ; }
;
write:
WRITE LPAR str TCOMMA size TCOMMA color RPAR {
fprintf(file, "setTextColor(%s);\n", $7);
fprintf(file, "matrix.setTextSize(%d);\n", $5);
fprintf(file, "matrix.println(\"%s\");\nmatrix.show();\n", $3);
}
| WRITE LPAR str TCOMMA size RPAR { // no color
fprintf(file, "matrix.setTextSize(%d);\n", $5);
fprintf(file, "matrix.println(\"%s\");\nmatrix.show();\n", $3);
}
| WRITE LPAR str TCOMMA color RPAR { // no size
fprintf(file, "matrix.setTextColor(%s);\n", $5);
fprintf(file, "matrix.println(\"%s\");\nmatrix.show();\n", $3);
}
| WRITE LPAR str RPAR { // no color or size
fprintf(file, "matrix.println(\"%s\");\nmatrix.show();\n", $3);
//printf("1: %s\n", $1);
//printf("2: %s\n", $2);
//printf("3: %s\n", $3);
}
;
draw:
DRAW LPAR x TCOMMA y TCOMMA color RPAR {
fprintf(file, "matrix.drawPixel(%d, %d, %s);\nmatrix.show();\n", $3, $5, $7);
printf("%s\n", $7);
}
| DRAW LPAR x TCOMMA y RPAR { // no color
fprintf(file, "matrix.drawPixel(%d, %d, %s);\nmatrix.show();\n", $3, $5, "white");
}
;
str:
STR { $$ = $1; printf("%s\n", $1);}
;
x:
TINT { $$ = $1; }
;
y:
TINT { $$ = $1; }
;
size:
TINT { $$ = $1; }
;
color:
STR { $$ = $1; printf("%s\n", $1);}
;
%%
/* EPILOGUE */
#include <ctype.h>
#include <stddef.h>
/* Called by yyparse on error. */
void yyerror (char *s) {
fprintf (stderr, "%s\n", s);
}
int main () {
file = fopen("out.c", "w");
fprintf(file, "void prog(void) {\n\tmatrix.fillScreen(0);\n");
yyparse();
fprintf(file, "matrix.show();\n}");
fclose(file);
}