| 1 |
The multi-function calculator needs a symbol table for keeping track of the |
| 2 |
names and meanings of variables and functions. This doesn't affect the grammar |
| 3 |
rules or the calculator's directives, but it requires that the parser class |
| 4 |
defines some additional bf(C++) types as well as several additional data |
| 5 |
members. |
| 6 |
|
| 7 |
The symbol table itself varies in size and contents once tt(mfcalc) is |
| 8 |
used. It is defined as the em(data member) |
| 9 |
tt(d_symbols) in the Parser's header file. In contrast, the em(function table) |
| 10 |
has a em(fixed) size and contents. Because of this the function table is |
| 11 |
defined as a em(static) data member. Both tables are defined as |
| 12 |
tt(std::unordered_map) containers: their keys are |
| 13 |
tt(std::string) objects, their values, respecively, tt(double)s and tt(double |
| 14 |
(*)(double))s. Here is the declaration of tt(d_symbols) and tt(s_functions) as |
| 15 |
used in tt(mfcalc)'s parser: |
| 16 |
verb( |
| 17 |
std::unordered_map<std::string, double> d_symbols; |
| 18 |
|
| 19 |
static std::unordered_map<std::string, double (*)(double)> s_functions; |
| 20 |
) |
| 21 |
As tt(s_functions) is a static member, it can be initialized em(compile |
| 22 |
time) from an em(array of pairs). To ease the definition of such an array a |
| 23 |
tt(private using) declaration |
| 24 |
verb( |
| 25 |
using FunctionPair = std::pair<char const *, double (*)(double)>; |
| 26 |
) |
| 27 |
is added to the parser class, as well as a private array |
| 28 |
verb( |
| 29 |
static FunctionPair s_funTab[]; |
| 30 |
) |
| 31 |
These definitions allow us to initialize tt(s_functions) in a separate |
| 32 |
source file (tt(data.cc)): |
| 33 |
verbinclude(mfcalc/parser/data.cc) |
| 34 |
By simply editing the definition of tt(s_funTab), additional |
| 35 |
functions can be added to the calculator. |