home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d01xx / d0192.lha / Eval / eval.h < prev    next >
C/C++ Source or Header  |  1989-03-14  |  3KB  |  87 lines

  1. /***********************************************************************
  2.  *                                       *
  3.  *         A  F U N C T I O N   E V A L U A T O R            *
  4.  *                                       *
  5.  * Placed in the Public Domain by David Gay, 1988               *
  6.  *                                       *
  7.  ***********************************************************************/
  8.  
  9. /* Includes to allow the use of lists */
  10. #include <exec/types.h>
  11. #include <exec/lists.h>
  12. #include <proto/exec.h>
  13.  
  14. typedef struct _value *value;        /* An expression */
  15.  
  16. typedef struct List var_list;        /* A list of variables */
  17.  
  18. typedef struct {            /* A variable */
  19.     char *name;             /* It's name */
  20.     struct var *adr;            /* Cache */
  21.     long key;                /* Cache valid ? */
  22. } variable;
  23.  
  24. typedef struct List context;        /* A context */
  25.  
  26. extern int eval_error;            /* Last error */
  27.  
  28. /* The possible errors */
  29. #define SYNTAX 1            /* Syntax error */
  30. #define OUT_OF_MEM 2            /* Not enough memory */
  31. #define UNMATCHED 3            /* Right bracket expected */
  32. #define WANT_LEFT 5            /* Left bracket expected */
  33. #define NOT_DIFFERENTIABLE 6        /* Function not differentiable */
  34. #define RECURSIVE 7            /* Recursion not allowed ... */
  35. #define NOTNUM 8            /* A numeric expression was expected */
  36.  
  37. /* The flags for eval */
  38. #define PAT    0x0001            /* Do pattern based simplification */
  39. #define NICE    0x0002            /* Only do arithmetic if result integer */
  40. #define VAR    0x0004            /* Substitute values of variables */
  41. #define REC    0x0008            /* Idem, but evaluate them */
  42. #define NORED    0x0010            /* No constant folding */
  43.  
  44. /* The various routines */
  45. int    init_expr(void);            /* Must be called first */
  46. void    cleanup_expr(void);         /* Once you've finished */
  47.  
  48. value    compile(char *);            /* "Compile" an expression */
  49. char    *decompile(value, char *, int); /* Reconstruct string from expression */
  50. void    free_expr(value);           /* Free memory used by expression */
  51.  
  52. value    eval(value, int);           /* Evaluate */
  53. double    quick_eval(value);          /* Evaluate quickly, get a numeric result */
  54.  
  55. value    differentiate(value, char *);
  56.  
  57. int    create_var(variable *);
  58. void    free_var(variable *);
  59. int    set_var(variable *, value);
  60. value    get_var(variable *);
  61.  
  62. int    create_var_name(char *);
  63. void    free_var_name(char *);
  64. value    get_var_name(char *);       /* by name, not through structure */
  65. int    set_var_name(char *, value);
  66.  
  67. int    create_quick(variable *);   /* Create a numeric variable */
  68. void    free_quick(variable *);
  69. void    set_quick(variable *, double);
  70. double    get_quick(variable *);
  71.  
  72. var_list *make_var_list(value, var_list *); /* make a list of vars used in expr */
  73. void    free_var_list(var_list *);
  74. int    create_vars(var_list *);    /* Create the vars in a list */
  75. void    free_vars(var_list *);
  76.  
  77. void    set_context(context *);     /* Set the new context */
  78.  
  79. #define init_context(cont) NEWLIST(cont) /* Initialise a context */
  80. #define init_var_list(list) NEWLIST(list)
  81. #define NEWLIST(list) \
  82.     { (list)->lh_Head = (struct Node *)&(list)->lh_Tail; \
  83.       (list)->lh_TailPred = (struct Node *)&(list)->lh_Head; \
  84.       (list)->lh_Tail = 0; \
  85.     }
  86.  
  87.