### Intrinsic Functions: "tads-gen" Function Set

The "tads-gen" function set provides general utility and data manipulation functions. These functions have no user interface component.

To use the "tads-gen" function set in a program, #include "tadsgen.h", or simply #include "tads.h" (which includes both "tadsio.h" and "tadsgen.h" for the full set of TADS intrinsics.)

dataType(val) – returns the datatype of the given value. The return value is one of the same TypeXxx values.

firstObj(cls?, flags?) – returns the first object of class cls, or the first object in the entire program if cls is not specified. This is used to start iterating over the set of all instances of a given class; use nextObj() to continue the iteration.. The order in which objects are enumerated by firstObj() and nextObj() is arbitrary, but each object will be returned exactly once. Returns nil if there are no objects of the given class.

If the flags argument is specified, it can a combination (with the '|' operator) of the following bit flags:

- ObjInstances – the function returns only instances, not "class" objects. This is the default.
- ObjClasses – the function returns only objects originally defined as "class" objects.
- ObjAll – this is defined for convenience as (ObjInstances | ObjClasses).

    If the flags argument is omitted, only instances are enumerated, as though ObjInstances had been specified.

getArg(idx) – retrieve the given argument to the current function. The first argument is at index 1. The index must be in the range 1 to `argcount`, or the function throws a run-time error (BAD_VAL_BIF).

getFuncParams(funcptr) – returns information on the parameters taken by the given function. The return value is a list with three elements:

returnValue[1] is the minimum number of arguments taken by the function;

returnValue[2] is the number of additional optional arguments taken by the function;

returnValue[3] is true if the function accepts any number of additional arguments, nil if not.

The second element gives the number of optional arguments; this element is always zero, because there is no way for a function to specify optional arguments. This element is included in the list specifically so that the list uses the same format as the Object.getPropParams() method.

If the third element is true, it indicates that the function was defined with the "..." varying argument list notation.

getMethodDefiner() – returns the object that defines the currently executing method. The definer is the object that actually contains the method definition in the source code, so it is not necessarily the current "self" object.

getTime(timeType?) – returns the current system time, according to timeType. If timeType is not specified, GetTimeDateAndTime is the default. The valid timeType values are:

- GetTimeDateAndTime – returns the current system date and time as a list with the following elements: [year, month, month_day, week_day, year_day, hour, minute, second, timer].

- The year is the AD year number (the full four-digit number – happily this product released in 2000 is Y2K compliant)
- The month is the month number (January = 1, February = 2, etc.)
- The month_day is the day of the month (on March 13, this returns 13)
- The week_day is the day of the week (Sunday = 1, Monday = 2, etc.)
- The year_day is the day of the year (January 1 = 1, January 2 = 2, etc.)
- The hour is the hour of the day on a 24-hour clock (2:00 PM returns 14)
- The minute is the minute of the hour (2:35 PM returns 35)
- The second is the second of the minute (2:35:12 PM returns 12)
- The timer is the number of seconds since January 1, 1970.

- GetTimeTicks – returns the number of milliseconds since some arbitrary zero point. The precision of the timer varies by system, so the fact that the return value is represented with millisecond precision doesn't necessarily mean that the system is actually capable of measuring time differences that precisely. The zero point is chosen as the time of the first call during a VM session, which reduces the likelihood that the program will ever encounter a timer rollover (i.e., the point at which the timer exceeds the 31-bit precision of the integer return value and thus resets back to zero), which occurs after about 23 days of continuous execution.

makeString(val, repeatCount?) – construct a string by repeating the given value the given number of times. The result of the function depends on the data type of val:

- If val is a string, the return value is the given number of copies of the string appended one after the other. For example, makeString('abc', 3) = 'abcabcabc'.
- If val is a list, the list must contain integers. Each integer in the list gives a Unicode character value. The function constructs a string with the same number of characters as the list has elements, and with each character of the string having the Unicode code point of the corresponding integer in the list. This string is then repeated the given number of times. For example, makeString([65,66,67]) = 'ABC'.
- If val is an integer, the function returns a string consisting of the single Unicode character whose code point is given by the integer, repeated the specified number of times.
- Other types are invalid.

If repeatCount is not specified, the default value is 1.

max(val1, …) – returns the least argument value. The values must be of comparable types.

min(val1, …) – returns the greatest argument value. The values must be of types that can be compared with one another, or the function throws an error.

nextObj(obj, cls?, flags?) – get the next object after obj of class cls. If cls is not specified, returns the next object of any type. Returns nil if obj is the last object of class cls. This is used (with firstObj()) to iterate over all objects of a given class, or over all objects. The order in which these functions enumerate objects is arbitrary, but each object will be returned exactly once. The flags argument has the same meaning as it does in firstObj().

rand(x, ...) – returns a pseudo-random number or randomly selects a value from a set of values. With a single integer argument, rand() generates a random integer from 0 to one less than the given value, inclusive, and returns the integer value; for example, rand(10) returns a value from 0 to 9. With a single list argument, rand() randomly selects one of the list elements and returns it. With two or more arguments, rand() randomly selects one of the argument values and returns it; note, however, that all of the arguments are evaluated (and hence any side effects of those evaluations will occur). In all cases, rand() chooses a number over the range uniformly, which means that each value in the desired range has equal probability.

randomize() – seeds the pseudo-random number generator with a value obtained in a system-dependent manner (in most cases, the seed value is based on the system clock). If randomize() is never called, the random number generator will return the same sequence of numbers every time a program is run; when testing a program, this is often useful, since it means that the program's results are repeatable.

restartGame() – resets all objects (except transient objects) to their initial state, as they were when the program was just loaded. Transient objects are not affected.

restoreGame(filename) – restore the saved state from the given file. All objects, except transient objects, are restored to the state they had when the state was saved to the given file.

If an error occurs, the function throws a run-time error. The errno_ property of the RuntimeError exception object gives a VM error code describing the problem; the possible errors are:

- 1201 – the file does not contain a saved state (it has some other type of data)
- 1202 – the state was saved by a different program, or by a different version of the same program
- 1207 – the file is corrupted

rexGroup(group_num) – returns information on the group match for the last regular expression search or match. group_num is the number of the parenthesized group for which to retrieve the information; the first parenthesized expression in the most recent search expression is number 1. Returns nil if there is no such group or no match for the group. If there's a match for the group, returns a three-element list: the character index of the group match within the original source string; the length of the group match; and the matching string.

rexMatch(pat, str, index?) – tests str to see if the substring starting at index index matches the given regular expression pat, which can be given as a string using regular expression syntax, or as a RexPattern object. If the leading substring of str matches the regular expression, the function returns the number of characters of the matching substring; if there is no match, the function returns nil. This does not search for a match, but merely determines if str matches the expression in its leading substring. Note that a regular expression can successfully match zero characters, so a return value of zero is distinct from a return value of nil: zero indicates a match of zero characters, and nil indicates no match.

If index is given, it indicates the starting index for the match; index 1 indicates the first character in the string, and is the default if index is omitted. This can be used to match a substring of str to the pattern without actually creating a separate substring value.

Refer to the regular expressions section for details on how to construct a pattern string.

rexReplace(pat, str, replacement, flags, index?) – replace one or more matches for the regular expression pat within str, starting at the character index. Each match is replaced with the string replacement. The return value is the resulting string with the substitutions applied. The pattern pat can be given as a string using regular expression syntax, or as a RexPattern object. The flags value specifies whether to replace multiple occurrences or not: ReplaceOnce specifies that only the first match is to be replaced, and ReplaceAll specifies that all matches are to be replaced.

If index is given, replacements start with the first instance of the pattern at or after the character index position. The first character is at index 1. If index is omitted, the search starts at the first character.

The replacement string is substituted for the original matching text of each match (or of the first match if ReplaceOnce is specified). The original substring that matches the regular expression pat is removed from the string, and replacement is inserted at the same position. The replacement text can include the special sequences '%1' through '%9' to indicate that the text that matches the corresponding parenthesized group in the regular expression should be substituted at that point. '%1' is replaced by the original matching text of the first parenthesized expression, '%2' by the second group's matching text, and so on. In addition, '%*' is replaced by the match for the entire regular expression. (To substitute a single percent sign, you must use '%%', because of the special meaning of the percent sign in the replacement string.)

Refer to the regular expressions section for details on how to construct a pattern string.

rexSearch(pat, str, index?) – searches for the regular expression pat in the string str, starting at the character position index. The pattern pat can be given as a string using regular expression syntax, or as a RexPattern object.

If index is given, it gives the starting character position in str for the search. The first character is at index 1. If index is omitted, the search starts with the first character. The index value can be used to search for repeated instances of the pattern, by telling the function to ignore matches before the given point in the string.

If the function finds a match, it returns a list with three elements: the index within str of the first character of the matching substring (the first character in the string is at index 1); the length of the matching substring; and a string giving the matching substring. If there is no match, the function returns nil.

Refer to the regular expressions section for details on how to construct a pattern string.

saveGame(filename) – saves the state of all objects (except transient objects) to the given file. If an error occurs, the function throws a run-time error to indicate the problem. The saved state can later be restored using restoreGame().

savepoint() – establish an undo savepoint. Multiple savepoints can be established to mark multiple points in time.

toInteger(str, radix?) – convert the given string value to an integer. If the radix value is specified, the conversion uses the given radix; only 8 (octal), 10 (decimal), and 16 (hexadecimal) are allowed. If no radix is specified, the default is 10 (decimal).

If the string contains the text 'nil' or 'true', the return values are nil and true, respectively. Otherwise, the function skips any leading spaces in the string, then tries to parse a number in the given radix. If the radix is 10, and the string (after leading spaces) starts with a dash, the function skips the dash and treats the number as negative; if the radix is 10, and the string starts with a plus sign, the function skips the plus sign. The function then scans all following consecutive numerals in the given radix and returns the resulting integer value.

toString(val, radix?) – convert the given value to a string. If the value is an integer, and radix is specified, the value is converted using the given radix (2 for binary, 8 for octal, 10 for decimal, or 16 for hexadecimal; other radix values below 37 are allowed as well, but these are the most common). A decimal conversion is done using a signed interpretation of the number, so a negative value will be preceded by a dash in the result string; the number is treated as unsigned for any other radix. If radix is not specified, the default radix is 10 (decimal).

If the value is anything other than an integer, the radix value is ignored. If the value is a string, it is returned unchanged. If the value is nil or true, the strings 'nil' and 'true', respectively, are returned. For any other type, the function throws a run-time error (NO_STR_CONV).

undo() – undo changes to the most recent savepoint, as established with the savepoint() function. Returns true if successful, nil if no more undo information is available. This can be called repeatedly; each call undoes changes to the next most recent savepoint. All changes affecting object state since the last savepoint are undone by this operation, except that transient objects are not affected.