From f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 03:49:53 +0000 Subject: Ok, no code is left in src, it's all in src/old. We'll gradually move code back into src as it's fixed and re-org'd. This includes tests, which, I may write a unit test system into libbu++ just to make my life easier. --- src/old/formula.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/old/formula.h (limited to 'src/old/formula.h') diff --git a/src/old/formula.h b/src/old/formula.h new file mode 100644 index 0000000..939eb09 --- /dev/null +++ b/src/old/formula.h @@ -0,0 +1,77 @@ +#ifndef FORMULA_H +#define FORMULA_H + +#include + +#include +#include +#include "sbuffer.h" + +#include "exceptionbase.h" +#include "hash.h" + +subExceptionDecl( ParseException ); + +/** + * Implements a very simple formula parser that allows use of variables and + * custom functions. This is based on a simple calculator-type parser that + * executes as it processes, accounting for operator precedence and grouping. + */ +class Formula +{ +public: + Formula(); + virtual ~Formula(); + + double run( char *sFormula ); + + typedef Hash varHash; + varHash hVars; + + typedef struct Func + { + double operator()( double x ) + { + return 0.0; + } + } Func; + + typedef Hash funcHash; + funcHash hFunc; + + typedef struct FuncSin : Func + { + double operator()( double x ) + { + return sin( x ); + } + } FuncSin; + +private: + enum + { + symEOS, + symAdd, + symSubtract, + symMultiply, + symDivide, + symOpenParen, + symCloseParen, + symNumber, + symVariable, + symExponent, + symModulus + }; + + typedef uint8_t symType; + + std::stack sOper; + std::stack sValue; + +private: + symType getPrec( symType nOper ); + symType nextToken( char **sBuf ); + void reduce( bool bCloseParen = false ); +}; + +#endif -- cgit v1.2.3