Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "trandom.h"
Toshihiro Shimizu 890ddd
#include "tutil.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "ttokenizer.h"
Toshihiro Shimizu 890ddd
#include "tunit.h"
Toshihiro Shimizu 890ddd
#include "tparser.h"
Toshihiro Shimizu 890ddd
#include "tdoubleparam.h"
Toshihiro Shimizu 890ddd
#include "tdoublekeyframe.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include <map></map>
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <functional></functional>
Campbell Barton 40cabe
#include <memory></memory>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgrammar.h"
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static const double PI = 4 * atan(1.0);
Campbell Barton 8c6c57
static const double toDeg(double rad) { return rad * 180.0 / PI; }
Campbell Barton 8c6c57
static const double toRad(double deg) { return deg / 180.0 * PI; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TSyntax {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// RandomManager
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//   es. x = RandomManager::instance()->getValue(seed, frame);
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class RandomSequence {
Shinya Kitaoka 120a6e
  TRandom m_rnd;
Shinya Kitaoka 120a6e
  std::vector<double> m_values;</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RandomSequence(UINT seed) : m_rnd(seed) {}
Shinya Kitaoka 120a6e
  double getValue(int i) {
Shinya Kitaoka 120a6e
    assert(i >= 0);
Shinya Kitaoka 120a6e
    if (i >= (int)m_values.size()) {
Shinya Kitaoka 120a6e
      m_values.reserve(i + 1);
Shinya Kitaoka 120a6e
      while (i >= (int)m_values.size()) m_values.push_back(m_rnd.getDouble());
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return m_values[i];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class RandomManager {  // singleton
Shinya Kitaoka 120a6e
  typedef std::map<uint, *="" randomsequence=""> Table;</uint,>
Shinya Kitaoka 120a6e
  Table m_table;
Shinya Kitaoka 120a6e
  RandomManager() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  ~RandomManager() {
Shinya Kitaoka 120a6e
    for (Table::iterator i = m_table.begin(); i != m_table.end(); ++i)
Shinya Kitaoka 120a6e
      delete i->second;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  static RandomManager *instance() {
Shinya Kitaoka 120a6e
    static RandomManager _instance;
Shinya Kitaoka 120a6e
    return &_instance;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  RandomSequence *getSequence(UINT seed) {
Shinya Kitaoka 120a6e
    Table::iterator i = m_table.find(seed);
Shinya Kitaoka 120a6e
    if (i == m_table.end()) {
Shinya Kitaoka 120a6e
      RandomSequence *seq = new RandomSequence(seed);
Shinya Kitaoka 120a6e
      m_table[seed]       = seq;
Shinya Kitaoka 120a6e
      return seq;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      return i->second;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double getValue(UINT seed, double t) {
Shinya Kitaoka 120a6e
    RandomSequence *seq = getSequence(seed);
Shinya Kitaoka 120a6e
    return seq->getValue(t > 0 ? tfloor(t) : 0);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// Calculator
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Calculator::Calculator() : m_rootNode(0), m_param(0), m_unit(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Calculator::~Calculator() { delete m_rootNode; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Calculator::setRootNode(CalculatorNode *node) {
Shinya Kitaoka 120a6e
  if (node != m_rootNode) {
Shinya Kitaoka 120a6e
    delete m_rootNode;
Shinya Kitaoka 120a6e
    m_rootNode = node;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// Nodes
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class op=""></class>
Shinya Kitaoka d1f6c4
class Op0Node final : public CalculatorNode {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Op0Node(Calculator *calc) : CalculatorNode(calc) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    Op op;
Shinya Kitaoka 120a6e
    return op();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class op=""></class>
Shinya Kitaoka d1f6c4
class Op1Node final : public CalculatorNode {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Op1Node(Calculator *calc, CalculatorNode *a) : CalculatorNode(calc), m_a(a) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    Op op;
Shinya Kitaoka 120a6e
    return op(m_a->compute(vars));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override { m_a->accept(visitor); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class op=""></class>
Shinya Kitaoka d1f6c4
class Op2Node final : public CalculatorNode {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a, m_b;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Op2Node(Calculator *calc, CalculatorNode *a, CalculatorNode *b)
Shinya Kitaoka 120a6e
      : CalculatorNode(calc), m_a(a), m_b(b) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    Op op;
Shinya Kitaoka 120a6e
    return op(m_a->compute(vars), m_b->compute(vars));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override {
Shinya Kitaoka 120a6e
    m_a->accept(visitor), m_b->accept(visitor);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class op=""></class>
Shinya Kitaoka d1f6c4
class Op3Node final : public CalculatorNode {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a, m_b, m_c;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Op3Node(Calculator *calc, CalculatorNode *a, CalculatorNode *b,
Shinya Kitaoka 120a6e
          CalculatorNode *c)
Shinya Kitaoka 120a6e
      : CalculatorNode(calc), m_a(a), m_b(b), m_c(c) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    Op op;
Shinya Kitaoka 120a6e
    return op(m_a->compute(vars), m_b->compute(vars), m_c->compute(vars));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override {
Shinya Kitaoka 120a6e
    m_a->accept(visitor), m_b->accept(visitor), m_c->accept(visitor);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class ChsNode final : public CalculatorNode {
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ChsNode(Calculator *calc, CalculatorNode *a) : CalculatorNode(calc), m_a(a) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override { return -m_a->compute(vars); }
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override { m_a->accept(visitor); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class QuestionNode final : public CalculatorNode {
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a, m_b, m_c;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  QuestionNode(Calculator *calc, CalculatorNode *a, CalculatorNode *b,
Shinya Kitaoka 120a6e
               CalculatorNode *c)
Shinya Kitaoka 120a6e
      : CalculatorNode(calc), m_a(a), m_b(b), m_c(c) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    return (m_a->compute(vars) != 0) ? m_b->compute(vars) : m_c->compute(vars);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override {
Shinya Kitaoka 120a6e
    m_a->accept(visitor), m_b->accept(visitor), m_c->accept(visitor);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class NotNode final : public CalculatorNode {
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  NotNode(Calculator *calc, CalculatorNode *a) : CalculatorNode(calc), m_a(a) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 38fd86
  double compute(double vars[3]) const override {
Shinya Kitaoka 38fd86
    return m_a->compute(vars) == 0;
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override { m_a->accept(visitor); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class CycleNode final : public CalculatorNode {
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_a;</calculatornode>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  CycleNode(Calculator *calc, CalculatorNode *a)
Shinya Kitaoka 120a6e
      : CalculatorNode(calc), m_a(a) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    struct locals {
Shinya Kitaoka 120a6e
      static inline double compute(const TDoubleParam ¶m, double f) {
Shinya Kitaoka 120a6e
        if (param.getKeyframeCount() >= 2 &&
Shinya Kitaoka 120a6e
            f < param.keyframeIndexToFrame(0)) {
Shinya Kitaoka 120a6e
          TDoubleKeyframe kf = param.getKeyframe(0);
Shinya Kitaoka 120a6e
          if (kf.m_type == TDoubleKeyframe::Expression ||
Shinya Kitaoka 120a6e
              kf.m_type == TDoubleKeyframe::SimilarShape)
Shinya Kitaoka 120a6e
            return param.getDefaultValue();
Shinya Kitaoka 120a6e
        }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        double value = param.getValue(f);
Shinya Kitaoka 120a6e
        return value;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    };
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    double delta = std::max(1.0, m_a->compute(vars));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    const TDoubleParam *ownerParam = getCalculator()->getOwnerParameter();
Shinya Kitaoka 120a6e
    if (!ownerParam) return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    double value = locals::compute(*ownerParam, vars[FRAME] - 1 - delta);
Shinya Kitaoka 120a6e
    if (getCalculator()->getUnit())
Shinya Kitaoka 120a6e
      value = getCalculator()->getUnit()->convertTo(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    return value;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override { m_a->accept(visitor); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class RandomNode final : public CalculatorNode {
Shinya Kitaoka 2a7129
  std::unique_ptr<calculatornode> m_seed, m_min, m_max, m_arg;</calculatornode>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  RandomNode(Calculator *calc)
Shinya Kitaoka 2a7129
      : CalculatorNode(calc)
Shinya Kitaoka 2a7129
      , m_seed()
Shinya Kitaoka 2a7129
      , m_min()
Shinya Kitaoka 2a7129
      , m_max()
Shinya Kitaoka 2a7129
      , m_arg(new VariableNode(calc, CalculatorNode::FRAME)) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setSeed(CalculatorNode *arg) {
Shinya Kitaoka 120a6e
    assert(m_seed.get() == 0);
Shinya Kitaoka 120a6e
    m_seed.reset(arg);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void setMax(CalculatorNode *arg) {
Shinya Kitaoka 120a6e
    assert(m_max.get() == 0);
Shinya Kitaoka 120a6e
    m_max.reset(arg);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void setMin(CalculatorNode *arg) {
Shinya Kitaoka 120a6e
    assert(m_min.get() == 0);
Shinya Kitaoka 120a6e
    m_min.reset(arg);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  double compute(double vars[3]) const override {
Shinya Kitaoka 120a6e
    double s = (m_seed.get() != 0) ? m_seed->compute(vars) : 0;
Shinya Kitaoka 120a6e
    double r =
Shinya Kitaoka 120a6e
        RandomManager::instance()->getValue(s, fabs(m_arg->compute(vars)));
Shinya Kitaoka 120a6e
    if (m_min.get() == 0)
Shinya Kitaoka 120a6e
      if (m_max.get() == 0)
Shinya Kitaoka 120a6e
        return r;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        return m_max->compute(vars) * r;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return (1 - r) * m_min->compute(vars) + r * m_max->compute(vars);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void accept(CalculatorNodeVisitor &visitor) override {
Shinya Kitaoka 120a6e
    m_arg->accept(visitor);
Shinya Kitaoka 120a6e
    if (m_seed.get()) m_seed->accept(visitor);
Shinya Kitaoka 120a6e
    if (m_min.get()) m_min->accept(visitor);
Shinya Kitaoka 120a6e
    if (m_max.get()) m_max->accept(visitor);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// Patterns
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CalculatorNode *Pattern::popNode(std::vector<calculatornode *=""> &stack) const {</calculatornode>
Shinya Kitaoka 120a6e
  CalculatorNode *node = stack.back();
Shinya Kitaoka 120a6e
  stack.pop_back();
Shinya Kitaoka 120a6e
  return node;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class NumberPattern final : public Pattern {
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.empty() && token.getType() == Token::Number;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return Number;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 1);
Shinya Kitaoka 120a6e
    assert(tokens[0].getType() == Token::Number);
Shinya Kitaoka 120a6e
    stack.push_back(new NumberNode(calc, tokens[0].getDoubleValue()));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class ConstantPattern final : public Pattern {
Shinya Kitaoka 120a6e
  std::string m_constantName;
Shinya Kitaoka 120a6e
  double m_value;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  ConstantPattern(std::string constantName, double value,
Shinya Kitaoka 120a6e
                  std::string description = "")
Shinya Kitaoka 120a6e
      : m_constantName(constantName), m_value(value) {
Shinya Kitaoka 120a6e
    setDescription(description);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return m_constantName; }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.empty() && token.getText() == m_constantName;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return Constant;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 1);
Shinya Kitaoka 120a6e
    stack.push_back(new NumberNode(calc, m_value));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class VariablePattern final : public Pattern {
Shinya Kitaoka 120a6e
  std::string m_variableName;
Shinya Kitaoka 120a6e
  int m_varIdx;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  VariablePattern(std::string variableName, int varIdx,
Shinya Kitaoka 120a6e
                  std::string description = "")
Shinya Kitaoka 120a6e
      : m_variableName(variableName), m_varIdx(varIdx) {
Shinya Kitaoka 120a6e
    setDescription(description);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return m_variableName; }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.empty() && token.getText() == m_variableName;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return Variable;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 1);
Shinya Kitaoka 120a6e
    assert(tokens[0].getText() == m_variableName);
Shinya Kitaoka 120a6e
    stack.push_back(new VariableNode(calc, m_varIdx));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class op=""></class>
Shinya Kitaoka d1f6c4
class Op2Pattern final : public Pattern {
Shinya Kitaoka 120a6e
  std::string m_opName;
Shinya Kitaoka 120a6e
  int m_priority;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  Op2Pattern(std::string opName, int priority)
Shinya Kitaoka 120a6e
      : m_opName(opName), m_priority(priority) {}
Shinya Kitaoka 473e70
  int getPriority() const override { return m_priority; }
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return m_opName; }
Shinya Kitaoka 38fd86
  bool expressionExpected(
Shinya Kitaoka 38fd86
      const std::vector<token> &previousTokens) const override {</token>
Shinya Kitaoka 120a6e
    return previousTokens.empty() || previousTokens.size() == 2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1 && token.getText() == m_opName;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 3;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1 ? Operator : InternalError;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 3);
Shinya Kitaoka 120a6e
    assert(tokens[1].getText() == m_opName);
Shinya Kitaoka 120a6e
    CalculatorNode *b = popNode(stack);
Shinya Kitaoka 120a6e
    CalculatorNode *a = popNode(stack);
Shinya Kitaoka 120a6e
    stack.push_back(new Op2Node<op>(calc, a, b));</op>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class UnaryMinusPattern final : public Pattern {
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  UnaryMinusPattern() {}
Shinya Kitaoka 473e70
  int getPriority() const override { return 50; }
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return "-"; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool expressionExpected(
Shinya Kitaoka 38fd86
      const std::vector<token> &previousTokens) const override {</token>
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.empty() && token.getText() == "-";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return Operator;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 2);
Shinya Kitaoka 120a6e
    assert(tokens[0].getText() == "-");
Shinya Kitaoka 120a6e
    stack.push_back(new ChsNode(calc, popNode(stack)));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class NotPattern final : public Pattern {
Shinya Kitaoka 120a6e
  std::string m_prefix;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  NotPattern(std::string prefix, std::string description) : m_prefix(prefix) {
Shinya Kitaoka 120a6e
    setDescription(description);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  int getPriority() const override { return 5; }
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return m_prefix; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool expressionExpected(
Shinya Kitaoka 38fd86
      const std::vector<token> &previousTokens) const override {</token>
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.empty() && token.getText() == m_prefix;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return Operator;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 2);
Shinya Kitaoka 120a6e
    assert(tokens[0].getText() == m_prefix);
Shinya Kitaoka 120a6e
    stack.push_back(new NotNode(calc, popNode(stack)));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class QuestionTernaryPattern final : public Pattern {
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  QuestionTernaryPattern() {}
Shinya Kitaoka 473e70
  int getPriority() const override { return 5; }
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return "?"; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool expressionExpected(
Shinya Kitaoka 38fd86
      const std::vector<token> &previousTokens) const override {</token>
Shinya Kitaoka 120a6e
    int i = (int)previousTokens.size();
Shinya Kitaoka 120a6e
    return i == 0 || i == 2 || i == 4;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    int i = (int)previousTokens.size();
Shinya Kitaoka 120a6e
    return i == 1 && token.getText() == "?" || i == 3 && token.getText() == ":";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 5;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    int i = (int)previousTokens.size();
Shinya Kitaoka 120a6e
    return (i == 1 || i == 3) ? Operator : InternalError;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    CalculatorNode *node1 = popNode(stack);
Shinya Kitaoka 120a6e
    CalculatorNode *node2 = popNode(stack);
Shinya Kitaoka 120a6e
    CalculatorNode *node3 = popNode(stack);
Shinya Kitaoka 120a6e
    stack.push_back(new QuestionNode(calc, node3, node2, node1));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class BraketPattern final : public Pattern {
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  BraketPattern() {}
Shinya Kitaoka 473e70
  int getPriority() const override { return 5; }
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return "("; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool expressionExpected(
Shinya Kitaoka 38fd86
      const std::vector<token> &previousTokens) const override {</token>
Shinya Kitaoka 120a6e
    return previousTokens.size() == 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.empty() && token.getText() == "(" ||
Shinya Kitaoka 120a6e
           previousTokens.size() == 2 && token.getText() == ")";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() == 3;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    return previousTokens.size() != 1 ? Parenthesis : InternalError;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    assert(tokens.size() == 3);
Shinya Kitaoka 120a6e
    assert(tokens[0].getText() == "(");
Shinya Kitaoka 120a6e
    assert(tokens[2].getText() == ")");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class FunctionPattern : public Pattern {
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  std::string m_functionName;
Shinya Kitaoka 120a6e
  bool m_implicitArgAllowed;
Shinya Kitaoka 120a6e
  // if m_implicitArgAllowed == true then the first argument is the frame number
Shinya Kitaoka 120a6e
  // e.g. f(5) means f(frame,5)
Shinya Kitaoka 120a6e
  // to use a different first argument (e.g. t*10) you have to write f(t*10;5)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int m_minArgCount;
Shinya Kitaoka 120a6e
  std::vector<double> m_optionalArgDefaults;</double>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  FunctionPattern(std::string functionName, int minArgCount)
Shinya Kitaoka 120a6e
      : m_functionName(functionName)
Shinya Kitaoka 120a6e
      , m_implicitArgAllowed(false)
Shinya Kitaoka 120a6e
      , m_minArgCount(minArgCount) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void allowImplicitArg(bool allowed) { m_implicitArgAllowed = allowed; }
Shinya Kitaoka 120a6e
  void addOptionalArg(double value) { m_optionalArgDefaults.push_back(value); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  std::string getFirstKeyword() const override { return m_functionName; }
Shinya Kitaoka 38fd86
  bool expressionExpected(
Shinya Kitaoka 38fd86
      const std::vector<token> &previousTokens) const override {</token>
Shinya Kitaoka 120a6e
    int n = (int)previousTokens.size();
Shinya Kitaoka 120a6e
    return 2 <= n && (n & 1) == 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool matchToken(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    int i         = (int)previousTokens.size();
Shinya Kitaoka 120a6e
    std::string s = toLower(token.getText());
Shinya Kitaoka 120a6e
    if (i == 0)
Shinya Kitaoka 120a6e
      return s == toLower(m_functionName);
Shinya Kitaoka 120a6e
    else if (i == 1)
Shinya Kitaoka 120a6e
      return s == "(";
Shinya Kitaoka 120a6e
    else if ((i & 1) == 0)
Shinya Kitaoka 120a6e
      return true;
Shinya Kitaoka 120a6e
    else if (s == ",")
Shinya Kitaoka 120a6e
      return true;
Shinya Kitaoka 120a6e
    else if (s == ";")
Shinya Kitaoka 120a6e
      return i == 3 && m_implicitArgAllowed;
Shinya Kitaoka 120a6e
    else if (s == ")") {
Shinya Kitaoka 120a6e
      int n = (i - 1) / 2;
Shinya Kitaoka 120a6e
      if (previousTokens.size() > 3 && previousTokens[3].getText() == ";") n--;
Shinya Kitaoka 120a6e
      if (n < m_minArgCount ||
Shinya Kitaoka 120a6e
          n > m_minArgCount + (int)m_optionalArgDefaults.size())
Shinya Kitaoka 120a6e
        return false;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        return true;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isFinished(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                  const Token &token) const override {
Shinya Kitaoka 120a6e
    if (previousTokens.empty()) return false;
Shinya Kitaoka 120a6e
    return m_minArgCount == 0 && previousTokens.size() == 1 &&
Shinya Kitaoka 120a6e
               token.getText() != "(" ||
Shinya Kitaoka 120a6e
           previousTokens.back().getText() == ")";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TokenType getTokenType(const std::vector<token> &previousTokens,</token>
Shinya Kitaoka 473e70
                         const Token &token) const override {
Shinya Kitaoka 120a6e
    int i = (int)previousTokens.size();
Shinya Kitaoka 120a6e
    if (i == 0)
Shinya Kitaoka 120a6e
      return Function;
Shinya Kitaoka 120a6e
    else if (i == 1 || token.getText() == ")")
Shinya Kitaoka 120a6e
      return Function;
Shinya Kitaoka 120a6e
    else if (i == 3)
Shinya Kitaoka 120a6e
      return token.getText() == ";" ? Comma : Comma;
Shinya Kitaoka 120a6e
    else if (i & 1)
Shinya Kitaoka 120a6e
      return Comma;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return InternalError;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void getArgs(std::vector<calculatornode *=""> &nodes, Calculator *calc,</calculatornode>
Shinya Kitaoka 120a6e
               std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 120a6e
               const std::vector<token> &tokens) const {</token>
Shinya Kitaoka 120a6e
    bool implicitArgUsed =
Shinya Kitaoka 120a6e
        m_implicitArgAllowed && tokens.size() > 3 && tokens[3].getText() == ";";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // n = number of arguments to provide (mandatory + optional + implicit)
Shinya Kitaoka 120a6e
    int n = m_minArgCount + (int)m_optionalArgDefaults.size() +
Shinya Kitaoka 120a6e
            (m_implicitArgAllowed ? 1 : 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // m = number of default arguments to assign (with their default value)
Shinya Kitaoka 120a6e
    int m = n - (tokens.size() - 2) / 2;
Shinya Kitaoka 120a6e
    if (m_implicitArgAllowed && !implicitArgUsed) m--;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    assert(m <= (int)m_optionalArgDefaults.size());
Shinya Kitaoka 120a6e
    if (m > (int)m_optionalArgDefaults.size())
Shinya Kitaoka 120a6e
      m = (int)m_optionalArgDefaults.size();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    nodes.resize(n);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // fetch arguments from the stack
Shinya Kitaoka 120a6e
    int k = n - m;
Shinya Kitaoka 120a6e
    if (implicitArgUsed) {
Shinya Kitaoka 120a6e
      while (k > 0) nodes[--k] = popNode(stack);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      while (k > 1) nodes[--k] = popNode(stack);
Shinya Kitaoka 120a6e
      nodes[0]                 = new VariableNode(calc, CalculatorNode::FRAME);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // add default values
Shinya Kitaoka 120a6e
    for (int i         = 0; i < m; i++)
Shinya Kitaoka 120a6e
      nodes[n - m + i] = new NumberNode(calc, m_optionalArgDefaults[i]);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class function=""></class>
Shinya Kitaoka d1f6c4
class F0Pattern final : public FunctionPattern {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  F0Pattern(std::string functionName) : FunctionPattern(functionName, 0) {}
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    stack.push_back(new Op0Node<function>(calc, m_functionName));</function>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class function=""></class>
Shinya Kitaoka d1f6c4
class F1Pattern final : public FunctionPattern {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  F1Pattern(std::string functionName, std::string descr = "")
Shinya Kitaoka 120a6e
      : FunctionPattern(functionName, 1) {
Shinya Kitaoka 120a6e
    setDescription(descr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    stack.push_back(new Op1Node<function>(calc, popNode(stack)));</function>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class function=""></class>
Shinya Kitaoka d1f6c4
class F2Pattern final : public FunctionPattern {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  F2Pattern(std::string functionName, std::string descr = "")
Shinya Kitaoka 120a6e
      : FunctionPattern(functionName, 2) {
Shinya Kitaoka 120a6e
    setDescription(descr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    CalculatorNode *b = popNode(stack);
Shinya Kitaoka 120a6e
    CalculatorNode *a = popNode(stack);
Shinya Kitaoka 120a6e
    stack.push_back(new Op2Node<function>(calc, a, b));</function>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class function=""></class>
Shinya Kitaoka d1f6c4
class F3Pattern final : public FunctionPattern {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  F3Pattern(std::string functionName, std::string descr = "")
Shinya Kitaoka 120a6e
      : FunctionPattern(functionName, 3) {
Shinya Kitaoka 120a6e
    setDescription(descr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    CalculatorNode *c = popNode(stack);
Shinya Kitaoka 120a6e
    CalculatorNode *b = popNode(stack);
Shinya Kitaoka 120a6e
    CalculatorNode *a = popNode(stack);
Shinya Kitaoka 120a6e
    stack.push_back(new Op3Node<function>(calc, a, b, c));</function>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class function=""></class>
Shinya Kitaoka d1f6c4
class Fs2Pattern final : public FunctionPattern {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Fs2Pattern(std::string functionName, std::string description)
Shinya Kitaoka 120a6e
      : FunctionPattern(functionName, 1) {
Shinya Kitaoka 120a6e
    allowImplicitArg(true);
Shinya Kitaoka 120a6e
    setDescription(description);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    std::vector<calculatornode *=""> nodes;</calculatornode>
Shinya Kitaoka 120a6e
    getArgs(nodes, calc, stack, tokens);
Shinya Kitaoka 120a6e
    stack.push_back(new Op2Node<function>(calc, nodes[0], nodes[1]));</function>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class function=""></class>
Shinya Kitaoka d1f6c4
class Fs3Pattern final : public FunctionPattern {
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  Fs3Pattern(std::string functionName, double defVal, std::string descr)
Shinya Kitaoka 120a6e
      : FunctionPattern(functionName, 1) {
Shinya Kitaoka 120a6e
    allowImplicitArg(true);
Shinya Kitaoka 120a6e
    addOptionalArg(defVal);
Shinya Kitaoka 120a6e
    setDescription(descr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    std::vector<calculatornode *=""> nodes;</calculatornode>
Shinya Kitaoka 120a6e
    getArgs(nodes, calc, stack, tokens);
Shinya Kitaoka 120a6e
    stack.push_back(new Op3Node<function>(calc, nodes[0], nodes[1], nodes[2]));</function>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class CyclePattern final : public FunctionPattern {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  CyclePattern(std::string functionName) : FunctionPattern(functionName, 1) {
Shinya Kitaoka 120a6e
    setDescription(
Shinya Kitaoka 120a6e
        "cycle(period)\nCycles the transitions of the period previous frames "
Shinya Kitaoka 120a6e
        "to the selected range");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    CalculatorNode *a = popNode(stack);
Shinya Kitaoka 120a6e
    stack.push_back(new CycleNode(calc, a));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class RandomPattern final : public FunctionPattern {
Shinya Kitaoka 120a6e
  bool m_seed;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  RandomPattern(std::string functionName, bool seed, std::string description)
Shinya Kitaoka 120a6e
      : FunctionPattern(functionName, seed ? 1 : 0), m_seed(seed) {
Shinya Kitaoka 120a6e
    allowImplicitArg(true);
Shinya Kitaoka 120a6e
    addOptionalArg(0);
Shinya Kitaoka 120a6e
    addOptionalArg(0);
Shinya Kitaoka 120a6e
    setDescription(description);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void createNode(Calculator *calc, std::vector<calculatornode *=""> &stack,</calculatornode>
Shinya Kitaoka 473e70
                  const std::vector<token> &tokens) const override {</token>
Shinya Kitaoka 120a6e
    int n = ((int)tokens.size() - 1) / 2;
Shinya Kitaoka 120a6e
    if (m_seed) n--;
Shinya Kitaoka 120a6e
    RandomNode *randomNode = new RandomNode(calc);
Shinya Kitaoka 120a6e
    if (n > 0) {
Shinya Kitaoka 120a6e
      randomNode->setMax(popNode(stack));
Shinya Kitaoka 120a6e
      if (n > 1) randomNode->setMin(popNode(stack));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (m_seed) randomNode->setSeed(popNode(stack));
Shinya Kitaoka 120a6e
    stack.push_back(randomNode);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class PatternTable {
Shinya Kitaoka 120a6e
  std::map<std::string, *="" pattern=""> m_kTable;</std::string,>
Shinya Kitaoka 120a6e
  std::vector<pattern *=""> m_uTable;</pattern>
Shinya Kitaoka 120a6e
  Grammar::Position m_position;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  PatternTable(Grammar::Position position) : m_position(position) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~PatternTable() {
Shinya Kitaoka 120a6e
    for (std::map<std::string, *="" pattern="">::iterator it = m_kTable.begin();</std::string,>
Shinya Kitaoka 120a6e
         it != m_kTable.end(); ++it)
Shinya Kitaoka 120a6e
      delete it->second;
Shinya Kitaoka 120a6e
    for (std::vector<pattern *="">::iterator it = m_uTable.begin();</pattern>
Shinya Kitaoka 120a6e
         it != m_uTable.end(); ++it)
Shinya Kitaoka 120a6e
      delete *it;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void addPattern(Pattern *pattern) {
Shinya Kitaoka 120a6e
    std::string keyword = pattern->getFirstKeyword();
Shinya Kitaoka 120a6e
    if (keyword != "") {
Shinya Kitaoka 120a6e
      // first keyword should be unique
Shinya Kitaoka 120a6e
      assert(m_kTable.count(keyword) == 0);
Shinya Kitaoka 120a6e
      m_kTable[keyword] = pattern;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      m_uTable.push_back(pattern);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const Pattern *getPattern(const Token &token) const {
Shinya Kitaoka 120a6e
    std::vector<token> tokens;</token>
Shinya Kitaoka 120a6e
    if (m_position == Grammar::ExpressionEnd) tokens.push_back(Token());
Shinya Kitaoka 120a6e
    if (token.getType() == Token::Punct || token.getType() == Token::Ident) {
Shinya Kitaoka 120a6e
      std::string keyword = token.getText();
Shinya Kitaoka 120a6e
      std::map<std::string, *="" pattern="">::const_iterator it =</std::string,>
Shinya Kitaoka 120a6e
          m_kTable.find(keyword);
Shinya Kitaoka 120a6e
      if (it != m_kTable.end()) {
Shinya Kitaoka 120a6e
        Pattern *pattern = it->second;
Shinya Kitaoka 120a6e
        if (pattern->matchToken(tokens, token)) {
Shinya Kitaoka 120a6e
          return pattern;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    for (int i = 0; i < (int)m_uTable.size(); i++) {
Shinya Kitaoka 120a6e
      Pattern *pattern = m_uTable[i];
Shinya Kitaoka 120a6e
      if (pattern->matchToken(tokens, token)) {
Shinya Kitaoka 120a6e
        return pattern;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void getSuggestions(Grammar::Suggestions &suggestions) const {
Shinya Kitaoka 120a6e
    std::map<std::string, *="" pattern="">::const_iterator it;</std::string,>
Shinya Kitaoka 120a6e
    for (it = m_kTable.begin(); it != m_kTable.end(); ++it) {
Shinya Kitaoka 120a6e
      suggestions.push_back(
Shinya Kitaoka 120a6e
          std::make_pair(it->first, it->second->getDescription()));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    for (int i = 0; i < (int)m_uTable.size(); i++) {
Shinya Kitaoka 120a6e
      std::vector<std::string> keywords;</std::string>
Shinya Kitaoka 120a6e
      m_uTable[i]->getAcceptableKeywords(keywords);
Shinya Kitaoka 120a6e
      for (int j = 0; j < (int)keywords.size(); j++)
Shinya Kitaoka 120a6e
        suggestions.push_back(
Shinya Kitaoka 120a6e
            std::make_pair(keywords[j], m_uTable[i]->getDescription()));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// funzioni trigonometriche, log, exp, ecc.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class Pow {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return pow(x, y); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
// class Mod   {public: double operator()(double x, double y) const {return
Shinya Kitaoka 120a6e
// (int)x % (int)y;} };
Shinya Kitaoka 120a6e
class Sin {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return sin(toRad(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Cos {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return cos(toRad(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Tan {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return tan(toRad(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Sinh {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return sinh(toRad(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Cosh {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return cosh(toRad(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Tanh {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return tanh(toRad(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Atan {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return toDeg(atan(x)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Atan2 {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return toDeg(atan2(x, y)); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Log {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return log(x); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Exp {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return exp(x); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Floor {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return tfloor(x); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Ceil {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return tceil(x); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Round {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return tround(x); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Abs {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return fabs(x); }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Sign {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return x > 0 ? 1 : x < 0 ? -1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Sqrt {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return x >= 0.0 ? sqrt(x) : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Sqr {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return x * x; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Crop {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double a, double b) const {
Shinya Kitaoka 120a6e
    return tcrop(x, a, b);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Step {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x < y ? 0 : 1; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class Mod {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const {
Shinya Kitaoka 120a6e
    if (y == 0.0) return 0;
Shinya Kitaoka 120a6e
    return x - y * floor(x / y);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class Min {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x < y ? x : y; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Max {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x > y ? x : y; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class Gt {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x > y ? 1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Ge {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x >= y ? 1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Lt {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x < y ? 1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Le {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x <= y ? 1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Ne {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x != y ? 1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Eq {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const { return x == y ? 1 : 0; }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class And {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const {
Shinya Kitaoka 120a6e
    return (x != 0) && (y != 0) ? 1 : 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Or {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double y) const {
Shinya Kitaoka 120a6e
    return (x != 0) || (y != 0) ? 1 : 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Not {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x) const { return x == 0; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class Smoothstep {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double v, double min, double max) const {
Shinya Kitaoka 120a6e
    if (v <= min)
Shinya Kitaoka 120a6e
      return 0;
Shinya Kitaoka 120a6e
    else if (v >= max)
Shinya Kitaoka 120a6e
      return 1;
Shinya Kitaoka 120a6e
    double t = (v - min) / (max - min);
Shinya Kitaoka 120a6e
    return -2 * t * t * t + 3 * t * t;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Pulse {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double x0, double length) const {
Shinya Kitaoka 120a6e
    // double length=5.0;
Shinya Kitaoka 120a6e
    double b = (.69315 * 4.0) / (length * length);
Shinya Kitaoka 120a6e
    x -= x0;
Shinya Kitaoka 120a6e
    return exp(-x * x * b);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Saw {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double length, double height) const {
Shinya Kitaoka 120a6e
    if (length <= 0.0) return 0.0;
Shinya Kitaoka 120a6e
    if (height <= 0.0) height = length;
Shinya Kitaoka 120a6e
    double q                  = x / length;
Shinya Kitaoka 120a6e
    return height * (q - floor(q));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Shinya Kitaoka 120a6e
class Wave {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  double operator()(double x, double length) const {
Shinya Kitaoka 120a6e
    if (length <= 0.0) return 0.0;
Shinya Kitaoka 120a6e
    return sin(x * 2 * PI / length);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class Grammar::Imp {
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
  PatternTable m_prePatterns, m_postPatterns;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Imp()
Shinya Kitaoka 120a6e
      : m_prePatterns(Grammar::ExpressionStart)
Shinya Kitaoka 120a6e
      , m_postPatterns(Grammar::ExpressionEnd) {}
Shinya Kitaoka 120a6e
  ~Imp() {}
Shinya Kitaoka 120a6e
};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
Grammar::Grammar() : m_imp(new Imp()) {
Shinya Kitaoka 120a6e
  addPattern(new NumberPattern());
Shinya Kitaoka 120a6e
  addPattern(new ConstantPattern("pi", PI, "3.14159265..."));
Shinya Kitaoka 120a6e
  addPattern(new VariablePattern(
Shinya Kitaoka 120a6e
      "t", CalculatorNode::T, "ranges from 0.0 to 1.0 along the transition"));
Shinya Kitaoka 120a6e
  const std::string f_desc = "the current frame number";
Shinya Kitaoka 120a6e
  addPattern(new VariablePattern("f", CalculatorNode::FRAME, f_desc));
Shinya Kitaoka 120a6e
  addPattern(new VariablePattern("frame", CalculatorNode::FRAME, f_desc));
Shinya Kitaoka 120a6e
  const std::string r_desc =
Shinya Kitaoka 120a6e
      "the current frame number, relative to the transition";
Shinya Kitaoka 120a6e
  addPattern(new VariablePattern("r", CalculatorNode::RFRAME, r_desc));
Shinya Kitaoka 120a6e
  addPattern(new VariablePattern("rframe", CalculatorNode::RFRAME, r_desc));
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<std::plus<double>>("+", 10));</std::plus<double>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<std::multiplies<double>>("*", 20));</std::multiplies<double>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<pow>("^", 30));</pow>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<std::minus<double>>("-", 10));</std::minus<double>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<std::divides<double>>("/", 20));</std::divides<double>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<mod>("%", 8));</mod>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<gt>(">", 6));</gt>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<ge>(">=", 6));</ge>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<lt>("<", 6));</lt>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<le>("<=", 6));</le>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<eq>("==", 6));</eq>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<ne>("!=", 6));</ne>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<and>("&&", 3));</and>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<or>("||", 2));</or>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<and>("and", 3));</and>
Shinya Kitaoka 120a6e
  addPattern(new Op2Pattern<or>("or", 2));</or>
Shinya Kitaoka 120a6e
  addPattern(new NotPattern("!", "not"));
Shinya Kitaoka 120a6e
  addPattern(new NotPattern("not", ""));
Shinya Kitaoka 120a6e
  addPattern(new UnaryMinusPattern());
Shinya Kitaoka 120a6e
  addPattern(new BraketPattern());
Shinya Kitaoka 120a6e
  addPattern(new QuestionTernaryPattern());
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<sin>("sin", "sin(degree)"));</sin>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<cos>("cos", "cos(degree)"));</cos>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<tan>("tan", "tan(degree)"));</tan>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<sinh>("sinh", "sinh(degree)\nHyperbolic sine"));</sinh>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<cosh>("cosh", "cosh(degree)\nHyperbolic cosine"));</cosh>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<tanh>("tanh", "tanh(degree)\nHyperbolic tangent"));</tanh>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<atan>("atan",</atan>
Shinya Kitaoka 120a6e
                                 "atan(x)\nArctangent : the inverse of tan()"));
Shinya Kitaoka 120a6e
  addPattern(new F2Pattern<atan2>("atan2",</atan2>
Shinya Kitaoka 120a6e
                                  "atan2(y,x)\nThe counter-clockwise angle in "
Shinya Kitaoka 120a6e
                                  "degree between the x-axis and the "
Shinya Kitaoka 120a6e
                                  "point(x,y)"));
Shinya Kitaoka 120a6e
  addPattern(
Shinya Kitaoka 120a6e
      new F1Pattern<log>("log", "log(x)\nThe natural logarithm of x (base e)"));</log>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<exp>("exp", "exp(x)\nThe base-e exponential of x"));</exp>
Shinya Kitaoka 120a6e
  addPattern(
Shinya Kitaoka 120a6e
      new F1Pattern<floor>("floor", "floor(x)\nThe greatest integer <= x"));</floor>
Shinya Kitaoka 120a6e
  const std::string ceil_desc = "The smallest integer >= x";
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<ceil>("ceil", "ceil(x)\n" + ceil_desc));</ceil>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<ceil>("ceiling", "ceiling(x)\n" + ceil_desc));</ceil>
Shinya Kitaoka 120a6e
  addPattern(
Shinya Kitaoka 120a6e
      new F1Pattern<round>("round", "round(x)\nThe integer nearest to x"));</round>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<abs>("abs", "abs(x)\nThe absolute value of x"));</abs>
Shinya Kitaoka 120a6e
  addPattern(
Shinya Kitaoka 120a6e
      new F1Pattern<sign>("sign", "sign(x)\n-1 if x<0, 1 if x>0 and 0 if x=0"));</sign>
Shinya Kitaoka 120a6e
  const std::string sqrt_desc = "Square root of x";
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<sqrt>("sqrt", "sqrt(x)\n" + sqrt_desc));</sqrt>
Shinya Kitaoka 120a6e
  addPattern(new F1Pattern<sqr>("sqr", "sqr(x)\n" + sqrt_desc));</sqr>
Shinya Kitaoka 120a6e
  addPattern(new F3Pattern<crop>(</crop>
Shinya Kitaoka 120a6e
      "crop", "crop(x,a,b)\na if x<a, b="" if="" x="">b, x if x in [a,b]"));</a,>
Shinya Kitaoka 120a6e
  addPattern(new F3Pattern<crop>(</crop>
Shinya Kitaoka 120a6e
      "clamp", "clamp(x,a,b)\na if x<a, b="" if="" x="">b, x if x in [a,b]"));</a,>
Shinya Kitaoka 120a6e
  addPattern(new F2Pattern<min>("min", "min(a,b)"));</min>
Shinya Kitaoka 120a6e
  addPattern(new F2Pattern<max>("max", "max(a,b)"));</max>
Shinya Kitaoka 120a6e
  addPattern(new F2Pattern<step>("step", "min(x,x0)\n0 if x<x0, 1="" if="" x="">=x0"));</x0,></step>
Shinya Kitaoka 120a6e
  addPattern(new F3Pattern<smoothstep>("smoothstep",</smoothstep>
Shinya Kitaoka 120a6e
                                       "smoothstep(x,x0)\n0 if x
Shinya Kitaoka 120a6e
                                       "x>=x0\nas step, but with smooth "
Shinya Kitaoka 120a6e
                                       "transition"));
Shinya Kitaoka 120a6e
  const std::string pulse_desc =
Shinya Kitaoka 120a6e
      "Generates a bump ranging from 0.0 to 1.0 set at position pos";
Shinya Kitaoka 120a6e
  addPattern(new Fs3Pattern<pulse>("pulse", 0.5,</pulse>
Shinya Kitaoka 120a6e
                                   "pulse(pos)\npulse(pos,length)\npulse(arg; "
Shinya Kitaoka 120a6e
                                   "pos)\npulse(arg;pos,length)\n" +
Shinya Kitaoka 120a6e
                                       pulse_desc));
Shinya Kitaoka 120a6e
  addPattern(new Fs3Pattern<pulse>(</pulse>
Shinya Kitaoka 120a6e
      "bump", 0.5,
Shinya Kitaoka 120a6e
      "bump(pos)\nbump(pos,length)\nbump(arg; pos)\nbump(arg;pos,length)\n" +
Shinya Kitaoka 120a6e
          pulse_desc));
Shinya Kitaoka 120a6e
  const std::string saw_desc = "Generates a periodic sawtooth shaped curve";
Shinya Kitaoka 120a6e
  addPattern(new Fs3Pattern<saw>("sawtooth", 0.0,</saw>
Shinya Kitaoka 120a6e
                                 "sawtooth(length)\nsawtooth(length, "
Shinya Kitaoka 120a6e
                                 "height)\nsawtooth(arg; "
Shinya Kitaoka 120a6e
                                 "length)\nsawtooth(arg; length, height)\n" +
Shinya Kitaoka 120a6e
                                     saw_desc));
Shinya Kitaoka 120a6e
  addPattern(new Fs3Pattern<saw>("saw", 0.0,</saw>
Shinya Kitaoka 120a6e
                                 "saw(length)\nsaw(length, height)\nsaw(arg; "
Shinya Kitaoka 120a6e
                                 "length)\nsaw(arg; length, height)\n" +
Shinya Kitaoka 120a6e
                                     saw_desc));
Shinya Kitaoka 120a6e
  addPattern(new Fs2Pattern<wave>(</wave>
Shinya Kitaoka 120a6e
      "wave", "wave(_length)\nwave(_arg;_length)\nsame as sin(f*180/length)"));
Shinya Kitaoka 120a6e
  const std::string rnd_desc = "Generates random number between min and max";
Shinya Kitaoka 120a6e
  addPattern(new RandomPattern(
Shinya Kitaoka 120a6e
      "random", false,
Shinya Kitaoka 120a6e
      "random = random(0,1)\nrandom(max) = random(0,max)\nrandom(min,max)\n" +
Shinya Kitaoka 120a6e
          rnd_desc));
Shinya Kitaoka 120a6e
  addPattern(new RandomPattern(
Shinya Kitaoka 120a6e
      "rnd", false,
Shinya Kitaoka 120a6e
      "rnd = rnd(0,1)\nrnd(max) = rnd(0,max)\nrnd(min,max)\n" + rnd_desc));
Shinya Kitaoka 120a6e
  const std::string rnd_s_desc =
Shinya Kitaoka 120a6e
      rnd_desc + "; seed select different random sequences";
Shinya Kitaoka 120a6e
  addPattern(new RandomPattern("random_s", true,
Shinya Kitaoka 120a6e
                               "random_s(seed) = random_s(seed, "
Shinya Kitaoka 120a6e
                               "0,1)\nrandom_s(seed,max) = random_s(seed, "
Shinya Kitaoka 120a6e
                               "0,max)\nrandom_s(seed,min,max)\n" +
Shinya Kitaoka 120a6e
                                   rnd_s_desc));
Shinya Kitaoka 120a6e
  addPattern(new RandomPattern("rnd_s", true,
Shinya Kitaoka 120a6e
                               "rnd_s(seed) = rnd_s(seed, "
Shinya Kitaoka 120a6e
                               "0,1)\nrnd_s(seed,max) = rnd_s(seed, "
Shinya Kitaoka 120a6e
                               "0,max)\nrnd_s(seed,min,max)\n" +
Shinya Kitaoka 120a6e
                                   rnd_s_desc));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  addPattern(new CyclePattern("cycle"));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Grammar::~Grammar() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Grammar::addPattern(Pattern *pattern) {
Shinya Kitaoka 120a6e
  std::vector<token> noTokens;</token>
Shinya Kitaoka 120a6e
  if (pattern->expressionExpected(noTokens))
Shinya Kitaoka 120a6e
    m_imp->m_postPatterns.addPattern(pattern);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    m_imp->m_prePatterns.addPattern(pattern);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const Pattern *Grammar::getPattern(Position position,
Shinya Kitaoka 120a6e
                                   const Token &token) const {
Shinya Kitaoka 120a6e
  Pattern *pattern = 0;
Shinya Kitaoka 120a6e
  if (position == ExpressionStart)
Shinya Kitaoka 120a6e
    return m_imp->m_prePatterns.getPattern(token);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return m_imp->m_postPatterns.getPattern(token);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Grammar::getSuggestions(Grammar::Suggestions &suggestions,
Shinya Kitaoka 120a6e
                             Position position) const {
Shinya Kitaoka 120a6e
  if (position == ExpressionStart)
Shinya Kitaoka 120a6e
    return m_imp->m_prePatterns.getSuggestions(suggestions);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return m_imp->m_postPatterns.getSuggestions(suggestions);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace TSyntax