Expressions

Documentation for Expressions.

Operators

OperatorDescription
a + bAddition
a - bSubtraction
a * bMultiplication
a / bDivision
a % bModulus
a >> bBitshift right
a << bBitshift left
~aBitwise NOT
a & bBitwise AND
a | bBitwise OR
a ^ bBitwise XOR
a == bEquality comparison
a != bInequality comparison
a > bGreater-than comparison
a < bLess-than comparison
a >= bGreater-than-or-equals comparison
a <= bLess-than-or-equals comparison
!aBoolean NOT
a && bBoolean AND
a || bBoolean OR
a ^^ bBoolean XOR
a ? b : cTernary
(a)Parenthesis
function(a)Function call

a, b and c can be any numeric literal or another expression.

Type Operators

Type Operators are operators that work on types. They can only be used on a variable, not on a mathematical expression.

OperatorDescription
addressof(a)Address of variable
sizeof(a)Size of variable
typenameof(a)String representation of the variable's type

a can be a variable, either by naming it directly or finding it through member access

PROVIDER OPERATORS

a can also be replaced with the $ operator to query information about the loaded data

OperatorDescription
addressof($)Base address of the loaded data
sizeof($)Size of the loaded data

String Operators

String operators are any operators acting on strings directly.

OperatorDescription
a + bString concatenation
str * numberString repetition
a == bLexical equality
a != bLexical inequality
a > bLexical greater-than
a >= bLexical greater-than-or-equals
a < bLexical less-than
a <= bLexical less than-or-equals

Member Access

Member access is the act of accessing members inside a struct, union or bitfield or referencing the index of an array to access its value.

Below the simplest operations are shown, however they may be concatenated and extended indefinitely as suitable.

OperationAccess type
structVar.varAccessing a variable inside a struct, union or bitfield
arrayVar[x]Accessing a variable inside an array
parent.varAccessing a variable inside the parent struct or union of the current struct or union
thisReferring to the current pattern. Can only be used inside of a struct or union

The parent operation refers to the caller struct or union, not the "parent struct" in the Inheritance section. It can also be used in a function:

struct Field {
    u8 x;
};
fn rename_field() {
    std::core::set_display_name(parent.fields[0], "version");
};
struct Struct {
    Field fields[3];
    rename_field();
};
Struct s @ 0;  // Pattern Data shows s > fields > version

$ Dollar Operator

The Dollar Operator is a special operator which expands to the current offset within the current pattern.

#pragma base_address 0x00
 
std::print($); // 0
u32 x @ 0x00;
std::print($); // 4

It’s also possible to assign a value to the dollar operator to change the current cursor position.

$ += 0x100;

The dollar operator can also be used to access single bytes of the main data.

std::print($[0]); // Prints the value of the byte at address 0x00

You can store it in a global variable and access it later:

 
u32 readLaterOffset;
 
struct Header {
    u32 id;
    u32 bodyOffset;
    readLaterOffset = $; 
};
 
struct Body {
    u32 name;
    u32 type;
    u32 data[4] @ readLaterOffset;
};
 
struct Main {
    Header header;
    Body body @ header.bodyOffset;
};
 
Main main @ 0x0;

Casting Operator

The cast operator changes the type of an expression into another.

fn test(float x) {
    return 1 + u32(x);
}
 
test(3.14159); // 4