expr.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE: expr.h
00004 
00005     DESCRIPTION: expression object include file.
00006 
00007     CREATED BY: Don Brittain
00008 
00009     HISTORY:
00010 
00011  *> Copyright (c) 1994, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #pragma once
00015 
00016 #include "maxheap.h"
00017 #include "export.h"
00018 #include "strclass.h"
00019 #include "Point3.h"
00020 #include "TabTypes.h"
00021 
00024 #define SCALAR_EXPR     1   //!< A single floating point value.
00025 
00027 #define VECTOR_EXPR     3   //!< An array of floating point values.
00028 
00029 
00033 #define SCALAR_VAR      SCALAR_EXPR //!< A single floating point value.
00034 
00038 #define VECTOR_VAR      VECTOR_EXPR //!< A Point3 value when passed to the eval() method.
00039 
00040 
00041 class Expr;
00042 
00043 typedef int (*ExprFunc)(Expr *e, float f);
00044 
00045 class DllExport Inst: public MaxHeapOperators {
00046 public:
00047     ExprFunc    func;
00048     float       sVal;
00049 };
00050 
00051 class ExprVar : public MaxHeapOperators {
00052 public:
00053     MSTR        name;
00054     int     type;
00055     int     regNum;
00056 };
00057 
00058 
00059 typedef Tab<Inst>    InstTab;
00060 typedef Tab<ExprVar> ExprVarTab;
00061 
00332 class Expr: public MaxHeapOperators {
00333 public:
00336     Expr()  { sValStk = vValStk = instStk = nextScalar = nextVector = 0; }
00338     ~Expr() { deleteAllVars(); }
00339 
00348     DllExport int       load(MCHAR *s);
00366     DllExport int       eval(float *ans, int sRegCt, float *sRegs, int vRegCt=0, Point3 *vRegs=NULL);
00368     int                 getExprType(void)   { return exprType; }
00371     MCHAR *             getExprStr(void)    { return origStr; }
00375     MCHAR *             getProgressStr(void){ return progressStr; }
00385     DllExport int       defVar(int type, MCHAR *name);
00393     DllExport int       getVarCount(int type);
00401     DllExport MCHAR *   getVarName(int type, int i);
00427     DllExport int       getVarRegNum(int type, int i);
00431     DllExport BOOL      deleteAllVars();
00442     DllExport BOOL      deleteVar(MCHAR *name);
00443 
00444 // pseudo-private: (only to be used by the "instruction" functions
00445     void        setExprType(int type)   { exprType = type; }
00446     void        pushInst(ExprFunc fn, float f) 
00447                     { if(instStk >= inst.Count()) inst.SetCount(instStk+30); 
00448                     inst[instStk].func = fn; inst[instStk++].sVal = f; }
00449     void        pushSVal(float f)   { if(sValStk>=sVal.Count())sVal.SetCount(sValStk+10);sVal[sValStk++]=f; }
00450     float       popSVal()           { return sVal[--sValStk]; }
00451     void        pushVVal(Point3 &v) { if(vValStk>=vVal.Count())vVal.SetCount(vValStk+10);vVal[vValStk++]=v; }
00452     Point3 &    popVVal()           { return vVal[--vValStk]; }
00453     int         getSRegCt(void)     { return sRegCt; }
00454     float       getSReg(int index)  { return sRegPtr[index]; }
00455     int         getVRegCt(void)     { return vRegCt; }
00456     Point3 &    getVReg(int index)  { return vRegPtr[index]; }
00457 
00458     ExprVarTab  vars;           // named variables
00459 private:
00460     MCHAR *     exprPtr;        // pointer to current str pos during parsing
00461     MCHAR *     exprStr;        // ptr to original expression string to parse
00462     MSTR        origStr;        // original expression string that was loaded
00463     MSTR        progressStr;    // string to hold part of expr successfully parsed
00464     int         sRegCt;         // actual number of scalar registers passed to "eval"
00465     float       *sRegPtr;       // pointer to the scalar register array
00466     int         vRegCt;         // actual number of vector registers passed to "eval"
00467     Point3      *vRegPtr;       // pointer to the vector register array
00468     int         exprType;       // expression type: SCALAR_EXPR or VECTOR_EXPR (set by load)
00469 
00470     int         sValStk;        // scalar value stack
00471     floatTab    sVal;
00472     int         vValStk;        // vector value stack
00473     Point3Tab   vVal;
00474     int         instStk;        // instruction stack
00475     InstTab     inst;
00476 
00477     int         nextScalar;     // next scalar slot
00478     int         nextVector;     // next vector slot
00479 
00480     friend      int yylex();
00481     friend      int yyerror(char *);
00482 };
00483 
00486 #define EXPR_NORMAL          0  //!< No problems, expression evaluated successfully.
00487 #define EXPR_INST_OVERFLOW  -1  //!< Instruction stack overflow during parsing.
00488 #define EXPR_UNKNOWN_TOKEN  -2  //!< Unknown function, const, or reg during parsing.
00489 #define EXPR_TOO_MANY_VARS  -3  //!< Value stack overflow.
00490 #define EXPR_TOO_MANY_REGS  -4  //!< Register array overflow, or reg number too big.
00491 #define EXPR_CANT_EVAL      -5  //!< Function can't be evaluated with given arguments.
00492 #define EXPR_CANT_PARSE     -6  //!< Expression can't be parsed syntactically.
00493 
00494