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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/* Global definitions of limits and constants */
#define MAX_LINE_LENGTH 1024
#define MAX_LINE_TOKENS 1024
typedef enum
{
ArrayTag,
FunctionTag,
HybridTag,
MonadicOpTag,
DyadicOpTag,
BoundFunctionTag, /* Function with left arg bound */
LParTag,
RParTag,
ArrowTag,
AssignmentTag,
NameTag,
} datumTag;
typedef enum
{
AtypeInt,
AtypeFloat,
AtypeRune,
AtypeMixed,
AtypeArray,
} arrayDataType;
typedef enum
{
OperatortypeDop,
OperatortypePrim,
OperatortypeHybrid,
} operatorType;
typedef enum
{
FunctypeDfn,
FunctypePrim,
FunctypeOp,
FunctypeQuad,
FunctypeHybrid,
FunctypeTrain,
} functionType;
typedef enum
{
/* The numbers are the same as in dyalog */
ESyntax = 2,
EIndex = 3,
ERank = 4,
ELength = 5,
EValue = 6,
EDomain = 11,
ETimeout = 12, /* not in dyalog */
ENotImplemented = 100, /* not in dyalog */
} errorCodes;
/* Data types */
typedef struct Mixed Mixed;
typedef struct Array Array;
typedef struct Statement Statement;
typedef struct Operator Operator;
typedef struct FunctionTrain FunctionTrain;
typedef struct Function Function;
typedef struct Datum Datum;
typedef struct Symbol Symbol;
typedef struct Symtab Symtab;
typedef struct QuadnameDef QuadnameDef;
typedef struct ErrorGuard ErrorGuard;
typedef struct DfnFrame DfnFrame;
typedef struct ThreadData ThreadData;
typedef struct Mail Mail;
struct Mixed
{
u8int type;
union {
vlong i;
double f;
Rune r;
};
};
#define GetSize(a) ((u32int)(a->info & 0x00000000FFFFFFFF))
#define GetRank(a) ((u8int)((a->info & 0x0000000F00000000) >> 32))
#define GetType(a) ((u8int)((a->info & 0x000000F000000000) >> 36))
#define GetStrand(a) ((u8int)((a->info & 0x0000010000000000) >> 40))
#define GetRefs(a) ((u32int)((a->info & 0xFFFFFE0000000000) >> 41))
#define SetSize(a,v) (a->info ^= ((u64int)GetSize(a)^v))
#define SetRank(a,v) (a->info ^= ((u64int)(GetRank(a)^v) << 32))
#define SetType(a,v) (a->info ^= ((u64int)(GetType(a)^v) << 36))
#define SetStrand(a,v) (a->info ^= ((u64int)(GetStrand(a)^v) << 40))
#define SetRefs(a,v) (a->info ^= ((u64int)(GetRefs(a)^v) << 41))
struct Array
{
u32int *shape;
union {
char *rawdata;
vlong *intdata;
double *floatdata;
Rune *runedata;
Mixed *mixeddata;
Array **arraydata;
Array *prototype; /* only in use when size == 0 and type == AtypeArray */
};
u64int info;
/* info is a bitmap of information, divided as follows:
* 0-31: 32 bits for size (max size = 4294967296)
* 32-35: 4 bits for the rank (max rank 15)
* 36-39: 4 bits for the type (16 options)
* 40: 1 bit for stranding
* 41-63: 23 bits for reference counting (max refs = 8388608)
* NOTE: should only be modified through the macros defined above
*/
};
struct Statement
{
int ntoks;
int errorguard; /* if guard != nil, is the guard an error guard? */
Datum **toks;
Statement *guard;
Statement *next;
};
struct Operator
{
operatorType type;
int dyadic;
union {
int code;
Rune *dop;
};
DfnFrame *scope;
Datum *left;
Datum *right;
};
struct FunctionTrain
{
int nfuncs;
Function *funcs;
};
struct Function
{
functionType type;
union {
int code;
Rune *dfn;
Operator operator;
QuadnameDef *quad;
FunctionTrain train;
};
DfnFrame *scope;
Array *left;
};
struct Datum
{
datumTag tag;
int shy;
int refs;
union {
Array *array;
Statement stmt;
Function func;
Operator operator;
int hybrid;
Rune *name;
Statement names;
};
};
struct Symbol
{
Rune *name;
Datum *value;
Datum *(*getfn)(void);
void (*setfn)(Datum *);
};
struct Symtab
{
int nsyms;
int io; /* index origin */
int div; /* division method */
Symbol **syms;
};
typedef Array* (*fnmonad)(Array*);
typedef Array* (*fndyad)(Array*, Array*);
typedef Array* (*opmonad)(Datum *, Array *, Array *);
typedef Array* (*opdyad)(Datum *, Datum *, Array *, Array *);
struct QuadnameDef
{
Rune *name;
datumTag tag;
Datum *(*get)(void);
void (*set)(Datum *);
fnmonad monadfn;
fndyad dyadfn;
opmonad monadop;
opdyad dyadop;
};
struct ErrorGuard
{
u64int code; /* a bitmask of errors */
int active; /* can the guard be used? */
jmp_buf jmp;
Statement *guard;
DfnFrame *frame; /* the frame to replace with upon activation of error guard */
ErrorGuard *next;
};
struct DfnFrame
{
Rune *code;
Symtab *symtab;
Datum *left;
Array *right;
Datum *lefto;
Datum *righto;
ErrorGuard *errorguards; /* a linked list of error handlers */
DfnFrame *prev; /* prev in the call stack */
DfnFrame *chain; /* prev in the lexical scope */
};
struct ThreadData
{
int id;
DfnFrame *currentdfn;
Mail *mail;
Mail *lastmail;
int lasterror;
int timeout; /* number of milli seconds to timeout */
int timedout; /* true if the recv timed out */
Rune *lasterrormsg;
ErrorGuard *globalerrorguard;
QLock lock;
Rendez newmail;
Array *name;
};
struct Mail
{
Array *contents;
Mail *next;
};
/* Function prototypes for the different source files */
/* main.c */
Datum *evalline(Rune *, Biobuf *, int);
/* print.c */
Rune *ppdatum(Datum *);
Rune *ppdatums(Datum **, int);
Rune *pparray(Array *);
Rune *ppoperator(Operator);
Rune *ppfunction(Function);
/* lexer.c */
Statement *lexlinebio(Biobuf *, int);
Statement *lexlinestr(Rune *, int);
/* array.c */
Array *mkscalarint(vlong);
Array *mkscalarfloat(double);
Array *mkscalarrune(Rune);
Array *mkrunearray(Rune *);
Array *duparray(Array *);
Array *duparrayshape(Array *, int);
int simplearray(Array *);
int simplescalar(Array *);
Array *extend(Array *, Array *);
int scalarextend(Array *, Array *, Array **, Array **);
int commontype(Array *, Array *, Array **, Array **, int);
Array *arrayitem(Array *, int);
Array *simplifyarray(Array *);
int comparearray(Array *, Array *, int);
Array *fillelement(Array *);
uvlong arrayspaceused(Array *);
int arraydepth(Array *, int *);
/* eval.c */
Datum *eval(Statement *, int);
/* symbol.c */
Symbol *getsym(Rune *, int);
void initsymtab(void);
DfnFrame *getcurrentdfn(void);
DfnFrame *pushdfnframe(Rune *, DfnFrame *, Datum *, Datum *, Array *, Array *);
DfnFrame *dupdfnframe(DfnFrame *);
void freedfnframe(DfnFrame *, int);
void popdfnframe(void);
vlong globalIO(void);
void globalIOset(vlong);
int globalDIV(void);
void globalDIVset(int);
DfnFrame *dupscope(DfnFrame *);
/* memory.c */
void *emalloc(ulong);
void *emallocz(ulong, int);
void *erealloc(void *, ulong);
void checkmem(char *);
Array *allocarray(int, int, int);
void freearray(Array *);
void incarrayref(Array *);
Datum *allocdatum(int, int);
void freedatum(Datum *);
void incdatumref(Datum *);
void freefunction(Function);
void freeoperator(Operator);
void freestatement(Statement);
Function dupfunction(Function);
Operator dupoperator(Operator);
void freeerrorguards(ErrorGuard *);
void freeerrorguard(ErrorGuard *);
/* functions.c */
Array *runfunc(Function, Array *,Array *);
Array *rundfn(Rune *, Datum *, Datum *, Array *, Array *);
Array *runtrain(Function *, int, Array *, Array *, Array *);
/* quadnames.c */
void initquadnames(void);
Datum *quadnamedatum(QuadnameDef);
void outputmain(Datum *);
Array *runfile(Array *);
/* error.c */
ErrorGuard *newerrorguard(Array *, Statement *);
void throwerror(Rune *, int);
Rune *errorstr(int);
void displayerror(void);
/* inverse.c */
Function inverse(Function);
/* concurrency.c */
void initthreads(void);
int spawnthread(Function, Array *, Array *, Array *);
ThreadData *getthreaddata(void);
void messagesend(Array *, int);
Array *messagerecv(Function, int);
Array *runningtasks(void);
Array *taskproperty(vlong, vlong);
/* Monadic functions from function.c */
Array *fnNegate(Array *);
Array *fnSign(Array *);
Array *fnRecip(Array *);
Array *fnExponent(Array *);
Array *fnNaturalLog(Array *);
Array *fnMatrixInverse(Array *);
Array *fnPiTimes(Array *);
Array *fnFactorial(Array *);
Array *fnRoll(Array *);
Array *fnMagnitude(Array *);
Array *fnCeiling(Array *);
Array *fnFloor(Array *);
Array *fnSame(Array *);
Array *fnUniqueMask(Array *);
Array *fnDepth(Array *);
Array *fnTally(Array *);
Array *fnMix(Array *);
Array *fnSplit(Array *);
Array *fnEnclose(Array *);
Array *fnDisclose(Array *);
Array *fnNest(Array *);
Array *fnGradeUp(Array *);
Array *fnGradeDown(Array *);
Array *fnIndexGenerator(Array *);
Array *fnWhere(Array *);
Array *fnEnlist(Array *);
Array *fnUnique(Array *);
Array *fnNot(Array *);
Array *fnRavel(Array *);
Array *fnTable(Array *);
Array *fnShape(Array *);
Array *fnReverseLast(Array *);
Array *fnReverseFirst(Array *);
Array *fnTranspose(Array *);
Array *fnExecute(Array *);
Array *fnFormat(Array *);
Array *fnSelfReference1(Array *);
/* Dyadic functions from function.c */
Array *fnPlus(Array *, Array *);
Array *fnMinus(Array *, Array *);
Array *fnTimes(Array *, Array *);
Array *fnDivide(Array *, Array *);
Array *fnPower(Array *, Array *);
Array *fnLogarithm(Array *, Array *);
Array *fnMatrixDivide(Array *, Array *);
Array *fnCircular(Array *, Array *);
Array *fnBinomial(Array *, Array *);
Array *fnDeal(Array *, Array *);
Array *fnResidue(Array *, Array *);
Array *fnMaximum(Array *, Array *);
Array *fnMinimum(Array *, Array *);
Array *fnDecode(Array *, Array *);
Array *fnEncode(Array *, Array *);
Array *fnLeft(Array *, Array *);
Array *fnRight(Array *, Array *);
Array *fnEqual(Array *, Array *);
Array *fnNotEqual(Array *, Array *);
Array *fnLessEqual(Array *, Array *);
Array *fnLess(Array *, Array *);
Array *fnGreater(Array *, Array *);
Array *fnGreaterEqual(Array *, Array *);
Array *fnMatch(Array *, Array *);
Array *fnNotMatch(Array *, Array *);
Array *fnOr(Array *, Array *);
Array *fnAnd(Array *, Array *);
Array *fnNand(Array *, Array *);
Array *fnNor(Array *, Array *);
Array *fnTake(Array *, Array *);
Array *fnDrop(Array *, Array *);
Array *fnPick(Array *, Array *);
Array *fnPartition(Array *, Array *);
Array *fnIndex(Array *, Array *);
Array *fnIndexOf(Array *, Array *);
Array *fnIntervalIndex(Array *, Array *);
Array *fnMembership(Array *, Array *);
Array *fnFind(Array *, Array *);
Array *fnUnion(Array *, Array *);
Array *fnIntersection(Array *, Array *);
Array *fnExcluding(Array *, Array *);
Array *fnCatenateLast(Array *, Array *);
Array *fnCatenateFirst(Array *, Array *);
Array *fnReshape(Array *, Array *);
Array *fnRotateLast(Array *, Array *);
Array *fnRotateFirst(Array *, Array *);
Array *fnSelfReference2(Array *, Array *);
Array *fnSend(Array *, Array *);
/* Monadic operators from operators.c */
Array *opEach(Datum *, Array *, Array *);
Array *opSwitch(Datum *, Array *, Array *);
Array *opKey(Datum *, Array *, Array *);
Array *opOuterProduct(Datum *, Array *, Array *);
Array *opSelfReference1(Datum *, Array *, Array *);
Array *opReceive(Datum *, Array *, Array *);
/* Dyadic operators from operators.c */
Array *opPower(Datum *, Datum *, Array *, Array *);
Array *opInnerProduct(Datum *, Datum *, Array *, Array *);
Array *opBind(Datum *, Datum *, Array *, Array *);
Array *opAtop(Datum *, Datum *, Array *, Array *);
Array *opOver(Datum *, Datum *, Array *, Array *);
Array *opUnder(Datum *, Datum *, Array *, Array *);
Array *opObverse(Datum *, Datum *, Array *, Array *);
Array *opSelfReference2(Datum *, Datum *, Array *, Array *);
Array *opSpawn(Datum *, Datum *, Array *, Array *);
/* Dyadic functions from hybrids.c */
Array *fnReplicateLast(Array *, Array *);
Array *fnExpandLast(Array *, Array *);
Array *fnReplicateFirst(Array *, Array *);
Array *fnExpandFirst(Array *, Array *);
/* Monadic operators from hybrids.c */
Array *opReduceLast(Datum *, Array *, Array *);
Array *opScanLast(Datum *, Array *, Array *);
Array *opReduceFirst(Datum *, Array *, Array *);
Array *opScanFirst(Datum *, Array *, Array *);
/* Global variables */
extern Biobuf *stdin; /* main.c */
extern int traceeval; /* eval.c */
extern int debugmem; /* memory.c */
extern int datasizes[]; /* array.c */
extern Rune primfuncnames[]; /* functions.c */
extern Rune primmonopnames[]; /* operators.c */
extern Rune primdyadopnames[]; /* operators.c */
extern Rune primhybridnames[]; /* hybrids.c */
extern fnmonad monadfunctiondefs[]; /* functions.c */
extern fndyad dyadfunctiondefs[]; /* functions.c */
extern opmonad monadoperatordefs[]; /* operators.c */
extern opdyad dyadoperatordefs[]; /* operators.c */
extern fndyad hybridfunctiondefs[]; /* hybrids.c */
extern opmonad hybridoperatordefs[]; /* hybrids.c */
extern int arrayalloccounts; /* memory.c */
extern int datumalloccounts; /* memory.c */
extern QuadnameDef quadnames[]; /* quadnames.c */
extern int printprecision; /* print.c */
extern int mainstacksize; /* concurrency.c */
|