SPVM::Document::Language::Types − Types in the SPVM Language
This document describes types in the SPVM language.
This section describes data.
A data is called value.
The value of numeric types is called number.
Normally, numbers are created by numeric literals.
# byte −
8bit signed integer
my $number = (byte)1;
# short − 16bit signed integer
my $number = (short)1;
# int − 32bit signed integer
my $number = 1;
# long − 64bit signed integer
my $number = 1L;
# float − 32bit floating point
my $number = 1.5f;
# double − 64bit floating point
my $number = 1.0;
A character created by character literal is a number of byte type.
# A number of
byte type created by a character literal
my $char = 'a';
See the following section operations for numbers.
|
• |
Numeric Operators |
|||
|
• |
Numeric Comparison Operators |
Internal Representation of Negative Integers
Negative integers are represented by two's complement <https://en.wikipedia.org/wiki/Two%27s_complement>.
The value of string type is called string.
A string consists of characters of the "byte" type.
A string has its length.
A string is an object.
Normally, a string is created by a string literal or new_string_len operator.
# A string
created by a string literal
my $string = "Hello";
my $char = $string−>[0];
# A mutable string created by new_string_len operator
my $string = new_string_len 3;
$string−>[0] = 'a';
See the following sections about operations for strings.
|
• |
length Operator |
|||
|
• |
String Concatenation Operator |
|||
|
• |
Character Get Operation |
|||
|
• |
Character Set Operation |
|||
|
• |
new_string_len Operator |
|||
|
• |
make_read_only Operator |
|||
|
• |
is_read_only Operator |
|||
|
• |
String Comparison Operators |
|||
|
• |
copy Operator |
Sting Native Level Representation
At native level, the character just after the last character of the string is set to "\0", so the characters in the string can be used as a C language string.
# The characters
in the string can be used as a C language string
void* obj_string = stack[0].oval;
const char* chars = env−>get_chars(env, stack,
obj_string);
if (strcmp(chars, "Hello") == 0) {
}
The value of an array type is called array.
An array consists of a set of numbers, a set of objects, or a set of multi−numeric numbers.
An array has its length.
The elements of an array are arranged by index and the index starts from 0.
An array is an object.
Normally, an array is created by new Operator and an array initialization.
# An array
created by new operator
my $numbers = new int[3];
$numbergers−>[0] = 1;
my $strings = new string[3];
my $objects = new Point[3];
my $mulnum_numbers = new Complex_2d[3];
# An array created by an array initialization
my $numbers = [1, 2, 3];
All elements of an array can be got by for statement.
# for statement
for (my $i = 0; $i < @$numbers; $i++) {
my $number = $numbers−>[$i];
}
# for−each statement
for my $number (@$numbers) {
}
See the following sections about operations for arrays.
|
• |
Creating Array in new Operator |
|||
|
• |
Array Initialization |
|||
|
• |
Array Length Operator |
|||
|
• |
Array Element Get Operation |
|||
|
• |
Array Element Set Operation |
The value of an object type is called object.
A string is an object.
An array is an object.
An objcet of class type has its fields. A field is a number or an object.
Normally, an object is created by new operator.
# An object
created by new operator
my $point = new Point;
When an object is created, memory for the object is allocated in heap memory.
Created objects are destroyed by garbage collection.
See the following sections about operations for objects.
|
• |
new Operator |
|||
|
• |
dump Operator |
|||
|
• |
Field Get Operation |
|||
|
• |
Field Set Operation |
|||
|
• |
isa Operator |
|||
|
• |
is_type Operator |
|||
|
• |
type_name Operator |
Object Native Level Representation
At native level, an object is a memory address.
void* obj_point = stack[0].oval;
The value of undef type is called undefined value.
An undefined value means the value is undefined.
An undefined value is created by undef operator.
undef
An undefined value is able to be assigned to an object type.
my $point : Point = undef;
Examples:
# Examples of
undefined values
my $string : string = undef;
if (undef) {
}
my $message = "Hello";
if ($message == undef) {
}
Undefined Value Native Level Representation
At native level, an undefined value is equal to 0, normally a null pointer "NULL" defined in "stddef.h".
NULL
The value of a multi−numeric type is called multi−numeric number.
A multi−numeric number is a set of numbers of the same type.
my $z :
Complex_2d;
$z−>{re} = 1;
$z−>{im} = 2;
See the following sections about operations for multi−numeric numbers.
|
• |
Multi−Numeric Field Get Operation |
|||
|
• |
Multi−Numeric Field Set Operation |
The value of a reference type is called reference.
A reference has a referencing value.
A referencing value must be a number or a multi−numeric number
The reference operator "\" creates a reference.
my $number :
int;
my $number_ref = \$number;
See the following sections about operations for multi−numeric numbers.
|
• |
Referenced Value Get Operation |
|||
|
• |
Referenced Value Set Operation |
|||
|
• |
Reference Operator |
|||
|
• |
Dereference Operator |
|||
|
• |
Referenced Multi−Numeric Field Get Operation |
|||
|
• |
Referenced Multi−Numeric Field Set Operation |
Reference Native Level Representation
At native level, a reference is a memory address.
int32_t* num_ref = stack[0].iref;
This section describes types.
This section describes numeric types.
Integer Types
This section describes integer types.
An interger type is a numeric type.
byte Type
The "byte" type is the type for a signed 8−bit integer.
byte
The "byte" type is an integer type.
short Type
The "short" type is the type for a signed 16−bit integer.
short
The "short" type is an integer type.
int Type
The "int" type is the type for a signed 32−bit integer.
int
The "int" type is an integer type.
long Type
The "long" type is the type for a signed 64−bit integer.
long
The "long" type is an integer type.
Floating Point Types
This section describes floating point types.
A floating point type is a numeric type.
float Type
The "float" type is the type for 32bit floating point.
float
The "float" type is a floating point type.
double Type
The "double" type is the type for 64bit floating point.
double
The "double" type is a floating point type.
Numeric Types Order
numeric types have its order.
The order is "byte", "short", "int", "long", "float", "double" from smallest to largest.
This section lists object types.
string Type
The "string" type is the type for strings.
string
The "string" type is an object type.
The "string" type can be qualified with mutable type qualifier.
mutable string
Class Types
A class type is the type for a class.
A class type is defined by class definition.
class CLASS_TYPE
{
}
An object can be created from a class by a new operator.
Note that an interface type and a multi−numeric type is not a class type although these types are defined by class definition.
Numeric Object Types
A numeric object type is a class type that owns the corresponding field of a numeric type.
The List of Numeric Object Types:
Interface Types
An interface type is a type for an interface.
An interface type is defined by an interface definition.
class
INTERFACE_TYPE : interface_t {
}
Any Object Type
Any object type "object" is the type to which any object type can be assigned.
object
Examples:
# Examples of
any object type
my $object: object = new Foo;
A basic type is a type whose type dimension is 0 and that can be an element of an array.
The List of Basic Types:
|
• |
Numeric types |
|||
|
• |
Multi−numeric types |
|||
|
• |
Class types |
|||
|
• |
Interface types |
|||
|
• |
any object type |
|||
|
• |
string type |
The undef type is the type of an undefined value.
The "void" type is the type that represents a method defined by a method definition does not return a return value.
void
An array type is a type for an array. An array type consists of a basic type and a type dimension such as "[]", "[][]".
BASIC_TYPE[]..
("[].." means one more "[]")
Examples:
# Numeric array
int[]
double[]
# String array
string []
# Class array
Point[]
# Any object array
object[]
# 2 dimensional array
int[][]
# 3 dimensional array
int[][][]
An array type is an object type.
Compilation Errors:
The dimesion is less than or equal to 255. Otherwise, a compilation error occurs.
Numeric Array Types
A numeric array type is an array type of a numeric type.
The List of Numeric Array Types:
byte[]
short[]
int[]
long[]
float[]
double[]
Object Array Types
An object array type is an array type of an object type.
Examples:
Point[]
Point[][]
Stringable[]
string[]
object[]
String Array Type
The string array type is array type string type.
string[]
Class Array Types
A class array type is an array type of a class type.
Examples:
Int[]
Point[]
Interface Array Types
An interface array type is an array type of an interface type.
Examples:
Stringable[]
Cloneable[]
Multi−Dimensional Array Types
A multi−dimensional array type is an array type of an array type.
Examples:
int[][]
Int[][]
string[][][]
object[][]
A multi−dimensional array is created by the syntax of creating a multi−dimensional array of the "new" operator.
Multi−Numeric Array Types
A multi−numeric array type is an array type of a multi−numeric type.
Examples:
Complex_2d[]
Complex_2f[]
Any Object Array Type
The any object array type "object[]" is array type to which any object array type can be assigned.
object[]
A multi−numeric type is a type for a multi−numeric number.
A multi−numeric type is defined by a multi−numeric type definition.
class
MULNUM_TYPE : mulnum_t {
}
A reference type is a type for a reference.
TYPE*
A reference type consists of a type followed by "*".
TYPE must be a numeric type or a multi−numeric type.
Numeric Reference Types
A numeric reference type is a reference type of a numeric type.
The List of Numeric Reference Types:
byte*
short*
int*
long*
float*
double*
Multi−Numeric Reference Types
A multi−numeric reference types is a reference type of a multi−numeric type.
MULNUM_TYPE*
Examples:
Complex_2d*
Complex_2f*
A type qualifier qualify a type.
QUALIFIER TYPE
The QUALIFIER qualified the type TYPE.
mutable Type Qualifier
The "mutable" type qualifier qualifies string type.
mutable string;
The string of string type with the "mutable" type qualifier is able to be set a character.
my $string =
(mutable string)copy "abc";
$string−>[0] = 'd';
The value of a type is initialized by its type initial value.
The List of Type Initial Values:
The type width is the length of runtime stack of the type.
If the type is a multi−numeric type, the type width is the length of the fields, owhterwise it is 1.
A union type expresses the logical OR of types.
TYPE1|TYPE2|TYPEn
A union type is treated as any object type "object".
Examples:
my $union =
(string|string[])undef;
method : foo ($union : string|string[]) {
}
The generic type is
TYPE of ELEMENT_TYPE
TYPE is a type.
ELEMENT_TYPE is a type.
A generic type is treated as "TYPE". "ELEMETN_TYPE" is ignored in compilation time type checks.
"ELEMETN_TYPE" is only used for the type cast for "element" type.
Examples:
# Examples of
generic types
has points : List of Point;
has hash_of_list_of_point : Hash of List of Point;
our $POINTS : List of Point;
my $points : List of Point;
static method foo : List of Point ($arg : List of Point) {
... }
my $replace : object of string|Regex::Replacer;
Compilation Errors:
If the type specified as the generic type is not found, a compilation error occurs.
The assignment requirement is the requirement whether one type is able to be assigned to another type.
What does it mean to assign one type to another type?
Typically, it is sufficient to consider a case where a value of a type TYPE_FROM is assigned to a variable of a type TYPE_TO.
my $value :
TYPE_FROM;
my $var : TYPE_TO = $value;
Abstracting this, type−to−type assignment is defined.
TYPE_TO = TYPE_FROM
Note that this is a concept, not an actual syntax.
Some assinments perform a data conversion.
In the following description, the word "Type" is omitted when it is obvious.
Assignment Requirement from Numeric to Numeric
To Larger:
To Smaller:
"Conditional Yes" means the followings.
For Integral Types:
If the value of TYPE_FROM is represented by an interger literal and between the max and minimal value of the type of TYPE_TO, Yes, otherwize No.
For Floating Point types:
If the value of TYPE_FROM is represented by a floating point literal, Yes, otherwize No.
Assignment Requirement from NumericObject to Numeric
Assignment Requirement from Any Object to Numeric
Assignment Requirement from Other to Numeric
NumericX is a numeric type.
Multi−NumericX is a multi−numeric type.
ReferenceX is a reference type.
NumericX is a numeric type.
NumericObjectX is a numeric object type.
NumericX is a numeric type.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
InterfaceSatisfiedX is a class type or an interface type that satisfied interface requirement of InterfaceX.
ObjectX is an object type.
NumericX is a numeric type.
X is a type.
X is a type.
NumericX is a numeric type.
Multi−NumericX is a multi−numeric type.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
InterfaceSatisfiedX is a class type or an interface type that satisfied interface requirement of InterfaceX.
ObjectX is an object type.
"[].." is one or more "[]".
X is a type.
"[].." is one or more "[]".
D means its type dimension that is greater than or eausl to 2.
X[]..D is a multi−dimensional array.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
InterfaceSatisfiedX is a class type or an interface type that satisfied interface requirement of InterfaceX.
The cast requirement is the requirement whether one type is able to be cast to another type.
What does it mean to cast one type to another type?
Typically, it is sufficient to consider a case where a value of a type TYPE_FROM is casted to TYPE_TO.
my $value :
TYPE_FROM;
(TYPE_TO)$value;
Abstracting this, type−to−type cast is defined.
(TYPE_TO)TYPE_FROM
Note that this is a concept, not an actual syntax.
Some type casts perform a data conversion.
Some type casts perform a data check.
In the following description, the word "Type" is omitted when it is obvious.
Cast Requirement from Numeric to Numeric
To Larger:
To Smaller:
Cast Requirement from NumericObject to Numeric
Cast Requirement from Any Object to Numeric
NumericX is a numeric type.
Cast Requirement from Other to Numeric
NumericX is a numeric type.
Multi−NumericX is a multi−numeric type.
ReferenceX is a reference type.
NumericX is a numeric type.
NumericObjectX is a numeric object type.
NumericX is a numeric type.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
InterfaceY is a an interface type.
InterfaceSatisfiedX is a class type or an interface type that satisfied interface requirement of InterfaceX.
ObjectX is an object type.
NumericX is a numeric type.
NumericX is a numeric type.
Multi−NumericX is a multi−numeric type.
ClassX is a class type.
SuperClassX is a super class of ClassX.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
InterfaceY is a an interface type.
InterfaceSatisfiedX is a class type or an interface type that satisfied interface requirement of InterfaceX.
ObjectX is an object type.
"[].." is one or more "[]".
X is a type.
"[].." is one or more "[]".
D means its type dimension that is greater than or eausl to 2.
X[]..D is a multi−dimensional array.
ClassX is a class type.
SuperClassX is a super class of ClassX.
InterfaceX is a an interface type.
InterfaceSatisfiedX is a class type or an interface type that satisfied interface requirement of InterfaceX.
The interface requirement is the requirement whether an object type is able to be assigned to an interface type.
INTERFACE_TYPE_TO = OBJECT_TYPE_FROM
This is the same concept as type−to−type assignment explained in Assignment Requirement.
INTERFACE_TYPE_TO must be an interface type.
OBJECT_TYPE_FROM must be a class type or an interface type.
The following check is performed on every instance method of OBJECT_TYPE_FROM.
If an instance method of INTERFACE_TYPE_TO has the "required" method attribute, OBJECT_TYPE_FROM or one of its super classes must have a method with the same name.
If OBJECT_TYPE_FROM or one of its super classes has an instance method(this is named METHOD_FROM) with the same name as an instance method of INTERFACE_TYPE_TO, METHOD_FROM must be an instance method and satisfy interface method requirement.
The interface method requirement is the requirement whether a method is able to be assigned to an interface method.
INTERFACE_METHOD_TO = METHOD_FROM
This is a concept that converts the type−to−type assignment explained in Assignment Requirement to method−to−method assignment.
INTERFACE_METHOD_TO must be an instance method.
METHOD_FROM must be an instance method.
The length of the arguments of the method of the INTERFACE_METHOD_TO type must be greater than or equal to the length of the required arguments the method of the INSTANT_METHOD_TYPE_FROM type.
The every argument other than at 0 index of the method of the INSTANT_METHOD_TYPE_FROM must satisfy assignment requirement to the argument as the same index of the method of the INTERFACE_METHOD_TO without a data conversion and with interface exactly matched.
The return type of the method of the INSTANT_METHOD_TYPE_FROM must must satisfy assignment requirement to the return type of the method of the INTERFACE_METHOD_TO without a data conversion and with interface exactly matched.
|
• |
SPVM::Document::Language::Class |
|||
|
• |
SPVM::Document::Language::Operators |
|||
|
• |
SPVM::Document::Language::Statements |
|||
|
• |
SPVM::Document::Language |
|||
|
• |
SPVM::Document |
Copyright (c) 2023 Yuki Kimoto
MIT License