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 SPVM Language

Description

This document describes operators in SPVM language.

Operators

Unary Plus Operator

The unary plus operator + returns OPERAND.

  +OPERAND

Before this operation, the integer promotional conversion is performed on OPERAND.

The return type is the type after the integer promotional conversion has been performed.

Compilation Errors:

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

Examples:

  # The unary plus operator
  my $num = +10;

Unary Minus Operator

  -OPERAND

The unary minus operator - returns the negative value of OPERAND.

Before this operation, the integer promotional conversion is performed on OPERAND.

The return type is the type after the integer promotional conversion has been performed.

Compilation Errors:

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

Examples:

  # A unary minus operator
  my $num = -10;

Addition Operator

The addition operator + calculates the addition of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND + RIGHT_OPERAND

The addition operator performs the same operation as the following C language operation.

  LEFT_OPERAND + RIGHT_OPERAND

Before this operation, The binary numeric conversion is performed on LEFT_OPERAND and RIGHT_OPERAND.

The return type is the type after the binary numeric conversion has been 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.

Subtraction Operator

The subtraction operator - calculates the subtraction of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND - RIGHT_OPERAND

The subtraction operator performs the same operation as the following C language operation.

  LEFT_OPERAND - RIGHT_OPERAND

Before this operation, The binary numeric conversion is performed on LEFT_OPERAND and RIGHT_OPERAND.

The return type is the type after the binary numeric conversion has been 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.

Multiplication Operator

The multiplication operator * calculates the multiplication of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND * RIGHT_OPERAND

Before this operation, The binary numeric conversion is performed on LEFT_OPERAND and RIGHT_OPERAND.

The multiplication operator performs the same operation as the following C language operation.

  LEFT_OPERAND * RIGHT_OPERAND;

The return type is the type after the binary numeric conversion has been 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.

Division Operator

The division operator / calculates the division of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND / RIGHT_OPERAND

Before this operation, The binary numeric conversion is performed on LEFT_OPERAND and RIGHT_OPERAND.

The division operator performs the same operation as the following C language operation.

  LEFT_OPERAND / RIGHT_OPERAND;

The return type is the type after the binary numeric conversion has been 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.

Exceptions:

If LEFT_OPERAND and RIGHT_OPERAND are an integer type and RIGHT_OPERAND is 0, an exception is thrown.

Division Unsigned Int Operator

The division unsigned int operator div_uint calculates the unsigned int division of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND div_uint RIGHT_OPERAND

The division unsigned int operator performs the same operation as the following C language operation.

  (uint32_t)LEFT_OPERAND / (uint32_t)RIGHT_OPERAND;

The return type is the int type.

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.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Division Unsigned Long Operator

The division unsigned long operator div_ulong calculates the unsigned long division of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND div_ulong RIGHT_OPERAND

The division unsigned long operator performs the same operation as the following C language operation.

  (uint64_t)LEFT_OPERAND / (uint64_t)RIGHT_OPERAND;

The return type of the division operator is the long type.

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.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Modulo Operator

The modulo operator % is calculates a modulo of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND % RIGHT_OPERAND

Before this operation, the binary numeric conversion is performed on LEFT_OPERAND and RIGHT_OPERAND.

The modulo operator performs the same operation as the following C language operation.

  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 has been 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.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Modulo Unsigned Int Operator

The modulo unsigned int operator mod_uint calculates a unsigned int modulo of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND mod_uint RIGHT_OPERAND

The modulo unsigned int operator performs the same operation as the following C language operation.

  (uint32_t)LEFT_OPERAND % (uint32_t)RIGHT_OPERAND;

The return type is the int type.

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.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Modulo Unsigned Long Operator

The modulo unsigned long operator mod_ulong calculates a unsigned long modulo of LEFT_OPERAND and RIGHT_OPERAND.

  LEFT_OPERAND mod_ulong RIGHT_OPERAND

The modulo unsigned long operator performs the same operation as the following C language operation.

  (uint64_t)LEFT_OPERAND % (uint64_t)RIGHT_OPERAND;

The return type of the modulo unsigned long operator is the long type.

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.

Exceptions:

If RIGHT_OPERAND is 0, an exception is thrown.

Increment Operator

Increment operators are the pre-increment operator and post-increment operator.

Pre-Increment Operator

The pre-increment operator adds 1 to OPERAND and returns the value after the incrementation.

  # Pre-increment operator
  ++OPERAND

The pre-increment operator performs the same operation as the following.

  (OPERAND = (TYPE_OF_OPERAND)(OPERAND + 1))

For example, if the type of OPERAND is the byte type, the following operation is performed.

  ($num = (byte)($num + 1))

Compilation Errors:

The type of OPERAND must be a local variable, a class variable, a field access</a>, an array access, a dereference. Otherwise a compilation error occurs.

Examples:

  # Pre-increment of a local variable
  ++$num;
  
  # Pre-increment of a class variable
  ++$NUM;
  
  # Pre-increment of an element of an array
  ++$point->{x};
  
  # Pre-increment of a field
  ++$nums->[0];
  
  # Pre-increment of a dereferenced value
  ++$$num_ref;

Post-Increment Operator

The post-increment operator adds 1 to OPERAND and returns the value before the incrementation.

  # Post-increment operator
  OPERAND++

The post-increment operator performs the same operation as the following.

  (my TMP_VARIABLE = OPERAND, OPERAND = (TYPE_OF_OPERAND)(OPERAND + 1), TMP_VARIABLE)

For example, if the type of OPERAND is the byte type, the following operation is performed.

  (my $tmp = $num, $num = (byte)($num + 1), $tmp)

Compilation Errors:

The type of OPERAND must be a local variable, a class variable, a field access</a>, an array access, a dereference. Otherwise a compilation error occurs.

Examples:

  # Post-increment of a local variable
  $num++;
  
  # Post-increment of a class variable
  $NUM++;
  
  # Post-increment of an element of an array
  $point->{x}++;
  
  # Post-increment of a field
  $nums->[0]++;
  
  # Post-increment of a dereferenced value
  $$num_ref++;

Decrement Operator

Decrement operators are the pre-decrement operator and post-decrement operator.

Pre-Decrement Operator

The pre-decrement operator subtracts 1 to OPERAND and returns the value after the decrementation.

  # Pre-decrement operator
  --OPERAND

The pre-decrement operator performs the same operation as the following.

  (OPERAND = (TYPE_OF_OPERAND)(OPERAND - 1))

For example, if the type of OPERAND is the byte type, the following operation is performed.

  ($num = (byte)($num - 1))

Complation Errors:

The type of OPERAND must be a local variable, a class variable, a field access, an array access, a dereference. Otherwise a compilation error occurs.

Examples:

  # Pre-decrement of a local variable
  --$num;
  
  # Pre-decrement of a class variable
  --$NUM;
  
  # Pre-decrement of an element of an array
  --$point->{x};
  
  # Pre-decrement of a field
  --$nums->[0];
  
  # Pre-decrement of a dereferenced value
  --$$num_ref;

Post-Decrement Operator

The post-decrement operator subtracts 1 to OPERAND and returns the value before the decrementation.

  # Post-decrement operator
  OPERAND--

The post-decrement operator performs the same operation as the following.

  (my TMP_VARIABLE = OPERAND, OPERAND = (TYPE_OF_OPERAND)(OPERAND - 1), TMP_VARIABLE)

For example, if the type of OPERAND is the byte type, the following operation is performed.

  (my $tmp = $num, $num = (byte)($num - 1), $tmp)

Compilation Errors:

The type of OPERAND must be a local variable, a class variable, a field access</a>, an array access, a dereference. Otherwise a compilation error occurs.

Examples:

  # Post-decrement of a local variable
  $num--;
  
  # Post-decrement of a class variable
  $NUM--;
  
  # Post-decrement of an element of an array
  $point->{x}--;
  
  # Post-decrement of a field
  $nums->[0]--;
  
  # Post-decrement of a dereferenced value
  $$num_ref--;

Bit Operator

Bit operators perform bit operations.

Bit AND Operator

The bit AND operator & performes a bit AND operation.

  LEFT_OPERAND & RIGHT_OPERAND

Before this operation, the binary numeric conversion is performed.

The return value is the same as the follwoing operation of the C language.

  LEFT_OPERAND & RIGHT_OPERAND;

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

Compilation Errors:

LEFT_OPERAND and RIGHT_OPERAND must be an "Integer Type" in integer type. Otherwise a compilation error occurs.

Examples:

  # The bit AND operator
  my $num1 = 0xff;
  my $num2 = 0x12;
  my $result = $num1 & $num2;
  

Bit OR Operator

The bit OR operator | performes a bit OR operation.

  LEFT_OPERAND | RIGHT_OPERAND

Before this operation, the binary numeric conversion is performed.

The return value is the same as the follwoing operation of the C language.

  LEFT_OPERAND | RIGHT_OPERAND;

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

Compilation Errors:

LEFT_OPERAND and RIGHT_OPERAND must be an "Integer Type" in integer type. Otherwise a compilation error occurs.

Examples:

  # The bit OR operator
  my $num1 = 0xff;
  my $num2 = 0x12;
  my $result = $num1 | $num2;

Bit NOT Operator

The bit NOT operator ~ performs the bit NOT operation.

  ~OPERAND

The numeric widening conversion is performed.

The return value is the same as the follwoing operation of the C language.

  ~OPERAND

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

Compilation Errors:

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

Examples:

  # The bit NOT operator
  my $num = ~0xFF0A;

Shift Operator

Shift operators perform bit shift operations.

Left Shift Operator

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

  LEFT_OPERAND << RIGHT_OPERAND

Before this operation, the numeric widening conversion is performed on LEFT_OPERAND.

Before this operation, the numeric widening conversion is performed on RIGHT_OPERAND.

The left shift operator performs the same operation as the following C language operation.

  LEFT_OPERAND << RIGHT_OPERAND;

The return type is the same as 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 type within int. Otherwise a compilation error occurs.

Arithmetic Right Shift Operator

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

  LEFT_OPERAND >> RIGHT_OPERAND

Before this operation, the numeric widening conversion is performed on LEFT_OPERAND.

Before this operation, the numeric widening conversion is performed on RIGHT_OPERAND.

The operation result of the arithmetic right shift Operator is the operation that exactly same as the following operation in the C language.

  LEFT_OPERAND >> RIGHT_OPERNAD;

The return type is the same as 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 type within int. Otherwise a compilation error occurs.

Logical Right Shift Operator

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

  LEFT_OPERAND >>> RIGHT_OPERAND

Before this operation, the numeric widening conversion is performed on LEFT_OPERAND.

Before this operation, the numeric widening conversion is performed on RIGHT_OPERAND.

The operation result of logical right shift Operator is the same as the following calculation in the C language.

  // The type of LEFT_OPERAND is the int type
  (uint32_t)LEFT_OPERAND >> RIGHT_OPERAND;

  // The type of LEFT_OPERAND is the long type
  (uint64_t)LEFT_OPERAND >> RIGHT_OPERAND;

The return type is the same as 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 type within int. Otherwise a compilation error occurs.

Logical Operator

The logical operators performs logical operations.

Logical AND Operator

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

  LEFT_OPERAND && RIGHT_OPERAND
  

The return type of the logical AND operator is the int type.

Thg logical AND operator performs the boolean conversion to LEFT_OPERAND. If the evaluated value is 0, returns 0. Otherwise proceed to the evaluation of RIGHT_OPERAND.

It performs the boolean conversion to RIGHT_OPERAND. If the evaluated value is 0, returns 0. Otherwise return the evaluated value.

Logical OR Operator

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

  LEFT_OPERAND || RIGHT_OPERAND

The return type of the logical OR operator is the int type.

Thg logical OR operator performs the boolean conversion to LEFT_OPERAND. If the evaluated value is not 0, return the evaluated value. Otherwise proceed to the evaluation of RIGHT_OPERAND.

It performs the boolean conversion to RIGHT_OPERAND. If the evaluated value is not 0, return the evaluated value. Otherwise returns 0.

Logical NOT Operator

The logical NOT operator ! performes a logical NOT operation.

  !OPERAND

The return type of the logical NOT operator is the int type.

Thg logical NOT operator performs the boolean conversion to OPERAND. If the evaluated value is 0, returns 1. Otherwise returns 0.

String Concatenation Operator

String concatenation operator . concats two strings.

  LEFT_OPERAND . RIGHT_OPERAND

If the type of OPERAND is numeric type, a numeric-to-string conversion is performed.

The return type is the string type.

A string concatenation operator returns the result to concat two operands.

If both LEFT_OPERAND and RIGHT_OPERAND are a string literal, the two string literals are concatenated at compile-time.

If LEFT_OPERAND or RIGHT_OPERAND is undef, an exception occurs.

Compilation Errors:

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

Examples:

  my $str = "abc" . "def";
  my $str = "def" . 34;
  my $str = 123 . 456;

Array Length Operator

The array length operator gets the length of the array.

  @OPERAND

The array length operator returns the int type value that is the length of the array.

Array Length Operator returns the operator

Compilation Errors:

The type of the operand must be an array type. Otherwise a compilation error occurs.

Examples:

  # Getting the length of the array.
  my $nums = new byte[10];
  my $length = @$nums;
  
  # Getting the length of the array with a scalar operator. This is exactly same as the avobe
  my $nums = new byte[10];
  my $length = scalar @$nums;

Note that SPVM does not have the context different from Perl, and array length operators always return the length of the array.

Sequential Operator

The sequential operator evaluates operands from left to right, and returns the value of the rightmost operand.

  (OPERAND1, OPERAND2, ..., OPERNADN)

Exampless:

  # 3 is assigned to $foo
  my $foo = (1, 2, 3);
  
  # $x is 3, $ret is 5
  my $x = 1;
  my $y = 2;
  my $ret = ($x += 2, $x + $y);

Assignment Operator

The assignment operator = assigns a value.

  LEFT_OPERAND = RIGHTH_OPERAND

The assignment operator has different meanings depending on LEFT_OPERAND and RIGHT_OPERAND.

Local Variable Assignment

See "Setting Local Variable".

Class Variable Assignment

See "Setting Class Variable".

Array Element Assignment

See "Setting Array Element".

Field Assignment

See "Setting Field".

Special Assignment Operator

A special assignment operator is the alias for the combination of an operator OPERATOR and "Assignment Operator" =.

  LEFT_OPERAND OPERATOR= RIGHTH_OPERAND

Above is the alias for the following code.

  LEFT_OPERAND = (TYPE_OF_LEFT_OPERAND)(LEFT_OPERAND OPERATOR RIGHTH_OPERAND)

For example, See a byte case.

  # Addition assignment operator
  $x += 1;
  
  # Above is the same as the following code.
  $x = (byte)($x + 1)

The following operators are used as the operators of the special assignment operators.

Addition assignment operator +=
Subtraction assignment operator -=
Multiplication assignment operator *=
Division assignment operator /=
Modulo assignment operator %=
Bit AND assignment operator &=
Bit OR assignment operator |=
Left shift assignment operator <<=
Arithmetic right shift assignment operator >>=
Logical right shift assignment operator >>>=
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";

Comparison Operator

Comparison operators compare LEFT_OPERAND and RIGHT_OPERAND.

Numeric Comparison Operator

Numeric comparison operators compare LEFT_OPERAND and RIGHT_OPERAND in the numeric order.

  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
Operator Description
== If LEFT_OPERAND is equal to RIGHT_OPERNAD in the numeric order, returns 1, otherwise returns 0. The comparation of operands of object types and reference types are also available.
!= If LEFT_OPERAND is not equal to RIGHT_OPERNAD in the numeric order, returns 1, otherwise returns 0. The comparation of operands of object types and reference types are also available.
> If LEFT_OPERAND is greater than RIGHT_OPERNAD in the numeric order, returns 1, otherwise returns 0.
>= If LEFT_OPERAN is greater than or equal to RIGHT_OPERNAD in the numeric order, returns 1, otherwise returns 0.
< If LEFT_OPERAND is less than RIGHT_OPERNAD in the numeric order, returns 1, otherwise returns 0.
<= If LEFT_OPERAND is less than or equal to RIGHT_OPERNAD in the numeric order, returns 1, otherwise returns 0.
<=> If LEFT_OPERAND is greater than RIGHT_OPERNAD in the numeric order, returns 1. If LEFT_OPERAND is less than RIGHT_OPERNAD in the numeric order, return -1. If LEFT_OPERAND is equals to RIGHT_OPERNAD in the numeric order, returns 0.

Before this operation, the binary numeric conversion is performed on LEFT_OPERAND and RIGHT_OPERAND.

The numeric comparison operators performed the same operations as the following C language operations.

  (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 LEFT_OPERAND of the == operator and the != operator must be a numeric type, an object type, or an reference type. Otherwise a compilation error occurs.

The type of RIGHT_OPERAND of the == operator and the != operator must be a numeric type, an object type, or an reference type. Otherwise a compilation error occurs.

If the type of RIGHT_OPERAND of the == operator and the != operator is an object type or an reference type, and the type of LEFT_OPERAND is different from the type of RIGHT_OPERAND, a compilation error occurs.

The type of LEFT_OPERAND of the > operator, the >= operator, the < operator, the <= operator, and the <=> operator must be a numeric type. Otherwise a compilation error occurs.

The type of RIGHT_OPERAND of the > operator, the >= operator, the < operator, the <= operator, and the <=> operator must be a numeric type. Otherwise a compilation error occurs.

String Comparison Operator

String comparison operators compare LEFT_OPERAND and RIGHT_OPERAND 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
Operators Descriptions
eq If LEFT_OPERAND is equal to RIGHT_OPERNAD in the dictionary order, returns 1, otherwise returns 0.
ne If LEFT_OPERAND is not equal to RIGHT_OPERNAD in the dictionary order, returns 1, otherwise returns 0.
gt If LEFT_OPERAND is greater than RIGHT_OPERNAD in the dictionary order, returns 1, otherwise returns 0.
ge If LEFT_OPERAND is greater than or equal to RIGHT_OPERNAD in the dictionary order, returns 1, otherwise returns 0.
lt If LEFT_OPERAND is less than RIGHT_OPERNAD in the dictionary order, returns 1, otherwise returns 0.
le If LEFT_OPERAND is less than or equal to RIGHT_OPERNAD in the dictionary order, returns 1, otherwise returns 0.
cmp If LEFT_OPERAND is greater than RIGHT_OPERNAD in the dictionary order, returns 1. If LEFT_OPERAND is less than RIGHT_OPERNAD in the dictionary order, return -1. If LEFT_OPERAND is equal to RIGHT_OPERNAD 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.

isa Operator

The isa operator checks whether LEFT_OPERAND can be assigned to the right type.

  LEFT_OPERAND isa RIGHT_TYPE

If the right type is a numeric type, a multi-numeric type, a reference type, the any object type, the any object array type, this operator checks the type assignability.

If the assignability without implicite convertion is true, it returns 1, otherwise returns 0.

If the right type is another object type, this operator checks the runtime assignability.

If the runtime assignability is true, it returns 1, otherwise returns 0.

The return type is the int type.

Compilation Errors:

If the runtime assignability is checked, LEFT_OPERAND of the isa operator 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) {
    
  }

isa_error Operator

The isa_error operator checks whether the basic type ID given by LEFT_OPERAND can be assigned to the right type.

  LEFT_OPERAND isa RIGHT_TYPE

The return type is int type.

If the assignability is true, returns 1. Otherwise returns 0.

Compilation Errors:

LEFT_OPERAND of the isa_error operator must be an integer type within int. Otherwise a compilation error occurs.

RIGHT_OPERAND of the isa_error operator 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_type Operator

The is_type operator checks whether the type of LEFT_OPERAND is equal to the right type.

  LEFT_OPERAND is_type RIGHT_TYPE

If the right type is a numeric type, a multi-numeric type, a reference type, the any object type, the any object array type, this operator checks the compile type of LEFT_OPERAND is equal to the right type.

If the check is true, it returns 1, otherwise returns 0.

If the right type is another object type, this operator checks the runtime type of LEFT_OPERAND is equal to the right type.

If the runtime check is true, it returns 1, otherwise returns 0.

The return type is int type.

Compilation Errors:

If the runtime check is performed, LEFT_OPERAND of the is_type operator 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_error Operator

The is_error operator checks whether the basic type ID given by LEFT_OPERAND is the basic type of the right type.

  LEFT_OPERAND isa RIGHT_TYPE

The return type is int type.

If it is ok, returns 1. Otherwise returns 0.

Compilation Errors:

LEFT_OPERAND of the is_error operator must be an integer type within int. Otherwise a compilation error occurs.

RIGHT_OPERAND of the is_error operator 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) {
    
  }
  

is_compile_type Operator

The is_compile_type operator is a comparison operator to check whether the compilation-time type of LEFT_OPERAND is the right type.

  LEFT_OPERAND is_compile_type RIGHT_TYPE

If the compilation-time type of LEFT_OPERAND is the right 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
    }
  }
  

type_name Operator

The type_name operator returns the type name of the object.

  type_name OPERAND

If OPERAND is defined, returns the type name of the object. Otherwise returns undef.

The return type is the string type.

Compilation Errors.

If OPERAND is not an object type, a compilation error occurs.

Examples:

  # "Point"
  my $poitn = Point->new;
  my $type_name = type_name $point;

compile_type_name Operator

The compile_type_name operator returns the type name at compilation time.

  type_name OPERAND

The return type is the string type.

Examples:

  # int
  my $num = 1;
  my $compile_type_name = compile_type_name $num;

dump Operator

The dump operator gets the string representation of the object.

  dump OPERAND

It returns the string representation of the object.

The return type is the string type.

The string representation may be changed. Please don't use the dump operator for the purpose of the data serialization.

Compilation Errors:

If OPERAND is not an object type or the undef type, a compilation error occurs.

new_string_len Operator

The new_string_len operator creates a string with the length.

  new_string_len OPERAND

The integer promotional conversion is performed on OPERAND.

The new_string_len operator returns a new string that length is the length specified by OPERAND and all characters are \0.

The character just after the last character is \0. The string created by the new_string_len operator can be used as the C language string ending with \0.

The return type is the string type.

The length specified by 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:

  # New a string with the length
  my $message = new_string_len 5;

copy Operator

The copy operator copies the object.

  copy OPERAND

If the type of operand is none of the string type, a numeric type, a multi-numeric type, An exception is thorwn.

The copy operator returns the copied object.

The return type is the same as the type of operand.

Read-only flag of the string is dropped.

Compilation Errors:

The type of the operand must be an object type. Otherwise a compilation error occurs.

Examples:

  # New a string with the length
  my $message = copy "abc";

is_read_only Operator

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

  is_read_only OPERAND

If the string is read-only, the is_read_only operator returns 1, otherwise returns 0.

The return type is an int type.

Compilation Errors:

The operand must be the string type. Otherwise a compilation error occurs.

Examples:

  # New a string with the length
  my $message = "Hello";
  my $is_read_only = is_read_only $message;

length Operator

The string length operator length gets the length of the string.

  length OPERAND

The returned length is the byte size. Note that the length is not the count of UTF-8 characters.

The return type is the int type.

Compilation Errors:

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

Examples:

  # Getting the string length. The length is 5.
  my $message = "Hello";
  my $length = length $message;
  
  # Getting the string length of UTF-8. The length is 9.
  my $message = "あいう";
  my $length = length $message;

scalar Operator

The scalar operator is an Operator that returns OPERAND.

  scalar OPERAND

Compilation Errors:

The operand must be an "The array Length Operator". Otherwise a compilation error occurs.

Examples:

  # Getting the array length 
  my $nums = new int[3];
  foo(scalar @$nums);

  # This is exactlly same as the above.
  my $nums = new int[3];
  foo(@$nums);

Note that the sclara operator exists only to reduce the confusion.

isweak Operator

The isweak operator checks whether a field is referenced by a weak reference

  isweak OBJECT->{FIELD_NAME};

If the field is weaken, the isweak operator returns 1, otherwise returns 0.

The return type of the isweak operator is the int type.

Compilation Errors:

The type of the object must be the class type. Otherwise a compilation error occurs.

If the field name is not found, a compilation error occurs.

The type of the field targetted by the isweak operator is not an object type, a compilation error occurs.

Examples:

  # isweak
  my $isweak = isweak $object->{point};

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 "") {
    # ...
  }

basic_type_id Operator

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

  basic_type_id TYPE

The return value is the basic type ID.

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;
  

eval_error_id Operator

The eval_error_id operatoer gets the error ID of the exception caught by an eval block.

  eval_error_id

This value is set to 0 at the beginning of the eval block.

warn Operator

The warn operator prints a message to the standard error.

  warn OPERNAD;
  warn;

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

The return type is the void type.

If the end character of the OPERNAD is \n, the warn operator prints the OPERNAD itself.

Otherwise the current file name and the current line number by the format "\n at $file_name line $line\n" are added to the end of the OPERNAD.

The buffer of the standard error is flushed after the printing.

Compilation Errors:

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

Examples:

  warn "Something is wrong.";

The print operator prints a string to the standard output.

  print OPERAND;

OPERAND must be the string type.

The return type is the void type.

If OPERAND is an undef, print nothing.

say Operator

The say operator prints a string with a line break \n to the standard output.

  say OPERAND;

OPERAND must be the string type.

The return type is the void type.

If OPERAND is an undef, print \n.

make_read_only Operator

The make_read_only operator makes the string read-only.

  make_read_only OPERAND;

OPERAND must be the string type.

The return type is the void type.

Read-only strings cannnot be cast to string type qualified by mutable.

  # A string
  my $string = new_string_len 3;
  
  # Make the string read-only
  make_read_only $string;
  
  # The conversion to the string type qualified by mutable throw an exception.
  my $string_mut = (mutable string)$string;

weaken Operator

The weaken operator creates a weak reference.

  weaken OBJECT->{FIELD_NAME};

The return type is the void type.

Compilation Errors:

The type of the object must be the class type. Otherwise a compilation error occurs.

If the field name is not found, a compilation error occurs.

The type of the field targetted by the weaken statement is not an object type, a compilation error occurs.

Examples:

  # weaken
  weaken $object->{point};

unweaken Operator

The unweaken operator unweakens a weak reference.

  unweaken OBJECT->{FIELD_NAME};

The return type is the void type.

Compilation Errors:

The type of the object must be the class type. Otherwise a compilation error occurs.

If the field name is not found, a compilation error occurs.

The type of the field targetted by the unweaken statement is not an object type, a compilation error occurs.

Examples:

  # unweaken
  unweaken $object->{point};

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);

__PACKAGE__ Operator

The __PACKAGE__ operator gets the current class name.

  __PACKAGE__

The return type is the string type.

If the __PACKAGE__ operator is used in anon method, it returns its outer class.

Examples:

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

__FILE__ Operator

The __FILE__ operator gets the current file name.

  __FILE__

The current file name means the relative path from the base path of the class file. For example, if the class loaded path is /mypath and the class name is Foo::Bar, the absolute path is /mypath/SPVM/Foo/Bar.spvm and the relative path is SPVM/Foo/Bar.spvm. SPVM/Foo/Bar.spvm is the current file name.

Examples:

  # SPVM/Foo/Bar.spvm
  class Foo::Bar {
    static method baz : void () {
      # Get the current file name - SPVM/Foo/Bar.spvm
      my $file_name == __FILE__;
    }
  }
  class Foo::Bar2 {
    static method baz : void () {
      # Get the current file name - SPVM/Foo/Bar.spvm
      my $file_name == __FILE__;
    }
  }

__LINE__ Operator

The __LINE__ operator gets the current line number of the current file.

  __LINE__

Examples:

  class Foo::Bar {
    static method baz : void () {
      # Get the current line number - 4
      my $line = __LINE__;
    }
  }

new Operator

The new operator creates an object or an array.

Creating Object

The syntax of creating object creates an object using the new operator.

  new CLASS_NAME;

The class name must be the name of the class defined by the class definition.

The fields of the created object are initialized by the initial value.

The reference count of the created object is 0. If the object is assigned to a local variable, a class variable, or a field by "Assignment Operator", the reference count is incremented by 1.

Examples:

  my $object = new Foo;

Creating Array

The syntax of creating array creates an array using the new operator.

  new BasicType[LENGTH]

The type must be a basic type.

The integer promotional conversion is performed on the length.

The length must be greater than or equal to 0. Otherwise an exception is thrown.

All elements of the array are initialized by the initial value.

The type of the created array is an array type.

Compilation Errors:

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

Examples:

  my $nums = new int[3];
  my $objects = new Foo[3];
  my $objects = new object[3];
  my $values = new Complex_2d[3]

Creating Multi-Dimensional Array

Multi dimensional arrays can be created using the new operator.

  new BasicType[][LENGTH]
  new BasicType[]..[][LENGTH]

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

Examples:

  # 2 dimentional int array
  my $nums = new int[][3];
  
  # 3 dimentional int array
  my $nums = new int[][][3];

The max dimention is 255.

Array Initialization

The array initialization creates an array and initialize the array easily.

  []
  [ELEMENT1, ELEMENT2, ELEMENT3]

The array initialization create an array that has the length of the elements.

And the array is initialized by the elements.

And the created array is returned.

The type of the created array is the type that 1 dimension is added to the type of the first element.

If no element is specified, the type of the create array becomes any object type.

Examples:

  # 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 first example is the same as the following codes.

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

The array initialization has another syntax using {}.

  {}
  {ELEMENT1, ELEMENT2, ELEMENT3, ELEMENT4}

This is the same as above array init syntax, but the type of the created array is always "Any Object Array Type" object[].

Compilation Errors:

If the length of the elements is odd number, a compilation error occurs.

Examples:

  # Key values empty
  my $key_values = {};
  
  # Key values
  my $key_values = {foo => 1, bar => "Hello"};

Getting Local Variable

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

  $var

The return value is the value of the local variable.

The return type is the type of the local variable.

Setting Local Variable

The setting local variable sets the value of "Local Variable" using the assignment operator.

  $var = VALUE

The return value is the value after the assignment.

If the type of the assigned value is an object type, the reference count of the object is incremented by 1.

If an object has already been assigned to $var before the assignment, the reference count of the object is decremented by 1.

See the scope to know the garbage collection of local variables.

Compilation Errors:

The assignment of the value must satisfy the type assignability. Otherwise a compilation error occurs.

Getting Class Variable

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

  $CLASS_NAME::CLASS_VARIABLE_NAME

CLASS_NAME:: can be omitted if the class variable belongs to the current class.

  $CLASS_VARIABLE_NAME

If the class variable is used in an anon method and CLASS_NAME:: can be omitted, its current class means its outer class.

Compilation Errors:

If the class variable does not found, a compilation error occurs.

If the class variable is private and it is accessed outside of the class, a compilation error occurs.

Examples:

  class Foo {
    our $VAR : int;
  
    static method bar : int () {
      my $var1 = $Foo::VAR;
      my $var2 = $VAR;
      
      my $cb = method : void () {
        # $Foo::BAR
        $VAR;
      }
    }
  }

Setting Class Variable

Setting Class Variable operator sets "Class Variable" Value using the assignment operator.

  $CLASS_NAME::CLASS_VARIABLE_NAME = VALUE

"CLASS_NAME::" can be omitted when the class Variable belongs to own class.

  $CLASS_VARIABLE_NAME = VALUE

The return value is the value after the setting.

The return type is the type of the class variable.

If the type of the assigned value is an object type, the reference count of the object is incremented by 1.

If an object has already been assigned to $CLASS_VARIABLE_NAME before the assignment, the reference count of the object is decremented by 1.

If the class variable is used in an anon method and CLASS_NAME:: can be omitted, its current class means its outer class.

Compilation Errors:

If the assignment does not satisfy the type assignability, a compilation error occurs.

If the class variable does not found, a compilation error occurs.

If the class variable is private and it is accessed outside of the class, a compilation error occurs.

Examples:

  class Foo {
    our $VAR : int;
  
    static method bar : int () {
      $Foo::VAR = 1;
      $VAR = 3;
    }
    my $cb = method : void () {
      # $Foo::VAR
      $VAR = 5;
    }
  }

Getting Exception Variable

The setting exception variable gets the value of the exception variable.

  $@

The return value is the value of exception variable.

The return type is the string type.

Examples:

  # Getting the exception variable
  my $message = $@;

Setting Exception Variable

The setting exception variable sets the value of "Exception Variable" using the assignment operator.

  $@ = VALUE

The type of the assigned value must be the string type.

The return value is the value after the setting.

The return type is the string type.

The reference count of the assigned value is incremented by 1.

If an string has already been assigned to the exception variable before the assignment, the reference count of the string is decremented by 1.

Examples:

  $@ = "Error";

Getting Field

The getting field gets the field of the object. This is one syntax of the field access.

  INVOCANT->{FIELD_NAME}

The type of invocant is a class type.

The retrun type is the type of the field.

Examples:

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

Setting Field

The setting field sets the field of the object. This is one syntax of the field access.

  INVOCANT->{FIELD_NAME} = VALUE

The type of invocant is a class type.

The return value is the value after the setting.

The return type is the field type.

If the type of assigned value is a basic object type, the reference count of the object is incremented by 1.

If an object has already been assigned to the field before the assignment, the reference count of that object is decremented by 1.

Compilation Errors:

If the assignment does not satisfy the type assignability, a compilation error occurs.

Examples:

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

Getting Multi-Numeric Field

Getting Multi-Numeric Field operator gets the field of the multi-numeric value. This is one syntax of the field access.

  INVOCANT->{FIELD_NAME}

The invocant is the multi-numeric type.

Getting Multi-Numeric Field operator returns the field value in the multi-numeric value.

The retrun type is the type of the field.

Compilation Errors:

If the field names does not found in the class, a compilation error occurs

Examples:

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

Setting Multi-Numeric Field

Setting Multi-Numeric Field operator sets the field of the multi-numeric value using "Assignment Operator". This is one syntax of the field access.

  INVOCANT->{FIELD_NAME} = RIGHT_OPERAND

The invocant is the multi-numeric type.

Setting Multi-Numeric Field operator returns the value of the field after setting.

The assignment must satisfy the type assignability.

The return type is the field type.

Compilation Errors:

If the field names does not found in the class, a compilation error occurs.

Examples:

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

Getting Array Element

The getting array element gets the element of the array.

  ARRAY->[INDEX]

The array must be an array type.

The integer promotional conversion is performed on the index.

The getting array element returns the element that is specifed by the index.

The return type is the type of the element.

The array must be defined. Otherwise an exception is thrown.

The index must be greater than or equal to 0. Otherwise an exception is thrown.

Compilation Errors:

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

Examples:

  my $nums = new int[3];
  my $num = $nums->[1];
  
  my $points = new Point[3];
  my $point = $points->[1];
  
  my $objects : object[] = $points;
  my $object = (Point)$objects->[1];

Setting Array Element

The setting array element sets the element of the array using the assignment operator.

  ARRAY->[INDEX] = RIGHT_OPERAND

The array must be an array type.

The integer promotional conversion is performed on the index.

RIGHT_OPERAND must satisfy type assignability.

The setting array element returns the value of the element that is set.

The array must be defined. Otherwise an exception is thrown.

The index must be greater than or equal to 0. Otherwise an exception is thrown.

If RIGHT_OPERAND is an object type, the reference count of the object is incremented by 1.

If an object has already been assigned to the field before the assignment, the reference count of the object is decremented by 1.

Compilation Errors:

The index must be an integer type within int. 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);
  
  my $objects : object[] = $points;
  $objects->[2] = Point->new(3, 5);

Type Cast Operator

The type cast operator performs an explicite type conversion.

  # Type Cast
  (TYPE)OPERAND
  
  # Postfix Type Cast
  OPERAND->(TYPE)

Compilation Errors:

If the operand of the type cast operator dose not satisfy type castability, a compilation error occurs.

Examples:

  # 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);

Method Call

The method call syntax calls a method.

Class Method Call

A method defined as the class method can be called using the class method call.

  ClassName->MethodName(ARGS1, ARGS2, ...);
  
  &MethodName(ARGS1, ARGS2, ...);

& means the current class.

If & is used in anon method, it means its outer class.

Compilation Errors:

If the number of arguments does not correct, a compilation error occurs.

If the types of arguments have no type compatible, a compilation error occurs.

Examples:

  class Foo {
    
    static method main : void () {
      
      my $ret = Foo->bar(1, 2, 3);
      
      # Same as Foo->bar
      my $ret = &bar(1, 2, 3);
      
      my $cb = method : void () {
        # Same as Foo->bar;
        my $ret = &foo;
      };
    }
    
    static method foo : int () {
      return 5;
    }
  }

Instance Method Call

A method defined as the instance method can be called using the instance method call.

  Instance->MethodName(ARGS1, ARGS2, ...);

The called method is resolved from the type of the instance.

Compilation Errors:

If the number of arguments does not correct, a compilation error occurs.

If the types of arguments have no type compatible, a compilation error occurs.

Examples:

  $object->bar(5, 3. 6);

The SUPER:: qualifier calls the method of the super class of the current class.

  $object->SUPER::bar(5, 3. 6);

A instance method can be called statically by specifing the calss name.

  $point3d->Point::clear;

Setting the value with Dereference

Setting a value with Dereference sets the actual value from Reference. It was designed to realize the C joint operator *.

  $VARIABLE = OPERAND

Setting a value with Dereference returns the set value.

Compilation Errors:

The variable type must be a reference type. Otherwise a compilation error occurs.

The type of operator must match the type of the variable when dereferenced. 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 Multi-Numeric Field via Dereference

The syntax of getting multi-numeric field via dereference gets the field of the multi-numeric value via "Dereference". This is one syntax of the field access

  INVOCANT->{FIELD_NAME}

The invocant is "Multi-Numeric Reference Type".

The getting multi-numeric field via dereference operator returns the field value in the multi-numeric value.

The retrun type is the type of the field.

Compilation Errors:

If the field names does not found in the class, a compilation error occurs

Examples:

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

Setting Multi-Numeric Field via Dereference

The setting multi-numeric field via dereference operator sets the field of the multi-numeric value via "Dereference" using "Assignment Operator". This is one syntax of the field access.

  INVOCANT->{FIELD_NAME} = RIGHT_OPERAND

The invocant is "Multi-Numeric Reference Type".

The setting multi-numeric field via dereference operator returns the value of the field after setting.

The assignment must satisfy the type assignability.

The return type is the field type.

Compilation Errors:

If the field names does not found in the class, a compilation error occurs

Examples:

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

Anon Method Operator

The anon method operator defines an anon calss that has an anon instance method.

And this operator creates an object which type is the anon class by the "new Operator" in new operator, and returns it.

  # Anon method
  method : TYPE  (VAR1 : TYPE1, VAR2 : TYPE2, ...) {
  
  }

The way to define the method is the same as the method definition.

Examples:

  # Anon method
  class Foo::Bar {
    method some_method : void () {
      my $comparator = (Comparator)method : int ($x1 : object, $x2 : object) {
        my $point1 = (Point)$x1;
        my $point2 = (Point)$x2;
        
        return $point1->x <=> $point2->x;
      };
    }
  }

See also Comparator.

The above example is the same as the following codes.

  # Foo/Bar.spvm
  class Foo::Bar {
    method some_method : void () {
      my $comparator = (Comparator)new Foo::Bar::anon::3::31;
    }
  }
  
  # Foo/Bar/anon/3/31.spvm
  class Foo::Bar::anon::3::31 : public {
    method : int ($x1 : object, $x2 : object) {
      my $point1 = (Point)$x1;
      my $point2 = (Point)$x2;
      
      return $point1->x <=> $point2->x;
    }
  }

Anon Method Field Definition

The anon method field definition is the syntax to define the field of the anon class of the anon method.

  # Anon method field definitions
  [has FIELD_NAME : TYPE1, has FIELD_NAME : TYPE2, ...] ANON_METHOD_DEFINITION
  
  # Anon method field definitions with field default values
  [has FIELD_NAME : TYPE1 = OPERAND1, has FIELD_NAME : TYPE2 = OPERAND2, ...] ANON_METHOD_DEFINITION
  
  [VAR1 : TYPE1, VAR2 : TYPE2, ...] ANON_METHOD_DEFINITION
  

Examples:

  class Foo::Bar {
    method some_method : void () {
      my $foo = 1;
      my $bar = 5L;
      
      my $comparator = (Comparator)[has foo : int = $foo, has bar : long = $bar] method : int ($x1 : object, $x2 : object) {
        my $foo = $self->{foo};
        my $bar = $self->{bar};
        
        print "$foo\n";
        print "$bar\n";
      };
    }
  }

Same as avobe but more simple:

  class Foo::Bar {
    method some_method : void () {
      my $foo = 1;
      my $bar = 5L;
      
      my $comparator = (Comparator)[$foo : int, $bar : long] method : int ($x1 : object, $x2 : object) {
        print "$foo\n";
        print "$bar\n";
      };
    }
  }

The above example is the same as the following codes.

  # Foo/Bar.spvm
  class Foo::Bar {
    method some_method : void () {
      # Externally defined local variables
      my $foo = 1;
      my $bar = 5L;
      
      my $anon = new Foo::Bar::anon::5::61;
      $anon->{foo} = $foo;
      $anon->{bar} = $bar;
      
      my $comparator = (Comparator)$anon;
    }
  }
  
  # Foo/Bar/anon/5/61.spvm
  class Foo::Bar::anon::5::61 : public {
    has foo : public int;
    has bar : public long;
    
    method : int ($x1 : object, $x2 : object) {
      my $foo = $self->{foo};
      my $bar = $self->{bar};
      
      print "$foo\n";
      print "$bar\n";
    }
  }

Reference Operator

The reference operator \ creates a reference, and returns it.

  \OPERAND

The return type is the reference type of OPERAND.

Compilation Errors:

The operand must be a local variable that type is a numeric type or a multi-numeric type. Otherwise a compilation error occurs.

Examples:

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

Dereference Operator

The dereference operator $ returns the value referenced by the variable VARIABLE.

  $VARIABLE

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

Compilation Errors:

The type of the variable must be a reference type. Otherwise a compilation error occurs.

Examples:

  my $num : int;
  my $num_ref : int* = \$num;
  my $num_deref : int = $$num_ref;
  
  my $z : Complex_2d;
  my $z_ref : Complex_2d* = \$z;
  my $z_deref : Complex_2d = $$z_ref;

See Also

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License