Counter interpolation

From MZXWiki
Revision as of 14:44, 20 May 2008 by Wervyn (talk | contribs) (typo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Counter interpolation is a programming concept in Robotic that allows the value of any counter to be placed (or interpolated) into almost any string. This includes into strings representing counters themselves, label names, and raw text. This powerful feature allows users of Robotic to overcome some of its many shortcomings, and create array-like data structures, switch-like branches, and more.

Counter interpolation is typically invoked using a pair of ampersands (&) enclosing the counter to be interpolated. So for instance, if the value of "counter" is 50, then * "~fThe value of counter is &counter&." will print "The value of counter is 50", as one would expect. It is also possible to use expressions to do the same thing, and incorporate more complicated logic at the same time. There are some limitations though, since expressions can only return numbers, and not string values.

Notable Uses for Counter Interpolation

As just shown, counter interpolation can be used trivially to insert a value into a printed string. However, it has far more powerful applications than just that.

Counter Arrays

Counters can be interpolated into the names of other counters. This provides us with an easy way to create a data structure resembling an array, by referring to a counter named "array&index&". Thanks to expressions and nesting, this sort of interpolation can be done virtually anywhere now, and not just in command parameters that reference counters. As an example, the following code uses this technique and expressions to generate a list of Fibonacci numbers.

set "fib0" to 0
set "fib1" to 1
set "count" to 2
: "loop"
set "fib&count&" to "('fib('count'-1)'+'fib('count'-2)')"
inc "count" by 1
if "count" < 1000 then "loop"

This technique can be extended to create multi-dimensional arrays if necessary. Ultimately all that is necessary is that a unique name be created for each individual value. For instance, "array&x&,&y&" behaves like a 2-dimensional array.

Switch Constructs

Counters can also be interpolated into label names, when a label is being sent or goto'ed. It used to be possible to define labels with dynamic names like this, but that option was removed as a speed optimization, since almost no one ever used labels that way. In any case, this can be used to create a switch-like statement, as demonstrated below.

input string "Enter a number between 1 and 3."
goto "case&INPUT&"
* "~fInvalid input."
goto "end"
: "case1"
* "~fThis is option 1."
goto "end"
: "case2"
* "~fThis is option b."
goto "end"
: "case3"
* "~fThis is option over 9000."
goto "end"
: "caseswordfish"
* "~fYou found the secret option!"
: "end"
end

Also, as you can see from the example, there is no reason this method can't be used with strings as well as counters, as long as ampersands are used.