-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexa.c
230 lines (197 loc) · 5.21 KB
/
lexa.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
/*
* lexa.c
*
* Copyright 2012-2014 Zdeněk Janeček <[email protected]>
*
* 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 of the License, or
* 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, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#ifdef __linux__
#include <stdio_ext.h>
#endif // __linux__
#include "lexa.h"
#include "symbols.h"
#include "tools.h"
const char *funs_names[FUNS_COUNT] =
{
"+", "-", "*", "/", "QUOTE", "SET", "HELP", "QUIT",
"CAR", "CDR", "ABOUT", "PRINT", "DEFUN", "=", "==", "!=",
">=", "<=", "&", "|", "IF", "LIST"
};
char lchar;
FILE *file = NULL;
static atom csym;
int is_operator(char o)
{
if (o == '+' || o == '*' || o == '-' || o == '/' || o == '=' ||
o == '<' || o == '>' || o == '!' || o == '&' || o == '|')
return OK_CODE;
else
return END_CODE;
}
// sets new stream and return old
lexa_state lexa_init(lexa_state *s)
{
lexa_state state;
state.stream = file;
state.lchar = lchar;
state.csym = csym;
if (s != NULL) file = s->stream;
if (s != NULL) lchar = s->lchar;
if (s != NULL) csym = s->csym;
return state;
}
#ifdef __linux__
void lexa_flush()
{
lchar = ' ';
__fpurge(file);
}
#endif // __linux__
const char *get_function_name(int id)
{
return funs_names[id];
}
// Pouze vrací aktuálně zpracovaný symbol.
void lexa_get(atom *sym)
{
assert(sym);
memcpy(sym, &csym, sizeof(atom));
}
void write_atom_fce(atom *sym, enum ftype type)
{
sym->type = AT_FCE;
sym->value = type;
}
void write_atom_var(atom *sym, char *value)
{
sym->type = AT_VAR;
strcpy(sym->string, value);
}
void write_atom_num(atom *sym, int value)
{
sym->type = AT_NUM;
sym->value = value;
}
void write_atom_quote(atom *sym)
{
sym->type = AT_QUOTE;
}
// Přesune se v řetězci na další symbol, zapíše na
// dodanou adresu tělo struktury atom.
int lexa_next(atom *sym)
{
int ival;
int i, j;
char buf[32];
char *ptr;
// přeskočit bílé znaky
while (isspace(lchar))
lchar = fgetc(file);
// konec řetězce
if (lchar == EOF)
return END_CODE;
if (isdigit(lchar))
{
// we already have one character
buf[0] = lchar;
ptr = (char *) (buf + 1);
while (isdigit(lchar = fgetc(file)))
*ptr++ = lchar;
*ptr = 0;
if (isspace(lchar) && (lchar == '(') && (lchar == ')'))
{
sprintf(error_message, "invalid number\n");
return ERROR_CODE;
}
ival = (int) strtol((char *) buf, NULL, 10);
write_atom_num(&csym, ival);
}
else if (lchar == '(')
{
csym.type = AT_LBRACKET;
lchar = fgetc(file);
}
else if (lchar == ')')
{
csym.type = AT_RBRACKET;
lchar = fgetc(file);
}
else if (isalpha(lchar) || is_operator(lchar) || lchar == '_' || lchar == '#')
{
buf[0] = lchar;
ptr = (char *) (buf + 1);
while (isalpha(lchar = fgetc(file)) || is_operator(lchar) || isdigit(lchar) || lchar == '_' || lchar == '#')
*ptr++ = lchar;
*ptr = 0;
// unary operator
if ((buf[0] == '+' || buf[0] == '-') && isdigit(buf[1]))
{
ival = (int) strtol((char *) buf, &ptr, 10);
if (*ptr != 0)
{
sprintf(error_message, "invalid number\n");
return ERROR_CODE;
}
write_atom_num(&csym, ival);
if (sym != NULL)
memcpy(sym, &csym, sizeof(atom));
return OK_CODE;
}
// is it function?
for (i = 0; i < FUNS_COUNT; i++)
{
ptr = (char *) buf;
j = 0;
while (funs_names[i][j] && toupper(*ptr) == funs_names[i][j])
{
ptr++;
j++;
}
if (*ptr == 0 && funs_names[i][j] == 0)
{
write_atom_fce(&csym, i);
if (sym != NULL)
memcpy(sym, &csym, sizeof(atom));
return OK_CODE;
}
}
// create string from the name
write_atom_var(&csym, supper(buf));
}
else if (lchar == '\'')
{
write_atom_quote(&csym);
lchar = fgetc(file);
}
else
{
buf[0] = lchar;
ptr = (char *) (buf + 1);
while (!isspace(lchar = fgetc(file)) && lchar != '(' && lchar != ')')
*ptr++ = lchar;
*ptr = 0;
strcpy(csym.string, buf);
csym.type = AT_UNKNOWN;
}
if (sym != NULL)
memcpy(sym, &csym, sizeof(atom));
return OK_CODE;
}