Control Structures in M7 Script

Overview

M7 Script provides various control structures for conditional execution, loops, and exception handling, allowing for structured flow control.

General Rules

  • Control statements may be followed by a block or a single statement, except do loops, which require a block.
  • Break (break) and continue (continue) are supported.
  • Labels are recognized but currently unimplemented (they compile but are ignored).

1. Conditional Statements

If-Else Statements

Used for branching logic based on conditions.

if (x > 10) {
    print("x is greater than 10");
} elsif (x == 10) {
    print("x is exactly 10");
} else {
    print("x is less than 10");
}

Elseif is NOT a Valid Construct

  • M7 supports elsif, and else if (as separate statements).
  • elseif is NOT valid syntax.

Unless Statement (Planned Feature - Not Implemented Yet)

  • M7 does not yet support unless, but it is planned as a future feature for conditional negation.

Switch Statement (Planned Feature - Not Implemented Yet)

  • A switch statement is planned but currently unimplemented.

2. Loop Structures

For Loop (Counter-Based Iteration)

Standard counter-based looping structure.

for (var i = 0; i < 10; i++) {
    print(i);
}

Foreach Loop (Iterating Over Collections)

M7 supports foreach loops for iterating over arrays and hashes.

foreach (array as value) {
    print(value);
}

foreach (array as i => value) {
    print("Index ".i . " => " . value);
}

foreach (hash as key => value) {
    print(key . " => " . value);
}

While Loop

Repeats as long as the condition is true.

while (x < 5) {
    print("Still under 5");
    x++;
}

Do-While Loop

Executes at least once before checking the condition.

do {
    print("Runs at least once");
} while (x < 5);

Do-Until Loop

Executes at least once and stops when the condition becomes true.

do {
    print("Runs until x is 10");
    x++;
} until (x == 10);

Until Loop (Planned Feature - Not Implemented Yet)

M7 does not yet support a standalone until loop, but it will be implemented in the future.


3. Exception Handling

Try-Catch-Finally

Handles errors gracefully.

try {
    riskyOperation();
} catch (error) {
    print("An error occurred: ".error);
} finally {
    print("Cleanup actions");
}

4. Pattern Matching (Planned Feature - Not Implemented Yet)

  • M7 will support pattern matching via a built-in regex function rather than a dedicated control structure.

This document provides an overview of control structures in M7 Script, including conditional statements, loops, and exception handling.