The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

SPVM::Document::Language::Statements - Statements in SPVM Language

Description

This document describes statements in SPVM language.

Statements

Statements are the list of the statement.

Statements are written direct under the scope block.

  # Scope block
  {
    # Statements
    STATEMENT1
    STATEMENT2
    STATEMENT3
  }

Conditional Statement

if Statement

The if statement is a conditional statement that is executed if the condition is true.

  if (CONDITION) {
  
  }

The boolean conversion is performed on the condition CONDITION.

If the condition is not 0, the program jumps to the beginning of the if block. Otherwise jumps to the end of the if block.

The local variable declartion in the condition of the if statement are allowed.

  if (my $condition = 1) {
  
  }

This is parsed as the following code.

  {
    my $condition = 1;
    if ($condition) {
    
    }
  }

Examples:

  # if statement.
  my $flag = 1;
  
  if ($flag == 1) {
    say "One";
  }

else Statement

The else statement is a conditional statement that is executed if the condition is false.

  if (CONDITION) {
    
  }
  else {
    
  }

If the condition CONDITION is evaluated 0, the program jumps to the beginning of the else block.

Examples:

  # else statement.
  my $flag = 3;
  
  if ($flag == 1) {
    say "One";
  }
  else {
    say "Other";
  }

elsif Statement

The elsif statement is a conditional statement that adds a condition branching.

  if (CONDITION1) {
  
  }
  elsif (CONDITION2) {
  
  }

The elsif statement is parsed as the following code.

  if (CONDITION1) {
  
  }
  else {
    if (CONDITION2) {
    
    }
  }

Examples:

  # elsif statement
  my $flag = 2;
  
  if ($flag == 1) {
    say "One";
  }
  elsif ($flag == 2) {
    say "Two";
  }
  else {
    say "Other";
  }

unless Statement

The unless statement is a conditional statement that is executed if the condition is false.

  unless (CONDITION) {
    
  }

The unless statement is parsed as the following code.

  if (!CONDITION) {
    
  }

Examples:

  # unless statement.
  my $flag = 1;
  
  unless ($flag == 0) {
    say "Not Zero";
  }

switch Statement

The switch statement is a conditional statement that is executed if the condition matches an integer value.

  switch (CONDITION) {
    case CASE_VALUE1: {
      # ...
    }
    case CASE_VALUE2: {
      # ...
    }
    case CASE_VALUE3: {
      # ...
    }
    default: {
      # ...
    }
  }

The integer promotional conversion is performed on the condition.

The value of the case statement must be one of the character literal, the integer literal or the getting enumeration value.

If it is a character literal, the value is converted to the int type at compile-time.

If the condition matches the value of a case statement, the program jumps to the beginning of the case block.

If the condition doesn't match any case statements and the default statement exists, the program jumps to the beginning the default block.

If the condition doesn't match any case statements and the default statement doesn't exists, the program jumps to the end of the switch block.

The case statements and the default statement can be ommited.

The break statement jumps to the end of the switch block.

  switch (CONDITION) {
    case CASE_VALUE1: {
      break;
    }
    case CASE_VALUE2: {
      break;
    }
    case CASE_VALUE3: {
      break;
    }
    default: {
      
    }
  }

If the last statment of the case block is not the break statement, a break statement is added to the end of the case block.

  # The break statement is ommitted.
  switch (CONDITION) {
    case CASE_VALUE1: {
    }
  }
  
  # The above becomes the following.
  switch (CONDITION) {
    case CASE_VALUE1: {
      break;
    }
  }

Multiple case statements before a case block can be specified at once.

  switch (CONDITION) {
    case CASE_VALUE1:
    case CASE_VALUE2:
    {
      # ...
    }
  }

Compilation Errors:

The condition must be an integer type within int. Otherwise a compilation error occurs.

The values of the case statements cannnot be duplicated. If so, a compilation error occurs.

Examples:

  # switch statement
  my $code = 2;
  my $flag = 1;
  switch ($code) {
    case 1: {
      say "1";
    }
    case 2: {
      say "2";
    }
    case 3: {
      if ($flag) {
        break;
      }
      say "3";
    }
    case 4:
    case 5:
    {
      say "4 or 5";
    }
    default: {
      say "Other";
    }
  }
  
  # switch statement using enumeration
  class Foo {
    enum {
      ID1,
      ID2,
      ID3,
    }
    
    static method main : int () {
      my $value = 1;
      switch ($value) {
        case Foo->ID1: {
          say "1";
        }
        case Foo->ID2: {
          say "2";
        }
        case Foo->ID3: {
          if ($flag) {
            break;
          }
          say "3";
        }
        default: {
          say "Other";
        }
      }
    }
  }

case Statement

The case statement specifies a case value and a branch of a switch statement.

  # The case statement
  switch (CONDITION) {
    case CASE_VALUE1: {
      # ...
    }
  }

default Statement

The default statement specifies a default branch of a switch statement.

  # The default statement
  switch (CONDITION) {
    default: {
      # ...
    }
  }

break Statement

The break statement is jumps to the end of the switch block of the switch statement.

  # The break statement
  break;

Loop Statement

while Statement

The while statement runs loop.

  while (CONDITION) {
  
  }

First, The boolean conversion is performed on the condition.

Next, If the condition is 0, the program jumps to the end of the while block. Otherwise the program goes to the beginning of the while block.

When the program reaches the end of the while block, it jumps back to the while statement and evaluates the condition again.

Examples:

  # The while statement
  my $i = 0;
  while ($i < 5) {
  
    say "$i";
  
    $i++;
  }

The last statement is used inside the while block. By The last statement, the program jumps to the end of the current while block.

  # The last statement
  while (1) {
    # The program jumps to the end fo the current while block.
    last;
  }

The next statement is used inside the while block. By The last statement, the program goes back to the while statement of the current while block.

  my $i = 0;
  while ($i < 5) {
  
    if ($i == 3) {
      $i++;
      
      # the program goes back to the while statement of the current while block.
      next;
    }
    
    say "$i";
    $i++;
  }

The while statement is enclosed by an inbisible simple block.

  # The while statement
  while (1) {
    
  }

  # The above is the same as the following.
  {
    while (1) {
      
    }
  }

for Statement

The for Statement writes loop syntax easily.

  # The for statement.
  for (INIT_STATEMENT; CONDITION; INCREMENT_STATEMENT) {
  
  }

The for statement is the alias for the while statement.

  # The above for statemenet is the same as the following while statemenet.
  {
    INIT_STATEMENT;
    while (CONDITION) {
      
      # ...
      
      INCREMENT_STATEMENT;
    }
  }

Exampels:

  # The for statement
  for (my $i = 0; $i < 5; $i++) {
    say "$i";
  }

for-each Statement

The for-each statement writes loop syntax easily for the simple iteration.

  # The for-each statemenet
  for my VAR (@ARRAY) {
  
  }
  
  for my VAR (@{ARRAY}) {
    
  }

The above for-each statement is the same as the following the for statement.

  for (my $i = 0; $i < @ARRAY; $i++) {
    my VAR = ARRAY->[$i];
    
  }

Example:

  # The for-each statemenet
  my $array = [1, 2, 3];
  for my $element (@$array) {
    say "$elemenet";
  }

next Statement

The next statement goes back to the while statement of the current while block.

  next;

See also the while statement.

last Statement

The last statement jumps to the end of the current while block.

  last;

See also the while statement.

return Statement

The return statement returns a value.

  // void
  return;
  
  // non-void
  return OPERAND;

Compilation Errors:

If the return type of the current method is the void typ, OPERAND cannnot exist. If so, a compilation error occurs.

If the return type of the current method is the non-void type, OPERAND must exist. Otherwise a compilation error occurs.

The type of OPERAND must satisfy type assignability to the return type of the current method. Otherwise a compilation error occurs.

die Statement

The die statement throws an exception.

  die OPERAND_MESSAGE
  die
  die ERROR_TYPE OPERAND_MESSAGE
  die ERROR_TYPE
  die OPERAND_ERROR_ID ',' OPERAND_MESSAGE

OPERAND_MESSAGE is an error message. The error message is set to the exception variable $@.

If an exception is thrown, the program prints the error message to the standard error with the stack traces and finishes with error ID 255.

If OPERAND_MESSAGE is omitted or undef, "Error" is set to the exception variable $@.

ERROR_TYPE is a class type. If ERROR_TYPE is given, the basic type ID of the class is the value got by the eval_error_id operator.

OPERAND_ERROR_ID is an integer value within int type. If OPERAND_ERROR_ID is given, it is the value got by the eval_error_id operator.

the integer promotional conversion is performed on OPERAND_ERROR_ID.

The return type is the void typ.

The following one is an example of a stack trace. Each line of the stack trace constains the class name, the method name, the file name and the line number of the caller.

  Error
    TestCase::Minimal->sum2 at SPVM/TestCase/Minimal.spvm line 1640
    TestCase->main at SPVM/TestCase.spvm line 1198

The exception can be caught by the eval block.

Comlication Errors:

OPERAND_MESSAGE must be the string type or the undef type. Otherwise a compilation error occurs.

ERROR_TYPE must be a class type. Otherwise a compilation error occurs.

OPERAND_ERROR_ID must be an integer type within int. Otherwise a compilation error occurs.

Examples:

  # Catch the exception
  eval {
    # Throw an exception
    die "Error";
  }
  
  # Check the exception
  if ($@) {
    # ...
  }
  
  die Error::System "System Error";
  
  my $error_id = 10;
  die $error_id, "Some Error";
  

Operator Statement

The operator statement executes an operator.

This operator is composed of an operator and ;.

  # The operator statemenet
  OPERATOR;

Examples:

  1;
  $var;
  1 + 2;
  &foo();
  my $num = 1 + 2;

Empty Statement

The empty statement ; does nothing.

  # The empty statemenet
  ;

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License