changed copyright years in source files
[fegdk.git] / engine2 / src / shared / mathexpression.h
blobb11abdd4aef73f9df5de5d3057f11c9925a84e4e
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #ifndef __F_MATHEXPRESSION_H
24 #define __F_MATHEXPRESSION_H
26 #include "f_string.h"
27 #include "f_types.h"
28 #include "f_baseobject.h"
29 #include <vector>
30 #include "f_helpers.h"
32 namespace fe
35 class mathExpression : public baseObject
37 private:
38 // FIXME: do we need more than 2?
39 // FIXME: hi-level expressions never use more than 2
40 // FIXME: but if we allow user to write something more complex (and optimized) in assembly - 2 regs are insufficient
41 // FIXME: increment maxregisters to whatever required when assembly programs support is available
42 enum { maxregisters = 2 };
44 enum operator_t { op_no, op_add, op_sub, op_mul, op_div, op_dot, op_mov, op_lookup, op_max };
46 enum { parmtypemask = (0x0003 << 14), constmask = (0x0000 << 14), regmask = (0x0001 << 14), varmask = (0x0002 << 14), resmask = (0x0003 << 14) };
48 struct command_t {
49 ubyte opcode;
50 uchar dst;
51 ushort src0;
52 ushort src1;
55 struct node {
56 node () {
57 root = false;
58 leaf = false;
59 op = op_no;
60 reg = -1;
61 left = NULL;
62 right = NULL;
64 bool root;
65 bool leaf;
66 operator_t op;
67 int reg;
68 cStr strvalue;
70 node *left;
71 node *right;
74 std::vector< float > mConstants;
75 std::vector< command_t > mCommands;
77 void getwords (const cStr &in, std::vector< cStr > &expression);
78 bool validate (const std::vector<cStr> &expr);
79 cStr opstring (int op);
80 operator_t getoperator (const std::vector<cStr> &expression, size_t curword);
81 int getparamidx (const char *name) const;
82 float* getvarvalueptr (int v) const;
83 bool iscomplex (const cStr &s);
84 node* buildtree (std::vector<cStr> &expr);
85 cStr strop (operator_t op) const;
86 bool isconst (const char *s);
87 void traverse (node *n, std::vector< mathExpression::command_t > &commands, std::vector< float > &constants, int reg = 0);
88 void deltree (node *n);
89 #ifdef _DEBUG
90 void printProgram (const std::vector< mathExpression::command_t > &commands, const std::vector< float > &constants) const;
91 #endif
93 public:
95 mathExpression (void);
96 mathExpression (const char *expr);
98 ~mathExpression (void);
100 float evaluate (void);
103 typedef smartPtr <mathExpression> mathExpressionPtr;
107 #endif // __F_MATHEXPRESSION_H