-
Notifications
You must be signed in to change notification settings - Fork 2
/
globals.h
335 lines (310 loc) · 7.48 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
/*
module : globals.h
version : 1.52
date : 08/30/24
*/
#ifndef GLOBALS_H
#define GLOBALS_H
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.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> /* alarm function */
#include <termios.h>
#include <sys/ioctl.h>
#endif
/*
* The following #defines are present in the source code.
*/
#if 0
#define USE_BIGNUM_ARITHMETIC
#define USE_MULTI_THREADS_JOY
#endif
#define USE_KHASHL
#include <gc.h> /* system installed BDW or local gc.h */
#include "kvec.h"
#include "khashl.h"
#ifdef USE_BIGNUM_ARITHMETIC
#include "bignum.h"
#endif
/* configure */
#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 */
typedef enum {
ANYTYPE,
DIP,
WHILE,
IFTE,
LINREC,
HELP,
INFRA,
UFLOAT,
MUL,
BFLOAT,
FGET,
FPUT,
STRFTIME,
FPUTCHARS,
STRTOD,
FOPEN,
UNMKTIME,
FREAD,
LDEXP,
STRTOL,
FSEEK,
TIMES,
MAXMIN,
PREDSUCC,
PLUSMINUS,
SIZE_,
STEP,
TAKE,
CONCAT,
ANDORXOR,
NOT,
PRIMREC,
SMALL,
BODY,
INTERN,
FORMAT,
FORMATF,
CONS,
IN_,
HAS,
CASE,
FIRST,
OF,
AT,
DIV,
REM,
DIVIDE,
FWRITE,
ASSIGN,
#ifdef USE_MULTI_THREADS_JOY
RECEIVE,
SEND
#endif
} Params;
typedef enum {
OK,
IGNORE_OK,
IGNORE_PUSH,
IGNORE_POP,
IMMEDIATE,
POSTPONE
} Flags;
typedef enum {
ABORT_NONE,
ABORT_RETRY,
ABORT_ERROR,
ABORT_QUIT
} Abort;
/* types */
typedef int Symbol; /* symbol created by scanner */
typedef struct Env *pEnv; /* pointer to global variables */
typedef void (*proc_t)(pEnv); /* procedure */
typedef struct NodeList NodeList; /* forward */
typedef unsigned char Operator; /* opcode / datatype */
/*
* Lists are stored in vectors of type Node.
*/
#include "pars.h" /* YYSTYPE */
/*
* Nodes are in consecutive memory locations. No next pointer needed.
*/
typedef struct Node {
YYSTYPE u;
Operator op;
} Node;
#include "pvec.h" /* struct NodeList */
#ifdef USE_MULTI_THREADS_JOY
#include "task.h"
#endif
/*
* The symbol table has a name/value pair. Type of value depends on is_user.
* The flags are used to distinguish between immediate and normal functions.
*/
typedef struct Entry {
char *name, is_user, flags;
union {
NodeList *body;
proc_t proc;
} u;
} Entry;
typedef struct Token {
YYSTYPE yylval;
Symbol symb;
} Token;
/*
* The symbol table is accessed through two hash tables, one with name as
* index; the other with function address as index, cast to uint64_t.
*/
KHASHL_MAP_INIT(KH_LOCAL, symtab_t, symtab, kh_cstr_t, int, kh_hash_str,
kh_eq_str)
KHASHL_MAP_INIT(KH_LOCAL, funtab_t, funtab, uint64_t, int, kh_hash_uint64,
kh_eq_generic)
/*
* Global variables are stored locally in the main function.
*/
typedef struct Env {
jmp_buf finclude; /* return point in finclude */
double calls; /* statistics */
double opers;
vector(Token) *tokens; /* read ahead table */
vector(Entry) *symtab; /* symbol table */
#ifdef USE_MULTI_THREADS_JOY
vector(Context) *context;
vector(Channel) *channel;
#endif
symtab_t *hash; /* hash tables that index the symbol table */
funtab_t *prim;
NodeList *stck, *prog; /* stack, code, and quotations are vectors */
clock_t startclock; /* main */
char **g_argv;
char *homedir; /* HOME or HOMEPATH */
vector(char *) *pathnames; /* pathnames to be searched when including */
int g_argc;
int token; /* yylex */
#ifdef USE_BIGNUM_ARITHMETIC
int scale; /* number of digits after the decimal point */
#endif
#ifdef USE_MULTI_THREADS_JOY
int current; /* currently executing thread */
#endif
int hide_stack[DISPLAYMAX];
struct {
char *name;
int hide;
} module_stack[DISPLAYMAX];
unsigned char inlining;
unsigned char autoput; /* options */
unsigned char autoput_set;
unsigned char echoflag;
unsigned char tracegc;
unsigned char undeferror;
unsigned char undeferror_set;
unsigned char alarming;
unsigned char bytecoding;
unsigned char compiling;
unsigned char debugging;
unsigned char ignore;
unsigned char overwrite;
unsigned char printing;
unsigned char recurse;
unsigned char finclude_busy;
} Env;
typedef struct table_t {
proc_t proc;
char *name;
} table_t;
/* Public procedures: */
/* arty.c */
int arity(pEnv env, NodeList *quot, int num);
/* comp.c */
void initcompile(pEnv env);
void exitcompile(pEnv env);
void compileprog(pEnv env, NodeList *list);
/* eval.c */
void trace(pEnv env, FILE *fp);
void evaluate(pEnv env, NodeList *list);
/* exec.c */
void execute(pEnv env, NodeList *list);
/* exeterm.c */
void exeterm(pEnv env, NodeList *list);
/* lexr.l */
void new_buffer(void);
void old_buffer(int num);
int my_yylex(pEnv env);
int get_input(void);
/* main.c */
void abortexecution_(int num);
/* modl.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);
/* otab.c */
char *showname(int i);
int operindex(pEnv env, proc_t proc);
char *opername(pEnv env, proc_t proc);
char *cmpname(pEnv env, proc_t proc);
char *operarity(int i);
int tablesize(void);
int operqcode(int index);
void inisymboltable(pEnv env); /* initialise */
/* parm.c */
void parm(pEnv env, int num, Params type, char *file);
/* prog.c */
void prog(pEnv env, NodeList *list);
void code(pEnv env, proc_t proc);
void push(pEnv env, int64_t num);
void prime(pEnv env, Node node);
Node pop(pEnv env);
/* read.c */
int readfactor(pEnv env); /* read a JOY factor */
void readterm(pEnv env);
/* repl.c */
int lookup(pEnv env, char *name);
void enteratom(pEnv env, char *name, NodeList *list);
NodeList *newnode(Operator op, YYSTYPE u);
/* save.c */
void save(pEnv env, NodeList *list, int num, int remove);
/* scan.c */
void inilinebuffer(pEnv env);
int include(pEnv env, char *str);
int my_yywrap(pEnv env); /* yywrap replacement */
void my_error(char *str, YYLTYPE *bloc);
void yyerror(pEnv env, char *str);
/* util.c */
int ChrVal(pEnv env, char *str);
char *StrVal(pEnv env, char *str);
/* writ.c */
void writefactor(pEnv env, Node node, FILE *fp);
void writeterm(pEnv env, NodeList *list, FILE *fp);
void writestack(pEnv env, NodeList *list, FILE *fp);
/* xerr.c */
void execerror(char *message, char *op);
/* ylex.c */
int yylex(pEnv env);
/* byte.c */
void initbytes(pEnv env);
void bytecode(pEnv env, NodeList *list);
void exitbytes(pEnv env);
/* code.c */
void readbytes(pEnv env, int flag);
/* dump.c */
void dumpbytes(pEnv env);
/* optm.c */
void rewritebic(pEnv env);
/* kraw.c */
void SetRaw(pEnv env);
#endif