00001 // Simple mathematical formula processing library 00002 // Written by Atsushi Watanabe 00003 // Intelligent Robot Laboratory, University of Tsukuba 00004 // 00005 // Copyright 2011 Atsushi Watanabe, All rights reserved. 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // * Redistributions of source code must retain the above copyright notice, this 00011 // list of conditions and the following disclaimer. 00012 // * Redistributions in binary form must reproduce the above copyright notice, 00013 // this list of conditions and the following disclaimer in the documentation 00014 // and/or other materials provided with the distribution. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 00017 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00018 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 00019 // EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00020 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 #ifndef FORMULA_CALC_H 00028 #define FORMULA_CALC_H 00029 00030 #ifdef __cplusplus 00031 // clang-format off 00032 extern "C" 00033 { 00034 // clang-format on 00035 #endif // __cplusplus 00036 00037 #include <stdio.h> 00038 00039 enum rpf_type_t 00040 { 00041 TYPE_VALUE, 00042 TYPE_MATH, 00043 TYPE_VARIABLE, 00044 TYPE_OP, 00045 TYPE_MAX, 00046 TYPE_RPF, 00047 TYPE_START 00048 }; 00049 00050 struct rpf_t 00051 { 00052 enum rpf_type_t type; 00053 void *value; 00054 struct rpf_t *next; 00055 }; 00056 00057 struct variables_t 00058 { 00059 const char *name; 00060 double *pointer; 00061 }; 00062 00063 int formula(const char *expr, struct rpf_t **rpf, const struct variables_t *variable); 00064 void formula_free(struct rpf_t *rpf); 00065 double formula_eval(struct rpf_t *rpf); 00066 struct rpf_t *formula_optimize(struct rpf_t *rpf); 00067 void formula_print(FILE *stream, struct rpf_t *rpf); 00068 00069 #ifdef __cplusplus 00070 } 00071 #endif // __cplusplus 00072 #endif // FORMULA_CALC_H