/
parser.c
240 lines (188 loc) · 6.19 KB
/
parser.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
/*
* parser.c - parser
*
* Written By: tyru <tyru.exe@gmail.com>
* Last Change: 2009-09-09.
*
*/
#include "parser.h"
#include "util.h"
#include "forth.h" // accessing to interp, must know full structure of ForthInterp.
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// NOTE:
// - max_size <= capacity of token->tokstr
// - this free() token if not NULL.
// XXX
// check null byte.
// TODO
// fallback to other types.
bool
forth_get_token_from_src(
ForthInterp *interp,
char *token,
size_t max_size)
{
bool found = false;
if (token == NULL) goto FORTH_ERR_ARGS;
if (interp->src == NULL) goto FORTH_ERR_ARGS;
// skip the non-word character.
while (1) {
if (interp->src[interp->cur_pos] == '\0')
goto FORTH_ERR_EOF;
if (! is_skip_char(interp->src[interp->cur_pos]))
break;
interp->cur_pos++;
}
// get token.
for (; interp->cur_pos < interp->src_len; interp->cur_pos++) {
int c = interp->src[interp->cur_pos];
if (is_skip_char(c))
continue;
// string
if (c == '"') {
char *begin = interp->src + interp->cur_pos;
char *cur_srch_pos = begin + 1; // position to begin to search.
char *end; // end of string.
while (1) {
end = strchr(cur_srch_pos, '"');
/* not found '"' */
if (end == NULL) {
interp->errid = FORTH_ERR_UNCLOSED_STR;
return false;
}
/* found it */
else if (*(end - 1) != '\\') { // if not escaped string.
size_t len = end - begin + 1;
ASSERT(interp, begin < end);
if (CAST(size_t, len) > max_size)
goto FORTH_ERR_OVERFLOW;
// copy string with double quotes.
strncpy(token, begin, len);
token[len] = '\0';
interp->cur_pos += len;
break;
}
/* found but it was escaped. search again. */
else {
cur_srch_pos = end;
}
}
if (! is_string(token)) {
fprintf(stderr, "%s: ", token);
interp->errid = FORTH_ERR_BAD_STRING;
return false;
}
forth_debugf(interp, "string token found: %s\n", token);
found = true;
goto END_PARSING;
}
// digit
if (isdigit(c)) {
char *begin = interp->src + interp->cur_pos;
char *end;
while (is_digit_char(interp->src[interp->cur_pos])) {
if (interp->cur_pos >= interp->src_len)
goto END_PARSING;
else if (interp->src[interp->cur_pos] == '\0')
goto END_PARSING;
else
interp->cur_pos++;
}
end = interp->src + interp->cur_pos;
ASSERT(interp, begin < end);
if (CAST(size_t, end - begin) > max_size)
goto FORTH_ERR_OVERFLOW;
// copy token to token.
strncpy(token, begin, end - begin);
token[end - begin] = '\0';
if (! is_digit(token)) {
fprintf(stderr, "%s: ", token);
interp->errid = FORTH_ERR_BAD_DIGIT;
return false;
}
forth_debugf(interp, "digit token found: %s\n", token);
found = true;
goto END_PARSING;
}
// or something word.
if (! is_skip_char(c)) {
char *begin = interp->src + interp->cur_pos;
char *end;
while (is_word_char(interp->src[interp->cur_pos])) {
if (interp->cur_pos >= interp->src_len)
goto END_PARSING;
else if (interp->src[interp->cur_pos] == '\0')
goto END_PARSING;
else
interp->cur_pos++;
}
end = interp->src + interp->cur_pos;
ASSERT(interp, begin < end);
if (CAST(size_t, end - begin) > max_size)
goto FORTH_ERR_OVERFLOW;
// copy token to token.
strncpy(token, begin, end - begin);
token[end - begin] = '\0';
// no checking.
forth_debugf(interp, "undefined token found: %s\n", token);
found = true;
goto END_PARSING;
}
// you would not reach this block...
ASSERT(interp, 0);
}
END_PARSING:
if (found) {
// there was no error.
interp->errid = FORTH_ERR_NOERR;
return true;
}
else {
interp->errid = FORTH_ERR_NOT_FOUND_TOKEN;
return false;
}
FORTH_ERR_EOF:
interp->errid = FORTH_ERR_EOF;
token[0] = '\0';
return false;
/* fatal error handling */
FORTH_ERR_ARGS:
forth_die(interp, "forth_get_token_from_src", FORTH_ERR_ARGS);
/* NOTREACHED */
// gcc won't read above!
return false;
FORTH_ERR_OVERFLOW:
forth_die(interp, "forth_get_token_from_src", FORTH_ERR_OVERFLOW);
/* NOTREACHED */
return false;
}
// get token, convert it, push it to interp->word.
bool
forth_get_word(ForthInterp *interp)
{
char token[interp->max_word_len]; // c99
ForthWord word;
word_init(&word);
// parse interp->src
// NOTE: token will be allocated.
bool success = forth_get_token_from_src(interp, token, interp->max_word_len);
if (success) {
forth_debugf(interp, "read [%s].\n", token);
// convert: char* -> ForthWord
forth_token2word(interp, token, &word);
// push it.
forth_debugf(interp, "push![%s]\n", token);
stack_push(interp->word_stack, &word);
}
// don't free pushed word! stack_push() copies only all bytes of struct.
// word_destruct(&word);
// to trap EOF outside of this function,
// this func must return this value.
// (do not die in this func!)
return success;
// 'word' at stack area is free()d but copied to interp->word_stack.
}