summaryrefslogtreecommitdiff
path: root/mtxlanguage.h
blob: 8ad8772d7654f82c450d4cb6e13cf91fb96500bc (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
/* $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 _MTXLANGUAGE_H
#define _MTXLANGUAGE_H

#include <string>
#include <vector>

#include "mtx.h"
#include "mtxsymbol.h"

/**
 * \def MTXLANGUAGE_VALUES_INDEX
 * \brief Index of the lexical group of values in lexical groups array.
 */
#define MTXLANGUAGE_VALUES_INDEX 0

/**
 * \def MTXLANGUAGE_OPERATORS_INDEX
 * \brief Index of the lexical group of operators in lexical groups array.
 */
#define MTXLANGUAGE_OPERATORS_INDEX 1

/**
 * \def MTXLANGUAGE_OPERATORS_MAX_LENGTH
 * \brief Maximum length of the string representation of an operator.
 */
#define MTXLANGUAGE_OPERATORS_MAX_LENGTH 2

/**
 * \def MTXLANGUAGE_ANS_VARIABLE
 * \brief name of the automatic variable \a ans
 */
#define MTXLANGUAGE_ANS_VARIABLE "ans"

/**
 * Main language object used as interface between user and engine.
 */
class MtxLanguage
{
  private:
    /**
     * Vector of known variables.
     * 
     * @see getSymbols()
     */
    std::vector<MtxSymbol> symbols;

    /**
     * Return the lexical group of the given character.
     * 
     * Throw an exception if the character isn't registered.
     */
    size_t findGroup(char c) const;

    /**
     * Check a lexical element.
     * 
     * Throw an exception if the lexical element is invalid.
     */
    void checkLexicalElement(const std::string &lexical_element) const;

    /**
     * Lexical analysis.
     * 
     * Return a vector of the lexical elements of the given expression. An
     * exception is thrown if an invalid character is present.
     */
    mtx_expression_t parseLexicalElements(const std::string &expression) const;

    /**
     * Compile a set of lexical elements into expressions.
     * 
     * Parse the given lexical elements and compile them in a vector of
     * expressions. An MtxException exception is thrown if an error occurs.
     */
    std::vector<MtxSymbol *>
    parseExpressions(const mtx_expression_t &lexical_elements);

    /**
     * Release the memory referenced by the pointers in the given vector.
     */
    void freeExpressions(std::vector<MtxSymbol *> &expressions);

    /**
     * Array of groups of lexical elements. E.g. + and = are in the same
     * group.
     */
    static const char *lexical_groups[];

  public:
    /**
     * Default constructor.
     */
    MtxLanguage();

    /**
     * Parse the given expression into a set of results.
     * 
     * This is the main interface between the user and the engine.
     * The caller should catch MtxException exceptions.
     */
    std::vector<MtxSymbol> parse(const std::string &expression);

    /**
     * Set a named variable.
     */
    void setSymbol(const MtxSymbol &symbol);

    /**
     * Get a variable from its name.
     */
    MtxSymbol getSymbol(const std::string &name) const;

    /**
     * Return a copy of the vector of symbols.
     * 
     * Mainly used to list all registered variables.
     * 
     * @see symbols.
     */
    std::vector<MtxSymbol> getSymbols() const;
};

#endif /* _MTXLANGUAGE_H */