00001
00002
00003
00004 #ifndef FITYK__AST__H__
00005 #define FITYK__AST__H__
00006
00007 #include <assert.h>
00008 #include "vm.h"
00009
00010 struct Tplate;
00011
00012
00013 struct OpTree
00014 {
00015
00016
00017
00018
00019 int op;
00020 OpTree *c1,
00021 *c2;
00022 realt val;
00023
00024 explicit OpTree(realt v) : op(0), c1(0), c2(0), val(v) {}
00025
00026 explicit OpTree(void*, int n) : op(-1-n), c1(0), c2(0), val(0.) {}
00027
00028 explicit OpTree(int n, OpTree *arg1) : op(n), c1(arg1), c2(0), val(0.)
00029 { assert(n >= OP_ONE_ARG && n < OP_TWO_ARG); }
00030
00031 explicit OpTree(int n, OpTree *arg1, OpTree *arg2)
00032 : op(n), c1(arg1), c2(arg2), val(0.) { assert(n >= OP_TWO_ARG); }
00033
00034 ~OpTree() { delete c1; delete c2; }
00035
00036 std::string str(const std::vector<std::string> *vars=NULL);
00037
00038 std::string str_b(bool b, const std::vector<std::string> *vars)
00039 { return b ? "(" + str(vars) + ")" : str(vars); }
00040 OpTree* clone() const;
00041
00042 OpTree* remove_c1() { OpTree *t=c1; c1=0; return t; }
00043 OpTree* remove_c2() { OpTree *t=c2; c2=0; return t; }
00044 void change_op(int op_) { op=op_; }
00045 bool operator==(const OpTree &t) const {
00046 return op == t.op && val == t.val
00047 && (c1 == t.c1 || (c1 && t.c1 && *c1 == *t.c1))
00048 && (c2 == t.c2 || (c2 && t.c2 && *c2 == *t.c2));
00049 }
00050 };
00051
00052 std::vector<OpTree*> prepare_ast_with_der(const VMData& vm, int len);
00053 std::string simplify_formula(const std::string &formula);
00054 void get_derivatives_str(const char* formula, std::string& result);
00055 void add_bytecode_from_tree(const OpTree* tree,
00056 const std::vector<int> &symbol_map, VMData& vm);
00057
00058 #endif
00059