-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathc_buf.c
301 lines (225 loc) · 4.88 KB
/
c_buf.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* c_buf.c
Mike's Enhanced Small C Compiler for Z80 & CP/M
I/O buffer module.
Copyright (c) 1999-2016 Miguel I. Garcia Lopez, FloppySoftware.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Revisions:
16 Jan 2001 : Last revision.
16 Apr 2007 : GPL'd.
27 May 2007 : issym() expanded to more speed.
Minor changes in inchar().
16 Aug 2015 : Minor changes in BfWordEq() and BfWordNeq().
26 Oct 2015 : Cleaned.
29 Nov 2015 : Added ChEq() y ChNeq().
11 Oct 2016 : Documented and slightly optimized.
12 Oct 2016 : Blanks are ' ' only. See cpp_read().
*/
// Test if character is valid as first character of symbol
issym1st(c)
char c;
{
return isalpha(c) || c == '_'; // FIXME - Optimize for MESCC ??
}
// Test if character is valid as character symbol
issym(c)
char c;
{
return isalpha(c) || c == '_' || isdigit(c); // FIXME - Optimize for MESCC ??
}
// -------------------------------------
// Functions related to the input stream
// -------------------------------------
// Skip symbol name if matchs with input parameter
// in: symbol name
// out: true on success; else false
InSymEq(s)
char *s;
{
int i;
InBlanks(); // FIXME -- Optimize this (see c_main.c)
i = strlen(s);
if(!memcmp(s, line + lptr, i))
{
if(!issym(line[lptr + i]))
{
lptr += i; return 1;
}
}
return 0;
}
// Skip character if matches with input parameter
// in: character
// out: true on success; else false
InChEq(c)
char c;
{
InBlanks(); // FIXME -- Optimize this (see c_main.c)
if(BfEq(c)) // FIXME -- Optimize this
{
BfGet(); // FIXME -- Optimize this
return 1;
}
return 0;
}
// Skip two characters if match with input parameter
// in: two characters
// out: true on succes; else false
InWordEq(w)
int w;
{
InBlanks(); // FIXME -- Optimize this (see c_main.c)
if(BfWordEq(w))
{
BfGet();
BfGet();
return 1;
}
return 0;
}
// Input byte
// out: byte on success; else 0
inbyte()
{
while(!Bf())
{
if(eof)
return 0;
BfRead();
}
return BfGet();
}
// Input character
// out: character on success; else 0
inchar()
{
if(!Bf())
BfRead();
return eof ? 0 : BfGet();
}
// Skip blanks
InBlanks()
{
while(1)
{
/*****************************
while(BfEq(' ') || BfEq('\t'))
BfGet();
******************************/
BfBlanks();
if(Bf())
break;
BfRead();
if(eof)
break;
}
}
// Test if the next character matchs with input parameter
ChEq(c)
char c;
{
InBlanks(); // FIXME -- Optimize this (see c_main.c)
return BfEq(c);
}
// Test if the next character does not match with input parameter
ChNeq(c)
char c;
{
InBlanks(); // FIXME -- Optimize this (see c_main.c)
return BfNeq(c);
}
// -------------------------------------
// Functions related to the input buffer
// -------------------------------------
Bf()
{
return line[lptr];
}
// Return following character to the current one
BfNext()
{
/*****************************
if(Bf())
return line[lptr + 1];
return 0;
******************************/
return Bf() ? line[lptr + 1] : 0;
}
// Input current character
BfGet()
{
char c;
if(c = Bf())
++lptr;
return c;
}
// Test if current character matchs with input parameter
BfEq(c)
char c;
{
return Bf() == c;
}
// Test if current character does not match with input parameter
BfNeq(c)
char c;
{
return Bf() != c;
}
// Test if following character to the current one matchs with input parameter
BfNextEq(c)
char c;
{
return BfNext() == c;
}
/*
int BfNextNeq(char c)
Devuelve TRUE si BfNext() != c.
*/
// Test if following character to the current one does not match with input parameter
BfNextNeq(c)
char c;
{
return BfNext() != c;
}
// Test if two characters are in the current position
BfWordEq(w)
int w;
{
return w == ((Bf() << 8) + BfNext()); // FIXME -- This is not transportable at all!
}
// Test if two characters are not in the current position
BfWordNeq(w)
int w;
{
return w != ((Bf() << 8) + BfNext()); // FIXME -- This is not transportable at all!
}
// Skip blanks
BfBlanks()
{
/********************************************************
while(BfEq(' ') || BfEq('\t')) // FIXME -- Optimize this
BfGet();
*********************************************************/
while(line[lptr] == ' ')
++lptr;
}
// Read a line into the buffer
BfRead()
{
eof = cpp_read();
}
// Discard the current contents
BfKill()
{
line[0] = lptr = 0;
}