summaryrefslogtreecommitdiff
path: root/mtxexpression.h
blob: 848a2a98202e806735319dc960bda5eb5e18c66a (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* $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,
  MTXEXPRESSION_FUNCTION_DETERMINANT,
  MTXEXPRESSION_FUNCTION_INVERSE,
  MTXEXPRESSION_FUNCTION_TRANSPOSE,
  MTXEXPRESSION_FUNCTION_AUGMENT,
  MTXEXPRESSION_FUNCTION_LUDECOMPOSITION
};

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

/**
 * A function.
 */
struct mtx_function
{
  /**
   * Function string as entered by the user.
   */
  char *str;

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

  /**
   * Number of arguments.
   */
  size_t argc;
};

/**
 * An operator.
 * 
 * XXX Inheritence from struct mtx_function seems to cause trouble. Why ?
 */
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 or function arguments is done or
     * failed.
     */
    void freeSymbols(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;

    /**
     * Parse the given lexical elements and extract symbols and expressions
     * from it to create the list of arguments of a function. An exception is
     * thrown if the syntax is invalid.
     * 
     * @param lexical_elements vector of strings to parse
     * @param symbols vector of symbols/expressions updated by this method
     */
    void parseArguments(const mtx_expression_t &lexical_elements,
                        std::vector<MtxSymbol *> &symbols) const;

    /**
     * Array of builtin functions.
     */
    static struct mtx_function functions[];

    /**
     * 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.
     * 
     * @see isOperator()
     */
    static struct mtx_operator operators[][6];

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

    /**
     * Return true if the given string is a valid variable name.
     * 
     * A valid variable name consist of [a-zA-Z0-9_] characters only,
     * and cannot begin with a digit.
     */
    static bool isVariableName(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 */