summaryrefslogtreecommitdiff
path: root/mtxexpression.h
blob: 16051b6d7745c34e0109cd61f96a42d388012834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* $Id$ */

/*
 * Copyright (C) 2006 Richard Braun
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _MTXEXPRESSION_H
#define _MTXEXPRESSION_H

#include <string>
#include <vector>

#include "mtx.h"
#include "mtxvalue.h"
#include "mtxsymbol.h"
#include "mtxlanguage.h"

#define MTXEXPRESSION_OPERATOR_NULL \
  {NULL, MTXEXPRESSION_FUNCTION_NULL, MTXEXPRESSION_ASSOCIATIVITY_NULL}

/**
 * \enum mtx_function_id
 * \brief ID of a function/operator (e.g. assignment, addition).
 */
enum mtx_function_id
{
  MTXEXPRESSION_FUNCTION_NULL,
  MTXEXPRESSION_FUNCTION_ASSIGNMENT,
  MTXEXPRESSION_FUNCTION_ADDITION,
  MTXEXPRESSION_FUNCTION_ADD_ASSIGN,
  MTXEXPRESSION_FUNCTION_SUBTRACTION,
  MTXEXPRESSION_FUNCTION_SUBTRACT_ASSIGN,
  MTXEXPRESSION_FUNCTION_MULTIPLY,
  MTXEXPRESSION_FUNCTION_MULTIPLY_ASSIGN,
  MTXEXPRESSION_FUNCTION_DIVIDE,
  MTXEXPRESSION_FUNCTION_DIVIDE_ASSIGN
};

/**
 * \enum mtx_operator_associativity
 * \brief Associativity of an operator.
 */
enum mtx_operator_associativity
{
  MTXEXPRESSION_ASSOCIATIVITY_NULL,
  MTXEXPRESSION_RIGHT_TO_LEFT,
  MTXEXPRESSION_LEFT_TO_RIGHT
};

/**
 * An operator.
 */
struct mtx_operator
{
  /**
   * Operator string as entered by the user.
   */
  char *str;

  /**
   * ID of the operator.
   */
  enum mtx_function_id id;

  /**
   * Associativity of the operator.
   */
  enum mtx_operator_associativity associativity;
};

/**
 * An expression.
 * 
 * All language expressions that are not atomic symbols (e.g. "a" or "2")
 * are expressions which consist of a function/operator and one or more
 * arguments. These arguments are in turn MtxExpression or MtxSymbol
 * objects. MtxExpression objects are always nodes in the tree of symbols,
 * whereas MtxSymbol objects are always leaves.
 */
class MtxExpression: public MtxSymbol
{
  private:
    /**
     * Function/operator compiled in this expression.
     */
    enum mtx_function_id function;

    /**
     * Vector of arguments.
     */
    std::vector<MtxSymbol *> expressions;

    /**
     * True if silent.
     * 
     * A silent expression is an expression ended with the ';' character.
     * Engine users should not display silent expressions. This property
     * is propagated to underlying expressions and symbols.
     * 
     * @see isSilent()
     */
    bool silent;

    /**
     * Find an operator in the vector of lexical elements.
     * 
     * This method uses the \a operators array to look for operators by
     * order of precedence.
     * 
     * @param lexical_elements vector of strings in which to look for an
     *                         operator
     * @param str operator string as entered by the user to look for
     * @see operators
     * @see rfindOperator()
     */
    mtx_expression_t::iterator
    findOperator(mtx_expression_t &lexical_elements, const char *str);

    /**
     * Find an operator in the vector of lexical elements in reverse order.
     * 
     * @see findOperator().
     */
    mtx_expression_t::reverse_iterator
    rfindOperator(mtx_expression_t &lexical_elements, const char *str);

    /**
     * Free the pointers stored in the given vector of symbols.
     * 
     * Mainly used after parsing a matrix is done or failed.
     */
    void freeMatrix(std::vector<MtxSymbol *> &symbols) const;

    /**
     * Parse the given lexical elements and extract symbols and expressions
     * from it to create the list of elements that will be used to create a
     * matrix. An exception is thrown if the syntax doesn't comply with
     * the matrix creation syntax.
     * 
     * @param lexical_elements vector of strings to parse
     * @param symbols vector of symbols/expressions updated by this method
     * @param rows number of rows found
     * @param columns number of columns found
     */
    void parseMatrix(const mtx_expression_t &lexical_elements,
                     std::vector<MtxSymbol *> &symbols,
                     size_t &rows, size_t &columns) const;


    /**
     * Multidimensional array of supported operators.
     * 
     * Each line of the array indicates a level of precedence. Lines must
     * be ordered in reverse order of precedence, e.g. = has low precedence,
     * so it is placed in first line. Operators of same precedence must have
     * the same associativity (this is never checked).
     * 
     * XXX There are at most 5 operators for the same precedence (+ a null
     * entry to indicate end of list), so the second dimension is 6.
     */
    static struct mtx_operator operators[][6];

    /**
     * Return true if the given string matches an operator.
     */
    static bool isOperator(const std::string &str);

  public:
    /**
     * Create an expression from a set of lexical elements.
     * 
     * @param language address of the MtxLanguage object used to get/set
     *                 variables
     * @param lexical_elements vector of lexical elements
     * @param silent see silent
     * @see silent
     */
    MtxExpression(MtxLanguage *language, mtx_expression_t &lexical_elements,
                  bool silent = false);

    virtual ~MtxExpression();
    virtual MtxSymbol getSymbol(bool create = false,
                                bool fail_if_special = true) const;
    virtual std::string getName() const;
    virtual MtxValue * getValue() const;
    virtual std::string toString() const;

    /**
     * Return the silent member.
     * 
     * As MtxExpression objects are always nodes, and there is always a node
     * on top of a leaf, it is not necessary to propagate the silent property
     * to underlying objects. When the engine calls isSilent(), it always get
     * a return value from the first node it is calling the function on.
     * That's why MtxSymbol objects don't have a silent property.
     */
    virtual bool isSilent() const;
};

#endif /* _MTXEXPRESSION_H */