| 1 |
#ifndef _Formula_h_ |
| 2 |
#define _Formula_h_ |
| 3 |
/* Formula.h |
| 4 |
* |
| 5 |
* Copyright (C) 1990-2005,2007,2008,2011-2020,2023 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "Data.h" |
| 22 |
#include <new> |
| 23 |
|
| 24 |
#define kFormula_EXPRESSION_TYPE_NUMERIC 0 |
| 25 |
#define kFormula_EXPRESSION_TYPE_STRING 1 |
| 26 |
#define kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR 2 |
| 27 |
#define kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX 3 |
| 28 |
#define kFormula_EXPRESSION_TYPE_NUMERIC_TENSOR3 4 |
| 29 |
#define kFormula_EXPRESSION_TYPE_NUMERIC_TENSOR4 5 |
| 30 |
#define kFormula_EXPRESSION_TYPE_STRING_ARRAY 6 |
| 31 |
#define kFormula_EXPRESSION_TYPE_UNKNOWN 7 |
| 32 |
|
| 33 |
Thing_declare (InterpreterVariable); |
| 34 |
|
| 35 |
/* |
| 36 |
A stack element may be 32 bytes large, so with a stack size of 1'000'000 |
| 37 |
we have 32 MB for the stack. A formula instruction may be 16 bytes large, |
| 38 |
so we have 16 MB for lexical analysis and 16 MB for the parse. |
| 39 |
*/ |
| 40 |
#define Formula_MAXIMUM_STACK_SIZE 1'000'000 |
| 41 |
|
| 42 |
/* |
| 43 |
`Stackel` is short for 'stack element'. |
| 44 |
*/ |
| 45 |
typedef struct structStackel { |
| 46 |
#define Stackel_NUMBER 0 |
| 47 |
#define Stackel_STRING 1 |
| 48 |
#define Stackel_NUMERIC_VECTOR 2 |
| 49 |
#define Stackel_NUMERIC_MATRIX 3 |
| 50 |
#define Stackel_NUMERIC_TENSOR3 4 |
| 51 |
#define Stackel_NUMERIC_TENSOR4 5 |
| 52 |
#define Stackel_STRING_ARRAY 6 |
| 53 |
#define Stackel_VARIABLE -1 |
| 54 |
#define Stackel_OBJECT -2 |
| 55 |
int which; // 0 or negative = no clean-up required, positive = requires clean-up |
| 56 |
bool owned; // relevant only to numeric vector/matrix/tensor3/tensor4/strvec/strmat |
| 57 |
/* |
| 58 |
The following member of structStackel can either be a struct or a union. |
| 59 |
The struct option is the easy option: no special care will have to be taken when assigning to its members. |
| 60 |
The union option attempts to optimize for space and perhaps speed by putting the variants into a smaller space, |
| 61 |
but it requires special logic for initialization, deletion, and assignment. |
| 62 |
The special logic is partly implemented by encapsulating _string as a "private" member. |
| 63 |
*/ |
| 64 |
#define STACKEL_VARIANTS_ARE_PACKED_IN_A_UNION 1 |
| 65 |
#if STACKEL_VARIANTS_ARE_PACKED_IN_A_UNION |
| 66 |
union |
| 67 |
#else |
| 68 |
struct |
| 69 |
#endif |
| 70 |
{ |
| 71 |
double number; |
| 72 |
autostring32 _string; |
| 73 |
Daata object; |
| 74 |
VEC numericVector; |
| 75 |
MAT numericMatrix; |
| 76 |
STRVEC stringArray; |
| 77 |
InterpreterVariable variable; |
| 78 |
}; |
| 79 |
structStackel () { |
| 80 |
memset ((void *) this, 0, sizeof (structStackel)); // union-safe zeroing of all members of structStackel |
| 81 |
Melder_assert (our which == Stackel_NUMBER); // check that on this computer, 0 is represented with zero bits only |
| 82 |
Melder_assert (our number == 0.0); // check that on this computer, 0.0 is represented with zero bits only |
| 83 |
Melder_assert (! our _string); // check that on this computer, a plain-old-data null pointer is represented with zero bits only |
| 84 |
Melder_assert (! our object); // check that on this computer, a class null pointer is represented with zero bits only |
| 85 |
Melder_assert (! our owned); // check that on this computer, false is represented with zero bits only |
| 86 |
} |
| 87 |
void reset () { // union-safe destruction: test which variant we have |
| 88 |
if (our which <= Stackel_NUMBER) |
| 89 |
return; |
| 90 |
if (our which == Stackel_STRING) { |
| 91 |
our _string. reset(); |
| 92 |
} else if (our which == Stackel_NUMERIC_VECTOR) { |
| 93 |
if (our owned) { |
| 94 |
{// scope |
| 95 |
autoVEC removable; |
| 96 |
removable. adoptFromAmbiguousOwner (our numericVector); |
| 97 |
} |
| 98 |
our numericVector = VEC (); // undangle |
| 99 |
} |
| 100 |
} else if (our which == Stackel_NUMERIC_MATRIX) { |
| 101 |
if (our owned) { |
| 102 |
{// scope |
| 103 |
autoMAT removable; |
| 104 |
removable. adoptFromAmbiguousOwner (our numericMatrix); |
| 105 |
} |
| 106 |
our numericMatrix = MAT (); // undangle |
| 107 |
} |
| 108 |
} else if (our which == Stackel_STRING_ARRAY) { |
| 109 |
if (our owned) { |
| 110 |
{// scope |
| 111 |
autoSTRVEC removable; |
| 112 |
removable. adoptFromAmbiguousOwner (our stringArray); |
| 113 |
} |
| 114 |
our stringArray = STRVEC (); // undangle |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
~structStackel () { // union-safe destruction: test which variant we have |
| 119 |
our reset(); |
| 120 |
} |
| 121 |
structStackel& operator= (structStackel&& other) noexcept { // generalized move assignment |
| 122 |
if (& other != this) { |
| 123 |
our reset(); |
| 124 |
memmove ((void *) this, & other, sizeof (structStackel)); // union-safe: even our biggest variant is bit-copied entirely |
| 125 |
memset ((void *) & other, 0, sizeof (structStackel)); // union-safe: even the biggest variant in `other` is erased |
| 126 |
} |
| 127 |
return *this; |
| 128 |
} |
| 129 |
conststring32 getString () { |
| 130 |
return our _string.get(); |
| 131 |
} |
| 132 |
autostring32 moveString () { |
| 133 |
return our _string.move(); |
| 134 |
} |
| 135 |
void setString (autostring32 newString) { |
| 136 |
our reset(); |
| 137 |
#if STACKEL_VARIANTS_ARE_PACKED_IN_A_UNION |
| 138 |
::new (& our _string) autostring32(); // a convoluted and type-unsafe, but portable, way to null those 32 or 64 bits |
| 139 |
#endif |
| 140 |
our which = Stackel_STRING; |
| 141 |
our _string = newString.move(); |
| 142 |
} |
| 143 |
conststring32 whichText (); |
| 144 |
} *Stackel; |
| 145 |
|
| 146 |
struct Formula_Result { |
| 147 |
int expressionType; |
| 148 |
double numericResult; |
| 149 |
autostring32 stringResult; |
| 150 |
VEC numericVectorResult; |
| 151 |
MAT numericMatrixResult; |
| 152 |
STRVEC stringArrayResult; |
| 153 |
bool owned; |
| 154 |
Formula_Result () { |
| 155 |
our expressionType = kFormula_EXPRESSION_TYPE_NUMERIC; |
| 156 |
our numericResult = 0.0; |
| 157 |
our stringResult = autostring32(); |
| 158 |
our numericVectorResult = VEC (); |
| 159 |
our numericMatrixResult = MAT (); |
| 160 |
our stringArrayResult = STRVEC (); |
| 161 |
our owned = false; |
| 162 |
} |
| 163 |
void reset () { |
| 164 |
our stringResult. reset(); |
| 165 |
if (our owned) { |
| 166 |
{// scope |
| 167 |
autoVEC removable; |
| 168 |
removable. adoptFromAmbiguousOwner (our numericVectorResult); |
| 169 |
} |
| 170 |
our numericVectorResult = VEC (); // undangle |
| 171 |
{// scope |
| 172 |
autoMAT removable; |
| 173 |
removable. adoptFromAmbiguousOwner (our numericMatrixResult); |
| 174 |
} |
| 175 |
our numericMatrixResult = MAT (); // undangle |
| 176 |
{// scope |
| 177 |
autoSTRVEC removable; |
| 178 |
removable. adoptFromAmbiguousOwner (our stringArrayResult); |
| 179 |
} |
| 180 |
our stringArrayResult = STRVEC (); // undangle |
| 181 |
} |
| 182 |
} |
| 183 |
~ Formula_Result () { |
| 184 |
our reset(); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
Thing_declare (Interpreter); |
| 189 |
|
| 190 |
void Formula_compile (Interpreter interpreter, Daata data, conststring32 expression, int expressionType, bool optimize); |
| 191 |
|
| 192 |
void Formula_run (integer row, integer col, Formula_Result *result); |
| 193 |
|
| 194 |
/* End of file Formula.h */ |
| 195 |
#endif |