/
uforth-linux.c
338 lines (300 loc) · 6.97 KB
/
uforth-linux.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
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
#include <readline/readline.h>
#include <readline/history.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/time.h>
#include "uforth.h"
#include "uforth-ext.h"
FILE *OUTFP;
#define CONFIG_IMAGE_FILE "uforth.img"
/*
#define INITIAL_DICT {DICT_VERSION, MAX_DICT_CELLS, 0, 0, 1, {0}}
struct dict ramdict = INITIAL_DICT;
*/
struct dict *dict;
struct timeval start_tv;
uint8_t rxc(void) {
return getchar();
}
/* A static variable for holding the line. */
static char *line_read = (char *)NULL;
/* Read a string, and return a pointer to it.
Returns NULL on EOF. */
char *
rl_gets ()
{
/* If the buffer has already been allocated,
return the memory to the free pool. */
if (line_read)
{
free (line_read);
line_read = (char *)NULL;
}
/* Get a line from the user. */
line_read = readline ("");
/* If the line has any text in it,
save it on the history. */
if (line_read && *line_read)
add_history (line_read);
return (line_read);
}
void rxgetline(char* str) {
fgets(str,128,stdin);
}
void txc(uint8_t c) {
fputc(c, OUTFP);
fflush(OUTFP);
}
void txs(char* s, int cnt) {
fwrite(s,cnt,1,OUTFP);
fflush(OUTFP);
}
#define txs0(s) txs(s,strlen(s))
static FILE *cfp;
bool config_open_w(char* f) {
cfp = fopen(f, "w+");
return (cfp != NULL);
}
bool config_open_r(char* f) {
cfp = fopen(f, "r");
return (cfp != NULL);
}
bool config_write(char *src, uint16_t size) {
fwrite((char*)src, size, 1, cfp);
return 1;
}
bool config_read(void *dest) {
uint16_t size;
fscanf(cfp,"%d\n",(int*)&size);
fread((char*)dest, size, 1, cfp);
return 1;
}
bool config_close(void) {
fclose(cfp);
return 1;
}
void interpret_from(FILE *fp);
#include <dlfcn.h>
void *dlhand;
uforth_stat c_handle(void) {
DCELL r2, r1 = dpop();
FILE *fp;
static char buf[80*2];
switch(r1) {
case UF_DLFFI:
{
typedef DCELL (*arbitrary)();
arbitrary my_function;
*(DCELL*)(&my_function) = dpop();
DCELL pcnt;
pcnt = dpop(); /* parameter count */
switch (pcnt) {
case 1:
dpush(my_function(dpop()));
break;
case 2:
dpush(my_function(dpop(),dpop()));
break;
case 3:
dpush(my_function(dpop(),dpop(),dpop()));
break;
case 4:
dpush(my_function(dpop(),dpop(),dpop(),dpop()));
break;
case 5:
dpush(my_function(dpop(),dpop(),dpop(),dpop()));
break;
}
}
break;
case UF_DLSYM:
{
char *s = uforth_next_word();
strncpy(buf, s, uforth_iram->tibwordlen+1);
buf[(uforth_iram->tibwordlen)+1] = '\0';
r2 = (DCELL)dlsym(dlhand,buf);
dpush(r2);
}
break;
case UF_MS: /* milliseconds */
{
struct timeval tv;
gettimeofday(&tv,0);
tv.tv_sec -= start_tv.tv_sec;
tv.tv_usec -= start_tv.tv_usec;
r2 = (tv.tv_sec * 1000) + (tv.tv_usec/1000);
dpush(r2);
}
break;
case UF_EMIT: /* emit */
txc(dpop()&0xff);
break;
case UF_KEY: /* key */
dpush((CELL)rxc());
break;
case UF_SAVE_IMAGE: /* save image */
{
int dict_size= (dict_here())*sizeof(CELL);
char *s = uforth_next_word();
strncpy(buf, s, uforth_iram->tibwordlen+1);
buf[(uforth_iram->tibwordlen)+1] = '\0';
printf("Saving raw dictionary into %s\n", buf);
fp = fopen(buf, "w+");
fprintf(fp,"%ld\n",(int)(dict_here())*sizeof(CELL));
fwrite(dict, dict_size,1,fp);
fclose(fp);
char *hfile = malloc(strlen(buf) + 3);
strcpy(hfile,buf);
strcat(hfile,".h");
printf("Saving raw dictionary into %s\n", hfile);
fp = fopen(hfile,"w");
free(hfile);
fprintf(fp,"struct dict flashdict = {%d,%d,%d,%d,%d,{\n",
dict->version,dict->max_cells,dict->here,dict->last_word_idx,
dict->varidx);
int i;
for(i = 0; i < dict_size-1; i++) {
fprintf(fp, "0x%0X,",dict->d[i]);
}
fprintf(fp, "%0X",dict->d[dict_size-1]);
fprintf(fp,"\n}};\n");
fclose(fp);
}
break;
case UF_READB:
{
char b;
r2=dpop();
read(r2,&b,1);
dpush(b);
}
break;
case UF_WRITEB:
{
char b;
r2=dpop();
b=dpop();
dpush(write(r2,&b,1));
}
break;
case UF_CLOSE:
dpush(close(dpop()));
break;
case UF_OPEN:
{
char *s;
r1 = dpop();
r2 = dpop();
s = (char*)&uforth_ram[r2+1];
strncpy(buf,s, uforth_ram[r2]);
buf[uforth_ram[r2]] = '\0';
dpush(open(buf, r1));
}
break;
case UF_INCLUDE: /* include */
{
char *s = uforth_next_word();
strncpy(buf,s, uforth_iram->tibwordlen+1);
buf[uforth_iram->tibwordlen] = '\0';
printf(" Loading %s\n",buf);
fp = fopen(buf, "r");
if (fp != NULL) {
interpret_from(fp);
fclose(fp);
} else {
printf("File not found: <%s>\n", buf);
}
}
break;
default:
return c_ext_handle_cmds(r1);
}
return OK;
}
static char linebuf[128];
char *line;
void interpret_from(FILE *fp) {
int stat;
int16_t lineno = 0;
while (!feof(fp)) {
++lineno;
if (fp == stdin) txs0(" ok\r\n");
if (fp == stdin) {
line=rl_gets(); if (line==NULL) exit(0);
} else {
if (fgets(linebuf,128,fp) == NULL) break;
line = linebuf;
}
if (line[0] == '\n' || line[0] == '\0') continue;
stat = uforth_interpret(line);
switch(stat) {
case E_NOT_A_WORD:
fprintf(stdout," line: %d: ", lineno);
txs0("Huh? >>> ");
txs(&uforth_iram->tib[uforth_iram->tibwordidx],uforth_iram->tibwordlen);
txs0(" <<< ");
txs(&uforth_iram->tib[uforth_iram->tibwordidx + uforth_iram->tibwordlen],
uforth_iram->tibclen -
(uforth_iram->tibwordidx + uforth_iram->tibwordlen));
txs0("\r\n");
break;
case E_ABORT:
txs0("Abort!:<"); txs0(line); txs0(">\n");
break;
case E_STACK_UNDERFLOW:
txs0("Stack underflow!\n");
break;
case E_DSTACK_OVERFLOW:
txs0("Stack overflow!\n");
break;
case E_RSTACK_OVERFLOW:
txs0("Return Stack overflow!\n");
break;
case OK:
break;
default:
txs0("Ugh\n");
break;
}
}
}
#include <sys/types.h>
#include <sys/stat.h>
#define MAX_DICT_CELLS (65535)
int main(int argc, char* argv[]) {
dict = malloc(sizeof(CELL)*MAX_DICT_CELLS) + sizeof(struct dict);
dict->version = DICT_VERSION;
dict->word_size = sizeof(uint64_t);
dict->max_cells = MAX_DICT_CELLS;
dict->here = 0;
dict->last_word_idx = 0;
dict->varidx = 1;
gettimeofday(&start_tv,0);
dlhand = dlopen(NULL, RTLD_NOW);
uforth_init();
OUTFP = stdout;
if (argc < 2) {
FILE *fp;
uforth_load_prims();
printf(" Loading ./init.f\n");
fp = fopen("./init.f", "r");
if (fp != NULL)
interpret_from(fp);
fclose(fp);
} else {
if (config_open_r(argv[1])) {
if (!config_read(dict))
exit(1);
config_close();
}
}
c_ext_init();
uforth_interpret("init");
interpret_from(stdin);
return 0;
}