forked from Wodan58/Joy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.h
282 lines (260 loc) · 6.61 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
/* FILE: globals.h */
/*
* module : globals.h
* version : 1.98
* date : 05/28/24
*/
#ifndef GLOBALS_H
#define GLOBALS_H
#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>
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h> /* pollute name space as much as possible */
#include <io.h> /* also import deprecated POSIX names */
#pragma warning(disable: 4244 4267 4996)
#define kh_packed /* forget about __attribute__ ((packed)) */
#else
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#endif
#ifndef NOBDW
#include <gc.h>
#else
#include "gc.h"
#endif
#include "kvec.h"
#include "khashl.h"
#ifdef NOBDW
#define nodetype(n) vec_at(env->memory, n).op
#define nodevalue(n) vec_at(env->memory, n).u
#define nextnode1(n) vec_at(env->memory, n).next
#define nextnode2(n) vec_at(env->memory, nextnode1(n)).next
#define nextnode3(n) vec_at(env->memory, nextnode2(n)).next
#define nextnode4(n) vec_at(env->memory, nextnode3(n)).next
#define nextnode5(n) vec_at(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
/* configure */
#define SHELLESCAPE '$'
#define INPSTACKMAX 10
#define INPLINEMAX 255
#define BUFFERMAX 80 /* smaller buffer */
#define HELPLINEMAX 72
#define MAXNUM 32 /* 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_,
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 */
#ifdef NOBDW
typedef unsigned Index;
#else
typedef struct Node *Index;
#endif
typedef struct Env *pEnv;
typedef void (*proc_t)(pEnv); /* procedure */
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;
typedef struct Node {
Types u;
Operator op;
Index next;
} Node;
typedef struct Entry {
char *name, is_user, flags, is_ok;
union {
Index body;
proc_t proc;
} u;
} Entry;
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)
typedef struct Env {
jmp_buf finclude; /* return point in finclude */
double maxnodes;
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 *pathname;
char *mod_name; /* name of module */
vector(char) *string; /* value */
vector(char) *pushback; /* push back buffer */
vector(Node) *tokens; /* read ahead table */
vector(Entry) *symtab; /* symbol table */
symtab_t *hash; /* hash tables that index the symbol table */
funtab_t *prim;
Types bucket; /* used by NEWNODE defines */
#ifdef NOBDW
clock_t gc_clock;
vector(Node) *memory; /* dynamic memory */
Index conts, dump, dump1, dump2, dump3, dump4, dump5;
#endif
Index prog, stck;
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;
} Env;
/* 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(FILE *fp, char *str);
int getch(pEnv env);
void ungetch(int ch);
void error(char *str);
void execerror(char *message, char *op);
int include(pEnv env, char *name);
int getsym(pEnv env, int ch);
/* utils.c */
Index newnode(pEnv env, Operator o, Types u, Index r);
void my_memoryindex(pEnv env);
void my_memorymax(pEnv env);
#ifdef NOBDW
void writedump(pEnv env, Index n, FILE *fp);
void inimem1(pEnv env, int status);
void inimem2(pEnv env);
void printnode(pEnv env, Index p);
void my_gc(pEnv env);
#endif
/* factor.c */
int readfactor(pEnv env, int ch, int *rv); /* read a JOY factor */
int readterm(pEnv env, int ch);
void writefactor(pEnv env, Index n, FILE *fp);
void writeterm(pEnv env, Index n, FILE *fp);
void writedump(pEnv env, Index n, FILE *fp);
/* 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 */
char *nickname(int ch);
char *opername(int o);
int operindex(pEnv env, proc_t proc);
void inisymboltable(pEnv env); /* initialise */
/* 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);
#endif