Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/matc/src/elmer/matc.h
5160 views
1
/***********************************************************************
2
|
3
| MATC.H - Last Edited 7. 8. 1988
4
|
5
************************************************************************/
6
7
8
/*
9
* $Id: matc.h,v 1.2 2007/06/08 08:12:19 jpr Exp $
10
*
11
* $Log: matc.h,v $
12
* Revision 1.2 2007/06/08 08:12:19 jpr
13
* *** empty log message ***
14
*
15
* Revision 1.1 2005/05/27 12:26:22 vierinen
16
* changed header install location
17
*
18
* Revision 1.1.1.1 2005/04/14 13:29:14 vierinen
19
* initial matc automake package
20
*
21
* Revision 1.3 2001/06/08 09:20:29 jpr
22
* *** empty log message ***
23
*
24
* Revision 1.2 1998/08/01 12:34:49 jpr
25
*
26
* Added Id, started Log.
27
*
28
*
29
*/
30
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <ctype.h>
34
#include <math.h>
35
#include <setjmp.h>
36
#include <string.h>
37
#include <signal.h>
38
#include <stdlib.h>
39
#include <stdarg.h>
40
#include <time.h>
41
#include <unistd.h>
42
#include <sys/types.h>
43
44
45
#ifdef MODULE_MATC
46
#define EXT
47
#else
48
#define EXT extern
49
#endif
50
51
/*******************************************************************
52
LIST HANDLING DEFINITIONS
53
*******************************************************************/
54
55
typedef struct list {
56
struct list *next; /* pointer to next item in list */
57
char *name; /* name of list item */
58
} LIST;
59
60
/*
61
pointers to start of global lists
62
*/
63
#ifdef MODULE_MATC
64
#ifdef _OPENMP
65
/* Move initialization to matc.c::mtc_init() for thread safety */
66
EXT LIST * listheaders;
67
#pragma omp threadprivate(listheaders)
68
69
#else
70
EXT LIST listheaders[5] = {
71
{
72
NULL, "Allocations" /* memory allocations */
73
}, {
74
NULL, "Constants" /* global CONSTANTS */
75
}, {
76
NULL, "Currently defined VARIABLES" /* global VARIABLES */
77
}, {
78
NULL, "Builtin Functions" /* internal commands */
79
}, {
80
NULL, "User Functions" /* user defined functions */
81
}
82
};
83
#endif /* _OPENMP */
84
#else
85
#ifdef _OPENMP
86
/* Move initialization to matc.c::mtc_init() for thread safety */
87
EXT LIST * listheaders;
88
#pragma omp threadprivate(listheaders)
89
#else
90
EXT LIST listheaders[];
91
#endif /* _OPENMP */
92
#endif /* MODULE_MATC */
93
94
#ifdef _OPENMP
95
/* Data with thread-local storage cannot be reliably accessed across DLL
96
borders. Use an accessor function instead. */
97
LIST * mtc_get_listheaders(void);
98
#endif /* _OPENMP */
99
100
#define ALLOCATIONS 0
101
#define CONSTANTS 1
102
#define VARIABLES 2
103
#define COMMANDS 3
104
#define FUNCTIONS 4
105
106
#define MAX_HEADERS 5
107
108
#ifdef _OPENMP
109
#define LISTHEADERS (mtc_get_listheaders())
110
#else
111
#define LISTHEADERS listheaders
112
#endif /* _OPENMP */
113
#define ALLOC_HEAD LISTHEADERS[ALLOCATIONS].next
114
#define CONST_HEAD LISTHEADERS[CONSTANTS].next
115
#define VAR_HEAD LISTHEADERS[VARIABLES].next
116
#define COM_HEAD LISTHEADERS[COMMANDS].next
117
#define FUNC_HEAD LISTHEADERS[FUNCTIONS].next
118
119
#define NEXT(lst) (lst)->next
120
#define NAME(lst) (lst)->name
121
122
/*******************************************************************
123
MEMORY HANDLING
124
********************************************************************/
125
126
/*
127
memory allocation and deallocation routines
128
*/
129
#define ALLOCMEM(size) mem_alloc(size)
130
#define FREEMEM(ptr) mem_free(ptr)
131
132
/*
133
we use a lot of string copying.
134
*/
135
#define STRCOPY(str) strcpy((char *)ALLOCMEM(strlen(str)+1),(str))
136
137
typedef struct alloc_list {
138
struct alloc_list *next;
139
char *mem;
140
} ALLOC_LIST;
141
142
#define ALLOC_LST(mem) (ALLOC_LIST *)((char *)mem-sizeof(ALLOC_LIST))
143
#define ALLOC_PTR(lst) (char *)((char *)lst+sizeof(ALLOC_LIST))
144
145
/*******************************************************************
146
VARIABLES
147
*******************************************************************/
148
149
/*
150
* MATC matrix is internally represented by this structure.
151
*/
152
typedef struct MATRIX
153
{
154
int type, /* TYPE_DOUBLE or TYPE_STRING */
155
refcount, /* reference count */
156
nrow, ncol; /* number of rows and columns */
157
double *data; /* pointer to double array */
158
} MATRIX;
159
160
161
/*
162
* list of VARIABLES
163
*/
164
165
typedef struct variable
166
{
167
struct variable *next; /* pointer to next item in list */
168
char *name; /* name of the item */
169
int changed;
170
MATRIX *this;
171
} VARIABLE;
172
173
/*
174
shortcuts for accessing structure MATRIX
175
*/
176
#define MATR(ptr) (ptr)->this->data
177
#define TYPE(ptr) (ptr)->this->type
178
#define NROW(ptr) (ptr)->this->nrow
179
#define NCOL(ptr) (ptr)->this->ncol
180
#define REFCNT(ptr) (ptr)->this->refcount
181
#define M(ptr,i,j) (ptr)->this->data[(i) * NCOL(ptr) + (j)]
182
183
#define VARIABLESIZE sizeof(VARIABLE)
184
#define MATRIXSIZE sizeof(MATRIX)
185
#define MATSIZE(ptr) NROW(ptr)*NCOL(ptr)*sizeof(double)
186
187
#define TYPE_DOUBLE 0
188
#define TYPE_COMPLEX 1 /* this is not */
189
#define TYPE_STRING 2
190
191
/*******************************************************************
192
INTERNAL COMMANDS AND USER FUNCTIONS
193
*******************************************************************/
194
195
typedef struct command
196
{
197
struct command *next; /* pointer to next item in list */
198
char *name; /* name of the item */
199
int flags, /* CMDFLAG_PW & CMDFLAG_CE */
200
minp, maxp; /* min. and max. no. of parameters */
201
VARIABLE *(*sub)(); /* function to execute */
202
char *help; /* help string... */
203
} COMMAND;
204
205
#define COMSIZE sizeof(COMMAND)
206
207
#define CMDFLAG_PW 1 /* element by element operation */
208
#define CMDFLAG_CE 2 /* command can be executed when
209
preprocessing if constant
210
arguments. */
211
212
/*******************************************************************
213
USER DEFINED FUNCTIONS
214
*******************************************************************/
215
216
typedef struct function
217
{
218
struct function *next; /* pointer to next function in list */
219
char *name, /* name of the function */
220
**parnames, /* function parameter names (if any) */
221
**exports, /* functions exported variables */
222
**imports, /* functions imported variables */
223
*help; /* functions help text */
224
int parcount; /* defined number of parameters */
225
struct clause *body; /* function body */
226
} FUNCTION;
227
228
#define FUNCSIZE sizeof(FUNCTION)
229
230
/*******************************************************************
231
MISC DEFINITIONS FOR PARSER
232
*******************************************************************/
233
234
typedef enum symbols {
235
nullsym, leftpar, rightpar, indopen, indclose, power, times, ptimes, divide,
236
plus, minus, reduction, transpose, eq, neq, lt, gt, le, ge, and, or, not,
237
assignsym, apply, resize, vector, statemend, argsep, name, number, string,
238
funcsym, import, export, ifsym, thensym, elsesym, whilesym, forsym,
239
beginsym, endsym, breaksym, comment, systemcall
240
} SYMTYPE;
241
242
#ifdef MODULE_MATC
243
244
/*--------------------------------------------------------------------*/
245
246
SYMTYPE ssymbols[] = {
247
leftpar, rightpar, indopen, indclose, beginsym, endsym, power, times, ptimes,
248
divide, plus, minus, reduction, transpose, lt, gt, and, or, not,
249
assignsym, apply, resize, vector, statemend, argsep, comment, systemcall
250
};
251
252
char csymbols[] = {
253
'(', ')', '[', ']', '{', '}', '^', '*', '#', '/', '+', '-', '\?',
254
'\'', '<', '>', '&', '|', '~', '=', '@', '%', ':', ';', ',', '!', '$'
255
};
256
257
/*--------------------------------------------------------------------*/
258
259
/*--------------------------------------------------------------------*/
260
261
char *reswords[] = {
262
"function", "import", "export", "if", "then", "else", "while", "for",
263
"begin", "end", "break", NULL
264
};
265
266
SYMTYPE rsymbols[] = {
267
funcsym, import, export, ifsym, thensym, elsesym, whilesym, forsym,
268
beginsym, endsym, breaksym
269
};
270
271
/*--------------------------------------------------------------------*/
272
273
char *symchars = "`._";
274
275
#else
276
277
EXT SYMTYPE ssymbols[];
278
EXT char csymbols[];
279
EXT char *reswords[];
280
EXT SYMTYPE rsymbols[];
281
EXT char *symchars;
282
/* #pragma omp threadprivate(ssymbols, csymbols, reswords, rsymbols, symchars)
283
* TODO: this should be added when GCC 4.8 arrives if needed */
284
#endif
285
286
/*
287
dataentry for expression trees
288
*/
289
typedef struct treeentry
290
{
291
struct tree *args, /* parameters for functions */
292
*subs; /* indexes for VARIABLES, equation results */
293
int entrytype; /* type of entrydata */
294
295
union data_entry
296
{
297
char *s_data; /* function or VARIABLE names or string constants */
298
double d_data; /* numeric constant */
299
VARIABLE *c_data; /* real constant, with no name references */
300
MATRIX *(*v_data)(); /* function address (for builtin operations) */
301
} entrydata;
302
303
} TREEENTRY;
304
305
#define ETYPE_NAME 0
306
#define ETYPE_NUMBER 1
307
#define ETYPE_STRING 2
308
#define ETYPE_OPER 3
309
#define ETYPE_CONST 4
310
#define ETYPE_EQUAT 5
311
312
/*
313
* four leaf tree, isn't that odd
314
*/
315
typedef struct tree {
316
struct tree *next;
317
struct tree *link;
318
struct tree *left, *right;
319
TREEENTRY tentry;
320
} TREE;
321
322
/*
323
shortcuts for accessing above structures
324
*/
325
#define SDATA(ptr) (ptr)->tentry.entrydata.s_data
326
#define DDATA(ptr) (ptr)->tentry.entrydata.d_data
327
#define CDATA(ptr) (ptr)->tentry.entrydata.c_data
328
#define VDATA(ptr) (ptr)->tentry.entrydata.v_data
329
#define ETYPE(ptr) (ptr)->tentry.entrytype
330
#define SUBS(ptr) (ptr)->tentry.subs
331
#define ARGS(ptr) (ptr)->tentry.args
332
#define LEFT(ptr) (ptr)->left
333
#define RIGHT(ptr) (ptr)->right
334
335
/*
336
this is an operations list. data can be one of
337
the following:
338
339
ifsym, elsesym, whilesym, forsym, assignsym, funcsym
340
341
every input line is compiled to this type of list,
342
and it is used to hold function bodies.
343
*/
344
345
typedef struct clause
346
{
347
struct clause *link;
348
struct clause *jmp;
349
TREE *this;
350
SYMTYPE data;
351
} CLAUSE;
352
353
#define LINK(ptr) (ptr)->link
354
355
/*******************************************************************
356
THIS AND THAT
357
*******************************************************************/
358
359
#ifdef sign
360
# undef sign
361
#endif
362
#ifdef max
363
# undef max
364
#endif
365
#ifdef min
366
# undef min
367
#endif
368
#ifdef abs
369
# undef abs
370
#endif
371
372
#define sign(x) ((x) > 0 ? 1 : ((x) < 0 ? -1 : 0))
373
#define max(x,y) ((x) > (y) ? (x) : (y))
374
#define min(x,y) ((x) > (y) ? (y) : (x))
375
#define abs(x) ((x) > 0 ? (x) : -(x))
376
377
#define FOREVER for(;;)
378
#ifndef TRUE
379
#define TRUE 1
380
#endif
381
#ifndef FALSE
382
#define FALSE 0
383
#endif
384
385
/*
386
promptmode flags:
387
388
PMODE_MAIN ===> MATC>
389
PMODE_BLOCK ===> ....>
390
*/
391
#define PMODE_MAIN "MATC> "
392
#define PMODE_BLOCK "....> "
393
#define PMODE_CONT "####> "
394
395
EXT FILE *math_in, *math_out, *math_err;
396
#pragma omp threadprivate(math_in, math_out, math_err)
397
/*
398
see doread(), error() in matc.c
399
*/
400
EXT jmp_buf *jmpbuf;
401
#pragma omp threadprivate(jmpbuf)
402
403
EXT int term;
404
#pragma omp threadprivate(term)
405
406
#ifdef VAX
407
struct desc
408
{
409
int length;
410
char *addr;
411
} ;
412
#endif
413
414
#define COMMENT '!' /* comment introducer */
415
#define SYSTEM '$' /* system()-call introducer */
416
417
#define STRING_OUTPUT
418
419
#ifdef STRING_OUTPUT
420
#ifdef MODULE_MATC
421
static char *math_out_str = NULL;
422
static int math_out_count;
423
#pragma omp threadprivate (math_out_str, math_out_count)
424
#endif
425
#endif
426
427
void mem_free_all(void);
428
429
430
#ifdef MODULE_MATC
431
static int math_out_allocated = 0;
432
#pragma omp threadprivate (math_out_allocated)
433
void error_matc( char *format, ... )
434
{
435
va_list args;
436
437
va_start( args, format );
438
#ifdef STRING_OUTPUT
439
if ( math_out_count+512 > math_out_allocated )
440
{
441
math_out_allocated += 512;
442
math_out_str = (char *)realloc( math_out_str, math_out_allocated );
443
}
444
math_out_count += sprintf( &math_out_str[math_out_count], "MATC ERROR: " );
445
math_out_count += vsprintf( &math_out_str[math_out_count], format, args );
446
#else
447
fprintf( math_err, "MATC ERROR: " );
448
vfprintf( math_err, format, args );
449
#endif
450
va_end( args );
451
452
(void)mem_free_all();
453
longjmp( *jmpbuf, 2 );
454
}
455
456
void PrintOut( char *format, ... )
457
{
458
459
va_list args;
460
461
va_start( args, format );
462
#ifdef STRING_OUTPUT
463
if ( math_out_count+512 > math_out_allocated )
464
{
465
math_out_allocated += 512;
466
math_out_str = (char *)realloc( math_out_str, math_out_allocated );
467
}
468
math_out_count += vsprintf( &math_out_str[math_out_count], format, args );
469
#else
470
vfprintf( math_out, format, args );
471
#endif
472
va_end( args );
473
}
474
#else
475
extern void error_matc( char *format, ... );
476
extern void PrintOut( char *format, ... );
477
#endif
478
479
#define error error_matc
480
481
/*******************************************************************
482
function prototypes
483
*******************************************************************/
484
#include "fnames.h"
485
486