| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1999, 2006 Michael J. Roberts |
| 6 | * |
| 7 | * This file is part of TADS 3 |
| 8 | * |
| 9 | * This header defines the tads-gen intrinsic function set. This function |
| 10 | * set provides some miscellaneous functions, including data conversions, |
| 11 | * object iteration, regular expressions, and state persistence operations. |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | /* |
| 16 | * The tads-gen function set. This set contains basic data manipulation |
| 17 | * and miscellaneous utility functions. |
| 18 | */ |
| 19 | intrinsic 'tads-gen/030008' |
| 20 | { |
| 21 | /* |
| 22 | * Get the type of the given value. This returns a TypeXxx value. |
| 23 | */ |
| 24 | dataType(val); |
| 25 | |
| 26 | /* |
| 27 | * Get the given parameter to the current function. 'idx' is 1 for the |
| 28 | * first argument in left-to-right order, 2 for the second, and so on. |
| 29 | */ |
| 30 | getArg(idx); |
| 31 | |
| 32 | /* |
| 33 | * Get the first object in memory. If 'cls' is provided, we return the |
| 34 | * first object of the given class; otherwise we return the first |
| 35 | * object of any kind. 'flags' is an optional bitwise combination of |
| 36 | * ObjXxx values, specifying whether classes, instances, or both are |
| 37 | * desired. If this isn't specified, ObjAll is assumed. This is used |
| 38 | * in conjunction with nextObj() to iterate over all objects in memory, |
| 39 | * or all objects of a given class. |
| 40 | * |
| 41 | * Note that firstObj-nextObj loops can retrieve objects that aren't |
| 42 | * otherwise reachable, because unreachable objects are only removed |
| 43 | * from memory when the garbage collector runs, which happens |
| 44 | * intermittently. If you want to ensure that any currently |
| 45 | * unreachable objects are removed from memory just before a |
| 46 | * firstObj-nextObj loop, you can do so by calling t3RunGC(). |
| 47 | */ |
| 48 | firstObj(cls?, flags?); |
| 49 | |
| 50 | /* |
| 51 | * Get the next object in memory after the given object, optionally of |
| 52 | * the given class and optionally limiting to instances, classes, or |
| 53 | * both. This is used to continue an iteration started with |
| 54 | * firstObj(). |
| 55 | */ |
| 56 | nextObj(obj, cls?, flags?); |
| 57 | |
| 58 | /* |
| 59 | * Random number generator (RNG) initialization. This selects and |
| 60 | * initializes the random number generator algorithm used by rand(). |
| 61 | * TADS provides several different RNG algorithms; each RNG has |
| 62 | * different properties, so some applications might have reasons to |
| 63 | * prefer a particular algorithm. For general purposes, any of them |
| 64 | * should produce good results. |
| 65 | * |
| 66 | * The interpreter automatically makes a call to randomize() (the |
| 67 | * no-arguments version) when it starts up, unless the user specifies |
| 68 | * the "-norand" option when launching the interpreter. For most |
| 69 | * programs, this means that you'll never have to make your own call to |
| 70 | * randomize() - you can just call rand() when you need random numbers. |
| 71 | * |
| 72 | * This function performs several tasks, depending on how you invoke |
| 73 | * it: |
| 74 | * |
| 75 | *. randomize() - selects the default RNG algorithm (ISAAC), and seeds |
| 76 | *. the RNG with truly random data from the operating system. |
| 77 | * |
| 78 | *. randomize(nil) - retrieves the current state of the RNG. Returns |
| 79 | *. a list: [id, state], where 'id' is the ID of the currently |
| 80 | *. selected RNG algorithm, and 'state' is a value containing |
| 81 | *. its internal state, which can be used in a later call to |
| 82 | *. randomize([id, state]) to restore the RNG state. The 'state' |
| 83 | *. value is opaque, meaning that it's not meant to be used |
| 84 | *. directly; the only thing you should do with it it save it |
| 85 | *. for later if you should want to restore this state later. |
| 86 | * |
| 87 | *. randomize([id, state]) - if you pass in a list that obtained from |
| 88 | *. an earlier call to randomize(nil), the RNG will be returned |
| 89 | *. to its state at the time of the randomize(nil) call. This |
| 90 | *. selects the same RNG algorithm that was in effect then and |
| 91 | *. restores the internal state of the generator. After calling |
| 92 | *. this, a series of calls to rand() will return the same |
| 93 | *. sequence of numbers that were returned after the call to |
| 94 | *. randomize(nil). |
| 95 | * |
| 96 | *. randomize(id) - selects the RNG algorithm identified by 'id' |
| 97 | *. (an RNG_Xxx value). This doesn't change the state of |
| 98 | *. the generator; it simply selects the algorithm. |
| 99 | * |
| 100 | *. randomize(id, nil) - selects the RNG algorithm identified by |
| 101 | *. 'id' (an RNG_Xxx value), and seeds the generator with |
| 102 | *. truly random data obtained from the operating system. |
| 103 | * |
| 104 | *. randomize(id, val) - selects the RNG algorithm identified by |
| 105 | *. 'id' (an RNG_Xxx value), and seeds the generator with |
| 106 | *. the initial value 'val'. This can be either an integer |
| 107 | *. value or a string; the preferred format varies by |
| 108 | *. algorithm, but they'll all accept either format. After |
| 109 | *. you set a given seed value, rand() will return a sequence |
| 110 | *. of numbers that's repeatable every time you set the same |
| 111 | *. seed value. This can be useful for things like testing, |
| 112 | *. where you want a sequence of numbers that's statistically |
| 113 | *. random, but which can be reproduced on demand. |
| 114 | * |
| 115 | * Most programs that require random numbers want a truly unpredictable |
| 116 | * series of numbers - that is, numbers that have a statistically |
| 117 | * random distribution, with no discernible patterns, and which will be |
| 118 | * different every time the program is run. There are two separate |
| 119 | * parts to this proposition, and they're handled by separate |
| 120 | * functions. rand() fulfills the first part: it uses a mathematical |
| 121 | * formula to generate a series of numbers that are statistically |
| 122 | * distributed in a random fashion (for example, so that "1" occurs as |
| 123 | * often as "10" or any other number, any given sequence of 2, 3, or |
| 124 | * more numbers is equally likely, and so on: mathematicians have many |
| 125 | * formal tests that RNGs must satisfy to be considered random.) |
| 126 | * randomize() fulfills the second part, which is making sure that the |
| 127 | * sequence of numbers is different every time you run the program. |
| 128 | * The reason this second part is important is that rand() is by its |
| 129 | * nature deterministic: it's defined entirely in terms of a formula, |
| 130 | * so given the same initial conditions, it'll always crank out the |
| 131 | * same sequence of numbers. The trick is to randomize its initial |
| 132 | * conditions - and what makes it tricky is that we can't just turn to |
| 133 | * rand(), since it's the thing we're trying to randomize! |
| 134 | * |
| 135 | * This is where the "seed" values come in. randomize() and |
| 136 | * randomize(id, nil) ask the operating system for truly random data to |
| 137 | * use for the initial conditions. The degree of entropy in this OS |
| 138 | * seed data varies by system; some systems have better entropy sources |
| 139 | * than others. But whatever the source, the seed data should be |
| 140 | * different each time you run the program. randomize() feeds this |
| 141 | * seed data into the RNG to set its initial conditions, so each time |
| 142 | * you run, rand() will be starting from a different initial state. |
| 143 | * This makes for a different series of numbers from rand() on each |
| 144 | * run. |
| 145 | * |
| 146 | * Note that it's not necessary (or desirable) to call randomize() |
| 147 | * every time you want a random number. Once you seed the RNG, rand() |
| 148 | * is all you need to call. It can be slow to gather the true random |
| 149 | * data that randomize() uses, since this sometimes requires |
| 150 | * interacting with hardware devices or scanning large amounts of |
| 151 | * system data. rand() is quite fast, since it just calculates a |
| 152 | * number using a formula. Depending on the system, rand() might also |
| 153 | * be more reliable at producing high volumes of statistically random |
| 154 | * data than the OS sources. The operating system sources of true |
| 155 | * entropy don't always change quickly, so it's better to use them |
| 156 | * infrequently, such as just once at the start of program execution, |
| 157 | * than to use them as a routine source of random numbers. |
| 158 | * |
| 159 | * The fixed seed values, with randomize(id, val), do something a |
| 160 | * little different. Rather than making the RNG produce different |
| 161 | * sequences on each run, a fixed seed makes rand() generate the same |
| 162 | * series of numbers every time. The numbers will still be |
| 163 | * statistically random, but each time you run the program, you'll get |
| 164 | * the same seaquence. (The sequence is a function of the seed value. |
| 165 | * You'll get a different sequence for each different seed value.) |
| 166 | * |
| 167 | * Why would you want a fixed series of rand() results? One big reason |
| 168 | * is testing. One popular way to test software is regression testing, |
| 169 | * where you run the program and compare its output to a reference |
| 170 | * version that you know is correct. If there are no differences, you |
| 171 | * know that changes you've made to the program since haven't broken |
| 172 | * anything in the test script. Randomization interferes with this |
| 173 | * kind of testing, because it makes the output different on each run - |
| 174 | * it's useless to do a simple mechanical comparison of the new and old |
| 175 | * output because they'll always differ. Fixed seeds to the rescue. |
| 176 | * Using a fixed seed, you can still exercise the program's random |
| 177 | * behavior, but the sequence of random behavior will repeat on every |
| 178 | * run, so you run those regression comparisons after all. The really |
| 179 | * great thing is that you don't have to make big changes to the |
| 180 | * program if you want to switch between test mode and real randomness |
| 181 | * - all you have to do is change the one call to randomize(). |
| 182 | */ |
| 183 | randomize(...); |
| 184 | |
| 185 | /* |
| 186 | * Select a random number or a random value. This uses the current |
| 187 | * random number algorithm as selected via randomize(). |
| 188 | * |
| 189 | * If no arguments are supplied, the result is a random integer |
| 190 | * distributed evenly over the full range of the 32-bit integer type. |
| 191 | * The result can be positive or negative. |
| 192 | * |
| 193 | * If exactly one argument is supplied, the result depends on the type |
| 194 | * of the argument: |
| 195 | * |
| 196 | * - Integer: the function returns an integer from 0 to one less than |
| 197 | * the argument value. For example, rand(10) returns a number from 0 |
| 198 | * to 9 inclusive. |
| 199 | * |
| 200 | * - List: the function randomly selects one of the values from the |
| 201 | * list and returns it. |
| 202 | * |
| 203 | * - String: the function generates a random string by replacing each |
| 204 | * character of the argument string with a randomly chosen character, |
| 205 | * selected from a specific range specified by the argument character. |
| 206 | * For example, each 'a' in the input string is replaced by a random |
| 207 | * lower-case letter from a to z, each 'A' is replaced by a capital |
| 208 | * letter, and each 'd' is replaced by a random digit 0 to 9. See the |
| 209 | * System Manual for the full list of the character codes. |
| 210 | * |
| 211 | * If more than one argument is supplied, the function randomly selects |
| 212 | * one of the arguments and returns it. Note that this is an ordinary |
| 213 | * function call, so all of the arguments are evaluated, triggering any |
| 214 | * side effects of those evaluations. |
| 215 | * |
| 216 | * In all cases, the random numbers are uniformly distributed, meaning |
| 217 | * that each possible return value has equal probability. |
| 218 | */ |
| 219 | rand(...); |
| 220 | |
| 221 | /* |
| 222 | * Convert the given value to a string representation. 'val' can be an |
| 223 | * integer, in which case it's converted to a string representation in |
| 224 | * the numeric base given by 'radix' (which can be any value from 2 to |
| 225 | * 36), or base 10 (decimal) if 'radix' is omitted; nil or true, in |
| 226 | * which case the string 'nil' or 'true' is returned; a string, which |
| 227 | * is returned unchanged; or a BigNumber, in which case the number is |
| 228 | * converted to a string representation in the given radix; a list or |
| 229 | * vector, in which case the individual elements are converted |
| 230 | * recursively, then the results concatenated together into a string |
| 231 | * with commas separating elements; or any of the built-in object types |
| 232 | * with default string conversions (ByteArray, StringBuffer, FileName, |
| 233 | * Date, TimeZone, FileName, etc). |
| 234 | * |
| 235 | * Note that when working with BigNumber values, you might prefer to |
| 236 | * use BigNumber.formatString(), as that gives you more control over |
| 237 | * the formatting style. |
| 238 | * |
| 239 | * 'radix' is only meaningful with numeric values (integers and |
| 240 | * BigNumbers). For BigNumbers, only whole integer values can be |
| 241 | * displayed in a non-decimal radix; if the number has a fractional |
| 242 | * part, the radix will be ignored and the number will be shown in |
| 243 | * decimal. |
| 244 | * |
| 245 | * 'isSigned' indicates whether or not the value should be treated as |
| 246 | * "signed", meaning that negative values are represented with a "-" |
| 247 | * sign followed by the absolute value. If 'isSigned' is nil, a |
| 248 | * negative value won't be converted to its absolute value before being |
| 249 | * displayed, but will instead be re-interpreted within its type system |
| 250 | * as an unsigned value. For regular integers, this means that the |
| 251 | * result depends on the native hardware storage format for negative |
| 252 | * integers. Most modern hardware uses two's complement notation, |
| 253 | * which represents -1 as 0xFFFFFFFF, -2 as 0xFFFFFFFE, etc. Most |
| 254 | * types other than integer don't have distinct signed and unsigned |
| 255 | * interpretations, so 'isSigned' isn't meaningful with most other |
| 256 | * types. With BigNumber in particular, the only effect is to omit the |
| 257 | * "-" sign for negative values. |
| 258 | */ |
| 259 | toString(val, radix?, isSigned?); |
| 260 | |
| 261 | /* |
| 262 | * Convert the given value to an integer. |
| 263 | * |
| 264 | * If 'val' is a string, the function parses the string's contents as |
| 265 | * an integer in the numeric base given by 'radix, which can be any |
| 266 | * integer from 2 to 36. If 'radix' is omitted or nil, the default is |
| 267 | * base 10 (decimal). The value is returned as an integer. If the |
| 268 | * number represented by the string is too large for a 32-bit integer, |
| 269 | * a numeric overflow error occurs. |
| 270 | * |
| 271 | * If 'val' is true, or the string 'true', the return value is 1. If |
| 272 | * 'val' is nil, or the string 'nil', the return value is 0. Leading |
| 273 | * and trailing spaces are ignored for these strings. |
| 274 | * |
| 275 | * If 'val' is a BigNumber value, the value is rounded to the whole |
| 276 | * number, and returned as an integer value. A numeric overflow error |
| 277 | * occurs if the number is out of range for a 32-bit integer. (If you |
| 278 | * want to round a BigNumber to the nearest integer and get the result |
| 279 | * as another BigNumber value, use the getWhole() method of the |
| 280 | * BigNumber.) |
| 281 | * |
| 282 | * See also toNumber(), which can also parse floating point values and |
| 283 | * whole numbers too large for the ordinary integer type. |
| 284 | */ |
| 285 | toInteger(val, radix?); |
| 286 | |
| 287 | /* |
| 288 | * Get the current local time. |
| 289 | * |
| 290 | * If timeType is GetTimeDateAndTime (or is omitted), this returns the |
| 291 | * calendar date and wall-clock time, as a list: [year, month, |
| 292 | * dayOfMonth, dayOfWeek, dayOfYear, hour, minute, second, timer]. |
| 293 | * Year is the year AD (for example, 2006); month is the current month, |
| 294 | * from 1 (January) to 12 (December); dayOfMonth is the calendar day of |
| 295 | * the month, from 1 to 31; dayOfWeek is the day of the week, from 1 |
| 296 | * (Sunday) to 7 (Saturday); dayOfYear is the current day of the year, |
| 297 | * from 1 (January 1) to 366 (December 31 in a leap year); hour is the |
| 298 | * hour on a 24-hour clock, ranging from 0 (midnight) to 23 (11pm); |
| 299 | * minute is the minute of the hour, from 0 to 59; second is the second |
| 300 | * of the minute, from 0 to 59; and timer is the number of seconds |
| 301 | * elapsed since the "Epoch," defined as midnight, January 1, 1970, |
| 302 | * midnight UTC. (This is the Epoch that Unix-like systems use, so it |
| 303 | * appears frequently in computer timekeeping systems.) See the Date |
| 304 | * class for more comprehensive date/time handling. |
| 305 | * |
| 306 | * If timeType is GetTimeTicks, this return the number of milliseconds |
| 307 | * since an arbitrary starting time. The first call to get this |
| 308 | * information sets the starting time, so it will return zero; |
| 309 | * subsequent calls will return the amount of time elapsed from that |
| 310 | * starting time. Note that because a signed 32-bit integer can only |
| 311 | * hold values up to about 2 billion, the maximum elapsed time that |
| 312 | * this value can represent is about 24.8 days; so, if your program |
| 313 | * runs continuously for more than this, the timer value will roll |
| 314 | * around to zero at each 24.8 day multiple. So, it's possible for |
| 315 | * this function to return a smaller value than on a previous |
| 316 | * invocation, if the two invocations straddle a 24.8-day boundary. |
| 317 | */ |
| 318 | getTime(timeType?); |
| 319 | |
| 320 | /* |
| 321 | * Match a string to a regular expression pattern. 'pat' can be either |
| 322 | * a string giving the regular expression, or can be a RexPattern |
| 323 | * object. 'str' is the string to match, and 'index' is the starting |
| 324 | * character index (the first character is at index 1) at which to |
| 325 | * start matching. Returns the length in characters of the match, or |
| 326 | * nil if the string doesn't match the pattern. (Note that a return |
| 327 | * value of zero doesn't indicate failure - rather, it indicates a |
| 328 | * successful match of the pattern to zero characters. This is |
| 329 | * possible for a pattern with a zero-or-more closure, such as 'x*' or |
| 330 | * 'x?'.) |
| 331 | */ |
| 332 | rexMatch(pat, str, index?); |
| 333 | |
| 334 | /* |
| 335 | * Search the given string for the given regular expression pattern. |
| 336 | * 'pat' is a string giving the regular expression to find, or a |
| 337 | * RexPattern object. 'str' is the string to search, and 'index' is |
| 338 | * the optional starting index (the first character is at index 1; |
| 339 | * negative indices are from the end of the string, so -1 is the last |
| 340 | * character, -2 is the second to last, and so on). If a match to the |
| 341 | * pattern isn't found, returns nil. If a match is found, the return |
| 342 | * value is a list: [index, length, string], where index is the |
| 343 | * starting character index of the match, length is the length in |
| 344 | * characters of the match, and string is the text of the match. |
| 345 | */ |
| 346 | rexSearch(pat, str, index?); |
| 347 | |
| 348 | /* |
| 349 | * Get the given regular expression group. This can be called after a |
| 350 | * successful rexMatch() or rexSearch() call to retrieve information on |
| 351 | * the substring that matched the given "group" within the regular |
| 352 | * expression. A group is a parenthesized sub-pattern within the |
| 353 | * regular expression; groups are numbered left to right by the open |
| 354 | * parenthesis, starting at group 1. If there is no such group in the |
| 355 | * last regular expression searched or matched, or the group wasn't |
| 356 | * part of the match (for example, because it was part of an |
| 357 | * alternation that wasn't matched), the return value is nil. If the |
| 358 | * group is valid and was part of the match, the return value is a |
| 359 | * list: [index, length, string], where index is the character index |
| 360 | * within the matched or searched string of the start of the group |
| 361 | * match, length is the character length of the group match, and string |
| 362 | * is the text of the group match. |
| 363 | */ |
| 364 | rexGroup(groupNum); |
| 365 | |
| 366 | /* |
| 367 | * Search for the given regular expression pattern (which can be given |
| 368 | * as a regular expression string or as a RexPattern object) within the |
| 369 | * given string, and replace one or more occurrences of the pattern |
| 370 | * with the given replacement text. |
| 371 | * |
| 372 | * The search pattern can also be given as a *list* of search patterns. |
| 373 | * In this case, we'll search for each of the patterns and replace each |
| 374 | * one with the corresponding replacement text. If the replacement is |
| 375 | * itself given a list in this case, each element of the pattern list |
| 376 | * is replaced by the corresponding element of the replacement list. |
| 377 | * If there are more patterns than replacements, the extra patterns are |
| 378 | * replaced by empty strings; any extra replacements are simply |
| 379 | * ignored. If the replacement is a single value rather than a list, |
| 380 | * each pattern is replaced by that single replacement value. |
| 381 | * |
| 382 | * 'flags' is a combination of the ReplaceXxx bit flags, using '|'. If |
| 383 | * the flags include ReplaceAll, all occurrences of the pattern are |
| 384 | * replaced; otherwise only the first occurrence is replaced. |
| 385 | * |
| 386 | * ReplaceOnce and ReplaceAll are mutually exclusive; they mean, |
| 387 | * respectively, that only the first occurrence of the match should be |
| 388 | * replaced, or that every occurrence should be replaced. ReplaceOnce |
| 389 | * and ReplaceAll are ignored if a 'limit' value is specified (this is |
| 390 | * true even if 'limit' is nil, which means that all occurrences are |
| 391 | * replaced). |
| 392 | * |
| 393 | * If ReplaceIgnoreCase is included, the capitalization of the match |
| 394 | * pattern is ignored, so letters in the pattern match both their |
| 395 | * upper- and lower-case equivalents. Otherwise the case will be |
| 396 | * matched exactly. If ReplaceFollowCase AND ReplaceIgnoreCase are |
| 397 | * included, lower-case letters in the replacement text are capitalized |
| 398 | * as needed to follow the capitalization pattern of the actual text |
| 399 | * matched: if all the letters in the match are lower-case, the |
| 400 | * replacement is lower case; if all are upper-case, the replacement is |
| 401 | * changed to all upper-case; if there's a mix of cases in the match, |
| 402 | * the first letter of the replacement is capitalized and the rest are |
| 403 | * left in lower-case. |
| 404 | * |
| 405 | * The ReplaceSerial flag controls how the search proceeds when |
| 406 | * multiple patterns are specified. By default, we search for each one |
| 407 | * of the patterns, and replace the leftmost match. If ReplaceOnce is |
| 408 | * specified, we're done; otherwise we continue by searching again for |
| 409 | * all of the patterns, this time in the remainder of the string (after |
| 410 | * that first replacement), and again we replace the leftmost match. |
| 411 | * This proceeds until we can't find any more matches for any of the |
| 412 | * patterns. If ReplaceSerial is included in the flags, we start by |
| 413 | * searching only for the first pattern, replacing one or all |
| 414 | * occurrences depending on the ReplaceOnce or ReplaceAll flag. Next, |
| 415 | * if ReplaceAll is specified OR we didn't find any matches for the |
| 416 | * first pattern, we start over with the result and search for the |
| 417 | * second pattern, replacing one or all occurrences of it. We repeat |
| 418 | * this for each pattern. |
| 419 | * |
| 420 | * If the flags are omitted entirely, the default is ReplaceAll (which |
| 421 | * means replace all occurrences, exact case matches only, parallel |
| 422 | * searching). |
| 423 | * |
| 424 | * 'index', if provided, is the starting character index of the search; |
| 425 | * instances of the pattern before this index will be ignored. Returns |
| 426 | * the result string with all of the desired replacements. When an |
| 427 | * instance of the pattern is found and then replaced, the replacement |
| 428 | * string is not rescanned for further occurrences of the text, so |
| 429 | * there's no danger of infinite recursion; instead, scanning proceeds |
| 430 | * from the next character after the replacement text. |
| 431 | * |
| 432 | * 'limit', if specified, is an integer indicating the maximum number |
| 433 | * of matches to replace, or nil to replace all matches. If the limit |
| 434 | * is reached before all matches have been replaced, no further |
| 435 | * replacements are performed. If this parameter is specified, it |
| 436 | * overrides any ReplaceOnce or ReplaceAll flag. |
| 437 | * |
| 438 | * The replacement text can use "%n" sequences to substitute group |
| 439 | * matches from the input into the output. %1 is replaced by the match |
| 440 | * to the first group, %2 the second, and so on. %* is replaced by the |
| 441 | * entire matched input. (Because of the special meaning of "%", you |
| 442 | * must use "%%" to include a percent sign in the replacement text.) |
| 443 | */ |
| 444 | rexReplace(pat, str, replacement, flags?, index?, limit?); |
| 445 | |
| 446 | /* |
| 447 | * Create an UNDO savepoint. This adds a marker to the VM's internal |
| 448 | * UNDO log, establishing a point in time for a future UNDO operation. |
| 449 | */ |
| 450 | savepoint(); |
| 451 | |
| 452 | /* |
| 453 | * UNDO to the most recent savepoint. This uses the VM's internal UNDO |
| 454 | * log to undo all changes to persistent objects, up to the most recent |
| 455 | * savepoint. Returns true if the operation succeeded, nil if not. A |
| 456 | * nil return means that there's no further UNDO information recorded, |
| 457 | * which could be because the program has already undone everything |
| 458 | * back to the start of the session, or because the UNDO log was |
| 459 | * truncated due to memory size such that no savepoints are recorded. |
| 460 | * (The system automatically limits the UNDO log's total memory |
| 461 | * consumption, according to local system parameters. This function |
| 462 | * requires at least one savepoint to be present, because otherwise it |
| 463 | * could create an inconsistent state.) |
| 464 | */ |
| 465 | undo(); |
| 466 | |
| 467 | /* |
| 468 | * Save the current system state into the given file. This uses the |
| 469 | * VM's internal state-save mechanism to store the current state of all |
| 470 | * persistent objects in the given file. Any existing file is |
| 471 | * overwritten. |
| 472 | * |
| 473 | * 'metatab' is an optional LookupTable containing string key/value |
| 474 | * pairs to be saved with the file as descriptive metadata. The |
| 475 | * interpreter and other tools can display this information to the user |
| 476 | * when browsing a collection of saved game files, to help the user |
| 477 | * remember the details of each saved position. It's up to the game to |
| 478 | * determine what to include; the list can include any information |
| 479 | * relevant to the game that would be helpful when reviewing saved |
| 480 | * position files, such as the room name, score, turn count, chapter |
| 481 | * name, etc. |
| 482 | */ |
| 483 | saveGame(filename, metatab?); |
| 484 | |
| 485 | /* |
| 486 | * Restore a previously saved state file. This loads the states of all |
| 487 | * persistent objects stored in the given file. The file must have |
| 488 | * been saved by the current version of the current running program; if |
| 489 | * not, an exception is thrown. |
| 490 | */ |
| 491 | restoreGame(filename); |
| 492 | |
| 493 | /* |
| 494 | * Restart the program from the beginning. This resets all persistent |
| 495 | * objects to their initial state, as they were when the program was |
| 496 | * first started. |
| 497 | */ |
| 498 | restartGame(); |
| 499 | |
| 500 | /* |
| 501 | * Get the maximum of the given arguments. The values must be |
| 502 | * comparable with the ordinary "<" and ">" operators. Note that |
| 503 | * because this is an ordinary function call, all of the arguments are |
| 504 | * evaluated (which means any side effects of these evaluations will be |
| 505 | * triggered). |
| 506 | */ |
| 507 | max(val1, ...); |
| 508 | |
| 509 | /* |
| 510 | * Get the minimum of the given arguments. The values must be |
| 511 | * comparable with the ordinary "<" and ">" operators. Note that |
| 512 | * because this is an ordinary function call, all of the arguments are |
| 513 | * evaluated (which means any side effects of these evaluations will be |
| 514 | * triggered). |
| 515 | */ |
| 516 | min(val1, ...); |
| 517 | |
| 518 | /* |
| 519 | * Create a string by repeating the given value the given number of |
| 520 | * times. If the repeat count isn't specified, the default is 1; a |
| 521 | * repeat count less than zero throws an error. 'val' can be a string, |
| 522 | * in which case the string is simply repeated the given number of |
| 523 | * times; an integer, in which case the given Unicode character is |
| 524 | * repeated; or a list of integers, in which case the given Unicode |
| 525 | * characters are repeated, in the order of the list. The list format |
| 526 | * can be used to create a string from a list of Unicode characters |
| 527 | * that you've been manipulating as a character array, which is |
| 528 | * sometimes a more convenient or efficient way to do certain types of |
| 529 | * string handling than using the actual string type. |
| 530 | */ |
| 531 | makeString(val, repeatCount?); |
| 532 | |
| 533 | /* |
| 534 | * Get a description of the parameters to the given function. 'func' |
| 535 | * is a function pointer. This function returns a list: [minArgs, |
| 536 | * optionalArgs, isVarargs], where minArgs is the minimum number of |
| 537 | * arguments required by the function, optionalArgs is the additional |
| 538 | * number of arguments that can be optionally provided to the function, |
| 539 | * and isVarargs is true if the function takes any number of additional |
| 540 | * ("varying") arguments, nil if not. |
| 541 | */ |
| 542 | getFuncParams(func); |
| 543 | |
| 544 | /* |
| 545 | * Convert the given value to a number. This is similar to |
| 546 | * toInteger(), but can parse strings containing floating point numbers |
| 547 | * and whole numbers too large for ordinary integers. |
| 548 | * |
| 549 | * If 'val' is an integer or BigNumber value, the return value is |
| 550 | * simply 'val'. |
| 551 | * |
| 552 | * If 'val' is a string, the function parses the string's contents as a |
| 553 | * number in the given 'radix', which can be any integer from 2 to 36. |
| 554 | * If 'radix' is omitted, the default is 10 for decimal. If the radix |
| 555 | * is decimal, and the number contains a decimal point (a period, '.') |
| 556 | * or an exponent (which consists of the letter 'e' or 'E', an optional |
| 557 | * '+' or '-' sign, and one or more digits), the value is parsed as a |
| 558 | * floating point number, and a BigNumber value is returned. For any |
| 559 | * other radix, decimal points and exponents are considered non-number |
| 560 | * characters. For an integral value, the result will be an integer if |
| 561 | * the number is within the range that fits in an integer, otherwise |
| 562 | * the result is a BigNumber. The routine will simply stop parsing at |
| 563 | * the first non-number character it encounters, so no error will occur |
| 564 | * if the string contains text following the number. If the text |
| 565 | * doesn't contain any number characters at all, the result is zero. |
| 566 | * |
| 567 | * If val is true or the string 'true', return 1; if nil or the string |
| 568 | * 'nil', returns 0. Leading and trailing spaces are ignored in the |
| 569 | * string versions of these values. |
| 570 | */ |
| 571 | toNumber(val, radix?); |
| 572 | |
| 573 | /* |
| 574 | * Format values into a string. This is similar to the traditional C |
| 575 | * language "printf" family of functions: it takes a "format string" |
| 576 | * containing a mix of plain text and substitution parameters, and a |
| 577 | * set of values to plug in to the substitution parameters, and returns |
| 578 | * a new string containing the formatted result. |
| 579 | * |
| 580 | * 'format' is the format string. Most characters of the format string |
| 581 | * are simply copied verbatim to the result. However, each '%' in the |
| 582 | * format string begins a substitution parameter; the '%' is followed |
| 583 | * by one or more optional qualifiers, then by a type code letter. The |
| 584 | * corresponding value from the argument list is formatted into a |
| 585 | * string according to the type code, and then replaces the entire '%' |
| 586 | * sequence in the result string. By default, the first '%' parameter |
| 587 | * corresponds to the first additional argument after 'format', the |
| 588 | * second '%' corresponds to the second additional argument, and so on. |
| 589 | * You can override the default argument position of a '%' using the |
| 590 | * '$' qualifier - see below. |
| 591 | * |
| 592 | * The arguments following 'format' are the values to be substituted |
| 593 | * for the '%' parameters in the format string. |
| 594 | * |
| 595 | * The return value is a string containing the formatted result. |
| 596 | * |
| 597 | * See the System Manual for the list of '%' codes. |
| 598 | */ |
| 599 | sprintf(format, ...); |
| 600 | |
| 601 | /* |
| 602 | * Create a list by repeating the given value the given number of |
| 603 | * times. If the repeat count isn't specified, the default is 1; a |
| 604 | * repeat count less than zero throws an error. 'val' can be any |
| 605 | * value; it's simply repeated in each element of the list. |
| 606 | */ |
| 607 | makeList(val, repeatCount?); |
| 608 | |
| 609 | /* |
| 610 | * Get the absolute value of a number. The argument can be an integer |
| 611 | * or a BigNumber; the return value is the absolute value of the |
| 612 | * argument, and has the same type as the argument. (The absolute |
| 613 | * value of a positive number X (or zero) is X; the absolute value of a |
| 614 | * negative number X is -X.) |
| 615 | */ |
| 616 | abs(num); |
| 617 | |
| 618 | /* |
| 619 | * Get the sign of a number. The argument can be an integer or a |
| 620 | * BigNumber. The return value is an integer: 1 if the argument is |
| 621 | * positive, 0 if the argument is zero, -1 if the argument is negative. |
| 622 | */ |
| 623 | sgn(num); |
| 624 | |
| 625 | /* |
| 626 | * Concatenate the arguments together into a single string. The |
| 627 | * arguments can be strings or any types that can be automatically |
| 628 | * converted to string for the regular "+" operator; non-strings are |
| 629 | * first converted to strings using the same rules that "+" uses when |
| 630 | * combining a string with a non-string. If there are no arguments, |
| 631 | * the result is an empty string. |
| 632 | * |
| 633 | * This function is essentially the same as concatenating a series of |
| 634 | * values with the "+" operator, but it's more efficient with three or |
| 635 | * more values, since the "+" operator has to be applied successively |
| 636 | * in pairs; that creates and copies an extra intermediate result |
| 637 | * string at each step. This function only creates one result string |
| 638 | * and only has to copy each input string once. |
| 639 | */ |
| 640 | concat(...); |
| 641 | |
| 642 | /* |
| 643 | * Search backwards in the given string for the given regular |
| 644 | * expression pattern. 'pat' is a string giving the regular expression |
| 645 | * to find, or a RexPattern object. 'str' is the string to search, and |
| 646 | * 'index' is the optional starting index (the first character is at |
| 647 | * index 1; negative indices are from the end of the string, so -1 is |
| 648 | * the last character, -2 is the second to last, and so on; 0 means the |
| 649 | * position just after the last character of the string). If 'index' |
| 650 | * is omitted, the default is to search the entire string from the end, |
| 651 | * which is equivalent to passing 0 or str.length()+1 for 'index'. |
| 652 | * |
| 653 | * If a match is found, the return value is a list: [index, length, |
| 654 | * string], where index is the starting character index of the match, |
| 655 | * length is the length in characters of the match, and string is the |
| 656 | * text of the match. |
| 657 | * |
| 658 | * This does the same work as rexSearch(), but searches the string |
| 659 | * backwards, from the end to the start. The match must end before the |
| 660 | * starting index, which allows for repeated searches: simply pass the |
| 661 | * match index from the previous search as the 'index' value for the |
| 662 | * next search to find the next earlier match that doesn't overlap the |
| 663 | * previous match. |
| 664 | * |
| 665 | * The meanings of the <FirstBegin> and <FirstEnd> flags for a reverse |
| 666 | * search are essentially the mirror image of their meanings in a |
| 667 | * regular forward search. This is easiest to understand by thinking |
| 668 | * about the flags in the abstract. <FirstBegin> means that the |
| 669 | * winning match is the one whose "near" endpoint is closest to the |
| 670 | * starting index; <FirstEnd> means that the winner is the match whose |
| 671 | * "far" endpoint is closest to the starting index. The near endpoint |
| 672 | * in a forward search is the start of the match, whereas it's the end |
| 673 | * of the match in a reverse search. Similarly, the far endpoint is |
| 674 | * the end of the match in a forward search and the start of the match |
| 675 | * in a reverse search. So in a reverse search, <FirstBegin> means |
| 676 | * that the winner is the match whose ending index is highest, and |
| 677 | * <FirstEnd> means that the winner is the one whose starting index is |
| 678 | * highest. |
| 679 | */ |
| 680 | rexSearchLast(pat, str, index?); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * flags for firstObj() and nextObj() |
| 685 | */ |
| 686 | #define ObjInstances 0x0001 |
| 687 | #define ObjClasses 0x0002 |
| 688 | #define ObjAll (ObjInstances | ObjClasses) |
| 689 | |
| 690 | /* |
| 691 | * rexReplace() flags |
| 692 | */ |
| 693 | #define ReplaceAll 0x0001 |
| 694 | #define ReplaceIgnoreCase 0x0002 |
| 695 | #define ReplaceFollowCase 0x0004 |
| 696 | #define ReplaceSerial 0x0008 |
| 697 | #define ReplaceOnce 0x0010 |
| 698 | |
| 699 | /* |
| 700 | * getTime() flags |
| 701 | */ |
| 702 | #define GetTimeDateAndTime 1 |
| 703 | #define GetTimeTicks 2 |
| 704 | |
| 705 | /* |
| 706 | * Random number generator IDs for randomize(). |
| 707 | */ |
| 708 | |
| 709 | /* |
| 710 | * |
| 711 | * RNG_ISAAC - ISAAC is the default generator. It's designed for both |
| 712 | * cryptographic application and more mainstream uses. Crypto applications |
| 713 | * generally have more stringent requirements for an RNG than ordinary |
| 714 | * applications. ISAAC does well with the usual statistical tests for RNGs |
| 715 | * and is reasonably fast. |
| 716 | * |
| 717 | * ISAAC's preferred format for a fixed seed is a string value. |
| 718 | */ |
| 719 | #define RNG_ISAAC 1 |
| 720 | |
| 721 | |
| 722 | /* |
| 723 | * RNG_LCG - LCG stands for Linear Congruential Generator, which generates |
| 724 | * numbers using a simple linear formula. This is the old standby of |
| 725 | * computer RNGs; it's the sort that comes standard with C compilers. |
| 726 | * These generators have been extensively studied and have reasonably good |
| 727 | * statistical properties and a number of known weaknesses. The TADS LCG |
| 728 | * uses the method described in Knuth, The Art of Computer Programming, |
| 729 | * volume 2, p170. |
| 730 | * |
| 731 | * The LCG's preferred format for a fixed seed is an integer value. |
| 732 | */ |
| 733 | #define RNG_LCG 2 |
| 734 | |
| 735 | /* |
| 736 | * RNG_MT19937 - Mersenne Twister MT19937 algorithm. This is a widely used |
| 737 | * RNG algorithm that's become especially popular for scientific |
| 738 | * simulations. It's relatively new, dating from 1997, and was designed to |
| 739 | * remedy most of the known shortcomings of linear congruential generators |
| 740 | * and other earlier RNGs. It's fast and does well on standard statistical |
| 741 | * tests. |
| 742 | * |
| 743 | * MT's preferred format for a fixed seed is a string value. |
| 744 | */ |
| 745 | #define RNG_MT19937 3 |
| 746 |