Expressions

An expression is a sequence of operators, operands and punctuators that returns a value.

The primary expressions include: literals, constants, variables and function calls. From them, using operators, more complex expressions can be created. Formally, expressions are defined recursively: subexpressions can be nested up to the limits of memory.

Expressions are evaluated according to certain conversion, grouping, associativity and precedence rules which depend on the operators in use, presence of parentheses and data types of the operands. The precedence and associativity of the operators are summarized in Operator Precedence and Associativity. The way operands and subexpressions are grouped does not necessarily specify the actual order in which they are evaluated by mikroBasic PRO for ARM.

Expression Evaluation

General Rule

Expression are evaluated according to the right side operands. Operations are done at higher operand level, with signed operands taking precedence.

Example :

a as byte
b as word  
c as integer
    
a * b ' word level
a * c ' integer level
b * c ' integer level

Left side exception

In arithmetic expression left side is considered in the following manner : If the left side size in bytes is greater than higher operand size, then evaluation is done at one level above higher operand level (to get correct calculations).

Example :

a as longword
b as byte

a = b * 5 ' this is done at word level

Conditional expressions

Conditional expressions may differ from the same code in assignment expressions (due to left side exception).

Example :

a as longword
b as byte

if b*5 then... ' byte level - general rule will not give same result as

a = b * 5     ' word level - general rule + left side exception
if a then...

if b*5 exceeds byte range.

Explicit Typecasting

Any expression can be evaluated at specific level by using explicit typecasting. Having in mind previous example, in order to get same calculation in conditional and assignment expression, the following should be done :

if word(b*5) then... ' word level 
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code