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::Operators - Operators in the SPVM Language

Description

This document describes operators in the SPVM language.

Operators

An operator is a basic instruction that normally a return value.

Numeric Operators

Unary Plus Operator

The unary plus operator + is a unary operator that returns its operand.

  +OPERAND

This operator performs the integer promotional conversion on the operand OPERAND, and returns it.

The return type is the type after the conversion is performed.

Compilation Errors:

The type of OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the unary plus operator
  my $num = +10;

Unary Minus Operator

The unary minus operator - is a unary operator that returns the negated value of its operand.

  -OPERAND

This operator performs the integer promotional conversion on the operand OPERAND, negates it, and returns it.

The return type is the type after the conversion is performed.

Compilation Errors:

The type of OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the unary minus operator
  my $num = -10;

Addition Operator

The addition operator + adds two operands.

  LEFT_OPERAND + RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND + RIGHT_OPERAND

The return type is the type after the binary numeric conversion is performed.

Compilation Errors:

The type of LEFT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the addition operator
  my $result = 1 + 2;

Subtraction Operator

The subtraction operator - subtracts its right operand from its left operand.

  LEFT_OPERAND - RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND - RIGHT_OPERAND

The return type is the type after the binary numeric conversion is performed.

Compilation Errors:

The type of LEFT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the subtraction operator
  my $result = 1 - 2;

Multiplication Operator

The multiplication operator * multiplies two operands.

  LEFT_OPERAND * RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND * RIGHT_OPERAND

The return type is the type after the binary numeric conversion is performed.

Compilation Errors:

The type of LEFT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the multiplication operator
  my $result = 1 * 2;

Division Operator

The division operator / divides its left operand by its right operand.

  LEFT_OPERAND / RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND / RIGHT_OPERAND

The return type is the type after the binary numeric conversion is performed.

Exceptions:

If the type of the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND are an integer type and RIGHT_OPERAND is 0, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the division operator
  my $result = 1 / 2;

Division Unsigned Int Operator

The division unsigned int operator div_uint interprets its two operands as unsigned 32bit integers, and divides its left operand by its right operand.

  LEFT_OPERAND div_uint RIGHT_OPERAND

This operator performs the same operation as the following C language operation, and returns its return value.

  (uint32_t)LEFT_OPERAND / (uint32_t)RIGHT_OPERAND

The return type is the int type.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be the int type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be the int type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the division unsigned int operator
  my $result = 1 div_uint 2;

Division Unsigned Long Operator

The division unsigned long operator div_ulong interprets its two operands as unsigned 64bit integers, and divides its left operand by its right operand.

  LEFT_OPERAND div_ulong RIGHT_OPERAND

This operator performs the same operation as the following C language operation, and returns its return value.

  (uint64_t)LEFT_OPERAND / (uint64_t)RIGHT_OPERAND

The return type is the long type.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be the long type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be the long type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the division unsigned long operator
  my $result = 1L div_ulong 2L;

Modulo Operator

The modulo operator % calculates the modulo of the division of its two operands.

  LEFT_OPERAND % RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  RETURN_VALUE = LEFT_OPERAND % RIGHT_OPERAND;
  if ((LEFT_OPERAND < 0) != (RIGHT_OPERAND < 0) && RETURN_VALUE) { RETURN_VALUE += RIGHT_OPERAND; }

The return type is the type after the binary numeric conversion is performed.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the modulo operator
  my $result = 1 % 2;

Modulo Unsigned Int Operator

The modulo unsigned int operator mod_uint interprets its two operands as unsigned 32bit integers, and calculates the modulo of the division of its two operands.

  LEFT_OPERAND mod_uint RIGHT_OPERAND

This operator performs the same operation as the following C language operation, and returns its return value.

  (uint32_t)LEFT_OPERAND % (uint32_t)RIGHT_OPERAND

The return type is the int type.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be the int type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be the int type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the modulo unsigned int operator
  my $result = 1 mod_uint 2;

Modulo Unsigned Long Operator

The modulo unsigned long operator mod_ulong interprets its two operands as unsigned 64bit integers, and calculates the modulo of the division of its two operands.

  LEFT_OPERAND mod_ulong RIGHT_OPERAND

This operator performs the same operation as the following C language operation, and returns its return value.

  (uint64_t)LEFT_OPERAND % (uint64_t)RIGHT_OPERAND

The return type is is the long type.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be the long type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be the long type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the modulo unsigned long operator
  my $result = 1L mod_ulong 2L;

Increment Operators

Pre-Increment Operator

The pre-increment operator ++ increases the value of an operand by 1, and returns it.

  ++OPERAND

This operator increases the value of the operand OPERAND by 1 using the additonal operator, performs a type cast to the type of OPERAND on it, and returns it.

The return type is the type of OPERAND.

Compilation Errors:

OPERAND must be a local variable, a class variable, a field access, an element access, a dereference. Otherwise, a compilation error occurs.

The type of OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the pre-increment operator
  
  # A local variable
  ++$num;
  
  # A class variable
  ++$NUM;
  
  # A field access
  ++$point->{x};
  
  # An element access
  ++$nums->[0];
  
  # A dereference
  ++$$num_ref;

Post-Increment Operator

The post-increment operator ++ increases the value of an operand by 1, and returns the value before performing the incrementation.

  OPERAND++

This operator increases the value of the operand OPERAND by 1 using the additonal operator, performs a type cast to the type of OPERAND on it, assigns it on OPERAND, and returns OPERAND before performing the incrementation.

The return type is the type of OPERAND.

Compilation Errors:

OPERAND must be a local variable, a class variable, a field access, an element access, a dereference. Otherwise, a compilation error occurs.

The type of OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the post-increment operator
  
  # A local variable
  $num++;
  
  # A class variable
  $NUM++;
  
  # A field access
  $point->{x}++;
  
  # An element access
  $nums->[0]++;
  
  # A dereference
  $$num_ref++;

Decrement Operators

Pre-Decrement Operator

The pre-decrement operator -- decreases the value of an operand by 1, and returns it.

  --OPERAND

This operator decreases the value of the operand OPERAND by 1 using the subtraction operator, performs a type cast to the type of OPERAND on it, and returns it.

The return type is the type of OPERAND.

Complation Errors:

OPERAND must be a local variable, a class variable, a field access, an element access, a dereference. Otherwise, a compilation error occurs.

The type of OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the pre-decrement operator
  
  # A local variable
  --$num;
  
  # A class variable
  --$NUM;
  
  # A field access
  --$point->{x};
  
  # An element access
  --$nums->[0];
  
  # A dereferenced value
  --$$num_ref;

Post-Decrement Operator

The post-increment operator -- decreases the value of an operand by 1, and returns the value before performing the decrementation.

  OPERAND--

This operator decreases the value of the operand OPERAND by 1 using the subtraction operator, performs a type cast to the type of OPERAND on it, assigns it on OPERAND, and returns OPERAND before performing the decrementation.

The return type is the type of OPERAND.

Compilation Errors:

OPERAND must be a local variable, a class variable, a field access, an element access, a dereference. Otherwise, a compilation error occurs.

The type of OPERAND must be a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the post-decrement operator
  
  # A local variable
  $num--;
  
  # A class variable
  $NUM--;
  
  # A field access
  $point->{x}--;
  
  # An element access
  $nums->[0]--;
  
  # A dereference
  $$num_ref--;

Bitwise Operators

Bitwise AND Operator

The bitwise AND operator & performs the bitwise AND operation.

  LEFT_OPERAND & RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND & RIGHT_OPERAND

The return type is the type after the binary numeric widening conversion is performed.

Compilation Errors:

The type of LEFT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the bitwise AND operator
  my $num1 = 0xff;
  my $num2 = 0x12;
  my $result = $num1 & $num2;

Bitwise OR Operator

The bitwise OR operator | performs the bitwise OR operation.

  LEFT_OPERAND | RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND | RIGHT_OPERAND

The return type is the type after the binary numeric widening conversion is performed.

Compilation Errors:

The type of LEFT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the bitwise OR operator
  my $num1 = 0xff;
  my $num2 = 0x12;
  my $result = $num1 | $num2;

Bitwise NOT Operator

The bitwise NOT operator ~ performs the bitwise NOT operation.

  ~OPERAND

This operator performs the numeric widening conversion on the operand OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  ~OPERAND

The return type is the type that the numeric widening conversion is performed.

Compilation Errors:

The type of OPERAND must be an integer type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the bitwise NOT operator
  my $result = ~0xFF0A;

Shift Operators

Left Shift Operator

The left shift operator << performs the arithmetic left shift.

  LEFT_OPERAND << RIGHT_OPERAND

This operator performs the numeric widening conversion on the left operand LEFT_OPERAND.

And it performs the numeric widening conversion on the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND << RIGHT_OPERAND

The return type is the type of LEFT_OPERAND.

Compilation Erorrs:

The type of LEFT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be an integer types within int. Otherwise, a compilation error occurs.

Examples:

  # Examples of the left shift operator
  my $result = 0xFF0A << 3;

Arithmetic Right Shift Operator

The arithmetic right shift operator >> performs the arithmetic right shift.

  LEFT_OPERAND >> RIGHT_OPERAND

This operator performs the numeric widening conversion on the left operand LEFT_OPERAND.

And it performs the numeric widening conversion on the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  LEFT_OPERAND >> RIGHT_OPERAND;

The return type is the type of LEFT_OPERAND.

Compilation Errors:

The type of LEFT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be an integer types within int. Otherwise, a compilation error occurs.

Examples:

  # Examples of the arithmetic right shift operator
  my $result = 0xFF0A >> 3;

Logical Right Shift Operator

The logical right shift operator >>> performs the logical right shift.

  LEFT_OPERAND >>> RIGHT_OPERAND

This operator performs the numeric widening conversion on the left operand LEFT_OPERAND.

And it performs the numeric widening conversion on the right operand RIGHT_OPERAND.

And if the type of LEFT_OPERAND is the int type, it performs the same operation as the following C language operation

  (uint32_t)LEFT_OPERAND >> RIGHT_OPERAND

If the type of LEFT_OPERAND is the long type, it performs the same operation as the following C language operation.

  (uint64_t)LEFT_OPERAND >> RIGHT_OPERAND

And returns its return value.

The return type is the type of LEFT_OPERAND.

Compilation Errors:

The type of LEFT_OPERAND must be an integer type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be an integer types within int. Otherwise, a compilation error occurs.

Examples:

  # Examples of the logical right shift operator
  my $result = 0xFF0A >>> 3;

Logical Operators

Logical AND Operator

The logical AND operator && performs a logical AND operation.

  LEFT_OPERAND && RIGHT_OPERAND

This operator performs the condition evaluation on the left operand LEFT_OPERAND.

If the evaluated value is 0, it returns 0. Otherwise, performs the condition evaluation on the right operand RIGHT_OPERAND.

And it returns the evaluated value of RIGHT_OPERAND.

The return type is the int type.

Examples:

  # Examples of the logical AND operator
  if (1 && 0) {
    
  }

Logical OR Operator

The logical OR operator || performes a logical OR operation.

  # The logical OR operator
  LEFT_OPERAND || RIGHT_OPERAND

Thg logical OR operator performs the condition evaluation on the left operand LEFT_OPERAND.

If the evaluated value is not 0, it returns the evaluated value. Otherwise, performs the condition evaluation on the right operand RIGHT_OPERAND.

And it returns the evaluated value of RIGHT_OPERAND.

The return type is the int type.

Examples:

  # Examples of the logical OR operator
  if (1 || 0) {
    
  }

Logical NOT Operator

The logical NOT operator ! performes a logical NOT operation.

  !OPERAND

Thg logical NOT operator performs the condition evaluation on the operand OPERAND.

If the evaluated value is 0, returns 1. Otherwise, returns 0.

The return type is the int type.

  # Examples of the logical NOT operator
  if (!1) {
    
  }

Condition Evaluation

The condition evaluation is the operation performed on the operand at a conditional part of the if statement, the while statement, and logical operators.

The condition evaluation performs the following operations corresponding to the type of the operand.

The return type is the int type.

byte:

short:

int:

The integer promotional conversion is performed on the operand.

And return the value after conversion.

undef:

Returns 0.

BOOL:

Return the value field in the Bool object.

long:

float:

double:

a reference type:

Performs the following C language operation, and returns it.

  !!OPERAND

an object type:

If the compile type of the operand is not the Bool class, performs the following C language operation, and returns it.

  !!OPERAND

Compilation Errors:

The type of the operand of the condition evaluation must be a numeric type, an object type, a reference type, or the undef type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the condition evaluation
  if (1) {
    # ok
  }
  
  if (0) {
    # not ok
  }
  
  if (1.5) {
    # ok
  }
  
  if (0.0) {
    # not ok
  }
  
  if (true) {
    # ok
  }
  
  if (Bool->TRUE) {
    # ok
  }
  
  if (false) {
    # not ok
  }
  
  if (Bool->FALSE) {
    # not ok
  }
  
  my $object = SPVM::Int->new(1);
  
  if ($object) {
    # ok
  }
  
  $object = undef;
  if ($object) {
    # not ok
  }
  
  my $value = 1;
  my $ref = \$value;
  
  if ($ref) {
    # ok
  }
  
  if (undef) {
    # not ok
  }

Array Length Operator

The array length operator @ gets the length of an array.

  @OPERAND
  @{OPERAND}

This operator returns the length the array OPERAND.

The return type is the int type.

Exceptions:

OPERAND must be defined. Otherwise, an exception is thrown.

Compilation Errors:

The type of OPERAND must be an array type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the array length operator
  my $nums = new int[10];
  my $length = @$nums;

scalar Operator

The scalar operator returns its operand.

  scalar OPERAND

This operator returns the operand OPERAND. OPERAND must be the "Array Length Operator" in array length operator.

This operator exists only for readability.

The return type is the int type.

Compilation Errors:

OPERAND must be the array length operator. Otherwise, a compilation error occurs.

Examples:

  # Examples of the scalar operator
  my $nums = new int[3];
  say scalar @$nums;

Sequential Operator

The sequential operator is an operator with the following syntax.

  (OPERAND1, OPERAND2, ..., OPERANDn)

This operator evaluates operands OPERAND1, OPERAND1 ... OPERANDn from left to right, and returns the value of the rightmost operand OPERANDn.

The return type is the type of OPERANDn.

Examples:

  # Examples of the sequential operator
  
  # 3 is assigned to $foo
  my $foo = (1, 2, 3);
  
  # $x is 3, $ret is 5
  my $x = 1;
  my $y = 2;
  my $result = ($x += 2, $x + $y);

Comparison Operators

Comparison operators compare two operands.

Numeric Comparison Operators

Numeric comparison operators compare two numbers or two addresses of objects.

  LEFT_OPERAND == RIGHT_OPERAND
  LEFT_OPERAND != RIGHT_OPERAND
  LEFT_OPERAND > RIGHT_OPERAND
  LEFT_OPERAND >= RIGHT_OPERAND
  LEFT_OPERAND < RIGHT_OPERAND
  LEFT_OPERAND <= RIGHT_OPERAND
  LEFT_OPERAND <=> RIGHT_OPERAND

This operator performs the binary numeric conversion on the left operand LEFT_OPERAND and the right operand RIGHT_OPERAND.

And it performs the same operation as the following C language operation, and returns its return value.

  (int32_t)(LEFT_OPERAND == RIGHT_OPERAND);
  (int32_t)(LEFT_OPERAND != RIGHT_OPERAND);
  (int32_t)(LEFT_OPERAND > RIGHT_OPERAND);
  (int32_t)(LEFT_OPERAND >= RIGHT_OPERAND);
  (int32_t)(LEFT_OPERAND < RIGHT_OPERAND);
  (int32_t)(LEFT_OPERAND <= RIGHT_OPERAND);
  (int32_t)(LEFT_OPERAND > RIGHT_OPERAND ? 1 : LEFT_OPERAND < RIGHT_OPERAND ? -1 : 0);

The return type is the int type.

Compilation Errors:

==, !=

The type of the LEFT_OPERAND must be a numeric type, an object type, a reference type, or the undef type. Otherwise, a compilation error occurs.

The type of the RIGHT_OPERAND must be a numeric type, an object type, a reference type, or the undef type. Otherwise, a compilation error occurs.

If the type of the LEFT_OPERAND is a numeric type, the type of the RIGHT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

If the type of the LEFT_OPERAND is an object type, the type of the RIGHT_OPERAND must be an object type or the undef type. Otherwise, a compilation error occurs.

If the type of the LEFT_OPERAND is the undef type, the type of the RIGHT_OPERAND must be an object type or the undef type. Otherwise, a compilation error occurs.

If the type of the LEFT_OPERAND is a reference type, the type of the RIGHT_OPERAND must be a reference type. Otherwise, a compilation error occurs.

<, <=, >, >=, <=>

The type of the LEFT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

The type of the RIGHT_OPERAND must be a numeric type. Otherwise, a compilation error occurs.

String Comparison Operators

String comparison operators compare tow strings in the dictionary order.

  LEFT_OPERAND eq RIGHT_OPERAND
  LEFT_OPERAND ne RIGHT_OPERAND
  LEFT_OPERAND gt RIGHT_OPERAND
  LEFT_OPERAND ge RIGHT_OPERAND
  LEFT_OPERAND lt RIGHT_OPERAND
  LEFT_OPERAND le RIGHT_OPERAND
  LEFT_OPERAND cmp RIGHT_OPERAND

These operators perform the following operations.

Operators Operations
eq If LEFT_OPERAND is equal to RIGHT_OPERAND in the dictionary order, returns 1. Otherwise, returns 0.
ne If LEFT_OPERAND is not equal to RIGHT_OPERAND in the dictionary order, returns 1. Otherwise, returns 0.
gt If LEFT_OPERAND is greater than RIGHT_OPERAND in the dictionary order, returns 1. Otherwise, returns 0.
ge If LEFT_OPERAND is greater than or equal to RIGHT_OPERAND in the dictionary order, returns 1. Otherwise, returns 0.
lt If LEFT_OPERAND is less than RIGHT_OPERAND in the dictionary order, returns 1. Otherwise, returns 0.
le If LEFT_OPERAND is less than or equal to RIGHT_OPERAND in the dictionary order, returns 1. Otherwise, returns 0.
cmp If LEFT_OPERAND is greater than RIGHT_OPERAND in the dictionary order, returns 1. If LEFT_OPERAND is less than RIGHT_OPERAND in the dictionary order, return -1. If LEFT_OPERAND is equal to RIGHT_OPERAND in the dictionary order, returns 0.

The return type is the int type.

Compilation Errors.

The type of LEFT_OPERAND must be the string type or the byte[] type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be the string type or the byte[] type. Otherwise, a compilation error occurs.

Constant Operator

The constant operator gets a constant value represented by a literal.

  LITERAL

LITERAL is a literal.

The return type is the type of LITERAL.

Compilation Errors:

Compilation errors casued by literal syntax could occur.

length Operator

The length operator gets the length of a string.

  length OPERAND

If the string OPERAND is defind, this operator returns the length of OPERAND. Note that this length is in bytes, not the number of UTF-8 characters.

If OPERAND is not defined, returns 0.

The return type is the int type.

Compilation Errors:

The type of OPERAND must be the string type. Otherwise, a compilation error occurs.

Examples:

  # Examples of The length operator
  
  # The length is 5
  my $message = "Hello";
  my $length = length $message;
  
  # The length is 9
  my $message = "あいう";
  my $length = length $message;

String Concatenation Operator

The string concatenation operator . concats two strings.

  LEFT_OPERAND . RIGHT_OPERAND

This operator performs the numeric-to-string conversion on the left operand LEFT_OPERAND if the type of LEFT_OPERAND is a numeric type.

And it performs the numeric-to-string conversion on the right operand RIGHT_OPERAND if the type of RIGHT_OPERAND is a numeric type.

And it concats LEFT_OPERAND and RIGHT_OPERAND, and returns its return value.

The type of LEFT_OPERAND and RIGHT_OPERAND are allowed to be the byte[] type.

The return type is the string type.

Exceptions:

LEFT_OPERAND must be defined. Otherwise, an exception is thrown.

RIGHT_OPERAND must be defined. Otherwise, an exception is thrown.

Compilation Errors:

The type of LEFT_OPERAND must be the string type, the byte[] type, or a numeric type. Otherwise, a compilation error occurs.

The type of RIGHT_OPERAND must be the string type, the byte[] type, or a numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the string concatenation operator
  my $result = "abc" . "def";
  my $result = "def" . 34;

new_string_len Operator

The new_string_len operator creates a new string with a length.

  new_string_len OPERAND

This operator performs the integer promotional conversion on the length OPERAND.

And creates a new string with the length, fills all characters in the string with \0, and returns it.

The return type is the string type.

Exceptions:

OPERAND must be greater than or equal to 0. Otherwise, an exception is thrown.

Compilation Errors:

The type of OPERAND must be an integer type within int. Otherwise, a compilation error occurs.

Examples:

  # Examples of the new_string_len operator
  my $message = new_string_len 5;

make_read_only Operator

The make_read_only operator makes a string read-only.

  make_read_only OPERAND

If the string OPERAND is defined, this operator makes OPERAND read-only.

A read-only string cannnot be cast to the mutable string type. If so, an exception is thrown.

The return type is the void type.

Compilation Errors:

OPERAND must be the string type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the make_read_only operator
  
  # A string
  my $string = new_string_len 3;
  
  # Make the string read-only
  make_read_only $string;
  
  # The conversion to the mutable string type throw an exception.
  my $string_mutable = (mutable string)$string;

is_read_only Operator

The is_read_only operator checks if a string is read-only.

  is_read_only OPERAND

If the string OPERAND is defined and read-only, the is_read_only operator returns 1. Otherwise, returns 0.

The return type is the int type.

Compilation Errors:

OPERAND must be the string type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the is_read_only operator
  my $message = "Hello";
  my $is_read_only = is_read_only $message;

The print operator prints a string to standard output.

  print OPERAND

This operator outputs the string OPERAND to the SPVM's standard output.

If OPERAND is not defined, this operator outputs nothing.

The return type is the void type.

Compilation Errors:

OPERAND must be the string type. Otherwise, a compilation error occurs.

say Operator

The say operator prints a string to standard output with a newline.

  say OPERAND

This operator outputs the string OPERAND to the SPVM's standard output with a newline \n.

If OPERAND is not defined, this operator outputs a newline \n.

The return type is the void type.

Compilation Errors:

OPERAND must be the string type. Otherwise, a compilation error occurs.

warn Operator

The warn operator prints a string to standard error with a stack trace.

  warn
  warn OPERAND

If OPERAND is omitted, OPERAND is set to the string "Warning".

This operator outputs OPERAND to the SPVM's standard error.

If OPERAND is not defined, this operator outputs the string "undef".

If the type of OPERAND is the string type and OPERAND is defined, this operator outputs OPERAND.

If the type of OPERAND is an object type except for the string type and OPERAND is defined, this operator outputs the type name and the address of OPERAND, such as "Point(0x55d8a44ed090)".

If the end character of the OPERAND is not \n, this operator outputs a newline, two tabs and a stack trace information following the output above.

A stack trace information consists of the current method name, file name, and line number.

  MyClass->test at path/MyClass.spvm line 33

The return type is the void type.

Compilation Errors:

The type of OPERAND must be an object type. Otherwise, a compilation error occurs.

Examples:

  warn;
  warn "Something is wrong.";
  
  # Point(0x55d8a44ed090)
  my $point = Point->new;
  warn $point;

__FILE__ Operator

The __FILE__ operator gets the path of the file where the current class is defined.

  __FILE__

This operator creates a string with the path of the file where the current class is defined, and returns it.

The return type is the string type.

The return value can be changed by the file directive.

Examples:

  # Examples of the __FILE__ operator
  class Foo::Bar {
    static method baz : void () {
      # path/SPVM/Foo/Bar.spvm
      my $file_name = __FILE__;
    }
  }

__LINE__ Operator

The __LINE__ operator gets the current line number.

  __LINE__

This operator returns the current line number.

The return type is the int type.

Examples:

  # Examples of the __LINE__ operator
  class Foo::Bar {
    static method baz : void () {
      my $line = __LINE__;
    }
  }

__PACKAGE__ Operator

The __PACKAGE__ operator gets the name of the outmost class.

  __PACKAGE__

This operator creates a string with the name of the outmost class, and returns it.

The return type is the string type.

Examples:

  class Foo::Bar {
    static method baz : void () {
      # Foo::Bar
      my $outmost_class_name = __PACKAGE__;
      
      my $anon_method = method : void () {
        # Foo::Bar
        my $outmost_class_name = __PACKAGE__;
      };
    }
  }

new Operator

The new operator creates a new object, a new array, and a new multi-dimensional array.

See also the use statement about the way to load classes.

See also SPVM::Document::Language::GarbageCollection about garbage collection of objects.

Creating an Object

The following syntax of the new operator creates a new object.

  new CLASS_TYPE;

The class type CLASS_TYPE must be a loaded class type.

This operator creates a new object of CLASS_TYPE, and returns it.

The fields of the new object are initialized by its type initial value.

The return type is CLASS_TYPE.

Compilation Errors:

CLASS_TYPE must be a loaded class type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the new operator - Creating a new object
  my $object = new Foo;

Creating an Array

The following syntax of the new operator creates a new array.

  new BASIC_TYPE[LENGTH]

The basic type BASIC_TYPE must be a loaded basic type.

This operator performs the integer promotional conversion on the length LENGTH.

And creates a new array of the length LENGTH which element type is BASIC_TYPE.

And all elements of the new array are initialized by its type initial value.

And returns the new array.

The return type is BASIC_TYPE[].

Exceptions:

LENGTH must be greater than or equal to 0. Otherwise, an exception is thrown.

Compilation Errors:

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

Examples:

  # Examples of the new operator - Creating a new array
  my $values = new int[3];
  my $objects = new Foo[3];
  my $objects = new object[3];
  my $mulnum_values = new Complex_2d[3]

Creating a Multi-Dimensional Array

The following syntax of the new operator creates a new multi-dimensional array.

  new BASIC_TYPE[]..[LENGTH]

([].. means one or more [].)

The basic type BASIC_TYPE must be a loaded basic type.

This operator performs the integer promotional conversion on the length LENGTH.

And creates a new multi-dimensional array of the length LENGTH which element type is BASIC_TYPE[]...

And all elements of the new multi-dimensional array are initialized by its type initial value.

And returns the new multi-dimensional array.

The return type is BASIC_TYPE[]..[].

Exceptions:

LENGTH must be greater than or equal to 0. Otherwise, an exception is thrown.

Compilation Errors:

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

The type dimension must be less than or equal to 255. Otherwise, a compilation error occurs.

Examples:

  # Examples of the new operator - Creating a new multi-dimensional array
  
  # 2 dimentional int array
  my $muldim_values = new int[][3];
  
  # 3 dimentional int array
  my $muldim_values = new int[][][3];

Array Initialization

The syntax of the array initialization creates a new array and sets the elements, and returns the new array.

  [ELEMENT1, ELEMENT2, .., ELEMENTn]

This is expanded to the following code.

  (
    # Create a new array
    my $array = new ELEMENT1_TYPE[n],
    
    # Set elements
    $array->[0] = ELEMENT1,
    $array->[1] = ELEMENT2,
    ...,
    $array->[n - 1] = ELEMENTn,
    $array,
  )

ELEMENT1_TYPE is the type of ELEMENT1.

If elements does not exist, ELEMENT1_TYPE is the any object type object, n is 0, and setting elements is not performed.

The return type is ELEMENT1_TYPE[].

Compilation Errors:

If the type of ELEMENT1 is the undef type, a compilation error occurs.

Examples:

  # Examples of the array initialization
  
  # int array
  my $nums = [1, 2, 3];
  
  # double array
  my $nums = [1.5, 2.6, 3.7];
  
  # string array
  my $strings = ["foo", "bar", "baz"];

The int array example is expanded to the following code.

  # int array
  my $nums = new int[3];
  $nums->[0] = 1;
  $nums->[1] = 2;
  $nums->[2] = 3;

Key-Value Array Initialization

The syntax of the key-value array initialization creates a new array and sets the elements with key-value pairs, and returns the new array.

  {ELEMENT1, ELEMENT2, ELEMENT3, ELEMENT4}

This syntax is the same as "Array Initialization", but the return type is always the any object array type object[].

And the length of the elements must be an even number.

Compilation Errors:

The length of the elements must be an even number, a compilation error occurs.

Examples:

  # Examples of the key-value array initialization
  
  # Empty
  my $key_values = {};
  
  # Key values
  my $key_values = {foo => 1, bar => "Hello"};

Anon Method Operator

An anon method operator creates an object of an anon method class.

  ANON_METHOD_CLASS_DEFINTION

This operator defines an anon method class using anon method class definition ANON_METHOD_CLASS_DEFINTION, creates a new object from the class, and retunrs it.

If the anon method class field definition in ANON_METHOD_CLASS_DEFINTION has field default values, the fields of the anon method object are set to these values.

Examples:

  # Examples of the anon method operator
  my $comparator = (Comparator)method : int ($x1 : object, $x2 : object) {
    my $point1 = (Point)$x1;
    my $point2 = (Point)$x2;
    
    return $point1->x <=> $point2->x;
  };
  
  # With an anon method class field definition
  my $num = 1;
  my $comparator = [$num : int] (Comparator)method : int ($x1 : object, $x2 : object) {
    my $point1 = (Point)$x1;
    my $point2 = (Point)$x2;
    
    say $num;
    
    return $point1->x <=> $point2->x;
  };
  

undef Operator

The undef operator returns an undefined value.

  undef

The return type is the undef type.

Examples:

  # Examples of the undef operator
  my $string = (string)undef;
  
  if (undef) {
    
  }
  
  my $message = "Hello";
  if ($message == undef) {
    
  }

copy Operator

The copy operator copies a numeric array, a multi-numeric array or a string.

  copy OPERAND

If the operand OPERAND is not an undefined value, this operator creates a new object of the same type as the operand OPERAND, and copies the elements of the array or the characters of the string into the new object, and returns it.

If OPERAND is not defined, this operator returns an undefined value.

The read-only flag of the string is not copied.

The return type is the type of OPERAND.

Compilation Errors:

The type of the operand must be the string type, a numeric array type, or a multi-numeric array type. Otherwise, a compilation error occurs.

Examples:

  # Exampels of the copy operator
  my $message = copy "abc";

dump Operator

The dump operator gets the string representation dumping the data contained in the object.

  dump OPERAND

This operator creates a new string with the string representation dumping the data contained in the object OPERAND and returns it.

The following is an example of the return value the dump operator.

  # An return vlaue of the dump operator
  TestCase::Operator::DumpTest1 (0x55f21f7e6050) {
    byte_value => 1,
    short_value => 2,
    int_value => 3,
    long_value => 4,
    float_value => 1.1,
    double_value => 1.2,
    string_value => "a",
    int_array => [
      1,
      2,
      3
    ] : int[](0x55f21fb9b8d0),
    object_value => TestCase::Operator::DumpTest1 (0x55f21f764640) {
      byte_value => 0,
      short_value => 0,
      int_value => 0,
      long_value => 0,
      float_value => 0,
      double_value => 0,
      string_value => undef,
      int_array => undef,
      object_value => undef
    }
  }

The return type is the string type.

The string representation might be changed to make it more readable. So don't use the dump operator for the purpose of the data serialization.

Compilation Errors:

OPERAND must be an object type, ohterwise a compilation error occurs.

Reference Operator

The reference operator \ creates the reference to the value owned by a variable.

  \VARIALBE

This operator creates the reference to the value owned by the variable VARIALBE, and returns it.

VARIALBE is must be a local variable of a numeric type or a multi-numeric type.

The return type is the Reference Types of VARIALBE.

Compilation Errors:

VARIALBE must be a local variable of a numeric type or a multi-numeric type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the reference operator
  
  # Create a reference of a numeric type
  my $num : int;
  my $num_ref = \$num;
  
  # Create a reference of a multi-numeric type
  my $z : Complex_2d;
  my $z_ref = \$z;

Dereference Operator

The dereference operator $ gets a referenced value.

  $VARIABLE

This operator returns the value referenced by the variable VARIABLE of a reference type.

The return type is the type of the value referenced by VARIABLE.

Compilation Errors:

The type of VARIABLE must be a reference type. Otherwise, a compilation error occurs.

Examples:

  # Examples of the dereference operator
  my $num : int;
  my $num_ref = \$num;
  my $num_deref = $$num_ref;
  
  my $z : Complex_2d;
  my $z_ref = \$z;
  my $z_deref = $$z_ref;

Assignment Operator

The assignment operator = performs an assignment.

  LEFT_OPERAND = RIGHTH_OPERAND

A data conversion described in Assignment Requirement is performed on RIGHTH_OPERAND if necessary.

And the assignment operator performs different operations depending on the left operand LEFT_OPERAND.

If LEFT_OPERAND is a local variable, this operator performs the operation that sets a local variable.

If LEFT_OPERAND is a class variable, this operator performs the operation that sets a class variable.

If LEFT_OPERAND is an element access, this operator performs the operation that sets an array element.

If LEFT_OPERAND is a field access, this operator performs the operation that sets a field.

If LEFT_OPERAND is a dereference, this operator performs the operation that sets a referenced value.

If LEFT_OPERAND is the exception variable, this operator performs the operation that sets the exception variable.

See also "Assignment" in SPVM::Document::Language::GarbageCollection about how the assignment operator changes the reference counts of LEFT_OPERAND and RIGHTH_OPERAND.

Examples:

  # Examples of the assignment operator
  
  # A local variable
  $num = 1;
  
  # A class variable
  $NUM = 1;
  
  # A field access
  $point->{x} = 1;
  
  # An element access
  $nums->[0] = 1;
  
  # A dereference
  $$num_ref = 1;
  
  # The exception variable
  $@ = 2;

Special Assignment Operators

A special assignment operator is the combination of an operator such as +, - and the assignment operator =.

  LEFT_OPERAND OPERATOR= RIGHTH_OPERAND

A special assignment operator is expanded to the following code.

  LEFT_OPERAND = (TYPE_OF_LEFT_OPERAND)(LEFT_OPERAND OPERATOR RIGHTH_OPERAND)

See the following code using a special assignment operator +=. $x is the int type.

  $x += 2;

This is expanded to the following code.

  $x = (int)($x + 2)

List of Special Assignment Operators:

The addition assignment operator +=
The subtraction assignment operator -=
The multiplication assignment operator *=
The division assignment operator /=
The modulo assignment operator %=
The bitwise AND assignment operator &=
The bitwise OR assignment operator |=
The left shift assignment operator <<=
The arithmetic right shift assignment operator >>=
The logical right shift assignment operator >>>=
The concatenation assignment operator .=

Examples:

  # Special assignment operators
  $x += 1;
  $x -= 1;
  $x *= 1;
  $x /= 1;
  $x &= 1;
  $x |= 1;
  $x ^= 1;
  $x %= 1;
  $x <<= 1;
  $x >>= 1;
  $x >>>= 1;
  $x .= "abc";

Getting and Setting Operators

Getting a Local Variable

The operation of getting local variable gets the value of a local variable.

  $var

$var is a local variable access.

This operation returns the value of $var.

The return type is the type of $var.

Compilation Errors:

Compiliation errors caused by the syntax of the local variable access could occur.

Examples:

  $var;

Setting a Local Variable

The operation of setting a local variable sets a local variable.

  $var = OPERAND

$var is a local variable access.

This operation sets $var to OPERAND using the assignment operator, and returns the value after setting.

The return value is the type of $var.

Compilation Errors:

Compiliation errors caused by the syntax of the local variable access could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  $var = 3;

Local Variable Access

The local variable access has the following syntax.

  VAR_NAME

See Variable Name Resolution about VAR_NAME and the resolution of a local variable name.

Examples:

  $var

Getting a Class Variable

The operation of getting a class variable gets the value of a class variable.

  $VAR

$VAR is a class variable access.

This operation returns the value of $VAR.

The return type is the type of $VAR.

Compilation Errors:

Compiliation errors caused by the syntax of the class variable access could occur.

Examples:

  # Examples of getting a class variable
  class Foo {
    our $VAR : int;
    
    static method bar : int () {
      
      my $var1 = $Foo::VAR;
      
      # $Foo::VAR
      my $var2 = $VAR;
      
      my $anon_method = method : void () {
        # $Foo::BAR
        $VAR;
      }
    }
  }

Setting a Class Variable

The operation of setting a class variable operator sets a class variable.

  $VAR = OPERAND

$VAR is a class variable access.

This operation sets $VAR to OPERAND using the assignment operator, and returns the value after setting.

The return type is the type of $VAR.

Compilation Errors:

Compiliation errors caused by the syntax of the class variable access could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  # Examples of setting a class variable
  class Foo {
    our $VAR : int;
    
    static method bar : int () {
      $Foo::VAR = 1;
      
      # $Foo::VAR = 3
      $VAR = 3;
    }
    my $anon_method = method : void () {
      # $Foo::VAR = 5
      $VAR = 5;
    }
  }

Class Variable Access

The class variable access has the following syntax.

  VAR_NAME
  CLASS_TYPE::VAR_NAME

See Variable Name Resolution about VAR_NAME, CLASS_TYPE, and the resolution of a class variable name.

Examples:

  $VAR
  $MyClass::VAR
  

Getting an Array Element

The operation of getting an array element gets an element of an array.

  ARRAY->[INDEX]

ARRAY->[INDEX] is an element access.

This operator performs the integer promotional conversion on INDEX.

And returns the element of ARRAY at INDEX.

The return type is the element type of ARRAY.

Exceptions:

ARRAY must be defined. Otherwise, an exception is thrown.

INDEX must be greater than or equal to 0. Otherwise, an exception is thrown.

Compilation Errors:

Compiliation errors caused by the syntax of the element access could occur.

Examples:

  my $nums = new int[3];
  my $num = $nums->[1];
  
  my $points = new Point[3];
  my $point = $points->[1];

Setting an Array Element

The operation of setting array element sets an element of an array.

  ARRAY->[INDEX] = OPERAND

ARRAY->[INDEX] is an element access.

This operator performs the integer promotional conversion on INDEX.

And sets the element of ARRAY at INDEX to OPERAND using the assignment operator, and returns the element after setting.

The return type is the element type.

Exceptions:

ARRAY must be defined. Otherwise, an exception is thrown.

INDEX must be greater than or equal to 0. Otherwise, an exception is thrown.

Compilation Errors:

Compiliation errors caused by the syntax of the element access could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  my $nums = new int[3];
  $nums->[1] = 3;
  
  my $points = new Point[3];
  $points->[1] = Point->new(1, 2);
  

Element Access

The element access has the following syntax.

  ARRAY->[INDEX]

The type of the array ARRAY is an array type.

The type of the index INDEX is an integer type within int.

Compilation Errors:

ARRAY must be an array type. Otherwise, a compilation error occurs.

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

Getting a Character

The operation of getting a character gets a character of a string.

  STRING->[INDEX]

STRING->[INDEX] is an character access.

The type of STRING is the string type.

This operator performs the integer promotional conversion on INDEX.

And returns the character of STRING at INDEX.

The return type is the byte type.

Exceptions:

STRING must be defined. Otherwise, an exception is thrown.

INDEX must be greater than or equal to 0. Otherwise, an exception is thrown.

Compilation Errors:

Compiliation errors caused by the syntax of the character access could occur.

Examples:

  my $string = "abc";
  my $char = $string->[0];

Setting a Character

The operation of setting a character sets the character of a string.

  STRING->[INDEX] = OPERAND

STRING->[INDEX] is an character access.

This operator performs the integer promotional conversion on INDEX.

And sets the character of STRING at INDEX to OPERAND using the assignment operator, and returns the character after setting.

The return type is the byte type.

Exceptions:

STRING must be defined. Otherwise, an exception is thrown.

INDEX must be greater than or equal to 0. Otherwise, an exception is thrown.

If STRING is not a mutable string, an exception is thrown.

Compilation Errors:

Compiliation errors caused by the syntax of the character access could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  my $string = new_string_len 3;
  $string->[0] = 'a';

Character Access

The character access has the following syntax.

  STRING->[INDEX]

The type of the string STRING is the string type.

The type of the index INDEX is an integer type within int.

Compilation Errors:

STRING must be the string type. Otherwise, a compilation error occurs.

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

Getting a Field

The operation of getting field gets the value of a field of a class type.

  INVOCANT->{FIELD_NAME}

INVOCANT->{FIELD_NAME} is a field access.

This operation gets the value of the field specified by FIELD_NAME of the type of INVOCANT.

The retrun type is the type of the field.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

Examples:

  my $point = Point->new;
  my $x = $point->{x};

Setting a Field

The operation of setting field sets the field of a class type.

  INVOCANT->{FIELD_NAME} = OPERAND

INVOCANT->{FIELD_NAME} is a field access.

The type of INVOCANT is a class type.

This operation sets the field specified by FIELD_NAME of the type of INVOCANT to OPERAND using the assignment operator, and returns the value of the field after setting.

The return type is the field type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  my $point = Point->new;
  $point->{x} = 1;

Getting a Multi-Numeric Field

The operation of getting a multi-numeric field gets the value of a field of a multi-numeric type.

  INVOCANT->{FIELD_NAME}

INVOCANT->{FIELD_NAME} is a field access.

The type of INVOCANT is a multi-numeric type.

This operation gets the value of the field specified by FIELD_NAME of the type of INVOCANT.

The retrun type is the type of the field.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

Examples:

  my $z : Complex_2d;
  my $re = $z->{re};

Setting a Multi-Numeric Field

The operation of setting multi-numeric field sets the field of the multi-numeric type.

  INVOCANT->{FIELD_NAME} = OPERAND

INVOCANT->{FIELD_NAME} is a field access.

The type of INVOCANT is a multi-numeric type.

This operation sets the field specified by FIELD_NAME of the type of INVOCANT to OPERAND using the assignment operator, and returns the value of the field after setting.

The return type is the field type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  my $z : Complex_2d;
  $z->{re} = 2.5;
  

Getting a Referenced Multi-Numeric Field

The operation of getting a multi-numeric field gets the value of a field of a multi-numeric type referenced by a multi-numeric Reference Types.

This operation is expaned to the following code.

  ($INVOCANT)->{FIELD_NAME}

The type of INVOCANT is a multi-numeric refenrece type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

Compiliation errors caused by the dereference operator could occur.

Examples:

  my $z : Complex_2d;
  my $z_ref = \$z;
  my $re = $z_ref->{re};

Setting a Referenced Multi-Numeric Field

The operation of setting a multi-numeric field sets the value of a field of a multi-numeric type referenced by a multi-numeric Reference Types.

This operation is expaned to the following code.

  ($INVOCANT)->{FIELD_NAME} = OPERAND

The type of INVOCANT is a multi-numeric refenrece type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

Compiliation errors caused by the dereference operator could occur.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  my $z : Complex_2d;
  my $z_ref = \$z;
  $z_ref->{re} = 2.5;

Field Access

The field access has the following syntax.

  INVOCANT->{FIELD_NAME}

See Field Access Resolution about INVOCANT, FIELD_NAME, and the resolution of a field access.

Examples:

  $point->{x}

Getting a Referenced Value

The operation of getting the referenced value gets a referenced value.

See the dereference operator.

Setting a Referenced Value

The operation of setting the referenced value sets a referenced value.

  $VARIABLE = OPERAND

Thie operation sets the value referenced by the reference VARIABLE to OPERAND using the assignment operator, and returns the value after setting.

The return type is the type of the referenced value.

Compilation Errors:

The type of VARIABLE must be a reference type. Otherwise, a compilation error occurs.

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  my $num : int;
  my $num_ref : int* = \$num;
  $$num_ref = 1;
  
  my $z : Complex_2d;
  my $z_ref : Complex_2d* = \$z;
  
  my $z2 : Complex_2d;
  
  $$z_ref = $z2;

Getting the Exception Variable

The operation of getting the exception variable gets the string stored in the exception variable.

  $@

This operator returns the string stored in the exception variable.

The return type is the string type.

Examples:

  # Examples of getting the exception variable
  my $message = $@;

Setting the Exception Variable

The operation of setting the exception variable sets the exception variable.

  $@ = OPERAND

This operator sets the exception variable to OPERAND using the assignment operator.

The return type is the string type.

Compilation Errors:

The assignment must satisfy the assignment requirement. Otherwise, a compilation error occurs.

Examples:

  # Examples of setting the exception variable
  $@ = "Error";

Data Conversions

This section describes data conversions.

Numeric Widening Conversion

The numeric widening conversion is the data conversion from a numeric type to a larger numeric type.

See numeric types order about the order of numeric types.

This conversion performs the same operation as the C language type cast.

  (TYPE)OPERAND

byte to short:

  (int16_t)OPERAND_int8_t;

byte to int:

  (int32_t)OPERAND_int8_t;

byte to long:

  (int64_t)OPERAND_int8_t;

byte to float:

  (float)OPERAND_int8_t;

byte to double:

  (double)OPERAND_int8_t;

short to int:

  (int32_t)OPERAND_int16_t;

short to long:

  (int64_t)OPERAND_int16_t;

short to float:

  (float)OPERAND_int16_t;

short to double:

  (double)OPERAND_int16_t;

int to long:

  (int64_t)OPERAND_int32_t;

int to float:

  (float)OPERAND_int32_t;

int to double:

  (double)OPERAND_int32_t;

long to float:

  (float)OPERAND_int64_t;

long to double:

  (double)OPERAND_int64_t;

float to double:

  (double)OPERAND_float;

Integer Promotional Conversion

The integer promotional conversion is the data conversion from an integer type within int to the int type using the numeric widening conversion.

Numeric Narrowing Conversion

The numeric narrowing conversion is the data conversion from a numeric type to a smaller numeric type.

See numeric types order about the order of numeric types.

This conversion performs the same operation as the C language type cast.

  (TYPE)OPERAND

double to float:

  (float)OPERAND_double;

double to long:

  (int64_t)OPERAND_double;

double to int:

  (int32_t)OPERAND_double;

double to short:

  (int16_t)OPERAND_double;

double to byte:

  (int8_t)OPERAND_double;

float to long:

  (int64_t)OPERAND_float;

float to int:

  (int32_t)OPERAND_float;

float to short:

  (int16_t)OPERAND_float;

float to byte:

  (int8_t)OPERAND_float;

long to int:

  (int32_)OPERAND_int64_t;

long to short:

  (int16_t)OPERAND_int64_t;

long to byte:

  (int8_t)OPERAND_int64_t;

int to short:

  (int16_t)OPERAND_int32_t;

int to byte:

  (int16_t)OPERAND_int32_t;

short to byte:

  (int8_t)OPERAND_int16_t;

Binary Numeric Conversion

The binary numeric conversion is the data conversion to upgrade the numeric type of the left operand and the right operand of a binary operator.

This conversion performs the following operations.

If the type of the left operand is smaller than the right operand, the numeric widening conversion from the type of the left operand to the type of the right operand is performed on the left operand.

If the type of the right operand is smaller than the left operand, the numeric widening conversion from the type of the right operand to the type of the left operand is performed on the right operand.

If the converted type of the left operand is the smaller than the int type, the numeric widening conversion from the type of the left operand to the int type is performed on the left operand.

If the converted type of the right operand is the smaller than the int type, the numeric widening conversion from the type of the right operand to the int type is performed on the right operand.

Numeric-to-String Conversion

The numeric-to-string conversion is the data conversion from a numeric type to the string type.

This conversion performs the same operation as the C language sprintf.

byte to string:

  sprintf(RETURN_VALUE, "%" PRId8, OPERAND_byte);

short to string:

  sprintf(RETURN_VALUE, "%" PRId16, OPERAND_short);

int to string:

  sprintf(RETURN_VALUE, "%" PRId32, OPERAND_int);

long to string:

  sprintf(RETURN_VALUE, "%" PRId64, OPERAND_long);

float to string:

  sprintf(RETURN_VALUE, "%g", OPERAND_float);

double to string:

  sprintf(RETURN_VALUE, "%g", OPERAND_double);

Examples:

  # Examples of the numeric-to-string conversion
  my $byte = (byte)1;
  my $string_byte = (string)$byte;
  
  my $short = (short)2;
  my $string_short = (string)$short;
  
  my $int = 3;
  my $string_int = (string)$int;
  
  my $long = 4L;
  my $string_long = (string)$long;
  
  my $float = 2.5f;
  my $string_float = (string)$float;
  
  my $double = 3.3;
  my $string_double = (string)$double;

String-to-Numeric Conversion

The string-to-numeric conversion is the data conversion from the string type to a numeric type.

string to byte:

If the string is not defined, it returns 0.

If it is defined, it is coverted to a number by the strtoll function in the C language.

The number is greater than INT8_MAX, the number is set to INT8_MAX.

The number is less than INT8_MIN, the number is set to INT8_MIN.

And returns the number.

string to short:

If the string is not defined, it returns 0.

If it is defined, it is coverted to a number by the strtoll function in the C language.

The number is greater than INT16_MAX, the number is set to INT16_MAX.

The number is less than INT16_MIN, the number is set to INT16_MIN.

And returns the number.

string to int:

If the string is not defined, it returns 0.

If it is defined, it is coverted to a number by the strtoll function in the C language.

The number is greater than INT32_MAX, the number is set to INT32_MAX.

The number is less than INT32_MIN, the number is set to INT32_MIN.

And returns the number.

string to long:

If the string is not defined, it returns 0.

If it is defined, it is coverted to a number by the strtoll function in the C language.

And returns the number.

string to float:

If the string is not defined, it returns 0.

If it is defined, it is coverted to a number by the strtof function in the C language.

And returns the number.

string to double:

If the string is not defined, it returns 0.

If it is defined, it is coverted to a number by the strtod function in the C language.

And returns the number.

Exampels:

  # Examples of string to numeric conversions
  
  # string to byte
  my $string : string = "Hello";
  my $number : byte = (byte)$string;
  
  # string to short
  my $string : string = "Hello";
  my $number : short = (short)$string;
  
  # string to int
  my $string : string = "Hello";
  my $number : int = (int)$string;
  
  # string to long
  my $string : string = "Hello";
  my $number : long = (long)$string;
  
  # string to float
  my $string : string = "Hello";
  my $float : float = (float)$string;
  
  # string to double
  my $string : string = "Hello";
  my $number : double = (double)$string;

String-to-byte[] Conversion

The string-to-byte[] conversion is the data conversion from the string type to the byte[] type.

This conversion creates a new array which type is the byte[] type, copies all characters in the string to the elements of the new array, and returns the new array.

If the string is not defined, it returns undef.

Examples:

  # Examples of the string-to-byte[] conversion
  my $string : string = "Hello";
  my $bytes : byte[] = (byte[])$string;

byte[]-to-string Conversion

The byte[]-to-string conversion is the data conversion from the byte[] type to the string type.

This conversion creates a new string, copies all elements in the array which type is the byte[] type to the characters of the new string, and returns the new string.

If the array is not defined, returns undef.

  # Examples of the byte[]-to-string conversion
  my $bytes : byte[] = new byte[3];
  $bytes->[0] = 'a';
  $bytes->[1] = 'b';
  $bytes->[2] = 'c';
  my $string : string = (string)$bytes;

Boxing Conversion

The boxing conversion is the type coversion from a numeric type to its corresponding numeric object type.

To From
Byte byte
Short short
Int int
Long long
Float float
Double double

A boxing conversion creates a new numeric object corresponding to its numeric type, and copyes the value of the numeric type to the value field of the new numeric object, and return the new numeric object.

Unboxing Conversion

The unboxing conversion is the type coversion from an object of a numeric object type to the value of its corresponding numeric type.

To From
byte Byte
short Short
int Int
long Long
float Float
double Double

An unboxing conversion returns the value of the value field of the numeric object.

An unboxing conversion could be performed on the object of any object type object.

Exceptions:

If the type of the object is not its corresponding numeric type, an exception is thrown.

What type combinations cause implicit data conversions is explained in "Assignment Requirement".

Examples:

  # Assignment operators
  # int to double 
  my $number : double = 5;
  
  # double to Double
  my $number_object : Double = 5.1;
  
  # Double to double
  my $number : double = Double->new(5.1);
  
  # int to string
  my $string : string = 4;
  
  # Method call
  # int to double
  my $double_object = Double->new(3);
  
  # Return value
  method my_method : double () {
    
    # int to double
    return 3;
  }

Type Cast

A type cast converts a value from its type to another type.

  # A type cast
  (TYPE)OPERAND
  
  # A postfix type cast
  OPERAND->(TYPE)

The return type is TYPE.

If a data conversion is needed in the type cast, a type cast performs a data conversion on OPERAND, and returns the value after the data conversion.

Otherwise it returns OPERAND.

If a data check is needed in the type cast, a type cast performs a data check on OPERAND.

Data conversions and data checks in type casts are explained in Cast Requirement.

Exceptions:

If a data check returns 0, an exception is thrown.

Compilation Errors:

OPERAND must satisfy the cast requirement. Otherwise, a compilation error occurs.

Examples:

  # Examples of the type cast
  
  # long to int 
  my $num = (int)123L;
  
  # byte[] to string
  my $num = (string)new byte[3];
  
  # string to byte[]
  my $num = (byte[])"Hello";
  
  # Postfix type cast
  my $stringable = Point->new->(Stringable);

isa Operator

The isa operator checks whether an operand can be assigned to a type.

  OPERAND isa TYPE

If the type TYPE is a numeric type, a multi-numeric type, a reference type, the any object type, or the any object array type, this operator checks the assignment requirement without data convertion.

If the assignment requirement is satisfied, this operator returns 1. Otherwise, returns 0.

If TYPE is an object type except for the any object type, or the any object array type, this operator checks the assignment requirement without data conversion.

If the runtime assignment requirement is satisfied, this operator returns 1. Otherwise, returns 0.

The return type is the int type.

Compilation Errors:

If the runtime assignment requirement is checked, OPERAND must be an object type. Otherwise, a compilation error occurs.

Examples:

  if ($value isa int) {
    
  }
  
  if ($value isa Point) {
    
  }
  
  if ($value isa Point3D) {
    
  }
  
  if ($value isa Stringable) {
    
  }
  
  if ($value isa int) {
    
  }

is_type Operator

The is_type operator checks whether the type of an operand is equal to a type.

  OPERAND is_type TYPE

If the type TYPE is a numeric type, a multi-numeric type, a reference type, the any object type, or the any object array type, this operator checks the compilation type of OPERAND is equal to TYPE.

If it is true, this operator returns 1. Otherwise, returns 0.

If the type is an object type except for the any object type, or the any object array type, this operator checks the runtime type of OPERAND is equal to TYPE.

If it is true, this operator returns 1. Otherwise, returns 0.

The return type is int type.

Compilation Errors:

If the runtime check is performed, OPERAND must be an object type. Otherwise, a compilation error occurs.

Examples:

  if ($object is_type int) {
    
  }
  
  if ($object is_type Point) {
    
  }
  
  if ($object is_type int[]) {
    
  }
  
  if ($object is_type Stringable[]) {
    
  }

is_compile_type Operator

The is_compile_type operator checks whether the compilation type of an operand is equal to a type.

  OPERAND is_compile_type TYPE

If the compilation type of OPERAND is equal to the type TYPE, returns 1. Otherwise, returns 0.

The return type is int type.

Examples:

  {
    my $value : int;
    if ($value is_compile_type int) {
      # Pass
    }
  }
  
  {
    my $object : object = new TestCase::Minimal;
    if ($object is_compile_type object) {
      # Pass
    }
  }
  
  {
    my $value : Stringer = method : string () { return "aaa"; };
    if ($value is_compile_type Stringer) {
      # Pass
    }
  }

isa_error Operator

The isa_error operator checks whether the type specified by a basic type ID can be assigned to a class type. This operator is normally used for error classes to check "eval_error_id Operator" in eval_error_id>.

  OPERAND isa_error TYPE

This operator performs the integer promotional conversion on the operand OPERAND.

And this operator checks whether the type specified by the basic type ID OPERAND satisfies the assignment requirement without data conversion to the type TYPE.

If it is satisfied, this operator returns 1. Otherwise, returns 0.

The return type is int type.

Compilation Errors:

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

TYPE must be a class type. Otherwise, a compilation error occurs.

Examples:

  if (eval_error_id isa_error Error) {
    
  }
  
  if (eval_error_id isa_error Error::System) {
    
  }
  

is_error Operator

The is_error operator checks whether the type specified by a basic type ID is equal to a class type. This operator is normally used for error classes to check "eval_error_id Operator" in eval_error_id>.

  OPERAND is_error TYPE

This operator performs the integer promotional conversion on the operand OPERAND.

And this operator checks whether the type specified by the basic type ID OPERAND is equal to the type TYPE.

If it is, this operator returns 1. Otherwise, returns 0.

The return type is int type.

Compilation Errors:

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

TYPE must be a class type. Otherwise, a compilation error occurs.

Examples:

  if (eval_error_id is_error Error) {
    
  }
  
  if (eval_error_id is_error Error::System) {
    
  }

type_name Operator

The type_name operator gets the type name of the object.

  type_name OPERAND

If the object OPERAND is defined, creates a string with the type name of OPERAND and returns it. Otherwise, returns an undefined value.

The return type is the string type.

Compilation Errors.

OPERAND must be an object type. Otherwise, a compilation error occurs.

Examples:

  # "Point"
  my $point = Point->new;
  my $type_name = type_name $point;
  
  # "Point"
  my $point = (object)Point->new;
  my $type_name = type_name $point;

compile_type_name Operator

The compile_type_name operator gets the compilation type of the operand OPERAND.

  compile_type_name OPERAND

This operator creates a new string with the compilation type name of OPERAND and returns it.

The return type is the string type.

Examples:

  # "Point"
  my $point = Point->new;
  my $type_name = type_name $point;
  
  # "object"
  my $point = (object)Point->new;
  my $type_name = type_name $point;

basic_type_id Operator

The basic_type_id operator gets the basic type ID of a type.

  basic_type_id TYPE

This operator returns the basic type ID of the type TYPE.

The return type is the int type.

Examples:

  my $basic_type_id = basic_type_id int;
  
  my $basic_type_id = basic_type_id int[];
  
  my $error_basic_type_id = basic_type_id Error;

can Operator

The can operator checks if a method can be called.

  OPERAND can METHOD_NAME

An empty string "" means an anon method.

If OPERAND can call the method given by METHOD_NAME, returns 1. Otherwise, returns 0.

The return type is int type.

Compilation Errors:

The type of OPERAND must be the class type or the interface type. Otherwise, a compilation error occurs.

The METHOD_NAME must be a method name or an empty string "". Otherwise, a compilation error occurs.

Examples:

  my $stringable = (Stringable)Point->new(1, 2);
  
  if ($stringable can to_string) {
    # ...
  }
  
  if ($stringable can "") {
    # ...
  }

args_width Operator

The args_width operator gets the stack length of the arguments passed to the method.

  args_width

Note that the stack length of the arguments is different from the length of the arguments.

If the method call is the instance method call, the stack length of the arguments is the length of the arguments + 1 for the invocant.

If an argument is a multi-numeric type, the stack length of the argument becomes the length of the fields.

Examples:

  static method my_static_method : int ($args : int, $bar : int = 0) {
    my $args_width = args_width;
    
    return $args_width;
  };
  
  # 1
  &my_static_method(1);
  
  # 2
  &my_static_method(1, 2);
  
  static method my_instance_method : int ($args : int, $bar : int = 0) {
    my $args_width = args_width;
    
    return $args_width;
  };
  
  # 2 (1 + the invocant)
  &my_instance_method(1);
  
  # 3 (2 + the invocant)
  &my_instance_method(1, 2);

  static method my_mulnum_method : int ($z : Complex_2d, $bar : int = 0) {
    my $args_width = args_width;
    
    return $args_width;
  };

  # 2 (The length of the fields of Complex_2d)
  my $z : Complex_2d;
  &my_mulnum_method($z);
  
  # 3 (The length of the fields of Complex_2d + 1)
  my $z : Complex_2d;
  &my_mulnum_method($z, 2);

eval_error_id Operator

The eval_error_id operatoer gets the value of eval_error_id.

  eval_error_id

See SPVM::Document::Language::ExceptionHandling about the way to use the eval_error_id operator.

weaken Operator

The weaken operator enables a weak reference.

  weaken INVOCANT->{FIELD_NAME};

INVOCANT->{FIELD_NAME} is a field access.

This operator enable a weak reference of the field specified by FIELD_NAME of the type of INVOCANT.

The return type is the void type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

The type of INVOCANT->{FIELD_NAME} must be an object type. Otherwise, a compilation error occurs.

Examples:

  # Exmaples of the weaken operator
  weaken $object->{point};

unweaken Operator

The unweaken operator disables a weak reference.

  unweaken INVOCANT->{FIELD_NAME};

INVOCANT->{FIELD_NAME} is a field access.

This operator enable a weak reference of the field specified by FIELD_NAME of the type of INVOCANT.

The return type is the void type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

The type of INVOCANT->{FIELD_NAME} must be an object type. Otherwise, a compilation error occurs.

Examples:

  # Exmaples of the unweaken operator
  unweaken $object->{point};

isweak Operator

The isweak operator checks if the weak reference of a field is enabled.

  isweak INVOCANT->{FIELD_NAME};

INVOCANT->{FIELD_NAME} is a field access.

If the field specified by FIELD_NAME of the object INVOCANT is weaken, this operator returns 1. Otherwise, returns 0.

The return type is the int type.

Compilation Errors:

Compiliation errors caused by the syntax of the field access could occur.

The type of INVOCANT->{FIELD_NAME} must be an object type. Otherwise, a compilation error occurs.

Examples:

  # Exmaples of the isweak operator
  my $isweak = isweak $object->{point};

Scope Operations

See the doc of scope about scope operations.

Method Call

A method call is an operator to call a method.

A method call resolves to one of the three types of method calls, a class method call, a static instance method call, and an instance method call by method call resolution.

If the method call is a static instance method call or an instance method call, the invocant is prepended to the given arguments.

The method call performs the method call execution given the arguments.

Class Method Call

A class method call calls a class method.

  CLASS_TYPE->METHOD_NAME
  CLASS_TYPE->METHOD_NAME(OPT_ARGS)
  &METHOD_NAME
  &METHOD_NAME(OPT_ARGS)

See Class Method Call Resolution about CLASS_TYPE, METHOD_NAME, OPT_ARGS, &, and the resolution of a class method call.

Examples:

  # Examples of static instance method calls
  my $point3d = Point3D->new;
  
  $point3d->Point::clear;
  
  $point3d->SUPER::clear;

Static Instance Method Call

A static instance method call calls an instance method specifying a class.

  INVOCANT->CLASS_TYPE::METHOD_NAME
  INVOCANT->CLASS_TYPE::METHOD_NAME(OPT_ARGS)

See Static Instance Method Call Resolution about INVOCANT, CLASS_TYPE, METHOD_NAME, OPT_ARGS, and the resolution of a static instance method call.

Examples:

  # Examples of static instance method calls
  $object->SUPER::bar(5, 3. 6);
  
  $point3d->Point::clear;

Instance Method Call

An instance method call calls an instance method.

  INVOCANT->METHOD_NAME
  INVOCANT->METHOD_NAME(OPT_ARGS)

See Instance Method Call Resolution about INVOCANT, CLASS_TYPE, METHOD_NAME, OPT_ARGS, and the resolution of an instance method call.

Examples:

  # Examples of instance method calls
  
  my $point = Point->new;
  
  $point->clear;
  
  my $stringable = (Stringable)$point;
  
  my $string = $strinble->to_string;

Method Call Execution

The argument width is stored to the runtime stack.

The call stack depth stored in the runtime stack is incremented by 1.

If the call stack depth is greater than 1000, an exception is thrown.

If the method call is not a class method call, the arugments of the object type are checked whether the following isa operator returns a true value.

  ARG isa TYPE_OF_ARG

ARG is an argument. TYPE_OF_ARG is the type of the corresponding arugment of the method.

If the return vlaue is not a true value, an exception is thrown.

If the method is a INIT method and it is already called, nothing is performed.

If the method is a native method, a native method call execution is performed.

Otherwise if the method is a precompilation method, a precompilation method call execution is performed.

Otherwise a VM method call execution is performed.

If an exception is thrown by the method call execution, the exception is thrown.

If the return type of the method is an object type, the object is pushed to the native mortal stack.

The call stack depth stored in the runtime stack is decremented by 1. This resotre is always performed even if an excetpion is thrown.

VM Method Call Execution

Heap memories for local variables are allocated.

The enter_scope native API is called.

SPVM operation codes generated from the method implementation are executed.

The leave_scope native API is called.

Heap memories for local variables are released.

Precompilation Method Call Execution

If a set of the machine codes of the precompilation method is loaded, the program executes it.

Otherwise if the is_precompile_fallback method native API returns a true value, the program executes "VM Method Call Execution".

Otherwise an exception is thrown.

Native Method Call Execution

The enter_scope native API is called.

If a set of the machine codes of the native function of the native method is loaded, the program executes it.

Otherwise an exception is thrown after executing the following code.

The leave_scope native API is called.

See Also

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License