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 Operator
Logical AND Operator
The logical AND operator &&
performs a logical AND operation.
LEFT_OPERAND && RIGHT_OPERAND
This operator performs the boolean conversion on the left operand LEFT_OPERAND.
If the evaluated value is 0, it returns 0, otherwise performs the boolean conversion 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 boolean conversion on the left operand LEFT_OPERAND.
If the evaluated value is not 0, it returns the evaluated value, otherwise performs the boolean conversion 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 boolean conversion 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) {
}
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;
print Operator
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
The anon method operator creates an anon method.
ANON_METHOD
This operator defines an anon method ANON_METHOD, creates a new object of the anon class that owns the anon method, and retunrs it.
Examples:
my $comparator = (Comparator)method : int ($x1 : object, $x2 : object) {
my $point1 = (Point)$x1;
my $point2 = (Point)$x2;
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
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;
head3 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;
}
}
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);
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';
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;
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";
Type Cast
A type cast performs a type conversion.
# A type cast
(TYPE)OPERAND
# A postfix type cast
OPERAND->(TYPE)
A type cast performs the type conversion on OPERAND from the type of the operand OPERAND to the type TYPE, and returns the value after the type conversion.
The return type is TYPE.
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 implicite type 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 implicite conversion at runtime.
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 implicite 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;
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 outmost 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 $result = Foo->bar(1, 2, 3);
# Same as Foo->bar
my $result = &bar(1, 2, 3);
my $anon_method = method : void () {
# Same as Foo->bar;
my $result = &foo;
};
}
static method foo : int () {
return 5;
}
}
Getting an Enumeration Value
The operation of getting an enumeration value gets a value of an enumeration.
The definition of an enumeration value is replaced to a class method, so this operation is the same as a class method call.
# Definition of an enumeration
class MyClass {
enum {
VALUE1,
VALUE2,
VALUE3,
}
}
# These are replaced to definitions of class methods
class MyClass {
static method VALUE1 : int () { return 0; }
static method VALUE2 : int () { return 1; }
static method VALUE3 : int () { return 2; }
}
However, there is one important difference.
The class method calls are replaced to interger literals at compilation time.
For this replacement, this operation is used as an operand of the case statement.
switch ($num) {
case MyClass->VALUE1: {
# ...
}
case MyClass->VALUE2: {
# ...
}
case MyClass->VALUE3: {
# ...
}
default: {
# ...
}
}
Note that if an enumeration value is changed, the binary compatibility is broken.
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;
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};
See Also
Copyright & License
Copyright (c) 2023 Yuki Kimoto
MIT License