twaddle

Functions

Twaddle offers a handful of functions. Most of these are related to behaviour in looping or branching and are described in more depth in the related documentation.

Syntax

Twaddle functions are enclosed within square brackets. The square brackets contain the function name, then its arguments.

The function name is separated from any arguments by a colon. If the function accepts multiple arguments, they are separated from each other by a semicolon. So this:

[rand:0,10]

calls the rand function with two arguments: 0, and 10.

Block functions

Some functions take effect wherever they are encountered in the Twaddle sentence, others are “block functions”. Block functions apply their effect to the next block encountered within the sentence.

See block functions for a fuller description.

Block functions are marked in the list below.

Available functions

Most available functions can be broadly grouped into a number of categories:

Additionally, some utility functions exist which don’t fall into these categories. They are generally useful only to manage the side-effects of other elements of Twaddle.

Repeat

The repeat function, rep, is a block function.
It repeats the block. See loops for a full description.

It requires one argument:

[rep:<n>]

n is the number of repetitions

Separator

The separator function, sep, is a block function. It defines a separator between repetitions of a block. See loops for a full description.

It requires one argument:

[sep:<text>]

text is the text to be inserted as a separator

First

The first function, first, is a block function. It defines text to be inserted before the first repetition of a block. See loops for a full description.

It requires one argument:

[first:<text>]

text is the text to be inserted before the first repetition### First

Last

The last function, last, is a block function. It defines text to be inserted before the last repetition of a block. See loops for a full description.

It requires one argument:

[last:<text>]

text is the text to be inserted before the last repetition

Text modifiers

Case

By default the output of Twaddle retains capitalization from the input and in the case of lookups from dictionary entries. The case function, case, changes this. It sets a capitalization strategy. It requires one argument:

[case:<strategy>]

strategy is the capitalization strategy to use.

The capitalization strategy affects the entire sentence from the point it is applied, it is not restricted by e.g. blocks. To cancel the effect of a capitalization strategy, apply [case:none].

Abbreviate

The abbreviate function, also callable as abbr, is a block function. It replaces the next block with an abbreviation formed from the initial letters of each word. It takes one optional argument:

[abbreviate]{<block contents>} [abbreviate:<case>]{<block contents>}

<case> is an optional argument defining the case in which the abbreviation is generated. The permitted values are retain, upper, lower, and first.

See the abbreviations documentation page for a full description of how abbreviations are created and the meanings of the case options.

Reverse

The reverse function is a block function. It prints the following block in reverse:

[reverse]{oh no, \a <noun>!}

It takes no arguments.

Regular Expressions

Regular expressions can be used to match and replace patterns within text. The pattern takes the place of the function name, and is demarcated by a double slash // at each end. Two arguments are accepted:

[//<regex>//:<text>;<replacement>]

regex is the regex to be run,

text is the text over which the regex should run

replacement is the text which should be used as a replacement for each regex match

See the regex documentation page for more details.

Match

The match function can only be used within the replacement text argument a regex. It inserts the text matching the regex. It takes no arguments.

[match]

See the regex documentation page for more details.

Reproduction

Save

The save function is a block function. It saves the next block as a pattern which can be reused. It takes one mandatory argument:

[save:<name>]{<pattern>}

name is the name with which to save the pattern. If a pattern is already saved with that name, it is overwritten.

Load

The load function loads a saved pattern. It takes one mandatory argument:

[load:<name>]

name is the name of the pattern to load. If no pattern has been saved with that name, a TwaddleInterpreterException is raised.

Copy

The copy function is a block function. It copies the evaluated result to a clipboard for later reuse. It takes one mandatory argument:

[copy:<name>]{<pattern>}

name is the name of the clipboard to which to save the block’s evaluated result. If the clipboard already exists, it is overwritten.

Paste

The paste function loads the contents of a clipboard. It takes one mandatory argument:

[paste:<name>]

name is the name of the clipboard to load. If nothing has been copied to that clipboard, a TwaddleInterpreterException is raised.

Randomization

Rand

The rand function generates a random integer. It requires two arguments:

[rand:<min>;<max>]

min is the minimum value of the integer generated

max is the maximum value of the integer generated

The limits are inclusive.

Synchronizer

The synchronizer function, sync, is a block function. It defines a synchronizer. On first use it requires two arguments:

[sync:<name>;<type>]

name is the name of the synchronizer

type is the type of synchronizer

On subsequent uses, it requires one argument:

[sync:<name>]

name is the name of the synchronizer.

For backwards-compatibility x is supported as a synonym of sync, with the same syntax.

Utility

Hide

The hide function is a block function. It allows for a block to be processed but excluded from the output.

It accepts no arguments:

[hide]

Clear

The clear function clears any defined labels, synchronizers, patterns, and clipboards. It takes no arguments:

[clear]

This function is primarily useful in persistent mode, although it can also be used within a Twaddle sentence if desired. When used within a sentence, any persistent items defined before the clear function is inserted will be reset from the point in the sentence where the clear function is used. For example:

<noun::=a> <noun::=a> [clear]<noun::=a> <noun::=a>

May produce sentences like

raisin raisin shirt shirt jug jug llama llama robin robin market market