-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunc.c
310 lines (269 loc) · 5.13 KB
/
func.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
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "defs.h"
#include "externs.h"
#include "protos.h"
struct t_func *func_tbl[256];
struct t_func *func_ptr;
char func_line[128];
char func_arg[8][10][80];
int func_idx;
/* ----
* do_func()
* ----
* .func pseudo
*/
void
do_func(int *ip)
{
if (pass == LAST_PASS)
println();
else {
/* error checking */
if (lablptr == NULL) {
error("No name for this function!");
return;
}
if (lablptr->refcnt) {
switch (lablptr->type) {
case MACRO:
fatal_error("Symbol already used by a macro!");
case FUNC:
fatal_error("Function already defined!");
return;
default:
fatal_error("Symbol already used by a label!");
return;
}
}
/* install this new function in the hash table */
if (!func_install(*ip))
return;
}
}
/* search a function */
int
func_look(void)
{
int hash;
/* search the function in the hash table */
hash = symhash();
func_ptr = func_tbl[hash];
while (func_ptr) {
if (!strcmp(&symbol[1], func_ptr->name))
break;
func_ptr = func_ptr->next;
}
/* ok */
if (func_ptr)
return (1);
/* didn't find a function with this name */
return (0);
}
/* install a function in the hash table */
int
func_install(int ip)
{
int hash;
/* mark the function name as reserved */
lablptr->type = FUNC;
/* check function name syntax */
if (strchr(&symbol[1], '.')) {
error("Invalid function name!");
return (0);
}
/* extract function body */
if (func_extract(ip) == -1)
return (0);
/* allocate a new func struct */
if ((func_ptr = (void *)malloc(sizeof(struct t_func))) == NULL) {
error("Out of memory!");
return (0);
}
/* initialize it */
strcpy(func_ptr->name, &symbol[1]);
strcpy(func_ptr->line, func_line);
hash = symhash();
func_ptr->next = func_tbl[hash];
func_tbl[hash] = func_ptr;
/* ok */
return (1);
}
/* extract function body */
int
func_extract(int ip)
{
char *ptr;
char c;
int i, arg, max_arg;
int end;
/* skip spaces */
while (isspace(prlnbuf[ip]))
ip++;
/* get function body */
ptr = func_line;
max_arg = 0;
end = 0;
i = 0;
while (!end) {
c = prlnbuf[ip++];
switch (c) {
/* end of line */
case ';':
case '\0':
*ptr++ = '\0';
end = 1;
break;
/* function arg */
case '\\':
*ptr++ = c;
i++;
c = prlnbuf[ip++];
if ((c < '1') || (c > '9')) {
error("Invalid function argument!");
return (-1);
}
arg = c - '1';
if (max_arg < arg)
max_arg = arg;
/* other */
default:
*ptr++ = c;
i++;
if (i == 127) {
error("Function line too long!");
return (-1);
}
break;
}
}
/* return the number of args */
return (max_arg);
}
/* extract function args */
int
func_getargs(void)
{
char c, *ptr, *line;
int arg, level, space, flag;
int i, x;
/* can not nest too much macros */
if (func_idx == 7) {
error("Too many nested function calls!");
return (0);
}
/* skip spaces */
while (isspace(*expr))
expr++;
/* function args must be enclosed in parenthesis */
if (*expr++ != '(')
return (0);
/* initialize args */
line = NULL;
ptr = func_arg[func_idx][0];
arg = 0;
for (i = 0; i < 9; i++)
func_arg[func_idx][i][0] = '\0';
/* get args one by one */
for (;;) {
/* skip spaces */
while (isspace(*expr))
expr++;
c = *expr++;
switch (c) {
/* empty arg */
case ',':
arg++;
ptr = func_arg[func_idx][arg];
if (arg == 9) {
error("Too many arguments for a function!");
return (0);
}
break;
/* end of line */
case ';':
case '\0':
error("Syntax error in function call!");
return (0);
/* end of function */
case ')':
return (1);
/* arg */
default:
space = 0;
level = 0;
flag = 0;
i = 0;
x = 0;
for (;;) {
if (c == '\0') {
if (flag == 0)
break;
else {
flag = 0;
c = *expr++;
continue;
}
}
else if (c == ';') {
break;
}
else if (c == ',') {
if (level == 0)
break;
}
else if (c == '\\') {
if (func_idx == 0) {
error("Syntax error!");
return (0);
}
c = *expr++;
if (c < '1' || c > '9') {
error("Invalid function argument index!");
return (0);
}
line = func_arg[func_idx - 1][c - '1'];
flag = 1;
c = *line++;
continue;
}
else if (c == '(') {
level++;
}
else if (c == ')') {
if (level == 0)
break;
level--;
}
if (space) {
if (c != ' ') {
while (i < x)
ptr[i++] = ' ';
ptr[i++] = c;
space = 0;
}
}
else if (c == ' ') {
space = 1;
}
else {
ptr[i++] = c;
}
if (i == 80) {
error("Invalid function argument length!");
return (0);
}
x++;
if (flag)
c = *line++;
else
c = *expr++;
}
ptr[i] = '\0';
expr--;
break;
}
}
}