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
|
/* 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 <histedit.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#define MASK(b) ((1ULL << (b)) - 1)
#define YYSTYPE uint64_t
static void yyerror(const char *str) {
warnx("%s", str);
}
static int yylex(void);
static uint64_t result;
%}
%left '|'
%left '^'
%left '&'
%left Shl Shr Sar
%left '+' '-'
%left '*' '/' '%'
%right '~'
%left 'K' 'M' 'G' 'T'
%token Int
%%
start:
expr { result = $1; }
;
expr:
Int
| '_' { $$ = result; }
| '(' expr ')' { $$ = $2; }
| expr 'K' { $$ = $1 << 10; }
| expr 'M' { $$ = $1 << 20; }
| expr 'G' { $$ = $1 << 30; }
| expr 'T' { $$ = $1 << 40; }
| '~' expr { $$ = ~$2; }
| '-' expr { $$ = -$2; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| expr '%' expr { $$ = $1 % $3; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr Shl expr { $$ = $1 << $3; }
| expr Shr expr { $$ = $1 >> $3; }
| expr Sar expr { $$ = (int64_t)$1 >> $3; }
| expr '&' expr { $$ = $1 & $3; }
| expr '^' expr { $$ = $1 ^ $3; }
| expr '|' expr { $$ = $1 | $3; }
;
%%
#define T(a, b) ((int)(a) << 8 | (int)(b))
static const char *input;
static int lexInt(uint64_t base) {
for (yylval = 0; input[0]; ++input) {
uint64_t digit;
if (input[0] == '_') {
continue;
} else if (input[0] >= '0' && input[0] <= '9') {
digit = input[0] - '0';
} else if (input[0] >= 'A' && input[0] <= 'F') {
digit = 0xA + input[0] - 'A';
} else if (input[0] >= 'a' && input[0] <= 'f') {
digit = 0xA + input[0] - 'a';
} else {
return Int;
}
if (digit >= base) return Int;
yylval *= base;
yylval += digit;
}
return Int;
}
static int yylex(void) {
while (isspace(input[0])) input++;
if (!input[0]) return EOF;
if (input[0] == '\'' && input[1] && input[2] == '\'') {
yylval = input[1];
input += 3;
return Int;
}
if (input[0] == '0') {
if (input[1] == 'b') {
input += 2;
return lexInt(2);
} else if (input[1] == 'x') {
input += 2;
return lexInt(16);
} else {
input += 1;
return lexInt(8);
}
} else if (isdigit(input[0])) {
return lexInt(10);
}
switch (T(input[0], input[1])) {
case T('<', '<'): input += 2; return Shl;
case T('>', '>'): input += 2; return Shr;
case T('-', '>'): input += 2; return Sar;
default: return *input++;
}
}
static char *prompt(EditLine *el) {
(void)el;
return "";
}
int main(void) {
HistEvent ev;
History *hist = history_init();
if (!hist) err(EX_OSERR, "history_init");
history(hist, &ev, H_SETSIZE, 100);
history(hist, &ev, H_SETUNIQUE, 1);
EditLine *el = el_init("bit", stdin, stdout, stderr);
if (!el) err(EX_IOERR, "el_init");
el_set(el, EL_PROMPT, prompt);
el_set(el, EL_HIST, history, hist);
for (;;) {
int len;
input = el_gets(el, &len);
if (len == 0) break;
if (len == 1) continue;
history(hist, &ev, H_ENTER, input);
int error = yyparse();
if (error) continue;
int bits = result > UINT32_MAX ? 64
: result > UINT16_MAX ? 32
: result > UINT8_MAX ? 16
: 8;
printf("0x%0*"PRIX64" %"PRId64"", bits >> 2, result, (int64_t)result);
if (bits == 8) {
char bin[9] = {0};
for (int i = 0; i < 8; ++i) {
bin[i] = '0' + (result >> (7 - i) & 1);
}
printf(" 0b%s", bin);
}
if (result < 128 && isprint(result)) {
printf(" '%c'", (char)result);
}
if (result) {
if (!(result & MASK(40))) {
printf(" %"PRIu64"T", result >> 40);
} else if (!(result & MASK(30))) {
printf(" %"PRIu64"G", result >> 30);
} else if (!(result & MASK(20))) {
printf(" %"PRIu64"M", result >> 20);
} else if (!(result & MASK(10))) {
printf(" %"PRIu64"K", result >> 10);
}
}
printf("\n\n");
}
el_end(el);
history_end(hist);
}
|