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
|
/* 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,
LBracketTag,
RBracketTag,
ArrowTag,
AssignmentTag,
NameTag,
} datumTag;
typedef enum
{
AtypeInt,
AtypeFloat,
AtypeRune,
AtypeArray,
} arrayDataType;
typedef enum
{
OperatortypeDop,
OperatortypePrim,
} operatorType;
typedef enum
{
FunctypeDfn,
FunctypePrim,
FunctypeOp,
} functionType;
/* Data types */
typedef struct Array Array;
typedef struct Statement Statement;
typedef struct Operator Operator;
typedef struct Function Function;
typedef struct Datum Datum;
typedef struct Symbol Symbol;
typedef struct Symtab Symtab;
typedef struct QuadnameDef QuadnameDef;
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 *next;
};
struct Operator
{
operatorType type;
int dyadic;
union {
int code;
Rune *dop;
};
Datum *left;
Datum *right;
};
struct Function
{
functionType type;
union {
int code;
Rune *dfn;
Operator operator;
};
Array *left;
};
struct Datum
{
datumTag tag;
int shy;
union {
Array *array;
Statement stmt;
Function func;
Operator operator;
Symbol *symbol;
};
};
struct Symbol
{
int undefined;
Rune *name;
Datum value;
Datum *(*getfn)(void);
int (*setfn)(Datum);
};
struct Symtab
{
int nsyms;
int io; /* index origin */
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);
int (*set)(Datum);
fnmonad monadfn;
fnmonad dyadfn;
opmonad monadop;
opdyad dyadop;
};
/* Function prototypes for the different source files */
/* main.c */
Datum *evalline(Rune *);
Rune *prompt(Rune *);
/* print.c */
Rune *ppdatum(Datum);
Rune *ppdatums(Datum *, int);
Rune *pparray(Array *);
Rune *ppoperator(Operator);
/* lexer.c */
Statement *lexline(Rune *);
/* array.c */
Array *mkscalarint(vlong);
Array *mkscalarfloat(double);
Array *mkscalarrune(Rune);
Array *mkrunearray(Rune *);
Array *duparray(Array *);
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 *);
/* eval.c */
Datum *eval(Statement *);
/* symbol.c */
Symbol *getsym(Symtab *, Rune *);
Symtab *newsymtab(void);
void freesymtab(Symtab *);
/* 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 *);
/* quadnames.c */
Datum quadnamedatum(QuadnameDef);
/* Monadic functions from function.c */
Array *fnSame(Array *);
Array *fnTally(Array *);
Array *fnEnclose(Array *);
Array *fnNest(Array *);
Array *fnIndexGenerator(Array *);
Array *fnRavel(Array *);
Array *fnTable(Array *);
Array *fnShape(Array *);
Array *fnReverseLast(Array *);
Array *fnReverseFirst(Array *);
Array *fnTranspose(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 *fnLeft(Array *, Array *);
Array *fnRight(Array *, Array *);
Array *fnMatch(Array *, Array *);
Array *fnCatenateFirst(Array *, Array *);
Array *fnReshape(Array *, Array *);
/* Monadic operators from operators.c */
Array *opEach(Datum *, Array *, Array *);
Array *opSwitch(Datum *, Array *, Array *);
/* Dyadic operators from operators.c */
Array *opOver(Datum *, Datum *, Array *, Array *);
/* Global variables */
extern int traceeval; /* eval.c */
extern int debugmem; /* memory.c */
extern Rune *errormsg; /* eval.c */
extern int datasizes[]; /* array.c */
extern Rune primfuncnames[]; /* functions.c */
extern Rune primmonopnames[]; /* operators.c */
extern Rune primdyadopnames[]; /* operators.c */
extern Rune primhybridnames[]; /* lexer.c */
extern fnmonad monadfunctiondefs[]; /* functions.c */
extern fndyad dyadfunctiondefs[]; /* functions.c */
extern opmonad monadoperatordefs[]; /* operators.c */
extern opdyad dyadoperatordefs[]; /* operators.c */
extern Symtab *globalsymtab; /* symbol.c */
extern Symtab *currentsymtab; /* symbol.c */
extern int alloccounts; /* memory.c */
extern QuadnameDef quadnames[]; /* quadnames.c */
extern int printprecision; /* print.c */
|