Name

SPVM::Document::Language::Class - Classes in the SPVM Language

Description

This document describes classes.

Class

The SPVM language is an object-oriented programing language and has class syntax.

This section describes class syntax.

See SPVM::Document::Language::SyntaxParsing about the grammer of the SPVM language.

Class Definition

The class keyword defines a class type.

class CLASS_NAME {

}

class CLASS_NAME;

class CLASS_NAME : ATTRIBUTES {

}

class CLASS_NAME : ATTRIBUTES;

CLASS_NAME is a class name.

ATTRIBUTES is one of

ATTRIBUTES ATTRIBUTE
ATTRIBUTE

ATTRIBUTE is a class attributre.

A class type is also simply called a class.

The new operator creates an object from a class.

Compilation Errors:

CLASS_NAME must be a class name. Otherwise, a compilation error occurs.

If more than one class is defined in a class file, a compilation error occurs.

Examples:

# Examepls of class definitions
class Point {

}

Class Attributes

The List of Class Attributes:

Class Attributes Descriptions
public This class is public. All classes are able to create an object of this class using the new operator.
private This class is private. Classes other than this class are not able to create an object of this class using the new operator. This is default.
protected This class is protected. Only this class and its child classes are able to create an object of this class using the new operator.
interface_t This class is an interface type. The class definition with this attribute is interpreted as an interface definiton.
mulnum_t This class is a multi-numeric type. The class definition with this attribute is interpreted as an multi-numeric type definiton.
pointer The class is a pointer class.
precompile The precompile method attribute of all methods other than a INIT block, methods generated by enumuration values, and methods that does not have their method block is specified.

Compilation Errors:

Only one of private, protected or public must be specified. Otherwise, a compilation error occurs.

If more than one of interface_t, mulnum_t, and pointer are specified, a compilation error occurs.

Examples:

# Examples of class attributes
class Point : public {

}

class Point : public pointer {

}

Pointer Class

The pointer class is a class with the pointer class attribute .

class CLASS_NAME : pointer {

}

An object of a pointer class has the pointer to a native address that is normally a pointer to a C language struct, or a C++ language struct or class.

Examples:

# Examples of the pointer attribute
class Foo : pointer {

}

Class File

A class file is a file where a class is defined.

The name of a class file is the same as a class name, but all :: are replaced to / and .spvm is added at the end of it.

# Foo
Foo.spvm

# Foo::Bar
Foo/Bar.spvm

# Foo::Bar::Baz
Foo/Bar/Baz.spvm

Compilation Errors:

A class definition must be written in its corresponding class file. Otherwise, a compilation error occurs.

Version Statement

The version statement declares the version of a class.

version VERSION_STRING;

This statement declares the version of a class given the version string VERSION_STRING.

Compilation Errors:

If the version has already been declared, a compilation error occurs.

VERSION_STRING must be a version string. Otherwise, a compilation error occurs.

Examples:

class MyClass {
  version "1.001";
}

class MyClass {
  version "10.001003";
}

Version String

The version string is the string that represents the version of a class.

A version string must be written by the following rules.

The type of a version string is the string type.

It consists of 0-9, ..

The count of . is less than or equal to 1.

It begins with 0-9.

It ends with 0-9.

The count of 0-9 after . is divisible by 3.

It is able to be parsed by the strtod function in the C language.

Complication Errors:

If a version string is not valid, a compilation error occurs.

Examples:

# Examples of version strings
"1.001"
"10.001003"
"1"
"10000"

Version Number

A version number is a floating point number created from a version string using the strtod function in the C language.

Examples:

# Examples of version numbers

# "1.001"
1.001

# "10.001003"
10.001003

use Statement

The use statemenet loads a class.

use BASIC_TYPE;
use BASIC_TYPE as CLASS_NAME;

This statement searches for the type BASIC_TYPE in class search directories from the beginning, and if found, loads the type.

BASIC_TYPE is a class type, an interface type, or a multi-numeric type.

See the require statement about how to load a type without causing a compile error when loading fails,

The follwoing use statement with as

use BASIC_TYPE as CLASS_NAME;

is expanded to the following code using the alias statement.

use BASIC_TYPE;
alias BASIC_TYPE as CLASS_NAME

Compilation Errors:

BASIC_TYPE must be a class name. Otherwise, a compilation error occurs.

CLASS_NAME must be a class name. Otherwise, a compilation error occurs.

If BASIC_TYPE does not found, a compilation error occurs.

Examples:

# Examples of the use statement
class MyClass {
  use Foo;
}

class MyClass {
  use Sys::Socket::Constant as SOCKET;
}

Class Search Directories

Class search directories are directories in which classes are searched for.

These are set outside the program.

Directories set by the -I option of the spvm command and the spvmcc command are added to class search directories.

And directories with /SPVM added to the end of each value of Perl's @INC are added to the end of the class search directories.

Default Loaded Classes

The following classes are loaded by default.

alias Statement

The alias statemenet creates an alias name for a type.

alias BASIC_TYPE as CLASS_NAME;

This statemenet creates an alias name CLASS_NAME for a type BASIC_TYPE.

BASIC_TYPE is a class type, an interface type, or a multi-numeric type.

Compilation Errors:

BASIC_TYPE must be a class name. Otherwise, a compilation error occurs.

CLASS_NAME must be a class name. Otherwise, a compilation error occurs.

Examples:

class MyClass {
  use Sys::Socket::Constant;
  alias Sys::Socket::Constant as SOCKET;
  
  SOCKET->AF_INET;
}

allow Statement

The allow statemenet allows a type private accesses to the current class.

allow BASIC_TYPE;

By default, objects of private classes cannot be created by the the new operator from other classes.

And private methods, private fields, and private class variables cannot be accessed from other classes.

This statement allows the type BASIC_TYPE private accesses to the current class.

BASIC_TYPE is loaded by the use statement.

Examples:

# Allowing private access
class MyClass {
  allow SomeClass;
}

Inheritance

A class inherits a class using the extends keyword.

class CLASS_NAME extends PARENT_CLASS_NAME {
  
}

The class CLASS_NAME that inherits the parent class PARENT_CLASS_NAME is defined.

The field definitions of the parent class PARENT_CLASS_NAME are added to the end of the field definitions of the class CLASS_NAME to the end of its fields.

The interface statements of the parent class PARENT_CLASS_NAME are added to the end of the interface statements of the class CLASS_NAME.

The instance of the class CLASS_NAME can calls instance methods of the parent class PARENT_CLASS_NAME and its super classes.

Compilation Errors:

The parent class PARENT_CLASS_NAME must be a class type. Otherwise, a compilation error occurs.

The name of the parant class must be different from the name of the class. Otherwise, a compilation error occurs.

The all super classes must be different from its own class. Otherwise, a compilation error occurs.

The field that name is the same as the field of the super class cannnot be defined. Otherwise, a compilation error occurs.

The class CLASS_NAME interpreted by an interface must satisfy the interface requirement to the parent class PARENT_CLASS_NAME. Otherwise, a compilation error occurs.

Examples:

class Point3D extends Point {
  
  has z : rw protected int;
  
  static method new : Point3D ($x : int = 0, $y : int = 0, $z : int = 0) {
    my $self = new Point3D;
    
    $self->{x} = $x;
    $self->{y} = $y;
    $self->{z} = $z;
    
    return $self;
  }
  
  method clear : void () {
    $self->SUPER::clear;
    $self->{z} = 0;
  }
  
  method to_string : string () {
    my $x = $self->x;
    my $y = $self->y;
    my $z = $self->z;
    
    my $string = "($x,$y,$z)";
    
    return $string;
  }
  
  method clone : object () {
    my $self_clone = Point3D->new($self->x, $self->y, $self->z);
    
    return $self_clone;
  }
}

Interface

This section describes interfaces.

Interface Definition

A class definition with the interface_t class attribute defines an interface type.

class CLASS_NAME : interface_t {
  
}

An interface type is also simply called an interface.

Objects cannot be created from interface types.

Normally, an interface has interface methods.

Compilation Errors:

An interface cannnot have field definitions. If so, a compilation error occurs.

An interface cannnot have class variable definitions. If so, a compilation error occurs.

Examples:

# Examples of interface definitions
class TestCase::Pointable : interface_t {
  interface Stringable;
  
  method x : int ();
  method y : int();
  method to_string : string ();
}

class Stringable: interface_t {
  method to_string : string ();
  method call_to_string : string () {
    return "foo " . $self->to_string;
  }
}

class Stringable: interface_t {
  required method to_string : string ();
  method foo : int ($num : long);
}

interface Statement

The interface statement checks if the current class satisfies the interface requirement to an interface.

interface BASIC_TYPE;

BASIC_TYPE is loaded by the use statement.

Compilation Errors:

The interface type BASIC_TYPE must be an interface type, ohterwise a compilation error occurs.

The current class must satisfy the interface requirement to the interface type BASIC_TYPE, ohterwise a compilation error occurs.

Examples:

# Examples of the interface statement
class Point {
  interface Stringable;
  
  method to_string : string () {
    my $x = $self->x;
    my $y = $self->y;
    
    my $string = "($x,$y)";
    
    return $string;
  }
}

Anon Class

An anon class is a class without specifying its class name.

Anon Class Definition

An anon class is defined by the following syntax.

class {

}

An anon class cannot be defined in a class file. It is able to be defined in a source code compiled by the compile_anon_class compiler native API.

An anon class has its class name, such as eval::anon_class::0, eval::anon_class::1, eval::anon_class::2.

eval::anon_class::123

Examples:

# Examples of the anon class definition
class {
  static method sum : int ($num1 : int, $num2 : int) {
    return $num1 + $num2;
  }
}

Anon Method Class

An anon method class is a class defined by an anon method operator.

Anon Method Class Definition

The anon method class definition has the following syntax.

ANON_METHOD_CLASS_FIELD_DEFINITION METHOD_DEFINITION

ANON_METHOD_CLASS_FIELD_DEFINITION is an anon method class field definition.

METHOD_DEFINITION is a method definition.

The anon method class definition defines a class to which this method belongs.

The name of this class is a string that joins the outmost class, a string "anon_method", the line number and the position of columns where an anon method class definition is written with ::.

MyClass::anon_method::3::23

An anon method class has the same access control as its outmost class.

An anon method class has the same alias names as its outmost class.

Compilation Errors:

The method name of METHOD_DEFINITION must be an empty string. Otherwise, a compilation error occurs.

The method must be an instance method. Otherwise, a compilation error occurs.

Examples:

# Examples of the anon method definition
class Foo::Bar {
  method my_method : void () {
    my $comparator = (Comparator)method : int ($x1 : object, $x2 : object) {
      my $point1 = (Point)$x1;
      my $point2 = (Point)$x2;
      
      return $point1->x <=> $point2->x;
    };
  }
}

Anon Method Class Field Definition

An anon method class field definition defines fields of an anon class, which is a part of the anon method class definition.

[ANON_METHOD_CLASS_FIELD_DEFINITION_ITEM1, ANON_METHOD_CLASS_FIELD_DEFINITION_ITEM, ANON_METHOD_CLASS_FIELD_DEFINITION_ITEMn]

ANON_METHOD_CLASS_FIELD_DEFINITION_ITEM are one of

has FIELD_NAME : TYPE
has FIELD_NAME : TYPE = OPERAND
VAR : TYPE

has FIELD_NAME : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE
has FIELD_NAME : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE = OPERAND
VAR : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE

FIELD_NAME is a field name.

TYPE is a type.

OPERAND is an operator.

VAR : TYPE is expaneded to has FIELD_NAME : TYPE = OPERAND. FIELD_NAME is the same as the name of VAR, but $ is removed. OPERAND is VAR.

ATTRIBUTE is a field attribute.

Compilation Errors:

Compilation errors caused by Field Definition could occur.

Examples:

# Examples of the anon method class field definition
class Foo::Bar {
  method my_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};
      
      say "$foo";
      say "$bar";
    };
  }
}

# More simple
class Foo::Bar {
  method my_method : void () {
    my $foo = 1;
    my $bar = 5L;
    
    my $comparator = (Comparator)[$foo : int, $bar : long] method : int ($x1 : object, $x2 : object) {
      say "$foo";
      say "$bar";
    };
  }
}

Outmost Class

An outmost class is the outmost defined class. This is the same as the class defined by the class definition.

class OutmostClass {
  
}

Examples:

# Examples of the outmost class
class MyClass {
  static method my_method : void () {
    
    # The __PACKAGE__ operator returns the outmost class "MyClass".
    my $outmost_class_name = __PACKAGE__;
    
    # Anon method
    my $cb = method : void () {
      
      # The __PACKAGE__ operator returns the outmost class "MyClass".
      my $outmost_class_name = __PACKAGE__;
    };
  }
}

Multi-Numeric Type Definition

A multi-numeric type is defined by the class definition with the mulnum_t class attribute.

class CLASS_NAME : mulnum_t {
  
}

Compilation Errors:

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

The types of all fields must be a same type. Otherwise, a compilation error occurs.

The length of fields must be less than or equal to 255. Otherwise, a compilation error occurs.

CLASS_NAME must ends with a multi-numeric type suffix corresponding to the type of fields. Otherwise, a compilation error occurs.

FIELD_LENGTH of the multi-numeric type suffix must be the same as the length of fields. Otherwise, a compilation error occurs.

TYPE_SUFFIX of the multi-numeric type suffix must correspond to the type of fields.

Examples:

# Examples of the multi-numeric type definition
class Complex_2f : mulnum_t {
  re : float;
  im : float;
}

class Complex_2d : mulnum_t {
  re : double;
  im : double;
}

class Quaternion_4f : mulnum_t {
  re : float;
  i : float;
  j : float;
  k : float;
}

class Quaternion_4d : mulnum_t {
  re : double;
  i : double;
  j : double;
  k : double;
}

Multi-Numeric Type Suffix

The multi-numeric type must end with the following suffix.

_(non-spaces)FIELD_LENGTH(non-spaces)TYPE_SUFFIX

FIELD_LENGTH is the length of fields.

TYPE_SUFFIX is a type suffix.

The List of Type Suffixes:

Numeric Types Type Suffixes
byte b
short s
int i
long l
float f
double d

Examples:

# Examples of the multi-numeric type suffix
_2f;
_2d;
_4f;
_4d;

Class Variable

A class variable is a global variable that belongs to a class.

Class Variable Definition

our keyword defines a class variable.

our CLASS_VARIABLE_NAME : OPT_ATTRIBUTES TYPE;

CLASS_VARIABLE_NAME is a class variable name that does not contains ::.

TYPE is a type.

OPT_ATTRIBUTES is one of

EMPTY
ATTRIBUTES

EMPTY means nothing exists.

ATTRIBUTES is one of

ATTRIBUTES ATTRIBUTE
ATTRIBUTE

ATTRIBUTE is a class variable attribute.

Each class variable is initialized by its type initial value.

The value of a class variable is got by the operation of getting a class variable.

The value of a class variable is set by the operation of setting a class variable.

Compilation Errors:

CLASS_VARIABLE_NAME must a class variable name that does not contains ::. Otherwise, a compilation error occurs.

TYPE must be a numeric type or an object type.

ATTRIBUTE must be a class variable attribute. Otherwise, a compilation error occurs.

If two or more class variables that has a same name are defined, a compilation error occurs.

Examples:

class MyClass {
  our $NUM_BYTE : byte;
  our $NUM_SHORT : short;
  our $NUM_INT : int;
  our $NUM_LONG : long;
  our $NUM_FLOAT : float;
  our $NUM_DOUBLE : double;
  our $POINT : Point;
  
  our $NUM_PUBLIC : public int;
  our $NUM_RO : ro int;
  our $NUM_RW : rw int;
}

Class Variable Attributes

The List of Class Variable Attributes:

Class Variable Attributes Descriptions
public This class variable is public. All classes can access this class variable.
private This class variable is private. All classes ohter than this class cannnot access this class variable. This is default.
protected This class variable is protected. All classes ohter than this class and its child classes cannot access this class variable.
ro This class variable defines a getter method.
wo This class variable defineds a setter method.
rw This class variable defines a getter method and a setter method.

Compilation Errors:

One of private, protected and public must be specified. Otherwise, a compilation error occurs.

One of ro, wo, and rw must be specified. Otherwise, a compilation error occurs.

Class Variable Getter Method

A class variable getter method is a method to perform the operation of getting a class variable.

This method is a class method that has no arguments.

If the type of the class variable is the byte type or the short type, the return type is the int type. Otherwise, it is the same type as the class variable.

The method name is the same as the class variable name, but $ is removed. For example, if the class variable name is $FOO, the method name is FOO.

Examples:

# Examples of class variable getter methods
class MyClass {
  our $NUM : ro int;
  
  static method main : void {
    my $num = Foo->NUM;
  }
}

Class Variable Setter Method

A class variable setter method is a method to perform the operation of setting a class variable.

This method is a class method that has an argument.

If the type of the class variable is the byte type or the short type, the argument type is the int type. Otherwise, it is the same type as the class variable.

The return type is the void type.

The method name is the same as the class variable name, but $ is removed and SET_ is added to the beginning of it. For example, if the class variable name is $FOO, the method name is SET_FOO.

Examples:

# Examples of class variable setter methods
class MyClass {
  our $NUM : wo int;
  
  static method main : void {
    Foo->SET_NUM(3);
  }
}

Field

A field is a data member of a class.

Field Definition

The has keyword defines a field.

has FIELD_NAME : OPT_ATTRIBUTES TYPE;

FIELD_NAME is a field name.

TYPE is a type.

OPT_ATTRIBUTES is one of

EMPTY
ATTRIBUTES

EMPTY means nothing exists.

ATTRIBUTES is one of

ATTRIBUTES ATTRIBUTE
ATTRIBUTE

ATTRIBUTE is a field attribute.

Each field of an object is initialized by its type initial value when the object is created.

The value of a field is got by the operation of getting a field.

The value of a field is set by the operation of setting a field.

Compilation Errors:

FIELD_NAME must a field name. Otherwise, a compilation error occurs.

TYPE must be a numeric type or an object type.

ATTRIBUTE must be a field attribute. Otherwise, a compilation error occurs.

If two or more fields that has a same name are defined, a compilation error occurs.

Examples:

class MyClass {
  has num_byte : byte;
  has num_short : short;
  has num_int : int;
  has num_long : long;
  has num_float : float;
  has num_double : double;
  
  has num_public : public int;
  has num_ro : ro int;
  has num_rw : rw int;
}

Field Attributes

The List of Field Attributes:

Field Attributes Descriptions
public This field is public. All classes can access this field.
private This field is private. All classes ohter than this class cannnot access this field. This is default.
protected This field is protected. All classes ohter than this class and its child classes cannot access this field.
ro This field defines a getter method.
wo This field defineds a setter method.
rw This field defines a getter method and a setter method.

Compilation Errors:

One of private, protected and public must be specified. Otherwise, a compilation error occurs.

One of ro, wo, and rw must be specified. Otherwise, a compilation error occurs.

Field Getter Method

A field getter method is a method to perform the operation of getting a field.

This method is an instance method that has no arguments.

If the type of the field is the byte type or the short type, the return type is the int type. Otherwise, it is the same type as the field.

The method name is the same as the field name. For example, if the field name is foo, the method name is foo.

Examples:

# Examples of field getter methods
class MyClass {
  has num : ro int;
  
  static method new {
    return new Foo;
  }
  
  static method main : void {
    my $foo = Foo->new;
    
    my $num = $foo->num;
  }
}

Field Setter Method

A field setter method is a method to perform the operation of setting a field.

This method is an instance method that has an argument.

If the type of the field is the byte type or the short type, the argument type is the int type. Otherwise, it is the same type as the field.

The return type is the void type.

The method name is the same as the field name, but set_ is added to the beginning of it. For example, if the field name is foo, the method name is set_foo.

Examples:

# Examples of field setter methods
class MyClass {
  has num : wo int;
  
  static method new {
    return new Foo;
  }
  
  static method main : void {
    my $foo = Foo->new;
    
    $foo->set_num(3);
  }
}

Method

Method Definition

The method keyword defines a method.

OPT_ATTRIBUTES method METHOD_NAME : RETURN_TYPE (OPT_ARGS) { }
OPT_ATTRIBUTES method METHOD_NAME : RETURN_TYPE (OPT_ARGS);

OPT_ATTRIBUTES is one of

EMPTY
ATTRIBUTES

EMPTY means nothing exists.

ATTRIBUTES is one of

ATTRIBUTES ATTRIBUTE
ATTRIBUTE

ATTRIBUTE is a method attribute.

METHOD_NAME is a method name.

RETURN_TYPE is a type.

ARG_ITEM is one of

ARG_NAME : TYPE
ARG_NAME : TYPE = VALUE

OPT_ATTRIBUTES is one of

EMPTY
ARGS

EMPTY means nothing exists.

ARGS is one of

ARGS , ARG
ARG

ARG is one of

ARG_NAME : ARG_TYPE
ARG_NAME : ARG_TYPE = VALUE

ARG_NAME is a local variable name.

ARG_TYPE is a type.

VALUE is a literal or undef.

A method with the static method attribute is a class method.

A method without the static method attribute is an instance method.

An argument with VALUE is an optional arguments. If a method call dose not give a value to an optional argument, VALUE is given to the argument.

A interface method defined in an interface is an interface method.

A method with the native method attribute is a native method.

Compilation Errors:

The count of ARGS must be less than or equal to 255. Otherwise, a compilation error occurs.

ARG_TYPE must be a numeric type, a multi-numeric type, an object type, or a reference type. Otherwise, a compilation error occurs.

RETURN_TYPE must be the void type, a numeric type, a multi-numeric type or an object type. Otherwise, a compilation error occurs.

Methods other than interface methods and native methods must have a method block. Otherwise, a compilation error occurs.

VALUE must satisfy assignment requirement to ARG_TYPE. Otherwise, a compilation error occurs.

METHOD_NAME must not be INIT. Otherwise, a compilation error occurs.

A non-optional argument must not be placed after an optional argument. Otherwise, a compilation error occurs.

Examples:

class MyClass {
  # Class method
  static method substr : string ($string : string, $offset : int, $length : int = -1) {
    # ...
  }
  
  # Instance method
  method clear : void () {
    
  }
}

Method Attributes

The List of Method Attributes:

Method Attributes Descriptions
public This method is public. All classes can call this method. This is default.
private This method is private. All classes ohter than this class cannnot call this method.
protected This method is protected. All classes ohter than this class and its child classes cannot call this method.
precompile This method is a precompilation method.
native This method is a native method.
required A method that implements this interface method must be defined.

Compilation Errors:

One of private, protected and public must be specified. Otherwise, a compilation error occurs.

required must be specified to an interface method. Otherwise, a compilation error occurs.

Examples:

# private method
private method : int sum ($num1 : int, $num2 : int) {
  return $num1 + $num2;
}

# precompile method
precompile method : int sum ($num1 : int, $num2 : int) {
  return $num1 + $num2;
}

# native method
native method : int sum ($num1 : int, $num2 : int);

Class Method

A method defined with the static method attribute is a class method.

A class method can be called by a class method call.

Instance Method

A method defined without the static method attribute is an instance method.

An instance method can be called by an instance method call.

Interface Method

An instance method defined in an interface is an interface method.

Normally, an interface method does not have a method block.

method my_method : int ();

But, an interface method can have a method block.

method my_method : int () {
  
}

An interface method can have the required method attribute that indicates classes must define a method with the same name that implements this interface method.

required method my_method : int ();

INIT Method

A INIT method is a method that is executed just after a program starts.

INIT Method Definition

A INIT block defines a INIT method to be executed just after the program starts. A INIT method is called only once.

INIT {
  
}

A INIT method has no arguments.

The return type of a INIT method is the void type.

If a INIT method is not defined in a class, a INIT method without statements is defined.

If the current class has a parent class, the INIT method of the parent class is called at the beginning of the INIT method of the current class.

And INIT methods of classes loaded by use statements are called in loaded order.

class MyClass extends ParentClass {
  use Foo;
  use Bar;
  
  INIT {
    # INIT methods of the parent class and classes loaded by use statements are called automatically 
    ParentClass->INIT;
    Foo->INIT;
    Bar->INIT;
    
    # Do something
  }
}

The execution order of INIT methods is not guaranteed except that INIT methods are called explictly and except for the above explanation.

The INIT methods in the default loaded class are called before INIT methods of other classes,

Examples:

# Examples of INIT methods
class MyClass {
  use Point;
  
  our $NUM : int;
  our $STRING : string;
  our $POINT : Point;
  
  # INIT method
  INIT {
    $NUM = 3;
    $STRING = "abc";
    
    $POINT = Point->new(1, 2);
  }
}

Destructor

A destructor is a method called just before an object is destroyed.

method DESTROY : void () {

}

A destructor is an instance method.

The name of a destructor is DESTROY.

A destructor has no arguments.

The retrun type is the void type.

If an exception is thrown in a destructor, the exception is not thrown. Instead, a warning message is output to SPVM's standard error.

See also Garbage Collection.

Compilation Errors:

A destructor must be an instance method. Otherwise, a compilation error occurs.

The name of a destructor must be DESTROY. Otherwise, a compilation error occurs.

If a destructor has arguments, a compilation error occurs.

The retrun type must be the void type. Otherwise, a compilation error occurs.

If two or more destructor are defined, a compilation error occurs.

Examples:

# Destructor
class MyClass {
  method DESTROY : void () {
    say "DESTROY";
  }
}

Method Override

An instance method in a parent class can be overridden by an instance method with the same name in a child class.

class ChildClass extends ParentClass {
  # Method Override
  method clear : void () {
    # ...
  }
}

class ParentClass {
  
  method clear : void () {
    # ...
  }
}

The overridding method in the child class must satisfy the interface method requirement to the parent method interpretted as an interface method.

Compilation Errors:

The overridding method in the child class must satisfy the interface requirement to the parent method. Otherwise, a compilation error occurs.

Native Method

A method with the native method attribute is a native method.

native sum : int ($num1 : int, $num2 : int);

A native method has no method block.

The implementation of a native method is written by native languages such as the C language, C++.

See SPVM::Document::NativeClass about the way to write the implementation of a native method.

Precompilation Method

A method with the precompile method attribute is a precompilation method.

precompile sum : int ($num1 : int, $num2 : int) {

}

The source code of each precompilation method is converted to a C source code and it is compiled to the machine code.

And the machine codes of all precompilation methods in a class is linked to a dynamic link library such as MyClass.so on Linux/Unix, MyClass.dll on Windows, MyClass.dylib on Mac.

The address of machine code of each precompilation method in the dynamic link library is bind to a precompilation method.

Precompilation methods need a build directory such as ~/.spvm_build to compile and link them.

Bootstrap Method

A bootstrap method is a method where a program start.

void main : void () {
  
}

The method name is main.

The return type is the void type.

It has no arguments.

Method Implementation

Normally a method has a method block. Statements can be written in a method block.

static method foo : int ($num1 : int, $num2 : int) {
  
  my $total = $num1 + $num2;
  
  return $total;
}

Local Variable

A local variable is a variable that has a scope.

Local Variable Declaration

A my keyword declares a local variable.

my LOCAL_VARIABLE_NAME
my LOCAL_VARIABLE_NAME : TYPE
my LOCAL_VARIABLE_NAME = VALUE
my LOCAL_VARIABLE_NAME : TYPE = VALUE

LOCAL_VARIABLE_NAME is a local variable name.

TYPE is a type.

If TYPE is ommited, TYPE is set to the type of VALUE. This is called type inference.

VALUE is an operator.

A local variable is declared in a scope block.

A local variable declaration performs the operation of pushing a local variable on the mortal stack.

A local variable declaration is a local variable access.

Compilation Errors:

LOCAL_VARIABLE_NAME must be a local variable name. Otherwise a compilation error occurs.

TYPE must be a numeric type, an object type, the multi-numeric type, or a reference type. Otherwise a compilation error occurs.

If TYPE is not resolved, a compilation error occurs.

Examples:

# Examples of local variable declarations
my $var : int;
my $var : Point;
my $var : Complex_2d;
my $var : int*;

my $num = 1;

my $num = 1.0;

my $ppp = my $bar = 4;

if (my $bar = 1) {

}

while (my $bar = 1) {

}

Type Inference

If the type of the local variable declaration is ommited, the type of the local variable is set to the type of the right operand of the assignment operator. This is called type inference.

# int
my $num = 1;

# double
my $num = 1.0;

# Foo
my $foo = new Foo;

Enumeration

Enumeration is a syntax that defines 32-bit integers that belongs to a class.

Enumeration Definition

The enum keyword defines an enumeration.

enum {
  ITEM1,
  ITEM2,
  ITEMn
}

, after the last enumeration item is allowed.

enum {
  ITEM1,
  ITEM2,
  ITEM3,
}

ITEM is one of

NAME
NAME = VALUE

NAME is a method name.

VALUE is an integer literal within the int type. VALUE is converted the value of the int type.

If VALUE of ITEM1 is omitted, it is set to 0.

If VALUE of ITEMn is ommited, it is set to the value of the previous item plus 1.

Each enumeration item is converted to a method definition that defines a class method that returns the value of the enumeration item.

The return type of the method is the int type.

Compilation Errors:

NAME must be a method name. Otherwise, a compilation error occurs.

VALUE must be an integer literal within the int type. Otherwise, a compilation error occurs.

Examples:

# Examples of enumerations
class MyClass {
  enum {
    FLAG1,
    FLAG2,
    FLAG3,
  }
  
  static method main : void () {
    my $flag1 = MyClass->FLAG1;
  }
}

class MyClass {
  enum {
    FLAG1,
    FLAG2 = 2,
    FLAG3,
  }
}

Enumeration Attributes

The List of Enumeration Attributes:

Attributes Descriptions
public This enumeration is public. All classes can access all items of this enumeration. This is default.
private This enumeration is private. All classes ohter than this class cannnot access all items of this enumeration.
protected This enumeration is protected. All classes ohter than this class and its child classes cannot access all items of this enumeration.

Compilation Errors:

One of private, protected and public must be specified. Otherwise, a compilation error occurs.

Block

A block is the part enclosed by { and }.

Class Block

A class block is a block used in a class definition.

class MyClass {

}

Enumeration Block

An enumeration block is a block used in an enumeration definition.

enum {
  ONE,
  TWO,
}

Scope Block

A scope block is a block that has its scope.

Zero or more statements are written in a scope block.

Simple Block

A simple block is a scope block.

{
  1;
}

A simple block must have at least one statements. Otherwise, it is intepreted as a key-value array initialization.

Method Block

A method block is a scope block.

static method foo : int () {

}

INIT block

An INIT block is a scope block.

INIT {
  
}

eval Block

An eval block is a scope block.

eval {

}

if Block

An if block is a scope block.

if (CONDITION) {

}

elsif Block

An elsif block is a scope block.

elsif (CONDITION) {

}

else Block

An else block is a scope block.

else {

}

for Block

A for block is a scope block.

for (my $i = 0; $i < 3; $i++) {

}

while Block

A while block is a scope block.

while (CONDITION) {

}

switch Block

A switch block is a scope block.

switch (CONDITION) {

}

case Block

A case block is a scope block.

case CASE_VALUE1: {
  # ...
}

default Block

A default block is a scope block.

default: {
  # ...
}

Type Comment

The type comment is a syntax to write a comment for a type.

TYPE of TYPE_COMMENT
TYPE of TYPE_COMMENT1|TYPE_COMMENT2|TYPE_COMMENTn

TYPE is a type.

TYPE is a type used in the field definition, the class variable definition, the local variable declaration, and the return value and the types of arguments of the method definition.

TYPE_COMMENT is a type.

Examples:

# Examples of type comments
has points : 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 type comment is not found, a compilation error occurs.

See Also

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License