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, you should #include either <tadsgen.h> or <tads.h> (the latter includes both <tadsio.h> and <tadsgen.h>, for the full set of TADS intrinsics). If you're using the adv3 library, you can simply #include <adv3.h>, since that automatically incluedes the basic system headers.

tads-gen functions

dataType(val)

Returns the datatype of the given value. The return value is one of the TypeXxx values (see the section on reflection).

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:

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. idx must be in the range 1 to argcount, or the function throws a run-time error ("bad value for built-in function").

getFuncParams(funcptr)

Returns information on the parameters taken by the given function. The return value is a list with three elements:

The second element gives the number of optional arguments; this element is always zero, because there's no way for an ordinary function (non-intrinsic) 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.

getTime(timeType?)

Returns the current system time, according to timeType. If timeType is not specified, GetTimeDateAndTime is the default. The valid timeType values are:

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 repeatCount is not specified, the default 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.

In all cases, rand() chooses numbers based on a "uniform distribution" over the range, 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. This function doesn't affect transient objects. restoreGame(filename) - restore the saved state from the file named by the filename string. 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:

rexGroup(groupNum)

Returns information on the group match for the last regular expression search or match. groupNum 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, and groups are numbered by the relative position of the left parenthesis in the regular expression.

Returns nil if there is no such group, or if there's 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 in characters of the group match; and the matching string.

rexMatch(pat, str, index?)

Tests str to see if the substring starting at character index index matches the given regular expression pat. pat can be given as a string containing a valid regular expression, 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 successful match that's zero characters long, 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 pattern pat within the source string str, starting at the character index given by 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 in its palce. 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 group expression, %2 by the second group's matching text, and so on. In addition, %* is replaced by the match for the entire regular expression. Because of the special meaning of the percent sign, you have to use the special code %% if you want to include a literal percent sign in the replacement text.

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 search string str, starting at the character position index. The pattern pat can be given as a string containing a valid regular expression, 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 character index within str of the first character of the matching substring (the first character in the string is at index 1); the length in characters 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 file named by the filename string. 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. For example, you could establish a savepoint just after reading a command line from the user, so that the user can subsequently undo the entire effect of the command if desired. Similarly, if you wanted to perform an operation speculatively, to see what would happen if you carried out some series of actions, you could set an undo savepoint, then undo to the savepoint once you've finished the speculative operation.

toInteger(val, radix?)

Convert the value given by val to an integer. If the radix value is specified, the conversion uses the given radix as the numeric base for the conversion; this value can be 2 (binary), 8 (octal), 10 (decimal), or 16 (hexadecimal). If radix isn't specified, the default is 10 (decimal).

toString(val, radix?)

Convert the given value val to a string.

undo()

Undoes all changes to object state back to the most recent undo savepoint, as established with the savepoint() function. Returns true if successful, nil if insufficient 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.

When the function returns nil, it will have made no changes to the system state. The function never makes any changes unless it has a complete set of undo information back to a savepoint, so the function will never leave the system in an inconsistent state. The VM has an internal limit on the total amount of undo information retained in memory at any given time, to keep memory consumption under control during a long-running session; as new undo information is added, the VM discards the oldest undo information as needed to keep within the memory limits. This maintains a rolling window of the most recent undo information.