-
Notifications
You must be signed in to change notification settings - Fork 9
/
globals.h
420 lines (389 loc) · 9.71 KB
/
globals.h
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* FILE: globals.h */
/*
* module : globals.h
* version : 1.120
* date : 11/15/24
*/
#ifndef GLOBALS_H
#define GLOBALS_H
#ifdef MALLOC_DEBUG
#include "rmalloc.h"
#endif
/* #define USE_KHASHL */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#include <setjmp.h>
#include <signal.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
/*
* Certain compilers are likely to compile for the Windows platform and that
* means that WINDOWS can be set. Other compilers need to set this explicitly,
* if so desired.
*/
#if defined(_MSC_VER) || defined(__MINGW64_VERSION_MAJOR) || defined(__TINYC__)
#define WINDOWS
#endif
/*
* The system call doesn't work when Windows is run in S-mode, so it might just
* as well be disabled, regardless of the compiler that is used. It is a bit of
* a security leak anyways.
*/
#ifdef WINDOWS
#define WINDOWS_S
#endif
#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h> /* pollute name space as much as possible */
#include <io.h> /* also import deprecated POSIX names */
#ifdef __TINYC__
#define strtoll _strtoi64 /* tcc 0.9.27 lacks strtoll */
#endif
#ifdef _MSC_VER
#pragma warning(disable: 4244 4267 4996)
#define kh_packed /* forget about __attribute__ ((packed)) */
#endif
#else
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#endif
#ifndef NOBDW
#ifdef _MSC_VER
#include "gc-8.2.8/include/gc.h"
#else
#include <gc.h>
#endif
#else
#include "gc.h"
#endif
#include "kvec.h"
#ifdef USE_KHASHL
#include "khashl.h"
#else
#include "khash.h"
#endif
#ifdef NOBDW
#define nodetype(n) env->memory[n].op
#define nodeleng(n) env->memory[n].len
#define nodevalue(n) env->memory[n].u
#define nextnode1(n) env->memory[n].next
#define nextnode2(n) env->memory[nextnode1(n)].next
#define nextnode3(n) env->memory[nextnode2(n)].next
#define nextnode4(n) env->memory[nextnode3(n)].next
#define nextnode5(n) env->memory[nextnode4(n)].next
#else
#define nodetype(p) (p)->op
#define nodevalue(p) (p)->u
#define nextnode1(p) (p)->next
#define nextnode2(p) (nextnode1(p))->next
#define nextnode3(p) (nextnode2(p))->next
#define nextnode4(p) (nextnode3(p))->next
#define nextnode5(p) (nextnode4(p))->next
#ifdef TRACEGC
#undef TRACEGC
#endif
#endif
#include "macros.h"
/* settings for cflags */
#define IS_ACTIVE 1 /* prevent recursion */
#define IS_USED 2 /* multiple inlining */
#define IS_PRINTED 4 /* print of contents */
/* configure */
#define SHELLESCAPE '$'
#define INPSTACKMAX 10
#define INPLINEMAX 255
#define BUFFERMAX 80 /* smaller buffer */
#define HELPLINEMAX 72
#define MAXNUM 40 /* even smaller buffer */
#define FILENAMEMAX 14
#define DISPLAYMAX 10 /* nesting in HIDE & MODULE */
#define INIECHOFLAG 0
#define INIAUTOPUT 1
#define INITRACEGC 1
#define INIUNDEFERROR 0
#define INIWARNING 1
/* installation dependent */
#define SETSIZE (int)(CHAR_BIT * sizeof(uint64_t)) /* from limits.h */
#define MAXINT_ INT64_MAX /* from stdint.h */
/* symbols from getsym */
enum {
ILLEGAL_,
COPIED_,
USR_,
ANON_FUNCT_,
BOOLEAN_,
CHAR_,
INTEGER_,
SET_,
STRING_,
LIST_,
FLOAT_,
FILE_,
BIGNUM_,
LIST_PRIME_,
LIBRA,
EQDEF,
HIDE,
IN__,
MODULE_,
PRIVATE,
PUBLIC,
CONST_
};
typedef enum {
OK,
IGNORE_OK,
IGNORE_PUSH,
IGNORE_POP,
IMMEDIATE,
POSTPONE
} Flags;
typedef enum {
ABORT_NONE,
ABORT_RETRY,
ABORT_QUIT
} Abort;
/* types */
typedef unsigned char Operator; /* opcode / datatype */
typedef struct Env *pEnv;
typedef void (*proc_t)(pEnv); /* procedure */
#ifdef NOBDW
typedef unsigned Index;
#else
typedef struct Node *Index;
#endif
typedef union {
int64_t num; /* USR, BOOLEAN, CHAR, INTEGER */
proc_t proc; /* ANON_FUNCT */
uint64_t set; /* SET */
char *str; /* STRING */
Index lis; /* LIST */
double dbl; /* FLOAT */
FILE *fil; /* FILE */
int ent; /* SYMBOL */
} Types;
#ifdef NOBDW
typedef struct Node {
unsigned op : 4,
len : 28; /* length of string */
Index next;
Types u;
} Node;
#else
typedef struct Node {
Operator op;
Index next;
Types u;
} Node;
#endif
typedef struct Token {
Operator op;
int x, y, pos;
Types u;
} Token;
typedef struct Entry {
char *name;
unsigned char is_user, flags, is_ok, is_root, is_last, qcode, nofun, cflags;
union {
Index body;
proc_t proc;
} u;
} Entry;
#ifdef USE_KHASHL
KHASHL_MAP_INIT(KH_LOCAL, symtab_t, symtab, const char *, int, kh_hash_str,
kh_eq_str)
KHASHL_MAP_INIT(KH_LOCAL, funtab_t, funtab, uint64_t, int, kh_hash_uint64,
kh_eq_generic)
#else
KHASH_MAP_INIT_STR(Symtab, int)
KHASH_MAP_INIT_INT64(Funtab, int)
#endif
typedef struct Env {
jmp_buf finclude; /* return point in finclude */
double nodes; /* statistics */
double avail;
double collect;
double calls;
double opers;
double dbl; /* numerics */
int64_t num;
char *str; /* string */
clock_t startclock; /* main */
char **g_argv; /* command line */
char *filename; /* first include file */
char *homedir; /* HOME or HOMEPATH */
char *mod_name; /* name of module */
vector(char *) *pathnames; /* pathnames to be searched when including */
vector(char) *string; /* value */
vector(char) *pushback; /* push back buffer */
vector(Token) *tokens; /* read ahead table */
vector(Entry) *symtab; /* symbol table */
#ifdef USE_KHASHL
symtab_t *hash; /* hash tables that index the symbol table */
funtab_t *prim;
#else
khash_t(Symtab) *hash;
khash_t(Funtab) *prim;
#endif
Types bucket; /* used by NEWNODE defines */
#ifdef NOBDW
clock_t gc_clock;
Node *memory; /* dynamic memory */
Index conts, dump, dump1, dump2, dump3, dump4, dump5, inits;
#endif
Index prog, stck;
#ifdef COMPILER
FILE *declfp, *outfp;
#endif
int g_argc; /* command line */
int hide_stack[DISPLAYMAX];
struct {
char *name;
int hide;
} module_stack[DISPLAYMAX];
Operator sym; /* symbol */
unsigned char inlining;
unsigned char autoput;
unsigned char autoput_set;
unsigned char echoflag;
unsigned char tracegc;
unsigned char undeferror;
unsigned char undeferror_set;
unsigned char debugging;
unsigned char ignore;
unsigned char overwrite;
unsigned char printing;
unsigned char finclude_busy;
unsigned char flibrary_busy;
unsigned char variable_busy;
signed char bytecoding; /* BDW only */
signed char compiling; /* BDW only */
} Env;
typedef struct table_t {
proc_t proc;
char *name;
} table_t;
/* GOOD REFS:
005.133l H4732 A LISP interpreter in C
Manna p139 recursive Ackermann SCHEMA
OTHER DATA TYPES
WORD = "ABCD" - up to four chars
LIST of SETs of char [S0 S1 S2 S3]
LISTS - binary tree [left right]
" with info [info left right] "
STRING of 32 chars = 32 * 8 bits = 256 bits = bigset
CHAR = 2 HEX
32 SET = 2 * 16SET
*/
/* Public procedures: */
/* main.c */
void abortexecution_(int num);
void fatal(char *str);
/* interp.c */
void exeterm(pEnv env, Index n);
/* scan.c */
void inilinebuffer(pEnv env);
int getch(pEnv env);
void ungetch(int ch);
void error(char *str);
int include(pEnv env, char *name);
int getsym(pEnv env, int ch);
/* utils.c */
Index newnode(pEnv env, Operator o, Types u, Index r);
Index newnode2(pEnv env, Index n, Index r);
void my_memoryindex(pEnv env);
void my_memorymax(pEnv env);
#ifdef NOBDW
void inimem1(pEnv env, int status);
void inimem2(pEnv env);
void printnode(pEnv env, Index p);
void my_gc(pEnv env);
#endif
/* error.c */
void execerror(pEnv env, char *message, char *op);
/* factor.c */
int readfactor(pEnv env, int ch, int *rv); /* read a JOY factor */
int readterm(pEnv env, int ch);
/* module.c */
void savemod(int *hide, int *modl, int *hcnt);
void undomod(int hide, int modl, int hcnt);
void initmod(pEnv env, char *name);
void initpriv(pEnv env);
void stoppriv(void);
void exitpriv(void);
void exitmod(void);
char *classify(pEnv env, char *name);
int qualify(pEnv env, char *name);
/* optable.c */
int tablesize(void);
char *nickname(int ch);
char *opername(int o);
int operindex(pEnv env, proc_t proc);
void inisymboltable(pEnv env); /* initialise */
void addsymbol(pEnv env, Entry ent, int index);
/* print.c */
void print(pEnv env);
/* repl.c */
void repl(pEnv env);
/* setraw.c */
void SetRaw(pEnv env);
/* symbol.c */
int lookup(pEnv env, char *name);
int enteratom(pEnv env, char *name);
int compound_def(pEnv env, int ch);
/* undefs.c */
void hide_inner_modules(pEnv env, int flag);
/* write.c */
void writefactor(pEnv env, Index n, FILE *fp);
void writeterm(pEnv env, Index n, FILE *fp);
#ifdef BYTECODE
/* bytecode.c */
void bytecode(pEnv env, Node *list);
void initbytes(pEnv env);
void exitbytes(pEnv env);
/* compeval.c */
void compeval(pEnv env, FILE *fp);
/* computil.c */
Node *reverse(Node *cur);
char *outputfile(char *inputfile, char *suffix);
/* dumpbyte.c */
void dumpbyte(pEnv env, FILE *fp);
/* optimize.c */
void optimize(pEnv env, FILE *fp);
/* readbyte.c */
void readbyte(pEnv env, FILE *fp);
unsigned char *readfile(FILE *fp);
#endif
#ifdef COMPILER
/* compiler.c */
void printnode(pEnv env, Node *node);
void printstack(pEnv env);
void compile(pEnv env, Node *node);
void initcompile(pEnv env);
void exitcompile(pEnv env);
/* computil.c */
Node *reverse(Node *cur);
char *outputfile(char *inputfile, char *suffix);
/* identify.c */
const char *identifier(const char *str);
const char *unidentify(const char *str);
/* instance.c */
int instance(pEnv env, char *name, int qcode);
/* outfiles.c */
void initout(void);
FILE *nextfile(void);
void closefile(FILE *fp);
void printout(FILE *fp);
void closeout(void);
/* readtemp.c */
int testtemp(char *file);
void readtemp(pEnv env, char *file, Node *nodes[], int found, int seqnr);
#endif
#endif