void Utility::TestExpr() { // Declare an expression instance and variable storage Expr expr; float sRegs[2]; // Must be at least getVarCount(SCALAR_VAR); Point3 vRegs[2]; // Must be at least getVarCount(VECTOR_VAR); float ans[3]; int status; // Define a few expressions char e0[] = "2+2"; char e1[] = "2.0 * pi * radius"; char e2[] = "[1,1,0] + axis"; char e3[] = "[sin(90.0), sin(radToDeg(0.5*pi)), axis.z]"; char e4[] = "2+2*!@#$%"; // Bad expression // Define variables int radiusReg = expr.defVar(SCALAR_VAR, _M("radius")); int axisReg = expr.defVar(VECTOR_VAR, _M("axis")); // Set the variable values sRegs[radiusReg] = 50.0f; vRegs[axisReg] = Point3(0.0f, 0.0f, 1.0f); // Get the number of each we have defined so far int sCount = expr.getVarCount(SCALAR_VAR); int vCount = expr.getVarCount(VECTOR_VAR); // Load and evaluate expression "e0" if (status = expr.load(e0)) HandleLoadError(status, expr); else { status = expr.eval(ans, sCount, sRegs, vCount, vRegs); if (status != EXPR_NORMAL) HandleEvalError(status, expr); else DisplayExprResult(expr, ans); } // Load and evaluate expression "e1" if (status = expr.load(e1)) HandleLoadError(status, expr); else { status = expr.eval(ans, sCount, sRegs, vCount, vRegs); if (status != EXPR_NORMAL) HandleEvalError(status, expr); else DisplayExprResult(expr, ans); } // Load and evaluate expression "e2" if (status = expr.load(e2)) HandleLoadError(status, expr); else { status = expr.eval(ans, sCount, sRegs, vCount, vRegs); if (status != EXPR_NORMAL) HandleEvalError(status, expr); else DisplayExprResult(expr, ans); } // Load and evaluate expression "e3" if (status = expr.load(e3)) HandleLoadError(status, expr); else { status = expr.eval(ans, sCount, sRegs, vCount, vRegs); if (status != EXPR_NORMAL) HandleEvalError(status, expr); else DisplayExprResult(expr, ans); } // Load and evaluate expression "e4" if (status = expr.load(e4)) HandleLoadError(status, expr); else { status = expr.eval(ans, sCount, sRegs, vCount, vRegs); if (status != EXPR_NORMAL) HandleEvalError(status, expr); else DisplayExprResult(expr, ans); } } // Display the expression and the result void Utility::DisplayExprResult(Expr expr, float *ans) { MCHAR msg[128]; if (expr.getExprType() == SCALAR_EXPR) { _stprintf(msg, _M("Answer to \"%s\" is %.1f"), expr.getExprStr(), *ans); Message(msg, _M("Expression Result")); } else { _stprintf(msg, _M("Answer to \"%s\" is [%.1f, %.1f, %.1f]"), expr.getExprStr(), ans[0], ans[1], ans[2]); Message(msg, _M("Expression Result")); } } // Display the load error message void Utility::HandleLoadError(int status, Expr expr) { MCHAR msg[128]; if(status == EXPR_INST_OVERFLOW) { _stprintf(_M("Inst stack overflow: %s"), expr.getProgressStr()); Message(msg, _M("Error")); } else if (status == EXPR_UNKNOWN_TOKEN) { _stprintf(msg, _M("Unknown token: %s"), expr.getProgressStr()); Message(msg, _M("Error")); } else { _stprintf(msg, _M("Cannot parse \"%s\". Error begins at last char of: %s"), expr.getExprStr(), expr.getProgressStr()); Message(msg, _M("Error")); } } // Display the evaluation error message void Utility::HandleEvalError(int status, Expr expr) { MCHAR msg[128]; _stprintf(msg, _M("Can't parse expression \"%s\""), expr.getExprStr()); Message(msg, _M("Error")); } // Display the specified message and title in a dialog box void Utility::Message(MCHAR *msg, MCHAR *title) { MessageBox(ip->GetMAXHWnd(), (LPCMSTR) msg, (LPCMSTR) title, MB_ICONINFORMATION|MB_OK); }
#include <expr.h>
Public Member Functions |
|
Expr () | |
~Expr () | |
DllExport int | load (MCHAR *s) |
DllExport int | eval (float *ans, int sRegCt, float *sRegs, int vRegCt=0, Point3 *vRegs=NULL) |
int | getExprType (void) |
MCHAR * | getExprStr (void) |
MCHAR * | getProgressStr (void) |
DllExport int | defVar (int type, MCHAR *name) |
DllExport int | getVarCount (int type) |
DllExport MCHAR * | getVarName (int type, int i) |
DllExport int | getVarRegNum (int type, int i) |
DllExport BOOL | deleteAllVars () |
DllExport BOOL | deleteVar (MCHAR *name) |
void | setExprType (int type) |
void | pushInst (ExprFunc fn, float f) |
void | pushSVal (float f) |
float | popSVal () |
void | pushVVal (Point3 &v) |
Point3 & | popVVal () |
int | getSRegCt (void) |
float | getSReg (int index) |
int | getVRegCt (void) |
Point3 & | getVReg (int index) |
Public Attributes |
|
ExprVarTab | vars |
Friends |
|
int | yylex () |
int | yyerror (char *) |
Expr | ( | ) | [inline] |
{ sValStk = vValStk = instStk = nextScalar = nextVector = 0; }
~Expr | ( | ) | [inline] |
{ deleteAllVars(); }
DllExport int load | ( | MCHAR * | s | ) |
DllExport int eval | ( | float * | ans, |
int | sRegCt, | ||
float * | sRegs, | ||
int | vRegCt = 0 , |
||
Point3 * | vRegs = NULL |
||
) |
int getExprType | ( | void | ) | [inline] |
{ return exprType; }
MCHAR* getExprStr | ( | void | ) | [inline] |
{ return origStr; }
MCHAR* getProgressStr | ( | void | ) | [inline] |
{ return progressStr; }
DllExport int defVar | ( | int | type, |
MCHAR * | name | ||
) |
DllExport int getVarCount | ( | int | type | ) |
DllExport MCHAR* getVarName | ( | int | type, |
int | i | ||
) |
DllExport int getVarRegNum | ( | int | type, |
int | i | ||
) |
DllExport BOOL deleteAllVars | ( | ) |
DllExport BOOL deleteVar | ( | MCHAR * | name | ) |
void setExprType | ( | int | type | ) | [inline] |
{ exprType = type; }
void pushInst | ( | ExprFunc | fn, |
float | f | ||
) | [inline] |
void pushSVal | ( | float | f | ) | [inline] |
float popSVal | ( | ) | [inline] |
{ return sVal[--sValStk]; }
void pushVVal | ( | Point3 & | v | ) | [inline] |
Point3& popVVal | ( | ) | [inline] |
{ return vVal[--vValStk]; }
int getSRegCt | ( | void | ) | [inline] |
{ return sRegCt; }
float getSReg | ( | int | index | ) | [inline] |
{ return sRegPtr[index]; }
int getVRegCt | ( | void | ) | [inline] |
{ return vRegCt; }
Point3& getVReg | ( | int | index | ) | [inline] |
{ return vRegPtr[index]; }
int yylex | ( | ) | [friend] |
int yyerror | ( | char * | ) | [friend] |