summaryrefslogtreecommitdiffstats
path: root/memedit.l
blob: 4db54036b4b37b1fef83f91165b441053e0f8829 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * $Id$
 */

%option nostack
%option noinput
%option nounput
%option noyywrap
%option pointer
%option nodefault
%option case-insensitive
%option prefix="zz"

%{
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string.h>

/* need this for the call to atol() below */
#include <math.h>
#include "y.tab.h"

extern int debug;
extern int in_line;

extern void yyerror(const char *);

/* Have input call the following function. */
#undef  YY_INPUT
#define YY_INPUT(buf,result,max_size) \
		rl_input((char *)buf, &result, max_size)

/* Variables to help interface readline with bc. */
static char *rl_line = (char *)NULL;
static char *rl_start = (char *)NULL;
static int   rl_len = 0;

/* Definitions for readline access. */
extern FILE *rl_instream;

/* rl_input puts upto MAX characters into BUF with the number put in
   BUF placed in *RESULT.  If the yy input file is the same as
   rl_instream (stdin), use readline.  Otherwise, just read it.
*/

static void
rl_input (char *buf, int  *result, int   max)
{
	if (yyin != rl_instream) {
		*result = read( fileno(rl_instream), buf, max );
		return;
	}

	/* Do we need a new string? */
	if (rl_len == 0) {
		if (rl_start)
		free(rl_start);
		rl_start = readline ("->");
		if (rl_start == NULL) {
			/* end of file */
			*result = 0;
			rl_len = 0;
			return;
		}
		rl_line = rl_start;
		rl_len = strlen (rl_line)+1;
		if (rl_len != 1)
			add_history (rl_line); 
		rl_line[rl_len-1] = '\n';
		fflush (stdout);
	}

	if (rl_len <= max) {
		strncpy (buf, rl_line, rl_len);
		*result = rl_len;
		rl_len = 0;
	} else {
		strncpy (buf, rl_line, max);
		*result = max;
		rl_line += max;
		rl_len -= max;
	}
}

%}

DIGIT	[0-9]
VAR	[a-z_][a-z0-9_]*
FILE	[a-z_/.][a-z0-9_/.]*
HEX	[0-9a-z]
OCT	[0-7]
BIN	[01]
%%

[#].* ;

q |
x |
exit |
quit {
	if (debug > 4) printf ("EXIT\n"); 
	return EXIT;
}

h |
help {
	if (debug > 4) printf ("HELP\n"); 
	return HELP;
}

e |
env |
printenv {
	if (debug > 4) printf ("ENV\n"); 
	return ENV;
}

m[dmf](\.[biw])* {
	if (strlen (yytext) == 2) yylval.val = 4;
	else switch (*(yytext + 3)) {
	case 'b': yylval.val = 1; break;
	case 'i': yylval.val = 4; break;
	case 'w': yylval.val = 2; break;
	}
	
	if (*(yytext + 1) == 'd') {
		if (debug > 4) printf ("MD %ld-bit\n",yylval.val*8); 
		return MD;
	}
	else if (*(yytext + 1) == 'f') {
		if (debug > 4) printf ("MF %ld-bit\n",yylval.val*8); 
		return MF;
	}
	else { 
		if (debug > 4) printf ("MM %ld-bit\n",yylval.val*8); 
		return MM;
	}
}

\+ |
add { 
	if (debug > 4) printf ("ADD\n"); 
	return ADD;
}

\- |
sub { 
	if (debug > 4) printf ("SUB\n"); 
	return SUB;
}

\* |
mul { 
	if (debug > 4) printf ("MUL\n"); 
	return MUL;
}

\/ |
div { 
	if (debug > 4) printf ("DIV\n"); 
	return DIV;
}

\>\> |
rshift { 
	if (debug > 4) printf ("RSHIFT\n"); 
	return RSHIFT;
}

\<\< |
shift |
lshift { 
	if (debug > 4) printf ("LSHIFT\n"); 
	return LSHIFT;
}

\( { 
	if (debug > 4) printf ("OBRA\n"); 
	return OBRA;
}

\) { 
	if (debug > 4) printf ("CBRA\n"); 
	return CBRA;
}

\% |
mod { 
	if (debug > 4) printf ("MOD\n"); 
	return MOD;
}

\= |
eq { 
	if (debug > 4) printf ("EQ\n"); 
	return EQ;
}

\@ |
at { 
	if (debug > 4) printf ("AT\n");
	return AT;
}

o |
open { 
	if (debug > 4) printf ("OPEN\n"); 
	return OPEN;
}

c |
close { 
	if (debug > 4) printf ("CLOSE\n"); 
	return CLOSE;
}

map { 
	if (debug > 4) printf ("MAP\n"); 
	return MAP;
}

unmap { 
	if (debug > 4) printf ("UNMAP\n"); 
	return UNMAP;
}

{VAR} {
	yylval.tptr = malloc(strlen(yytext)+1);
	if (yylval.tptr) strcpy(yylval.tptr, yytext);
	else printf ("no mem in %s", __FUNCTION__);
	if (debug > 4) printf ("VAR: >%s<\n", yytext); 
	return VARNAME;
}

{FILE} {
	yylval.tptr = malloc(strlen(yytext)+1);
	if (yylval.tptr) strcpy(yylval.tptr, yytext);
	else printf ("no mem in %s", __FUNCTION__);
	if (debug > 4) printf ("FILE: >%s<\n", yytext); 
	return FILENAME;
}

{DIGIT}+ {
	yylval.val = atol(yytext);
	if (debug > 4) printf ("DIGIT: %s (%ld)\n", yytext, yylval.val); 
	return NUM;
}

0x{HEX}+ {
	yylval.val = strtoul(yytext + 2, (char **)NULL, 16);
	if (debug > 4) printf ("HEX: %s (%ld)\n", yytext, yylval.val);
	return NUM;
}

0o{OCT}+ {
	yylval.val = strtoul(yytext + 2, (char **)NULL, 8);
	if (debug > 4) printf ("OCT: %s (%ld)\n", yytext, yylval.val); 
	return NUM;
}

0b{BIN}+ {
	yylval.val = strtoul(yytext + 2, (char **)NULL, 2);
	if (debug > 4) printf ("BIN: %s (%ld)\n", yytext, yylval.val);
	return NUM;
}

\n     {
	if (debug > 4) printf ("CR\n");
	in_line = 0;
	return CR;
}

.	{
	/* eat up any unmatched character */
	if (debug > 4) printf ("unmatched: 0x%02x\n", *yytext);
}
%%