Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.

Commit 4975a6e

Browse files
committed
[[ Constants ]] Refactor constant lookup and value creation
This patch cleans up the implementation of constant lookup, replacing the string and number value fields in the constant table's Cvalue struct with a typed union. This enables existing string or number-based constants to be specified as before, but constants with values that are MCValueRefs to be represented in the table using specific values of the CvalueType enum. The lookup mechanism is thereby unaffected. It also removes the MCConstant syntax class, as it is no longer needed, and uses MCLiteral instead. The constant values are interned so that there is only ever one instance of the value in memory. Previously the `true` and `false` constants were evaluated as strings. This patch changes them to be evaluated as booleans. The only material difference this should make is in use of the `is strictly` operator and when the constants are elements of arrays passed to LCB handlers; in the latter case it is already the LCB code's responsibility to handle variable typed in incoming array elements. Furthermore the constant `infinity` has been added, evaluating to +inf (as a real).
1 parent d17ce23 commit 4975a6e

9 files changed

Lines changed: 225 additions & 201 deletions

File tree

engine/engine-sources.gypi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
'src/cmdsm.cpp',
9090
'src/cmdsp.cpp',
9191
'src/cmdss.cpp',
92-
'src/constant.cpp',
9392
'src/date.cpp',
9493
'src/express.cpp',
9594
'src/external.cpp',

engine/src/constant.cpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

engine/src/constant.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

engine/src/exec-engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ MCExecSetTypeInfo *kMCEngineSecurityCategoriesTypeInfo = &_kMCEngineSecurityCate
120120

121121
extern const LT command_table[];
122122
extern const uint4 command_table_size;
123-
extern const Cvalue constant_table[];
123+
extern const Cvalue *constant_table;
124124
extern const uint4 constant_table_size;
125125
extern const LT factor_table[];
126126
extern const uint4 factor_table_size;

engine/src/keywords.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,7 @@ Parse_stat MCLocaltoken::parse(MCScriptPoint &sp)
212212
{
213213
// If the unquoted literal is a recognised constant and the constant's value
214214
// is identical (case-sensitively) to the value, it is fine to make it a literal.
215-
const char *t_value;
216-
if (sp . lookupconstantvalue(t_value) &&
217-
MCStringIsEqualToCString(sp . gettoken_stringref(), t_value, kMCStringOptionCompareExact))
215+
if (sp . constantnameconvertstoconstantvalue())
218216
type = ST_LIT;
219217
}
220218

engine/src/lextable.cpp

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
3535
// mandates that variables declared as "const" have internal linkage unless also
3636
// declared as "extern".
3737
extern const LT command_table[];
38-
extern const Cvalue constant_table[];
38+
extern const Cvalue *constant_table;
3939
extern const LT factor_table[];
4040
extern const LT * const table_pointers[];
4141
extern const uint2 table_sizes[];
@@ -184,58 +184,67 @@ const uint8_t unicode_type_table[256] =
184184
ST_ID, ST_UNDEFINED, ST_UNDEFINED, ST_ID, // 0xFC 0xFD 0xFE 0xFF
185185
};
186186

187-
const Cvalue constant_table[] =
187+
/* The constant_table must be constexpr to force the compiler to generate
188+
* it at compile time (the rule is that constexpr expressions may be evaluated
189+
* at compile-time or runtime *unless* they are used as a subexpression of a
190+
* constexpr in which case they must be evaluated at compile-time - MSVC
191+
* needs constexpr here to force compile-time eval; other compilers are happy
192+
* with just const - go figure!). */
193+
static constexpr const Cvalue constant_table_values[] =
188194
{
189-
{"arrow", "29", 29.0},
190-
{"backslash", "\\", BAD_NUMERIC},
191-
{"busy", "6", 6.0},
192-
{"clock", "14", 14.0},
193-
{"colon", ":", BAD_NUMERIC},
194-
{"comma", ",", BAD_NUMERIC},
195-
{"cr", "\n", BAD_NUMERIC},
196-
{"crlf", "\r\n", BAD_NUMERIC},
197-
{"cross", "7", 7.0},
198-
{"done", "done", BAD_NUMERIC},
199-
{"down", "down", BAD_NUMERIC},
200-
{"eight", "8", 8.0},
201-
{"empty", "", BAD_NUMERIC},
202-
{"end", "\004", BAD_NUMERIC},
203-
{"endoffile", "\004", BAD_NUMERIC},
204-
{"eof", "\004", BAD_NUMERIC},
205-
{"false", "false", BAD_NUMERIC},
206-
{"five", "5", 5.0},
207-
{"formfeed", "\014", BAD_NUMERIC},
208-
{"four", "4", 4.0},
209-
{"hand", "28", 28.0},
210-
{"help", "15", 15.0},
211-
{"ibeam", "9", 9.0},
212-
{"left", "left", BAD_NUMERIC},
213-
{"lf", "\n", BAD_NUMERIC},
214-
{"linefeed", "\n", BAD_NUMERIC},
215-
{"nine", "9", 9.0},
216-
{"none", "0", 0.0},
217-
{"null", "", BAD_NUMERIC},
218-
{"one", "1", 1.0},
219-
{"pi", "3.14159265358979323846", 3.14159265358979323846},
220-
{"plus", "13", 13.0},
221-
{"quote", "\"", BAD_NUMERIC},
222-
{"return", "\n", BAD_NUMERIC},
223-
{"right", "right", BAD_NUMERIC},
224-
{"scrollbarfactor", "65535", 65535.0},
225-
{"seven", "7", 7.0},
226-
{"six", "6", 6.0},
227-
{"slash", "/", BAD_NUMERIC},
228-
{"space", " ", BAD_NUMERIC},
229-
{"tab", "\t", BAD_NUMERIC},
230-
{"ten", "10", 10.0},
231-
{"three", "3", 3.0},
232-
{"true", "true", BAD_NUMERIC},
233-
{"two", "2", 2.0},
234-
{"up", "up", BAD_NUMERIC},
235-
{"watch", "14", 14.0},
236-
{"zero", "0", 0.0}
195+
/* Initializer lists are used as (in C++11) they map to constructors. */
196+
{"arrow", 29},
197+
{"backslash", "\\"},
198+
{"busy", 6},
199+
{"clock", 14},
200+
{"colon", ":"},
201+
{"comma", ","},
202+
{"cr", "\n"},
203+
{"crlf", "\r\n"},
204+
{"cross", 7},
205+
{"done", "done"},
206+
{"down", "down"},
207+
{"eight", 8},
208+
{"empty", kCValueTypeEmpty},
209+
{"end", "\004"},
210+
{"endoffile", "\004"},
211+
{"eof", "\004"},
212+
{"false", kCValueTypeFalse},
213+
{"five", 5},
214+
{"formfeed", "\014"},
215+
{"four", 4},
216+
{"hand", 28},
217+
{"help", 15},
218+
{"ibeam", 9},
219+
{"infinity", kCValueTypeInfinity},
220+
{"left", "left"},
221+
{"lf", "\n"},
222+
{"linefeed", "\n"},
223+
{"nine", 9},
224+
{"none", 0},
225+
{"null", kCValueTypeNull},
226+
{"one", 1},
227+
{"pi", 3.14159265358979323846},
228+
{"plus", 13},
229+
{"quote", "\""},
230+
{"return", "\n"},
231+
{"right", "right"},
232+
{"scrollbarfactor", 65535},
233+
{"seven", 7},
234+
{"six", 6},
235+
{"slash", "/"},
236+
{"space", " "},
237+
{"tab", "\t"},
238+
{"ten", 10},
239+
{"three", 3},
240+
{"true", kCValueTypeTrue},
241+
{"two", 2},
242+
{"up", "up"},
243+
{"watch", 14},
244+
{"zero", 0},
237245
};
238-
extern const uint4 constant_table_size = ELEMENTS(constant_table);
246+
const Cvalue *constant_table = constant_table_values;
247+
extern const uint4 constant_table_size = ELEMENTS(constant_table_values);
239248

240249
const static LT accept_table[] =
241250
{

0 commit comments

Comments
 (0)