summaryrefslogtreecommitdiff
path: root/apl9.h
blob: b19c6862b0b4baec116212686c5e737f94ee36cd (plain) (blame)
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
/* 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,
	AtypeArray,
} arrayDataType;

typedef enum
{
	OperatortypeDop,
	OperatortypePrim,
	OperatortypeHybrid,
} operatorType;

typedef enum
{
	FunctypeDfn,
	FunctypePrim,
	FunctypeOp,
	FunctypeQuad,
	FunctypeHybrid,
	FunctypeTrain,
} functionType;

typedef enum
{
	ESyntax = 1,
	EParse,
	EValue,
	EDomain,
	ERank,
	EType,
	ELength,
	EIndex,
	EShape,
	ENotImplemented,
} errorCodes;

/* Data types */
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 ErrorHandler ErrorHandler;
typedef struct DfnFrame DfnFrame;

struct Array
{
	arrayDataType type;
	int rank;
	int *shape;
	int size;
	int stranded; /* 1 if build directly by stranding */
	int refs; /* reference counting */
	union {
		char *rawdata;
		vlong *intdata;
		double *floatdata;
		Rune *runedata;
		Array **arraydata;
	};
};

struct Statement
{
	int ntoks;
	Datum *toks;
	Statement *guard;
	Statement *next;
};

struct Operator
{
	operatorType type;
	int dyadic;
	union {
		int code;
		Rune *dop;
	};
	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;
	};
	Array *left;
};

struct Datum
{
	datumTag tag;
	int shy;
	union {
		Array *array;
		Statement stmt;
		Function func;
		Operator operator;
		int hybrid;
		Symbol *symbol;
	};
};

struct Symbol
{
	int undefined;
	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 ErrorHandler
{
	Rune *msg;
	jmp_buf jmp;
};

struct DfnFrame
{
	Rune *code;
	Symtab *symtab;
	Datum *left;
	Array *right;
	Datum *lefto;
	Datum *righto;
	DfnFrame *prev;
};

/* Function prototypes for the different source files */
/* main.c */
Datum *evalline(Rune *, Biobuf *, int);
Rune *prompt(Rune *);

/* 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 *);

/* eval.c */
Datum *eval(Statement *, int);

/* symbol.c */
Symbol *getsym(Rune *, int);
void initsymtab(void);
DfnFrame *getcurrentdfn(void);
DfnFrame *pushdfnframe(Rune *, Datum *, Datum *, Array *, Array *);
void popdfnframe(void);
vlong globalIO(void);
void globalIOset(vlong);
int globalDIV(void);
void globalDIVset(int);

/* memory.c */
void *emalloc(ulong);
void checkmem(char *);

Array *allocarray(int, int, int);
void freearray(Array *);
void incref(Array *);

/* functions.c */
Array *runfunc(Function, Array *,Array *);
Array *rundfn(Rune *, Datum *, Datum *, Array *, Array *);
Array *runtrain(Function *, int, Array *, Array *, Array *);

/* quadnames.c */
Datum quadnamedatum(QuadnameDef);

/* error.c */
void throwerror(Rune *, int);
#define SETUPERROR(e) {\
	jmp_buf old;\
	memcpy(old, globalerror.jmp, sizeof(jmp_buf));\
	e = setjmp(globalerror.jmp);\
	if(e != 0)\
		memcpy(globalerror.jmp, old, sizeof(jmp_buf));\
}

/* Monadic functions from function.c */
Array *fnNegate(Array *);
Array *fnSign(Array *);
Array *fnRecip(Array *);
Array *fnExponent(Array *);
Array *fnNaturalLog(Array *);
Array *fnPiTimes(Array *);
Array *fnMagnitude(Array *);
Array *fnCeiling(Array *);
Array *fnFloor(Array *);
Array *fnSame(Array *);
Array *fnTally(Array *);
Array *fnMix(Array *);
Array *fnSplit(Array *);
Array *fnEnclose(Array *);
Array *fnNest(Array *);
Array *fnGradeUp(Array *);
Array *fnGradeDown(Array *);
Array *fnIndexGenerator(Array *);
Array *fnEnlist(Array *);
Array *fnNot(Array *);
Array *fnRavel(Array *);
Array *fnTable(Array *);
Array *fnShape(Array *);
Array *fnReverseLast(Array *);
Array *fnReverseFirst(Array *);
Array *fnTranspose(Array *);
Array *fnSelfRef1(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 *fnResidue(Array *, Array *);
Array *fnMaximum(Array *, Array *);
Array *fnMinimum(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 *fnOr(Array *, Array *);
Array *fnAnd(Array *, Array *);
Array *fnNand(Array *, Array *);
Array *fnNor(Array *, Array *);
Array *fnTake(Array *, Array *);
Array *fnIndex(Array *, Array *);
Array *fnCatenateLast(Array *, Array *);
Array *fnCatenateFirst(Array *, Array *);
Array *fnReshape(Array *, Array *);
Array *fnSelfRef2(Array *, Array *);

/* Monadic operators from operators.c */
Array *opEach(Datum *, Array *, Array *);
Array *opSwitch(Datum *, Array *, Array *);
Array *opSelfReference1(Datum *, Array *, Array *);

/* Dyadic operators from operators.c */
Array *opBind(Datum *, Datum *, Array *, Array *);
Array *opAtop(Datum *, Datum *, Array *, Array *);
Array *opOver(Datum *, Datum *, Array *, Array *);
Array *opSelfReference2(Datum *, Datum *, Array *, Array *);

/* Dyadic functions from hybrids.c */

/* 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 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 alloccounts; /* memory.c */
extern QuadnameDef quadnames[]; /* quadnames.c */
extern int printprecision; /* print.c */
extern ErrorHandler globalerror; /* error.c */
extern Rune *errorstrs[]; /* error.c */