File: coree.c

package info (click to toggle)
yforth 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 396 kB
  • ctags: 788
  • sloc: ansic: 4,426; makefile: 23
file content (352 lines) | stat: -rw-r--r-- 7,393 bytes parent folder | download
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* yForth? - A Forth interpreter written in ANSI C
 * Copyright (C) 2012 Luca Padovani
 *
 * 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
 * ------------------------------------------------------------------------
 * Module name:     coree.c
 * Abstract:        Core extension word set
 */

#include "yforth.h"

#include <string.h>
#include <stdio.h>
#include "core.h"
#include "coree.h"
#include "double.h"
#include "locals.h"
#include "block.h"
#include "search.h"

/**************************************************************************/
/* VARIABLES **************************************************************/
/**************************************************************************/

Char * _pad;

/**************************************************************************/
/* WORDS ******************************************************************/
/**************************************************************************/

void _dot_paren() {
    *--sp = ')';
    _word();
    _count();
    _type();
}

void _dot_r() {
	register Cell u = *sp++;
	_s_to_d();
	*--sp = u;
	_d_dot_r();
}

void _zero_not_equals() {
	sp[0] = FFLAG(sp[0] != 0);
}

void _zero_greater() {
    sp[0] = FFLAG(sp[0] > 0);
}

void _two_to_r() {
    rp -= 2;
    rp[0] = *sp++;
	rp[1] = *sp++;
}

void _two_r_from() {
    sp -= 2;
    sp[0] = *rp++;
    sp[1] = *rp++;
}

void _two_r_fetch() {
    sp -= 2;
    sp[0] = rp[0];
    sp[1] = rp[1];
}

void _colon_no_name() {
    register struct word_def *def;
    _align();
    def = (struct word_def *) _dp;
	def->name = 0;
	def->link = 0;
	def->class = A_COLON;
	_dp += sizeof(struct word_def) - sizeof(Cell);
	_state = COMPILE;
	*--sp = (Cell) def;
	init_locals();
}

void _not_equals() {
	sp[1] = FFLAG(sp[0] != sp[1]);
    sp++;
}

void _question_do() {
    compile_cell((Cell) _paren_question_do_paren);
    *--sp = (Cell) _dp;
    compile_cell(0);
    *--sp = (Cell) _dp;
    *--sp = 1;  /* e' un ?do */
}

void _paren_question_do_paren() {
    if (sp[0] == sp[1]) ip += 1 + (Cell) *ip;
    else {
		*--rp = *sp++;
		*--rp = *sp++;
		ip++;
	}
}

void _again() {
	register Cell *dest = (Cell *) *sp++;
	compile_cell((Cell) _branch);
	compile_cell(dest - ((Cell *) _dp) - 1);
}

void _c_quote() {
	register Char *cur;
	register Cell *patch;
	compile_cell((Cell) _branch);
	patch = (Cell *) _dp;
	compile_cell(0);
	cur = _dp;
	*--sp = '"';
	_word();
	sp++;
	_dp = (Char *) WORD_PTR(_dp);
    *patch = ((Cell *) _dp) - patch - 1;
    compile_cell((Cell) _do_literal);
    compile_cell((Cell) cur);
}

void _compile_comma() {
    compile_word((struct word_def *) *sp++);
}

void _erase() {
    register UCell u = (UCell) *sp++;
    register Char *addr = (Char *) *sp++;
    if (u) memset(addr, 0, u);
}

void _false() {
    *--sp = FFLAG(0);
}

void _hex() {
	_base = 16;
}

void _marker() {
	struct voc_marker vm;
	save_vocabulary(&vm);
	create_definition(A_MARKER);
	memcpy(_dp, &vm, sizeof(struct voc_marker));
	_dp += ALIGN_PTR(sizeof(struct voc_marker));
	mark_word(_last);
}

void _nip() {
    sp[1] = sp[0];
    sp++;
}

void _parse() {
    register Char delim = (Char) *sp;
    register Char *orig = &_input_buffer[_to_in];
    register int i = 0;
    while (_to_in < _in_input_buffer && _input_buffer[_to_in] != delim) {
        _to_in++;
        i++;
    }
    *sp = (Cell)orig;
    *--sp = i;

    if (_to_in < _in_input_buffer) _to_in++;
}

void _pick() {
	sp[0] = sp[sp[0] + 1];
}

void _refill() {
	if (_b_l_k != 0) {
		current_block = _b_l_k++;
		_to_in = 0;
		*--sp = _b_l_k;
		_block();
		_input_buffer = (Char *) *sp++;
		_in_input_buffer = BLOCK_SIZE;
		*sp = FFLAG(_b_l_k && _input_buffer != NULL);
	} else if (_source_id == 0) {
        *--sp = (Cell) _tib;
		*--sp = tib_size;
        _accept();
		_input_buffer = _tib;
        _in_input_buffer = *sp;
        _to_in = 0;
        *sp = FFLAG(1);
    } else if (_source_id == -1) {
		*--sp = FFLAG(0);
	} else if (_env_file) {
		if (fgets(_input_buffer, FILE_BUFFER_SIZE, (FILE *) _source_id)) {
			_in_input_buffer = strlen(_input_buffer);
			if (_in_input_buffer && _input_buffer[_in_input_buffer - 1] == '\n')
				_in_input_buffer--;
			_to_in = 0;
			*--sp = FFLAG(1);
		} else *--sp = FFLAG(0);
	} else *--sp = FFLAG(0);
}

void _restore_input() {
	sp++;
	_b_l_k = *sp++;
	_to_in = *sp++;
	_in_input_buffer = *sp++;
	_input_buffer = (Char *) *sp++;
	_source_id = *sp++;
	if (_source_id == 0) {
	} else if (_source_id == -1) {
	} else {
	}
	*--sp = FFLAG(1);
}

void _roll() {
	register Cell u = *sp++;
	register Cell xu = sp[u];
	register int i;
	for (i = u; i > 0; i--) sp[i] = sp[i - 1];
	sp[0] = xu;
}

void _save_input() {
	if (_source_id == 0) {
	} else if (_source_id == -1) {
	} else {
	}
	*--sp = _source_id;
	*--sp = (Cell) _input_buffer;
	*--sp = _in_input_buffer;
	*--sp = _to_in;
	*--sp = _b_l_k;
	*--sp = 5;
}

void _true() {
	*--sp = FFLAG(1);
}

void _tuck() {
	sp--;
	sp[0] = sp[1];
	sp[1] = sp[2];
	sp[2] = sp[0];
}

void _u_dot_r() {
    register Cell r = *sp++;
	*--sp = 0;
	_less_number_sign();
	_number_sign_s();
	_number_sign_greater();
	if (sp[0] < r) {
		sp--;
		sp[0] = r - sp[1];
		_spaces();
	}
    _type();
    putchar(' ');
}

void _u_greater_than() {
    sp[1] = FFLAG((UCell) sp[1] > (UCell) sp[0]);
	sp++;
}

void _unused() {
	*--sp = (dspace_size - (_dp - dp0)) * sizeof(Cell);
}

void _within() {
	register Cell n3 = *sp++;
    register Cell n2 = *sp++;
    register Cell n1 = *sp;
    sp[0] = FFLAG((n2 < n3 && (n2 <= n1 && n1 < n3)) ||
                  (n2 > n3 && (n2 <= n1 || n1 < n3)));
}

void _backslash() {
    _to_in = _in_input_buffer;
}

void _bracket_compile() {
	*--sp = ' ';
    _word();
    sp++;
    compile_word(search_word(_dp + 1, *_dp));
}

void _value() {
	create_definition(A_VALUE);
	compile_cell((Cell) sp[0]);
	sp++;
	mark_word(_last);
}

void _paren_write_value_paren() {
	register Cell *p = (Cell *) (*ip++);
	*p = *sp++;
}

void _to() {
	_b_l();
	_word();
	_find();
	if (*sp++) {
		register struct word_def *xt = (struct word_def *) *sp++;
		if ((xt->class & A_WORD) == A_VALUE) {
			if (_state == INTERPRET) xt->func[0] = (pfp) *sp++;
			else {
				compile_cell((Cell) _paren_write_value_paren);
				compile_cell((Cell) &xt->func[0]);
			}
		} else if (xt->class & A_WORD == A_LOCAL && _state == COMPILE) {
			compile_cell((Cell) _paren_write_local_paren);
			compile_cell((Cell) xt->func[0]);
		} else {
			/* ... */
		}
	} else sp++;
}

void _paren_marker_paren() {
	exec_marker((struct voc_marker *) ip++);
}

/**************************************************************************/
/* AUXILIARY FUNCTIONS ****************************************************/
/**************************************************************************/

void exec_marker(struct voc_marker *vm) {
	load_vocabulary(vm);
}