summaryrefslogtreecommitdiff
path: root/expr.y
blob: f1648e7909bfca2add9ea978b17a2bfea913e4f3 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
%{
#define _XOPEN_SOURCE 700
#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>

typedef struct {
	int type;
	union {
		intmax_t i;
		char *s;
	} u;
} expr_yystype;

static char **expr_args;

#define YYSTYPE expr_yystype

static int yylex(void);
static int yyerror(const char *s);
static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2);
static int expr_compare(YYSTYPE *y1, YYSTYPE *y2);

%}

%token INTEGER
%token STRING
%token '(' ')'
%left ':'
%left '*' '/' '%'
%left '+' '-'
%left '=' '<' '>' LE GE NE
%left '&'
%left '|'

%start expr

%%

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

	| INTEGER {
		printf("%d\n", $1.u.i);
	}

	| '(' expr ')' {
	}

	| expr ':' expr {
		printf("match\n");
	}

	| expr '*' expr {
		expr_only_integers(&$1, &$3);
		$$.u.i = $1.u.i * $3.u.i;
		printf("%d\n", $$.u.i);
	}

	| expr '/' expr {
		expr_only_integers(&$1, &$3);
		$$.u.i = $1.u.i / $3.u.i;
		printf("%d\n", $$.u.i);
	}

	| expr '%' expr {
		expr_only_integers(&$1, &$3);
		$$.u.i = $1.u.i % $3.u.i;
		printf("%d\n", $$.u.i);
	}

	| expr '+' expr {
		expr_only_integers(&$1, &$3);
		$$.u.i = $1.u.i + $3.u.i;
		printf("%d\n", $$.u.i);
	}

	| expr '-' expr {
		expr_only_integers(&$1, &$3);
		$$.u.i = $1.u.i - $3.u.i;
		printf("%d\n", $$.u.i);
	}

	| expr '=' expr {
		int r = expr_compare(&$1, &$3) == 0;
		printf("%d\n", r);
	}

	| expr NE expr {
		int r = expr_compare(&$1, &$3) != 0;
		printf("%d\n", r);
	}

	| expr '<' expr {
		int r = expr_compare(&$1, &$3) < 0;
		printf("%d\n", r);
	}

	| expr LE expr {
		int r = expr_compare(&$1, &$3) <= 0;
		printf("%d\n", r);
	}

	| expr '>' expr {
		int r = expr_compare(&$1, &$3) > 0;
		printf("%d\n", r);
	}

	| expr GE expr {
		int r = expr_compare(&$1, &$3) >= 0;
		printf("%d\n", r);
	}

	| expr '&' expr {
		printf("and\n");
	}

	| expr '|' expr {
		printf("or\n");
	}
	;

%%

static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2)
{
	if (y1->type == STRING) {
		fprintf(stderr, "expr: %s is not an integer\n", y1->u.s);
		exit(1);
	}
	if (y2->type == STRING) {
		fprintf(stderr, "expr: %s is not an integer\n", y2->u.s);
		exit(1);
	}
}

static int expr_compare(YYSTYPE *y1, YYSTYPE *y2)
{
	if (y1->type == INTEGER && y2->type == INTEGER) {
		return y1->u.i - y2->u.i;
	}

	char buf[64];
	char *s1 = y1->u.s;
	char *s2 = y2->u.s;

	if (y1->type == INTEGER) {
		snprintf(buf, sizeof(buf), "%d", y1->u.i);
		s1 = buf;
	} else if (y2->type == INTEGER) {
		snprintf(buf, sizeof(buf), "%d", y2->u.i);
		s2 = buf;
	}

	return strcmp(s1, s2);
}

static int yylex(void)
{
	if (expr_args[optind] == NULL) {
		return YYEOF;
	}

	char *s = expr_args[optind];

	if (strlen(s) == 1) {
		char c = s[0];
		if (strchr("|&=+-*/%():", c)) {
			optind++;
			return c;
		}
	}

	if (strlen(s) == 2) {
		if (!strcmp(s, ">=")) {
			optind++;
			return GE;
		}
		if (!strcmp(s, "<=")) {
			optind++;
			return LE;
		}
		if (!strcmp(s, "!=")) {
			optind++;
			return NE;
		}
	}

	char *end = NULL;
	yylval.type = INTEGER;
	yylval.u.i = strtoimax(s, &end, 10);
	if (end && *end != '\0') {
		yylval.type = STRING;
		yylval.u.s = s;
	}

	optind++;
	return yylval.type;
}

static int yyerror(const char *s)
{
	fprintf(stderr, "expr: %s\n", s);
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL, "");

	int c;
	while ((c = getopt(argc, argv, "")) != -1) {
		switch (c) {
		default:
			return -1;
		}
	}

	if (optind >= argc) {
		fprintf(stderr, "expr: missing operands\n");
		return 1;
	}

	expr_args = argv;

	return yyparse();
}