typedef struct cell { int row_fixed, row, col_fixed, col; } Cell; typedef struct range { Cell start, end; } Range; /* A0 reference cell constant */ extern Cell cell_00; /* print range and format */ extern Range print_Range; extern int print_Format; /* debug, output, and other globals */ extern int debug, headers, dir, depend; extern FILE *out; /* print cell in internal and string form */ void debug_cell( const Cell *c, const Cell *ref); /* print cell or range in string form */ void print_col( int col); void print_cell( const Cell *c, const Cell *ref); void print_cell_RC( const Cell *c); void print_cell_CR( const Cell *c); void print_range( const Range *r, const Cell *ref); void print_range_RC( const Range *r); void print_range_CR( const Range *r); /* check cell for bounds and min/max */ int check_cell( const Cell *c); /* set actual cell locations */ void adjust_cell( Cell *c, const Cell *ref_cell); void adjust_range( Range *r, const Cell *ref_cell); /* dynamically copy or concatenate strings or exit if malloc fails */ char *estrdup( const char *str); char *estrcat( char *str1, const char *str2); /* have to declare these somewhere... */ extern int ss_yywrap( void); extern void yyerror( const char *msg); /* routines from mat.c */ double **matalloc( int m, int n); void matfree( double **a); void matprint( const char *msg, const char *fmt, double **a, int m, int n); void vecprint( const char *msg, const char *fmt, double *a, int n); int svd( double **u, double *z, double **v, int m, int n, double eps); /*** state and eval ***/ #define UNEVALUATED 0 // for Symbol and SS state fields #define EVALUATED 1 #define INPROGRESS 2 extern int evaluated; // normally set to EVALUATED, but see rf/search.c #define EVAL_RANGE 0 // for eval and reset commands, #define EVAL_SYMS 1 // see ss.c: ss_eval() and ss_eval_reset() #define EVAL_RANGE_SYMS 2 #define EVAL_SYMS_RANGE 3 #define EVAL_ALL 4 /*** symbol table - linked list ***/ typedef struct symbol { const char *name; int constant; double val; int state; struct symbol *depend; // for indirect dependency struct node *n; struct symbol *next; } Symbol; Symbol *lookup_symbol( const char *); Symbol *find_symbol( const char *); void install_constant( const char *, double); void install_symbol( Symbol *, struct node *); int eval_symbols( void); void reset_symbols( void); void print_symbols( void); void print_symbol_states( void); void print_constants( void); /*** parse tree - binary tree ***/ /* node types: EMPTY STRING NUMBER CELL, RCCELL RANGE, RCRANGE, A0RCRANGE, RCA0RANGE ID operator: '*' '/' '%' '+' '-' UMINUS etc... nfunc token: ATAN SQRT etc... rfunc token: AVG COUNT MAX MIN PROD STDEV SUM etc... */ typedef struct children { struct node *left, *right; } Children; typedef struct node { int type; union { const char *str; /* for STRING */ double val; /* for NUMBER */ Cell c; /* for CELL */ Range r; /* for RANGE */ Symbol *s; /* for ID */ Children t; /* for tree left/right pointers */ } u; struct node *next; /* for expr_list */ } Node; // some handy macros for the Node union elements // #define Left(n) ((n)->u.t.left) #define Right(n) ((n)->u.t.right) // range size // #define Rsize(n) \ ( (abs(n->u.r.end.row - n->u.r.start.row)+1) * \ (abs(n->u.r.end.col - n->u.r.start.col)+1) ) typedef struct expr_list { Node *head, *tail; int count; } Expr_list; Node *add_func( int, Expr_list *, Expr_list *); Node *add_op( int, Node *, Node *); Node *add_cond( Node *, Node *, Node *); Node *add_string( const char *); Node *add_number( double); Node *add_empty( void); Node *add_cell( const Cell *, int); Node *add_range( const Range *, int); Node *add_symbol( Symbol *); Expr_list *listify_ranges( Expr_list *); Node *rangeify( Node *); void add_range_depend( Node *, Symbol *); Expr_list *new_expr_list( Node *); void append_expr_list( Expr_list *, Node *); /*** spreadsheet 2D array ***/ typedef struct ss { int used; double val; int state; struct symbol *depend; // for indirect dependency const char *format; Node *n; } SS; extern int SS_MAXROWS, SS_MAXCOLS; extern SS **ss; extern Range used; /* range of cells being used */ extern const char *global_format; extern const char **row_format; extern const char **col_format; /* allocate ss, row_format, and col_format arrays, initialize Range used */ void init_ss( void); /* print or plot the spreadsheet */ void ss_print( int); void ss_plot2d( const Node *rn); void ss_plot3d( const Node *rn); void print_tree( const Node *, const Cell *, int); double eval_symbol( Symbol *); double eval_cell( SS *, const Cell *); double eval_tree( const Node *, const Cell *); void ss_eval( const Node *, int, int); void ss_eval_reset( const Node *, int); extern const char *symbol_format; void install_symbol_format( const char *); void install_format( const char *); void install_col_format( int, const char *); void install_row_format( int, const char *); void install_cell_format( const Node *, const char *); void install_range_format( const Node *, const char *); void install_cell( const Node *, Node *, int, int); void install_range( const Node *, Expr_list *, int); void install_tree( Node *, const Cell *, int); /*** compile-time action routines ***/ void ss_srand( Node *); void ss_copy( const Node *rd, const Node *rs); void ss_fill_expr_list( Node *rn, Expr_list *e); void ss_fill( const Node *rn, Expr_list *e); void ss_ttfill( const Node *rn); void ss_dfill( const Node *rn, Expr_list *e); /*** for simplex.c and rf/search.c ***/ typedef struct { int N; // number of independent variables int L, NH, H; // indices of Lowest, Next-to-Highest, Highest, int C, D, R; // Centroid, Direction, Reflection, int S, T; // Stepsize and Temporary points int count; // function evaluation count double size; // simplex size double *d; // distances from x[L] double *f; // function values double **x; // x[i], i=0...N, the N+1 simplex points // i=N+1...N+5, Centroid, Direction, Reflection, // Stepsize, Temporary double **u; // for SVD condition check, x[i]-x[L], i=0...N-1 double *z; // squared singular values // SS-specific data: SS **xSS; // x cells Node *fSS; // function formula Cell *cSS; // reference cell } Simplex; Simplex *simplex_create( int); void simplex_free( Simplex *); double simplex( Simplex *, int); /*** ss functions ***/ /* function type bitmasks: operator, assignment operator, numeric, range */ #define SS_OP 1 #define SS_WORD 2 #define SS_ASGN 4 #define SS_NF 8 #define SS_RF 16 typedef double (*SS_Func)( const Node *, const Cell *); typedef struct { const char *name; int type, rcount, nargs; /* rcount and nargs are ignored if < 0 */ SS_Func f; const char *desc; } Func; extern Func *func[]; double bad_func( const Node *, const Cell *); void print_func( void); void init_func( void); void func_help( const char *what); /* operand functions */ double op_pp_mm( const Node *, const Cell *); double op_addr( const Node *, const Cell *); double op_cast_int( const Node *, const Cell *); double op_cast_long( const Node *, const Cell *); double op_cast_double( const Node *, const Cell *); double op_pow( const Node *, const Cell *); double op_mul( const Node *, const Cell *); double op_div( const Node *, const Cell *); double op_mod( const Node *, const Cell *); double op_add( const Node *, const Cell *); double op_sub( const Node *, const Cell *); double op_shl( const Node *, const Cell *); double op_shr( const Node *, const Cell *); double op_bit_and( const Node *, const Cell *); double op_bit_or( const Node *, const Cell *); double op_bit_xor( const Node *, const Cell *); double op_bit_not( const Node *, const Cell *); double op_and( const Node *, const Cell *); double op_or( const Node *, const Cell *); double op_xor( const Node *, const Cell *); double op_not( const Node *, const Cell *); double op_uminus( const Node *, const Cell *); double op_cond( const Node *, const Cell *); double op_lt( const Node *, const Cell *); double op_le( const Node *, const Cell *); double op_eq( const Node *, const Cell *); double op_ne( const Node *, const Cell *); double op_ge( const Node *, const Cell *); double op_gt( const Node *, const Cell *); double op_asgn( const Node *, const Cell *); double op_comma( const Node *, const Cell *); /* numeric functions */ #include "tmp/nf.h" /* range functions */ #include "tmp/rf.h"