about summary refs log tree commit diff
path: root/bin/order.y
blob: c2e8797172a8893fb044557680196fd1ce01965c (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

%{

#include <ctype.h>
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#define YYSTYPE char *

static char *fmt(const char *format, ...) {
	char *str = NULL;
	va_list ap;
	va_start(ap, format);
	vasprintf(&str, format, ap);
	va_end(ap);
	if (!str) err(EX_OSERR, "vasprintf");
	return str;
}

static int yylex(void);
static void yyerror(const char *str);

%}

%token Ident

%left ','
%right '=' MulAss DivAss ModAss AddAss SubAss ShlAss ShrAss AndAss XorAss OrAss
%right '?' ':'
%left Or
%left And
%left '|'
%left '^'
%left '&'
%left Eq Ne
%left '<' Le '>' Ge
%left Shl Shr
%left '+' '-'
%left '*' '/' '%'
%right '!' '~' Inc Dec Sizeof
%left '(' ')' '[' ']' Arr '.'

%%

start:
	expr { printf("%s\n", $1); }
	;

expr:
	Ident
	| '(' expr ')' { $$ = $2; }
	| expr '[' expr ']' { $$ = fmt("(%s[%s])", $1, $3); }
	| expr Arr Ident { $$ = fmt("(%s->%s)", $1, $3); }
	| expr '.' Ident { $$ = fmt("(%s.%s)", $1, $3); }
	| '!' expr { $$ = fmt("(!%s)", $2); }
	| '~' expr { $$ = fmt("(~%s)", $2); }
	| Inc expr { $$ = fmt("(++%s)", $2); }
	| Dec expr { $$ = fmt("(--%s)", $2); }
	| expr Inc { $$ = fmt("(%s++)", $1); }
	| expr Dec { $$ = fmt("(%s--)", $1); }
	| '+' expr %prec '!' { $$ = fmt("(+%s)", $2); }
	| '-' expr %prec '!' { $$ = fmt("(-%s)", $2); }
	| '*' expr %prec '!' { $$ = fmt("(*%s)", $2); }
	| '&' expr %prec '!' { $$ = fmt("(&%s)", $2); }
	| Sizeof expr { $$ = fmt("(sizeof %s)", $2); }
	| expr '*' expr { $$ = fmt("(%s * %s)", $1, $3); }
	| expr '/' expr { $$ = fmt("(%s / %s)", $1, $3); }
	| expr '%' expr { $$ = fmt("(%s %% %s)", $1, $3); }
	| expr '+' expr { $$ = fmt("(%s + %s)", $1, $3); }
	| expr '-' expr { $$ = fmt("(%s - %s)", $1, $3); }
	| expr Shl expr { $$ = fmt("(%s << %s)", $1, $3); }
	| expr Shr expr { $$ = fmt("(%s >> %s)", $1, $3); }
	| expr '<' expr { $$ = fmt("(%s < %s)", $1, $3); }
	| expr Le expr { $$ = fmt("(%s <= %s)", $1, $3); }
	| expr '>' expr { $$ = fmt("(%s > %s)", $1, $3); }
	| expr Ge expr { $$ = fmt("(%s >= %s)", $1, $3); }
	| expr Eq expr { $$ = fmt("(%s == %s)", $1, $3); }
	| expr Ne expr { $$ = fmt("(%s != %s)", $1, $3); }
	| expr '&' expr { $$ = fmt("(%s & %s)", $1, $3); }
	| expr '^' expr { $$ = fmt("(%s ^ %s)", $1, $3); }
	| expr '|' expr { $$ = fmt("(%s | %s)", $1, $3); }
	| expr And expr { $$ = fmt("(%s && %s)", $1, $3); }
	| expr Or expr { $$ = fmt("(%s || %s)", $1, $3); }
	| expr '?' expr ':' expr { $$ = fmt("(%s ? %s : %s)", $1, $3, $5); }
	| expr ass expr %prec '=' { $$ = fmt("(%s %s %s)", $1, $2, $3); }
	| expr ',' expr { $$ = fmt("(%s, %s)", $1, $3); }
	;

ass:
	'=' { $$ = "="; }
	| MulAss { $$ = "*="; }
	| DivAss { $$ = "/="; }
	| ModAss { $$ = "%="; }
	| AddAss { $$ = "+="; }
	| SubAss { $$ = "-="; }
	| ShlAss { $$ = "<<="; }
	| ShrAss { $$ = ">>="; }
	| AndAss { $$ = "&="; }
	| XorAss { $$ = "^="; }
	| OrAss { $$ = "|="; }
	;

%%

#define T(a, b) ((int)(a) << 8 | (int)(b))

static FILE *in;

static int yylex(void) {
	char ch;
	while (isspace(ch = getc(in)));

	if (isalnum(ch)) {
		char ident[64] = { ch, '\0' };
		for (size_t i = 1; i < sizeof(ident) - 1; ++i) {
			ch = getc(in);
			if (!isalnum(ch) && ch != '_') break;
			ident[i] = ch;
		}
		ungetc(ch, in);
		if (!strcmp(ident, "sizeof")) return Sizeof;
		yylval = fmt("%s", ident);
		return Ident;
	}

	char ne = getc(in);
	switch (T(ch, ne)) {
		case T('-', '>'): return Arr;
		case T('+', '+'): return Inc;
		case T('-', '-'): return Dec;
		case T('<', '='): return Le;
		case T('>', '='): return Ge;
		case T('=', '='): return Eq;
		case T('!', '='): return Ne;
		case T('&', '&'): return And;
		case T('|', '|'): return Or;
		case T('*', '='): return MulAss;
		case T('/', '='): return DivAss;
		case T('%', '='): return ModAss;
		case T('+', '='): return AddAss;
		case T('-', '='): return SubAss;
		case T('&', '='): return AndAss;
		case T('^', '='): return XorAss;
		case T('|', '='): return OrAss;
		case T('<', '<'): {
			if ('=' == (ne = getc(in))) return ShlAss;
			ungetc(ne, in);
			return Shl;
		}
		case T('>', '>'): {
			if ('=' == (ne = getc(in))) return ShrAss;
			ungetc(ne, in);
			return Shr;
		}
		default: {
			ungetc(ne, in);
			return ch;
		}
	}
}

static void yyerror(const char *str) {
	errx(EX_DATAERR, "%s", str);
}

int main(int argc, char *argv[]) {
	for (int i = 1; i < argc; ++i) {
		in = fmemopen(argv[i], strlen(argv[i]), "r");
		if (!in) err(EX_OSERR, "fmemopen");
		yyparse();
		fclose(in);
	}
	if (argc > 1) return EX_OK;
	in = stdin;
	yyparse();
}