00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <SeExpression.h>
00011 #include <cstdlib>
00012 #include <cstdio>
00013 #include <cstring>
00014
00015 #define STACK_DEPTH 256
00016
00019
00020 class CalculatorExpr : public SeExpression
00021 {
00022 public:
00024 CalculatorExpr(const std::string& expr)
00025 :SeExpression(expr), _count(0)
00026 {
00027 for(int i = 0; i < STACK_DEPTH; i++) {
00028 stack[i].val = SeVec3d(0.0);
00029 fail_stack[i] = false;
00030 };
00031 };
00032
00034 CalculatorExpr()
00035 :SeExpression(), _count(0)
00036 {
00037 for(int i = 0; i < STACK_DEPTH; i++)
00038 fail_stack[i] = false;
00039 };
00040
00042 void push() {
00043 stack[_count].val = evaluate();
00044 _count++;
00045 };
00046
00048 void fail_push() {
00049 fail_stack[_count] = true;
00050 stack[_count].val = SeVec3d(0.0);
00051 _count++;
00052 };
00053
00054 int count() const { return _count; };
00055
00056 private:
00058 struct SimpleVar:public SeExprVectorVarRef
00059 {
00060 SimpleVar()
00061 :val(SeVec3d(0.0))
00062 {}
00063
00064 SeVec3d val;
00065
00066 void eval(const SeExprVarNode* ,SeVec3d& result)
00067 {
00068 result = val;
00069 }
00070 };
00071
00073 mutable SimpleVar stack [STACK_DEPTH];
00074 mutable bool fail_stack[STACK_DEPTH];
00075 mutable int _count;
00076
00078 SeExprVarRef* resolveVar(const std::string& name) const {
00079 if(name[0] == '_') {
00080 int position = atoi(name.substr(1,name.size() - 1).c_str());
00081 if(position >= count())
00082 std::cerr << "Use of unused result line." << std::endl;
00083 if(fail_stack[position])
00084 std::cerr << "Use of invalid result line." << std::endl;
00085 return &(stack[position]);
00086 };
00087 return 0;
00088 };
00089 };
00090
00091
00092 void quit(const std::string & str) {
00093 if(str == "quit"
00094 || str == "q")
00095 exit(0);
00096 };
00097
00098
00099 int main()
00100 {
00101 CalculatorExpr expr;
00102 std::string str;
00103
00104 std::cout << "SeExpr Basic Calculator";
00105
00106 while(true) {
00107 std::cout << std::endl << expr.count() << "> ";
00108
00109 getline(std::cin, str);
00110
00111 if(std::cin.eof()) {
00112 std::cout << std::endl;
00113 str = "q";
00114 };
00115
00116 quit(str);
00117 expr.setWantVec(true);
00118 expr.setExpr(str);
00119
00120 if(!expr.isValid()) {
00121 expr.fail_push();
00122 std::cerr << "Expression failed: " << expr.parseError() << std::endl;
00123 } else {
00124 expr.push();
00125 std::cout << " " << expr.evaluate();
00126 };
00127 };
00128
00129 return 0;
00130 }