~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to src/structs.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GENIUS Calculator
 
2
 * Copyright (C) 1997-2004 Jiri (George) Lebl
 
3
 *
 
4
 * Author: Jiri (George) Lebl
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the  Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 
19
 * USA.
 
20
 */
 
21
 
 
22
#ifndef STRUCTS_H
 
23
#define STRUCTS_H
 
24
 
 
25
#include <stdio.h>
 
26
#include "mpwrap.h"
 
27
 
 
28
/*dictionary function structure*/
 
29
typedef enum {
 
30
        GEL_BUILTIN_FUNC = 0, /*function internal to genius*/
 
31
        GEL_USER_FUNC, /*function that points to an GelETree for evaluation*/
 
32
        GEL_VARIABLE_FUNC, /*function that points to an GelETree result */
 
33
        GEL_REFERENCE_FUNC /*a function that points to some other GelEFunc*/
 
34
} GelEFuncType; /* should fit in 3 bits (it does) */
 
35
 
 
36
typedef struct _GelEFunc GelEFunc;
 
37
typedef union _GelETree GelETree;
 
38
typedef struct _GelETreeAny GelETreeAny;
 
39
/*typedef struct _GelETreeNull GelETreeNull;*/
 
40
typedef struct _GelETreeValue GelETreeValue;
 
41
typedef struct _GelETreeMatrix GelETreeMatrix;
 
42
typedef struct _GelETreeSet GelETreeSet;
 
43
typedef struct _GelETreePolynomial GelETreePolynomial;
 
44
typedef struct _GelETreeOperator GelETreeOperator;
 
45
typedef struct _GelETreeIdentifier GelETreeIdentifier;
 
46
typedef struct _GelETreeString GelETreeString;
 
47
typedef struct _GelETreeFunction GelETreeFunction;
 
48
typedef struct _GelETreeComparison GelETreeComparison;
 
49
typedef struct _GelETreeUsertype GelETreeUsertype;
 
50
typedef struct _GelETreeBool GelETreeBool;
 
51
 
 
52
typedef struct _GelETreeMatrixRow GelETreeMatrixRow;
 
53
/*typedef struct _GelETreeMatrixStart GelETreeMatrixStart;*/
 
54
/*typedef struct _GelETreeExprlistStart GelETreeExprlistStart;*/
 
55
typedef struct _GelETreeSpacer GelETreeSpacer;
 
56
typedef struct _GelToken GelToken;
 
57
 
 
58
typedef struct _GelEvalStack GelEvalStack;
 
59
typedef struct _GelEvalLoop GelEvalLoop;
 
60
typedef struct _GelEvalFor GelEvalFor;
 
61
typedef struct _GelEvalForIn GelEvalForIn;
 
62
typedef struct _GelCtx GelCtx;
 
63
 
 
64
typedef struct _GelExecSetup GelExecSetup;
 
65
typedef struct _GelOutput GelOutput;
 
66
 
 
67
/*not defined here, but needed and we can't include matrixw.h, but
 
68
  matrixw.h includes structs.h*/
 
69
typedef struct _GelMatrixW GelMatrixW;
 
70
 
 
71
typedef GelETree *(* ParameterSetFunc) (GelETree *val);
 
72
typedef GelETree *(* ParameterGetFunc) (void);
 
73
 
 
74
typedef GelETree *(* GelBIFunction) (GelCtx *ctx, GelETree * *, gboolean *); /*the gboolean is exception*/
 
75
 
 
76
/* tokens point to this structure globaly, there is
 
77
   one such structure for each token.  */
 
78
struct _GelToken {
 
79
        char *token;
 
80
        GelEFunc *curref;
 
81
        GSList *refs;
 
82
 
 
83
        /* For built-in parameters this is the get and set function
 
84
         * of type ParameterGetFunc and ParameterSetFunc */
 
85
        gpointer data1;
 
86
        gpointer data2;
 
87
 
 
88
        guint8 protected_:1;
 
89
        guint8 parameter:1;
 
90
        guint8 built_in_parameter:1;
 
91
};
 
92
 
 
93
struct _GelEFunc {
 
94
        GelToken *id;
 
95
        GelToken *symbolic_id; /* id for symbolic math, preserved under
 
96
                                  assignment, if NULL we use the id */
 
97
        int context; /*the context number this is used for .. if we pop this
 
98
                       context, we will destroy the function*/
 
99
        GSList *named_args; /*names of arguments*/
 
100
 
 
101
        GSList *extra_dict;
 
102
 
 
103
        union {
 
104
                GelETree *user;
 
105
                GelBIFunction func;
 
106
                GelEFunc *ref;
 
107
                GelEFunc *next; /*this is for keeping a free list*/
 
108
        } data;
 
109
 
 
110
        guint16 nargs; /*number of arguments*/
 
111
 
 
112
        /* GelEFuncType type; */
 
113
        guint8 type:3;
 
114
 
 
115
        /* if true, we must take this off the subst list for a context pop,
 
116
         * before we free the function */
 
117
        guint8 on_subst_list:1;
 
118
        guint8 vararg:1;
 
119
        guint8 propagate_mod:1;
 
120
        guint8 no_mod_all_args:1;
 
121
};
 
122
 
 
123
typedef enum {
 
124
        NULL_NODE=0,
 
125
        VALUE_NODE,
 
126
        MATRIX_NODE,
 
127
        SET_NODE, /* FIXME: Note implemented */
 
128
        POLYNOMIAL_NODE, /* FIXME: Note implemented */
 
129
        OPERATOR_NODE,
 
130
        IDENTIFIER_NODE,
 
131
        STRING_NODE,
 
132
        FUNCTION_NODE, /*stores an anonymous function*/
 
133
        COMPARISON_NODE,
 
134
        USERTYPE_NODE, /*for user types, FIXME: not finished*/
 
135
        BOOL_NODE, /*boolean*/
 
136
        
 
137
        /*marker nodes*/
 
138
        MATRIX_ROW_NODE=1000,
 
139
        MATRIX_START_NODE,
 
140
        EXPRLIST_START_NODE,
 
141
        SPACER_NODE
 
142
} GelETreeType;
 
143
 
 
144
struct _GelETreeAny {
 
145
        GelETreeType type;
 
146
        GelETree *next;
 
147
};
 
148
 
 
149
/*struct _GelETreeNull {
 
150
        GelETreeType type;
 
151
        GelETree *next;
 
152
};*/
 
153
 
 
154
struct _GelETreeValue {
 
155
        GelETreeType type;
 
156
        GelETree *next;
 
157
        mpw_t value;
 
158
};
 
159
 
 
160
struct _GelETreeMatrix {
 
161
        GelETreeType type;
 
162
        GelETree *next;
 
163
        GelMatrixW *matrix;
 
164
        gboolean quoted;
 
165
        
 
166
        /* gboolean is faster, then a bitfield and we right now
 
167
           don't gain anything */
 
168
        /*
 
169
        guint quoted:1;
 
170
        */
 
171
};
 
172
 
 
173
/* FIXME: Not implemented */
 
174
struct _GelETreeSet {
 
175
        GelETreeType type;
 
176
        GelETree *next;
 
177
        GelETree *items;
 
178
        gboolean multiset;
 
179
 
 
180
        /* gboolean is faster, then a bitfield and we right now
 
181
           don't gain anything */
 
182
        /*
 
183
        guint multiset:1;
 
184
        */
 
185
};
 
186
 
 
187
struct _GelETreePolynomial {
 
188
        GelETreeType type;
 
189
        GelETree *next;
 
190
        guint16 largest; /* largest exponent */
 
191
        guint8 vars; /* number of variables */
 
192
 
 
193
        /* This needs redoing.  No need to store
 
194
           this in the ETree struct since we want to
 
195
           conserve memory.  Probably need an array
 
196
           type for this. */
 
197
        /*
 
198
        guint16 arraysize;
 
199
        */
 
200
 
 
201
        mpw_ptr *indexes; /* indexes when written out in standard form
 
202
                             from smallest to largest.  If more then one
 
203
                             variable then this is '(largest+1)^vars'
 
204
                             size array */
 
205
};
 
206
 
 
207
struct _GelETreeOperator {
 
208
        GelETreeType type;
 
209
        GelETree *next;
 
210
        gint8 oper;
 
211
        guint16 nargs;
 
212
        GelETree *args;
 
213
};
 
214
 
 
215
struct _GelETreeIdentifier {
 
216
        GelETreeType type;
 
217
        GelETree *next;
 
218
        GelToken *id;
 
219
};
 
220
 
 
221
struct _GelETreeString {
 
222
        GelETreeType type;
 
223
        GelETree *next;
 
224
        char *str;
 
225
 
 
226
        gboolean constant;
 
227
        /* gboolean is faster, then a bitfield and we right now
 
228
           don't gain anything */
 
229
        /* guint constant:1; */
 
230
};
 
231
 
 
232
struct _GelETreeFunction {
 
233
        GelETreeType type;
 
234
        GelETree *next;
 
235
        GelEFunc *func; /*anon function*/
 
236
};
 
237
 
 
238
struct _GelETreeComparison {
 
239
        GelETreeType type;
 
240
        GelETree *next;
 
241
        gint16 nargs;
 
242
        GelETree *args;
 
243
        GSList *comp;
 
244
};
 
245
 
 
246
struct _GelETreeUsertype {
 
247
        GelETreeType type;
 
248
        GelETree *next;
 
249
        gint16 ttype;
 
250
        gpointer data;
 
251
};
 
252
 
 
253
struct _GelETreeBool {
 
254
        GelETreeType type;
 
255
        GelETree *next;
 
256
        gboolean bool_;
 
257
 
 
258
        /* gboolean is faster, then a bitfield and we right now
 
259
           don't gain anything */
 
260
        /* guint bool_:1; */
 
261
};
 
262
 
 
263
 
 
264
struct _GelETreeMatrixRow {
 
265
        GelETreeType type;
 
266
        GelETree *next;
 
267
        gint16 nargs;
 
268
        GelETree *args;
 
269
};
 
270
 
 
271
/*struct _GelETreeMatrixStart {
 
272
        GelETreeType type;
 
273
        GelETree *next;
 
274
};*/
 
275
 
 
276
/*struct _GelETreeExprlistStart {
 
277
        GelETreeType type;
 
278
        GelETree *next;
 
279
};*/
 
280
 
 
281
struct _GelETreeSpacer {
 
282
        GelETreeType type;
 
283
        GelETree *next;
 
284
        GelETree *arg;
 
285
};
 
286
 
 
287
union _GelETree {
 
288
        GelETreeType type;
 
289
        GelETreeAny any; /*for allocation purposes only*/
 
290
        /*GelETreeNull null;*/
 
291
        GelETreeValue val;
 
292
        GelETreeMatrix mat;
 
293
        GelETreeSet set;
 
294
        GelETreePolynomial poly;
 
295
        GelETreeOperator op;
 
296
        GelETreeIdentifier id;
 
297
        GelETreeString str;
 
298
        GelETreeFunction func;
 
299
        GelETreeComparison comp;
 
300
        GelETreeUsertype ut;
 
301
        GelETreeBool bool_;
 
302
        GelETreeMatrixRow row;
 
303
        /*GelETreeMatrixStart mats;*/
 
304
        /*GelETreeExprlistStart exps;*/
 
305
        GelETreeSpacer sp;
 
306
};
 
307
 
 
308
/* Evaluation stack */
 
309
 
 
310
/* The flag for the stack */
 
311
enum {
 
312
        GE_EMPTY_STACK = 0,
 
313
        GE_RESULT      = 1, /*used for recursive evluation with the same ctx*/
 
314
        GE_PRE         = 2,
 
315
        GE_POST        = 3,
 
316
        GE_FUNCCALL    = 4,
 
317
        GE_AND         = 5,
 
318
        GE_OR          = 6,
 
319
        GE_LOOP_LOOP   = 7,
 
320
        GE_LOOP_COND   = 8,
 
321
        GE_FOR         = 9,
 
322
        GE_FORIN       = 10,
 
323
        GE_MODULOOP    = 11,
 
324
        GE_SETMODULO   = 12,
 
325
 
 
326
        /* flag mask */
 
327
        GE_MASK        = 0xff,
 
328
 
 
329
        /* bool flags */
 
330
        GE_WHACKARG    = 1<<16 /* only on GE_PRE, GE_POST, GE_MODULOOP */
 
331
};
 
332
 
 
333
#define GE_ADDWHACKARG(flag,whackarg) \
 
334
        ((whackarg) ? ((flag) | GE_WHACKARG) : (flag))
 
335
 
 
336
/*should take up about a page
 
337
  we will use a single pointer for data and the next pointer for post/pre flag
 
338
  we can use next as a marker that we have gone too far, and current as a marker
 
339
  that we have underpopped*/
 
340
#define STACK_SIZE 1022
 
341
struct _GelEvalStack {
 
342
        gpointer stack[STACK_SIZE];
 
343
        GelEvalStack *next;
 
344
};
 
345
 
 
346
typedef enum {
 
347
        GEL_EVAL_FOR,
 
348
        GEL_EVAL_SUM,
 
349
        GEL_EVAL_PROD
 
350
} GelEvalForType;
 
351
 
 
352
/*data structure for while/until loops*/
 
353
struct _GelEvalLoop {
 
354
        GelETree * condition;
 
355
        GelETree * body;
 
356
        guint8 is_while:1; /*if false, this is an until loop*/
 
357
        guint8 body_first:1; /*if true body is the first argument*/
 
358
};
 
359
/*data structure for 'for' loops*/
 
360
struct _GelEvalFor {
 
361
        GelEvalForType type;
 
362
        mpw_ptr x;
 
363
        mpw_ptr by;
 
364
        mpw_ptr to;
 
365
        gint8 init_cmp;
 
366
        GelETree * result;
 
367
        GelETree * body;
 
368
        GelETree * orig_body;
 
369
        GelToken * id;
 
370
};
 
371
 
 
372
/*data structure for 'forin' loops*/
 
373
struct _GelEvalForIn {
 
374
        GelEvalForType type;
 
375
        int i,j;
 
376
        GelMatrixW * mat;
 
377
        GelETree * result;
 
378
        GelETree * body;
 
379
        GelETree * orig_body;
 
380
        GelToken * id;
 
381
};
 
382
 
 
383
/* evaluation context structure */
 
384
struct _GelCtx {
 
385
        GelEvalStack *stack;
 
386
        gpointer *topstack;
 
387
        GelETree *res;
 
388
        gboolean post;
 
389
        gboolean whackarg;
 
390
        GelETree *current;
 
391
        mpw_ptr modulo;
 
392
};
 
393
 
 
394
enum {
 
395
        GEL_OUTPUT_FILE,
 
396
        GEL_OUTPUT_STRING,
 
397
        GEL_OUTPUT_BLACK_HOLE
 
398
};
 
399
 
 
400
typedef int (*GelOutputLineFunc)(GelOutput *);
 
401
typedef void (*GelOutputNotifyFunc)(GelOutput *);
 
402
 
 
403
struct _GelOutput {
 
404
        int ref_count;
 
405
 
 
406
        int output_type;
 
407
 
 
408
        gboolean length_limit; /* if TRUE limit by below, else don't */
 
409
        int line_length; /* limit the output to this number of characters,
 
410
                            if <=0 then always print normally */
 
411
        GelOutputLineFunc line_length_get;
 
412
                         /* if this is set, this function is used instead of
 
413
                            above */
 
414
        int cur_line_pos; /* position on current line */
 
415
        gboolean inside_escape; /* inside a shell escape, don't increment the
 
416
                                   above */
 
417
 
 
418
        FILE *outfp;
 
419
        GString *outs;
 
420
 
 
421
        /* new data notifier */
 
422
        GelOutputNotifyFunc notify;
 
423
        int no_notify;
 
424
        
 
425
        gpointer data;
 
426
};
 
427
 
 
428
#endif /* STRUCTS_H */