Expressions

A mathematical expression is just a formula that can be evaluated into a numerical result, like 2+2 or (12-18)*42. A conditional expression is a mathematical expression that can be interpreted as "true" or "false". MB treats expressions that evaluate to "0" as false and those that evaluate to anything else as true.

What makes these expressions useful in MB are functions that can be used in expressions, that return significant information about the bot's environment. The functions that are defined for MB are listed in the Functions section.

Expressions should not contain any spaces, unless they are surrounded by parentheses. For example

12+18-6*8.2
or
(12 + 18 – 6 * 8.2)
...are valid.

You can also use parentheses to control the order of evaluation of parts of the expression, such as 2*(3+8).

MB's expression evaluator recognizes the following operators:

+     add
-      subtract
*     multiply
/      divide
%   modulus division
^    exponent
#    random number (like a dice roll, n # d, where n is the number of dice and d is the die)
&& logical and
||   logical or
&    bitwise and
|     bitwise or
<    less than
>    greater than
<= less than or equal to
>= greater than or equal to
=    equal to
<> not equal to
Operator Precedence Levels

"Precedence" refers to the order that operations are carried out when evaluating an expression. For instance, multiplication has a higher precedence that addition, so in the expression 2+3*4, the 3*4 is calculated first, then 2+12. Elements within parentheses are always evaluated first.

The precedence levels for expressions in Magsbot are shown below, from higher to lower precedence (i.e. the ^ operation is performed before the # operation, and so forth). Operators with the same level of precedence are performed left-to-right.

 ^
 #
 *  /  %
 + -
 <   >   <<   >>
 =  <>
 &  |  &&  ||

Data types

MBot supports two data types, numeric and string (text).

Numeric data is always treated as floating point, although some functions may only use the integer part.  (Technically all numeric variables are variant type, internally.)

A conditional expression must evaluate to a number, not a string; strings are used either as parameters for functions that return numbers (e.g. @len[$x]), or as parameters for Actions.

String constants can be unquoted as long as they contain only letters, numbers and underscores, and begin with a letter, otherwise they should begin and end with a double quote (").

Names of numeric variables or functions start with a @ and names of string variables or functions start with a $. Assignment can be done using the @asn and $asn functions, or the ASN, ASNS, GLOBAL and GLOBALSTR commands, or (more commonly) using the short form of assignment. Assignment can also be done using several List processing commands such as ENLIST or SETITEM. See the Actions section for more information.

Assignment to variables in a VList are done using the STORE and STORE_ commands.

Examples of valid strings:

testing   "this is a test"

$test

$cat["this"," and that"]

$if[@x=12,"x equals twelve!","x does not equal twelve!"]

Examples of valid numbers:
12

(6+2)

9*@x

@test

@len["some string"]

@len[somestringwithnospaces]

@if[@x=12,8,7]


Variables

See Notes on variable assignment.

Scope

Variables in MB can be Global or Local in scope. "Local" means that variable is only visible and only exists in the context of the Action definition that it's in, whether it's a row of the Behavior Table, an immediate command in the Do menu item, or an Action Button. This prevents conflicts from occuring when two Actions are performed at the same time (for instance if two Events that trigger Actions occur at the same) and use the same variable name. Local variables are not visible to other Actions, even if they occur at the same time.

"Global" means that the variable continues to exist as long as MB is running, unless you free it using the "Free variables" menu item or programmatically such as with the FREEVAR command. Also, global variables are visible anywhere, unless a local variable has the same name. You can use global variables to communicate between Actions, for instance if you want an Action button to set some variable that will be visible to an Action in the Behavior Table.

When you refer to a variable name in any Action, MB first checks for a local variable of that name, and if it exists then it returns that value. If no local variable of that name exists, MB then looks for a global variable of that name. If no variable with the specified name exists, an error will occur and the Action will stopwith an error message.

Variables in MB are local by default. To create global variables, use GLOBAL or GLOBALSTR instead of ASN or ASNS. (You can also the short form of assignment for global variables. ) See the Actions section for details.

Items in numbered lists are always global, and commands such as ENLIST always create global variables.

Items in VLists can only be accessed using the @lv_ or $lv_ functions, and assigned using STORE or STORE_.