« blib/lib/SPVM/Document/Language.pm
↑ Summary
blib/lib/SPVM/Document/Language/ExceptionHandling.pm »
| 1 | =head1 Name | ||
| 2 | |||
| 3 | SPVM::Document::Language::Class - Classes in the SPVM Language | ||
| 4 | |||
| 5 | =head1 Description | ||
| 6 | |||
| 7 | This document describes classes. | ||
| 8 | |||
| 9 | =head1 Class | ||
| 10 | |||
| 11 | The SPVM language is an object-oriented programing language and has class syntax. | ||
| 12 | |||
| 13 | This section describes class syntax. | ||
| 14 | |||
| 15 | See L<SPVM::Document::Language::SyntaxParsing> about the grammer of the SPVM language. | ||
| 16 | |||
| 17 | =head2 Class Definition | ||
| 18 | |||
| 19 | The C<class> keyword defines a L<class type|SPVM::Document::Language::Types/"Class Types">. | ||
| 20 | |||
| 21 | class CLASS_NAME { | ||
| 22 | |||
| 23 | } | ||
| 24 | |||
| 25 | class CLASS_NAME; | ||
| 26 | |||
| 27 | class CLASS_NAME : ATTRIBUTES { | ||
| 28 | |||
| 29 | } | ||
| 30 | |||
| 31 | class CLASS_NAME : ATTRIBUTES; | ||
| 32 | |||
| 33 | I<CLASS_NAME> is a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. | ||
| 34 | |||
| 35 | I<ATTRIBUTES> is one of | ||
| 36 | |||
| 37 | ATTRIBUTES ATTRIBUTE | ||
| 38 | ATTRIBUTE | ||
| 39 | |||
| 40 | I<ATTRIBUTE> is a L<class attributre|"Class Attributes">. | ||
| 41 | |||
| 42 | A class type is also simply called a class. | ||
| 43 | |||
| 44 | The L<new operator|/"new Operator"> creates an object from a class. | ||
| 45 | |||
| 46 | Compilation Errors: | ||
| 47 | |||
| 48 | I<CLASS_NAME> must be a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. Otherwise, a compilation error occurs. | ||
| 49 | |||
| 50 | If more than one class is defined in a L<class file|/"Class File">, a compilation error occurs. | ||
| 51 | |||
| 52 | Examples: | ||
| 53 | |||
| 54 | # Examepls of class definitions | ||
| 55 | class Point { | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | =head2 Class Attributes | ||
| 60 | |||
| 61 | The List of Class Attributes: | ||
| 62 | |||
| 63 | =begin html | ||
| 64 | |||
| 65 | <table> | ||
| 66 | <tr> | ||
| 67 | <th> | ||
| 68 | Class Attributes | ||
| 69 | </th> | ||
| 70 | <th> | ||
| 71 | Descriptions | ||
| 72 | </th> | ||
| 73 | </tr> | ||
| 74 | <tr> | ||
| 75 | <td> | ||
| 76 | <b>public</b> | ||
| 77 | </td> | ||
| 78 | <td> | ||
| 79 | This class is public. All classes are able to create an object of this class using the <a href="https://metacpan.org/pod/SPVM::Document::Language::Operators#new-Operator">new operator</a>. | ||
| 80 | </td> | ||
| 81 | </tr> | ||
| 82 | <tr> | ||
| 83 | <td> | ||
| 84 | <b>private</b> | ||
| 85 | </td> | ||
| 86 | <td> | ||
| 87 | This class is private. Classes other than this class are not able to create an object of this class using the <a href="https://metacpan.org/pod/SPVM::Document::Language::Operators#new-Operator">new operator</a>. | ||
| 88 | </td> | ||
| 89 | </tr> | ||
| 90 | <tr> | ||
| 91 | <td> | ||
| 92 | <b>protected</b> | ||
| 93 | </td> | ||
| 94 | <td> | ||
| 95 | This class is protected. Only this class and its child classes are able to create an object of this class using the <a href="https://metacpan.org/pod/SPVM::Document::Language::Operators#new-Operator">new operator</a>. This is default. | ||
| 96 | </td> | ||
| 97 | </tr> | ||
| 98 | <tr> | ||
| 99 | <td> | ||
| 100 | <b>interface_t</b> | ||
| 101 | </td> | ||
| 102 | <td> | ||
| 103 | This class is an <a href="https://metacpan.org/pod/SPVM::Document::Language::Types#Interface-Types">interface type</a>. The class definition with this attribute is interpreted as an <a href="#Interface-Definition">interface definiton</a>. | ||
| 104 | </td> | ||
| 105 | </tr> | ||
| 106 | <tr> | ||
| 107 | <td> | ||
| 108 | <b>mulnum_t</b> | ||
| 109 | </td> | ||
| 110 | <td> | ||
| 111 | This class is a <a href="https://metacpan.org/pod/SPVM::Document::Language::Types#Multi-Numeric-Types">multi-numeric type</a>. The class definition with this attribute is interpreted as an <a href="#Multi-Numeric-Type-Definition">multi-numeric type definiton</a>. | ||
| 112 | </td> | ||
| 113 | </tr> | ||
| 114 | <tr> | ||
| 115 | <td> | ||
| 116 | <b>pointer</b> | ||
| 117 | </td> | ||
| 118 | <td> | ||
| 119 | The class is a <a href="#Pointer-Class">pointer class</a>. | ||
| 120 | </td> | ||
| 121 | </tr> | ||
| 122 | <tr> | ||
| 123 | <td> | ||
| 124 | <b>precompile</b> | ||
| 125 | </td> | ||
| 126 | <td> | ||
| 127 | The <code>precompile</code> method attribute of all methods other than <code>INIT</code> method, methods generated by enumuration values, and methods that does not have their method block is specified. | ||
| 128 | </td> | ||
| 129 | </tr> | ||
| 130 | </table> | ||
| 131 | |||
| 132 | =end html | ||
| 133 | |||
| 134 | Compilation Errors: | ||
| 135 | |||
| 136 | Only one of C<private>, C<protected> or C<public> must be specified. Otherwise, a compilation error occurs. | ||
| 137 | |||
| 138 | If more than one of C<interface_t>, C<mulnum_t>, and C<pointer> are specified, a compilation error occurs. | ||
| 139 | |||
| 140 | Examples: | ||
| 141 | |||
| 142 | # Examples of class attributes | ||
| 143 | class Point : public { | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | class Point : public pointer { | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | =head3 Pointer Class | ||
| 152 | |||
| 153 | The pointer class is a class with the C<pointer> L<class attribute|/"Class Attributes"> . | ||
| 154 | |||
| 155 | class CLASS_NAME : pointer { | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | 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. | ||
| 160 | |||
| 161 | Examples: | ||
| 162 | |||
| 163 | # Examples of the pointer attribute | ||
| 164 | class Foo : pointer { | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | =head2 Class File | ||
| 169 | |||
| 170 | A class file is a file where a class is defined. | ||
| 171 | |||
| 172 | The name of a class file is the same as a class name, but all C<::> are replaced to C</> and C<.spvm> is added at the end of it. | ||
| 173 | |||
| 174 | # Foo | ||
| 175 | Foo.spvm | ||
| 176 | |||
| 177 | # Foo::Bar | ||
| 178 | Foo/Bar.spvm | ||
| 179 | |||
| 180 | # Foo::Bar::Baz | ||
| 181 | Foo/Bar/Baz.spvm | ||
| 182 | |||
| 183 | Compilation Errors: | ||
| 184 | |||
| 185 | A class definition must be written in its corresponding class file. Otherwise, a compilation error occurs. | ||
| 186 | |||
| 187 | =head2 version Statement | ||
| 188 | |||
| 189 | The C<version> statement declares the version of a class. | ||
| 190 | |||
| 191 | version VERSION_STRING; | ||
| 192 | |||
| 193 | This statement declares the version of a class given L<version string|/"Version String"> I<VERSION_STRING>. | ||
| 194 | |||
| 195 | Compilation Errors: | ||
| 196 | |||
| 197 | If the version has already been declared, a compilation error occurs. | ||
| 198 | |||
| 199 | I<VERSION_STRING> must be a L<version string|/"Version String">. Otherwise, a compilation error occurs. | ||
| 200 | |||
| 201 | Examples: | ||
| 202 | |||
| 203 | class MyClass { | ||
| 204 | version "1.001"; | ||
| 205 | } | ||
| 206 | |||
| 207 | class MyClass { | ||
| 208 | version "10.001003"; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head3 Version String | ||
| 212 | |||
| 213 | The version string is the string that represents L<version|/"version Statement"> of a class. | ||
| 214 | |||
| 215 | A version string must be written by the following rules. | ||
| 216 | |||
| 217 | The type of a version string is string type. | ||
| 218 | |||
| 219 | It consists of C<0-9>, C<.>, and optionally the C<_000> suffix. | ||
| 220 | |||
| 221 | The count of C<.> is less than or equal to 1. | ||
| 222 | |||
| 223 | It begins with C<0-9>. | ||
| 224 | |||
| 225 | It ends with C<0-9>. | ||
| 226 | |||
| 227 | The count of C<0-9> after C<.> is divisible by 3. | ||
| 228 | |||
| 229 | It is able to be parsed by C<strtod> function in the C language. | ||
| 230 | |||
| 231 | Complication Errors: | ||
| 232 | |||
| 233 | If a version string is not valid, a compilation error occurs. | ||
| 234 | |||
| 235 | Examples: | ||
| 236 | |||
| 237 | # Examples of version strings | ||
| 238 | "1.001" | ||
| 239 | "10.001003" | ||
| 240 | "1" | ||
| 241 | "10000" | ||
| 242 | "0.001_000" | ||
| 243 | |||
| 244 | =head3 Version Number | ||
| 245 | |||
| 246 | A version number is a floating point number created from a L<version string|/"Version String"> using the C<strtod> function in the C language. | ||
| 247 | |||
| 248 | Examples: | ||
| 249 | |||
| 250 | # Examples of version numbers | ||
| 251 | |||
| 252 | # "1.001" | ||
| 253 | 1.001 | ||
| 254 | |||
| 255 | # "10.001003" | ||
| 256 | 10.001003 | ||
| 257 | |||
| 258 | =head2 version_from Statement | ||
| 259 | |||
| 260 | The C<version_from> statement allows you to make the version of the current class dependent on another class. | ||
| 261 | |||
| 262 | version_from CLASS_NAME; | ||
| 263 | |||
| 264 | I<CLASS_NAME> is a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. | ||
| 265 | |||
| 266 | It means the version of the current class is considered to be the same as the version of the class specified by I<CLASS_NAME>. | ||
| 267 | |||
| 268 | The class specified by I<CLASS_NAME> is loaded by L<use statement|/"use Statement">. | ||
| 269 | |||
| 270 | Compilation Errors: | ||
| 271 | |||
| 272 | If version_from has already been declared, a compilation error occurs. | ||
| 273 | |||
| 274 | Examples: | ||
| 275 | |||
| 276 | class MyClass::Foo { | ||
| 277 | version_from MyClass; | ||
| 278 | } | ||
| 279 | |||
| 280 | class MyClass { | ||
| 281 | version "1.001"; | ||
| 282 | } | ||
| 283 | |||
| 284 | =head2 use Statement | ||
| 285 | |||
| 286 | The C<use> statemenet loads a class. | ||
| 287 | |||
| 288 | use BASIC_TYPE; | ||
| 289 | use BASIC_TYPE as CLASS_NAME; | ||
| 290 | |||
| 291 | This statement searches for the type I<BASIC_TYPE> in L<class search directories|/"Class Search Directories"> from the beginning, and if found, loads the type. | ||
| 292 | |||
| 293 | I<BASIC_TYPE> is a L<class type|SPVM::Document::Language::Types/"Class Types">, an L<interface type|SPVM::Document::Language::Types/"Interface Types">, or a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Types">. | ||
| 294 | |||
| 295 | See L<require statement|SPVM::Document::Language::Statements/"require Statement"> about how to load a type without causing a compile error when loading fails, | ||
| 296 | |||
| 297 | The follwoing C<use> statement with C<as> | ||
| 298 | |||
| 299 | use BASIC_TYPE as CLASS_NAME; | ||
| 300 | |||
| 301 | is expanded to the following code using L<alias|/"alias Statement"> statement. | ||
| 302 | |||
| 303 | use BASIC_TYPE; | ||
| 304 | alias BASIC_TYPE as CLASS_NAME | ||
| 305 | |||
| 306 | Compilation Errors: | ||
| 307 | |||
| 308 | I<BASIC_TYPE> must be a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. Otherwise, a compilation error occurs. | ||
| 309 | |||
| 310 | I<CLASS_NAME> must be a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. Otherwise, a compilation error occurs. | ||
| 311 | |||
| 312 | If I<BASIC_TYPE> does not found, a compilation error occurs. | ||
| 313 | |||
| 314 | Examples: | ||
| 315 | |||
| 316 | # Examples of use statement | ||
| 317 | class MyClass { | ||
| 318 | use Foo; | ||
| 319 | } | ||
| 320 | |||
| 321 | class MyClass { | ||
| 322 | use Sys::Socket::Constant as SOCKET; | ||
| 323 | } | ||
| 324 | |||
| 325 | =head2 Class Search Directories | ||
| 326 | |||
| 327 | Class search directories are directories in which classes are searched for. | ||
| 328 | |||
| 329 | These are set outside the program. | ||
| 330 | |||
| 331 | Directories set by C<-I> option of L<spvm> command and L<spvmcc> command are added to class search directories. | ||
| 332 | |||
| 333 | And directories with C</SPVM> added to the end of each value of Perl's L<@INC|https://perldoc.perl.org/perlvar#@INC> are added to the end of the class search directories. | ||
| 334 | |||
| 335 | =head3 Default Loaded Classes | ||
| 336 | |||
| 337 | The following classes are loaded by default. | ||
| 338 | |||
| 339 | =over 2 | ||
| 340 | |||
| 341 | =item * L<Byte|SPVM::Byte> | ||
| 342 | |||
| 343 | =item * L<Short|SPVM::Short> | ||
| 344 | |||
| 345 | =item * L<Int|SPVM::Int> | ||
| 346 | |||
| 347 | =item * L<Long|SPVM::Long> | ||
| 348 | |||
| 349 | =item * L<Float|SPVM::Float> | ||
| 350 | |||
| 351 | =item * L<Double|SPVM::Double> | ||
| 352 | |||
| 353 | =item * L<Bool|SPVM::Bool> | ||
| 354 | |||
| 355 | =item * L<Error|SPVM::Error> | ||
| 356 | |||
| 357 | =item * L<Error::System|SPVM::Error::System> | ||
| 358 | |||
| 359 | =item * L<Error::NotSupported|SPVM::Error::NotSupported> | ||
| 360 | |||
| 361 | =item * L<Error::Compile|SPVM::Error::Compile> | ||
| 362 | |||
| 363 | =item * L<CommandInfo|SPVM::CommandInfo> | ||
| 364 | |||
| 365 | =item * L<Address|SPVM::Address> | ||
| 366 | |||
| 367 | =item * L<Error::Compile|SPVM::Error::Compile> | ||
| 368 | |||
| 369 | =item * L<SPVM|SPVM::SPVM> | ||
| 370 | |||
| 371 | =item * L<Error::MethodCallNotPermitted|SPVM::Error::MethodCallNotPermitted> | ||
| 372 | |||
| 373 | =item * L<Stringable|SPVM::Stringable> | ||
| 374 | |||
| 375 | =item * L<Countable|SPVM::Countable> | ||
| 376 | |||
| 377 | =item * L<CallerInfo|SPVM::CallerInfo> | ||
| 378 | |||
| 379 | =item * L<Cloneable|SPVM::Cloneable> | ||
| 380 | |||
| 381 | =item * L<Error::FieldNotSpecified|SPVM::Error::FieldNotSpecified> | ||
| 382 | |||
| 383 | =back | ||
| 384 | |||
| 385 | =head2 alias Statement | ||
| 386 | |||
| 387 | The C<alias> statemenet creates an class alias name for a type. | ||
| 388 | |||
| 389 | alias BASIC_TYPE as CLASS_NAME; | ||
| 390 | |||
| 391 | This statemenet creates an class alias name I<CLASS_NAME> for a type I<BASIC_TYPE>. | ||
| 392 | |||
| 393 | I<BASIC_TYPE> is a L<class type|SPVM::Document::Language::Types/"Class Types">, an L<interface type|SPVM::Document::Language::Types/"Interface Types">, or a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Types">. | ||
| 394 | |||
| 395 | Compilation Errors: | ||
| 396 | |||
| 397 | I<BASIC_TYPE> must be a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. Otherwise, a compilation error occurs. | ||
| 398 | |||
| 399 | I<CLASS_NAME> must be a L<class name|SPVM::Document::Language::Tokenization/"Class Name">. Otherwise, a compilation error occurs. | ||
| 400 | |||
| 401 | Examples: | ||
| 402 | |||
| 403 | class MyClass { | ||
| 404 | use Sys::Socket::Constant; | ||
| 405 | alias Sys::Socket::Constant as SOCKET; | ||
| 406 | |||
| 407 | SOCKET->AF_INET; | ||
| 408 | } | ||
| 409 | |||
| 410 | =head2 allow Statement | ||
| 411 | |||
| 412 | The C<allow> statemenet allows a type private accesses to the current class. | ||
| 413 | |||
| 414 | allow BASIC_TYPE; | ||
| 415 | |||
| 416 | By default, objects of private classes cannot be created by L<new operator|/"new Operator"> from other classes. | ||
| 417 | |||
| 418 | And private methods, private fields, and private class variables cannot be accessed from other classes. | ||
| 419 | |||
| 420 | This statement allows the type I<BASIC_TYPE> private accesses to the current class. | ||
| 421 | |||
| 422 | I<BASIC_TYPE> is loaded by L<use statement|/"use Statement">. | ||
| 423 | |||
| 424 | Examples: | ||
| 425 | |||
| 426 | # Allowing private access | ||
| 427 | class MyClass { | ||
| 428 | allow SomeClass; | ||
| 429 | } | ||
| 430 | |||
| 431 | =head2 Inheritance | ||
| 432 | |||
| 433 | A class inherits a class using the C<extends> keyword. | ||
| 434 | |||
| 435 | class CLASS_NAME extends PARENT_CLASS_NAME { | ||
| 436 | |||
| 437 | } | ||
| 438 | |||
| 439 | The class I<CLASS_NAME> that inherits the parent class I<PARENT_CLASS_NAME> is defined. | ||
| 440 | |||
| 441 | The field definitions of the parent class I<PARENT_CLASS_NAME> are added to the end of the field definitions of the class I<CLASS_NAME> to the end of its fields. | ||
| 442 | |||
| 443 | The L<interface statements|/"interface Statement"> of the parent class I<PARENT_CLASS_NAME> are added to the end of L<interface statements|/"interface Statement"> of the class I<CLASS_NAME>. | ||
| 444 | |||
| 445 | The instance of the class I<CLASS_NAME> can calls instance methods of the parent class I<PARENT_CLASS_NAME> and its super classes. | ||
| 446 | |||
| 447 | Compilation Errors: | ||
| 448 | |||
| 449 | The parent class I<PARENT_CLASS_NAME> must be a L<class type|SPVM::Document::Language::Types/"Class Types">. Otherwise, a compilation error occurs. | ||
| 450 | |||
| 451 | The name of the parant class must be different from the name of the class. Otherwise, a compilation error occurs. | ||
| 452 | |||
| 453 | The all super classes must be different from its own class. Otherwise, a compilation error occurs. | ||
| 454 | |||
| 455 | The field that name is the same as the field of the super class cannnot be defined. Otherwise, a compilation error occurs. | ||
| 456 | |||
| 457 | The class I<CLASS_NAME> interpreted by an interface must satisfy L<interface requirement|SPVM::Document::Language::Types/"Interface Requirement"> to the parent class I<PARENT_CLASS_NAME>. Otherwise, a compilation error occurs. | ||
| 458 | |||
| 459 | Examples: | ||
| 460 | |||
| 461 | class Point3D extends Point { | ||
| 462 | |||
| 463 | has z : rw protected int; | ||
| 464 | |||
| 465 | static method new : Point3D ($x : int = 0, $y : int = 0, $z : int = 0) { | ||
| 466 | my $self = new Point3D; | ||
| 467 | |||
| 468 | $self->{x} = $x; | ||
| 469 | $self->{y} = $y; | ||
| 470 | $self->{z} = $z; | ||
| 471 | |||
| 472 | return $self; | ||
| 473 | } | ||
| 474 | |||
| 475 | method clear : void () { | ||
| 476 | $self->SUPER::clear; | ||
| 477 | $self->{z} = 0; | ||
| 478 | } | ||
| 479 | |||
| 480 | method to_string : string () { | ||
| 481 | my $x = $self->x; | ||
| 482 | my $y = $self->y; | ||
| 483 | my $z = $self->z; | ||
| 484 | |||
| 485 | my $string = "($x,$y,$z)"; | ||
| 486 | |||
| 487 | return $string; | ||
| 488 | } | ||
| 489 | |||
| 490 | method clone : object () { | ||
| 491 | my $self_clone = Point3D->new($self->x, $self->y, $self->z); | ||
| 492 | |||
| 493 | return $self_clone; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | =head2 Interface | ||
| 498 | |||
| 499 | This section describes interfaces. | ||
| 500 | |||
| 501 | =head3 Interface Definition | ||
| 502 | |||
| 503 | A L<class definition|/"Class Definition"> with the C<interface_t> L<class attribute|/"Class Attributes"> defines an L<interface type|SPVM::Document::Language::Types/"Interface Types">. | ||
| 504 | |||
| 505 | class CLASS_NAME : interface_t { | ||
| 506 | |||
| 507 | } | ||
| 508 | |||
| 509 | An interface type is also simply called an interface. | ||
| 510 | |||
| 511 | Objects cannot be created from interface types. | ||
| 512 | |||
| 513 | Normally, an interface has L<interface methods|/"Interface Method">. | ||
| 514 | |||
| 515 | Compilation Errors: | ||
| 516 | |||
| 517 | An interface cannnot have L<field definitions|/"Field Definition">. If so, a compilation error occurs. | ||
| 518 | |||
| 519 | An interface cannnot have L<class variable definitions|/"Class Variable Definition">. If so, a compilation error occurs. | ||
| 520 | |||
| 521 | Examples: | ||
| 522 | |||
| 523 | # Examples of interface definitions | ||
| 524 | class TestCase::Pointable : interface_t { | ||
| 525 | interface Stringable; | ||
| 526 | |||
| 527 | method x : int (); | ||
| 528 | method y : int(); | ||
| 529 | method to_string : string (); | ||
| 530 | } | ||
| 531 | |||
| 532 | class Stringable: interface_t { | ||
| 533 | method to_string : string (); | ||
| 534 | method call_to_string : string () { | ||
| 535 | return "foo " . $self->to_string; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | class Stringable: interface_t { | ||
| 540 | required method to_string : string (); | ||
| 541 | method foo : int ($num : long); | ||
| 542 | } | ||
| 543 | |||
| 544 | =head3 interface Statement | ||
| 545 | |||
| 546 | The C<interface> statement checks if the current class satisfies L<interface requirement|SPVM::Document::Language::Types/"Interface Requirement"> to an interface. | ||
| 547 | |||
| 548 | interface BASIC_TYPE; | ||
| 549 | |||
| 550 | I<BASIC_TYPE> is loaded by L<use statement|/"use Statement">. | ||
| 551 | |||
| 552 | Compilation Errors: | ||
| 553 | |||
| 554 | The interface type I<BASIC_TYPE> must be an L<interface type|SPVM::Document::Language::Types/"Interface Types">, ohterwise a compilation error occurs. | ||
| 555 | |||
| 556 | The current class must satisfy L<interface requirement|SPVM::Document::Language::Types/"Interface Requirement"> to the interface type I<BASIC_TYPE>, ohterwise a compilation error occurs. | ||
| 557 | |||
| 558 | Examples: | ||
| 559 | |||
| 560 | # Examples of interface statement | ||
| 561 | class Point { | ||
| 562 | interface Stringable; | ||
| 563 | |||
| 564 | method to_string : string () { | ||
| 565 | my $x = $self->x; | ||
| 566 | my $y = $self->y; | ||
| 567 | |||
| 568 | my $string = "($x,$y)"; | ||
| 569 | |||
| 570 | return $string; | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | =head2 Anon Class | ||
| 575 | |||
| 576 | An anon class is a class without specifying its class name. | ||
| 577 | |||
| 578 | =head3 Anon Class Definition | ||
| 579 | |||
| 580 | An anon class is defined by the following syntax. | ||
| 581 | |||
| 582 | class { | ||
| 583 | |||
| 584 | } | ||
| 585 | |||
| 586 | An anon class cannot be defined in a L<class file|Class File>. It is able to be defined in a source code compiled by L<compile_anon_class|SPVM::Document::NativeAPI::Compiler/"compile_anon_class"> compiler native API. | ||
| 587 | |||
| 588 | An anon class has its L<class name|SPVM::Document::Language::Tokenization/"Class Name">, such as C<eval::anon_class::0>, C<eval::anon_class::1>, C<eval::anon_class::2>. | ||
| 589 | |||
| 590 | eval::anon_class::123 | ||
| 591 | |||
| 592 | L<Examples:> | ||
| 593 | |||
| 594 | # Examples of the anon class definition | ||
| 595 | class { | ||
| 596 | static method sum : int ($num1 : int, $num2 : int) { | ||
| 597 | return $num1 + $num2; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | =head2 Anon Method Class | ||
| 602 | |||
| 603 | An anon method class is a class defined by an L<anon method operator|SPVM::Document::Language::Operators/"Anon Method Operator">. | ||
| 604 | |||
| 605 | =head3 Anon Method Class Definition | ||
| 606 | |||
| 607 | The anon method class definition has the following syntax. | ||
| 608 | |||
| 609 | ANON_METHOD_CLASS_FIELD_DEFINITION METHOD_DEFINITION | ||
| 610 | |||
| 611 | I<ANON_METHOD_CLASS_FIELD_DEFINITION> is an L<anon method class field definition|/"Anon Method Class Field Definition">. | ||
| 612 | |||
| 613 | I<METHOD_DEFINITION> is a L<method definition|/"Method Definition">. | ||
| 614 | |||
| 615 | The anon method class definition defines a class to which this method belongs. | ||
| 616 | |||
| 617 | The name of this class is a string that joins L<outmost class|/"Outmost Class">, a string C<"anon_method">, the line number and the position of columns where an anon method class definition is written with C<::>. | ||
| 618 | |||
| 619 | MyClass::anon_method::3::23 | ||
| 620 | |||
| 621 | An anon method class has the same access control as its outmost class. | ||
| 622 | |||
| 623 | An anon method class has the same alias names as its outmost class. | ||
| 624 | |||
| 625 | Compilation Errors: | ||
| 626 | |||
| 627 | The method name of I<METHOD_DEFINITION> must be an empty string. Otherwise, a compilation error occurs. | ||
| 628 | |||
| 629 | The method must be an instance method. Otherwise, a compilation error occurs. | ||
| 630 | |||
| 631 | Examples: | ||
| 632 | |||
| 633 | # Examples of the anon method definition | ||
| 634 | class Foo::Bar { | ||
| 635 | method my_method : void () { | ||
| 636 | my $comparator = (Comparator)method : int ($x1 : object, $x2 : object) { | ||
| 637 | my $point1 = (Point)$x1; | ||
| 638 | my $point2 = (Point)$x2; | ||
| 639 | |||
| 640 | return $point1->x <=> $point2->x; | ||
| 641 | }; | ||
| 642 | } | ||
| 643 | } | ||
| 644 | |||
| 645 | =head4 Anon Method Class Field Definition | ||
| 646 | |||
| 647 | An anon method class field definition defines fields of an L<anon class|/"Anon Class">, which is a part of L<anon method class definition|Anon Method Class Definition>. | ||
| 648 | |||
| 649 | [ANON_METHOD_CLASS_FIELD_DEFINITION_ITEM1, ANON_METHOD_CLASS_FIELD_DEFINITION_ITEM, ANON_METHOD_CLASS_FIELD_DEFINITION_ITEMn] | ||
| 650 | |||
| 651 | I<ANON_METHOD_CLASS_FIELD_DEFINITION_ITEM> are one of | ||
| 652 | |||
| 653 | has FIELD_NAME : TYPE | ||
| 654 | has FIELD_NAME : TYPE = OPERAND | ||
| 655 | VAR : TYPE | ||
| 656 | VAR : TYPE = OPERAND | ||
| 657 | VAR : | ||
| 658 | |||
| 659 | has FIELD_NAME : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE | ||
| 660 | has FIELD_NAME : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE = OPERAND | ||
| 661 | VAR : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE | ||
| 662 | VAR : ATTRIBUTE1 ATTRIBUTE2 ATTRIBUTEn TYPE = OPERAND | ||
| 663 | |||
| 664 | I<FIELD_NAME> is a L<field name|SPVM::Document::Language::Tokenization/"Field Name">. | ||
| 665 | |||
| 666 | I<TYPE> is a L<type|SPVM::Document::Language::Types/"Types">. | ||
| 667 | |||
| 668 | I<OPERAND> is an L<operator|SPVM::Document::Language::Operators/"Operators">. | ||
| 669 | |||
| 670 | C<VAR : TYPE = OPERAND> is expaneded to C<has FIELD_NAME : TYPE = OPERAND>. I<FIELD_NAME> is the same as the name of I<VAR>, but C<$> is removed. I<VAR> is declared at the top of this anon method and set to the value of its corresponding field. | ||
| 671 | |||
| 672 | C<VAR : TYPE> is the same as above, but expaneded to C<has FIELD_NAME : TYPE = VAR>. | ||
| 673 | |||
| 674 | I<ATTRIBUTE> is a L<field attribute|/"Field Attributes">. | ||
| 675 | |||
| 676 | C<VAR :> is expanded to C<has FIELD_NAME : object = VAR>. I<FIELD_NAME> is the same as the name of I<VAR>, but C<$> is removed. The internal field type of the anon class is the any object type (object), but through a type cast performed during compilation, it is treated as the same type as the capture-source variable. I<VAR> is declared at the top of this anon method and set to the value of its corresponding field. | ||
| 677 | |||
| 678 | Compilation Errors: | ||
| 679 | |||
| 680 | Compilation errors caused by L<Field Definition|/"Field Definition"> could occur. | ||
| 681 | |||
| 682 | Examples: | ||
| 683 | |||
| 684 | # Examples of the anon method class field definition | ||
| 685 | class Foo::Bar { | ||
| 686 | method my_method : void () { | ||
| 687 | my $foo = 1; | ||
| 688 | my $bar = 5L; | ||
| 689 | |||
| 690 | my $comparator = (Comparator)[has foo : int = $foo, has bar : long = $bar] method : int ($x1 : object, $x2 : object) { | ||
| 691 | my $foo = $self->{foo}; | ||
| 692 | my $bar = $self->{bar}; | ||
| 693 | |||
| 694 | say "$foo"; | ||
| 695 | say "$bar"; | ||
| 696 | }; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | # More simple | ||
| 701 | class Foo::Bar { | ||
| 702 | method my_method : void () { | ||
| 703 | my $foo = 1; | ||
| 704 | my $bar = 5L; | ||
| 705 | |||
| 706 | my $comparator = (Comparator)[$foo : int, $bar : long] method : int ($x1 : object, $x2 : object) { | ||
| 707 | say "$foo"; | ||
| 708 | say "$bar"; | ||
| 709 | }; | ||
| 710 | } | ||
| 711 | } | ||
| 712 | |||
| 713 | # Change variable names | ||
| 714 | class Foo::Bar { | ||
| 715 | method my_method : void () { | ||
| 716 | my $foo = 1; | ||
| 717 | my $bar = 5L; | ||
| 718 | |||
| 719 | my $comparator = (Comparator)[$foo_x : int = $foo, $bar_x : long = $bar] method : int ($x1 : object, $x2 : object) { | ||
| 720 | say "$foo_x"; | ||
| 721 | say "$bar_x"; | ||
| 722 | }; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | # Type inference for VAR without explicit type specification | ||
| 727 | class Foo::Bar { | ||
| 728 | method my_method : void () { | ||
| 729 | my $self = Int->new(1); | ||
| 730 | my $foo = 2; | ||
| 731 | |||
| 732 | my $object = [$self : , $foo : ] method : int () { | ||
| 733 | unless (compile_type_name $foo eq "int") { | ||
| 734 | die; | ||
| 735 | } | ||
| 736 | |||
| 737 | unless (compile_type_name $self eq "Int") { | ||
| 738 | die; | ||
| 739 | } | ||
| 740 | |||
| 741 | return $self->(int) + $foo; | ||
| 742 | }; | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | =head2 Outmost Class | ||
| 747 | |||
| 748 | An outmost class is the outmost defined class. This is the same as the class defined by L<class definition|/"Class Definition">. | ||
| 749 | |||
| 750 | class OutmostClass { | ||
| 751 | |||
| 752 | } | ||
| 753 | |||
| 754 | Examples: | ||
| 755 | |||
| 756 | # Examples of the outmost class | ||
| 757 | class MyClass { | ||
| 758 | static method my_method : void () { | ||
| 759 | |||
| 760 | # The __PACKAGE__ operator returns the outmost class "MyClass". | ||
| 761 | my $outmost_class_name = __PACKAGE__; | ||
| 762 | |||
| 763 | # Anon method | ||
| 764 | my $cb = method : void () { | ||
| 765 | |||
| 766 | # The __PACKAGE__ operator returns the outmost class "MyClass". | ||
| 767 | my $outmost_class_name = __PACKAGE__; | ||
| 768 | }; | ||
| 769 | } | ||
| 770 | } | ||
| 771 | |||
| 772 | =head2 Multi-Numeric Type Definition | ||
| 773 | |||
| 774 | A L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Types"> is defined by L<class definition|/"Class Definition"> with the C<mulnum_t> L<class attribute|/"Class Attributes">. | ||
| 775 | |||
| 776 | class CLASS_NAME : mulnum_t { | ||
| 777 | |||
| 778 | has FIELD_NAME1 : NUMERIC_TYPE; | ||
| 779 | has FIELD_NAME2 : NUMERIC_TYPE; | ||
| 780 | has FIELD_NAMEn : NUMERIC_TYPE; | ||
| 781 | } | ||
| 782 | |||
| 783 | A multi-numeric field must have at least one fields. The type of a field must be a numeric type. The types of all fields must be same. | ||
| 784 | |||
| 785 | The length of fields must be less than or equal to 255. | ||
| 786 | |||
| 787 | I<CLASS_NAME> must ends with a L<multi-numeric type suffix|/"Multi-Numeric Type Suffix"> corresponding to the type of fields. | ||
| 788 | |||
| 789 | I<FIELD_LENGTH> of L<multi-numeric type suffix|/"Multi-Numeric Type Suffix"> must be the same as the length of fields. | ||
| 790 | |||
| 791 | I<TYPE_SUFFIX> of L<multi-numeric type suffix|/"Multi-Numeric Type Suffix"> must correspond to the type of fields. | ||
| 792 | |||
| 793 | A multi-numeric type cannnot have instance methods. | ||
| 794 | |||
| 795 | A multi-numeric type cannnot have class variables. | ||
| 796 | |||
| 797 | Compilation Errors: | ||
| 798 | |||
| 799 | If the difinition of multi-numeric type is invalid, a compilation error occurs. | ||
| 800 | |||
| 801 | Examples: | ||
| 802 | |||
| 803 | # Examples of the multi-numeric type definition | ||
| 804 | class Complex_2f : mulnum_t { | ||
| 805 | re : float; | ||
| 806 | im : float; | ||
| 807 | } | ||
| 808 | |||
| 809 | class Complex_2d : mulnum_t { | ||
| 810 | re : double; | ||
| 811 | im : double; | ||
| 812 | } | ||
| 813 | |||
| 814 | class Quaternion_4f : mulnum_t { | ||
| 815 | re : float; | ||
| 816 | i : float; | ||
| 817 | j : float; | ||
| 818 | k : float; | ||
| 819 | } | ||
| 820 | |||
| 821 | class Quaternion_4d : mulnum_t { | ||
| 822 | re : double; | ||
| 823 | i : double; | ||
| 824 | j : double; | ||
| 825 | k : double; | ||
| 826 | } | ||
| 827 | |||
| 828 | =head3 Multi-Numeric Type Suffix | ||
| 829 | |||
| 830 | The multi-numeric type must end with the following suffix. | ||
| 831 | |||
| 832 | _(non-spaces)FIELD_LENGTH(non-spaces)TYPE_SUFFIX | ||
| 833 | |||
| 834 | I<FIELD_LENGTH> is the length of fields. | ||
| 835 | |||
| 836 | I<TYPE_SUFFIX> is a type suffix. | ||
| 837 | |||
| 838 | The List of Type Suffixes: | ||
| 839 | |||
| 840 | =begin html | ||
| 841 | |||
| 842 | <table> | ||
| 843 | <tr> | ||
| 844 | <th> | ||
| 845 | Numeric Types | ||
| 846 | </th> | ||
| 847 | <th> | ||
| 848 | Type Suffixes | ||
| 849 | </th> | ||
| 850 | </tr> | ||
| 851 | <tr> | ||
| 852 | <td> | ||
| 853 | <b>byte</b> | ||
| 854 | </td> | ||
| 855 | <td> | ||
| 856 | b | ||
| 857 | </td> | ||
| 858 | </tr> | ||
| 859 | <tr> | ||
| 860 | <td> | ||
| 861 | <b>short</b> | ||
| 862 | </td> | ||
| 863 | <td> | ||
| 864 | s | ||
| 865 | </td> | ||
| 866 | </tr> | ||
| 867 | <tr> | ||
| 868 | <td> | ||
| 869 | <b>int</b> | ||
| 870 | </td> | ||
| 871 | <td> | ||
| 872 | i | ||
| 873 | </td> | ||
| 874 | </tr> | ||
| 875 | <tr> | ||
| 876 | <td> | ||
| 877 | <b>long</b> | ||
| 878 | </td> | ||
| 879 | <td> | ||
| 880 | l | ||
| 881 | </td> | ||
| 882 | </tr> | ||
| 883 | <tr> | ||
| 884 | <td> | ||
| 885 | <b>float</b> | ||
| 886 | </td> | ||
| 887 | <td> | ||
| 888 | f | ||
| 889 | </td> | ||
| 890 | </tr> | ||
| 891 | <tr> | ||
| 892 | <td> | ||
| 893 | <b>double</b> | ||
| 894 | </td> | ||
| 895 | <td> | ||
| 896 | d | ||
| 897 | </td> | ||
| 898 | </tr> | ||
| 899 | </table> | ||
| 900 | |||
| 901 | =end html | ||
| 902 | |||
| 903 | Examples: | ||
| 904 | |||
| 905 | # Examples of the multi-numeric type suffix | ||
| 906 | _2f; | ||
| 907 | _2d; | ||
| 908 | _4f; | ||
| 909 | _4d; | ||
| 910 | |||
| 911 | =head1 Enumeration | ||
| 912 | |||
| 913 | Enumeration is a syntax that defines 32-bit integers that belongs to a L<class|/"Class">. | ||
| 914 | |||
| 915 | =head2 Enumeration Definition | ||
| 916 | |||
| 917 | The C<enum> keyword defines an enumeration. | ||
| 918 | |||
| 919 | enum { | ||
| 920 | ITEM1, | ||
| 921 | ITEM2, | ||
| 922 | ITEMn | ||
| 923 | } | ||
| 924 | |||
| 925 | C<,> after the last enumeration item is allowed. | ||
| 926 | |||
| 927 | enum { | ||
| 928 | ITEM1, | ||
| 929 | ITEM2, | ||
| 930 | ITEM3, | ||
| 931 | } | ||
| 932 | |||
| 933 | I<ITEM> is one of | ||
| 934 | |||
| 935 | NAME | ||
| 936 | NAME = VALUE | ||
| 937 | |||
| 938 | I<NAME> is a L<method name|SPVM::Document::Language::Tokenization/"Method Name">. | ||
| 939 | |||
| 940 | I<VALUE> is an L<integer literal|SPVM::Document::Language::Tokenization/"Integer Literals"> within int type. I<VALUE> is converted the value of int type. | ||
| 941 | |||
| 942 | If I<VALUE> of I<ITEM1> is omitted, it is set to 0. | ||
| 943 | |||
| 944 | If I<VALUE> of I<ITEMn> is ommited, it is set to the value of the previous item plus 1. | ||
| 945 | |||
| 946 | The return type of the method is int type. | ||
| 947 | |||
| 948 | Every enumeration item is converted to a L<method definition|/"Method Definition"> that defines a class method that returns the value of the enumeration item. | ||
| 949 | |||
| 950 | # Definition of an enumeration | ||
| 951 | class MyClass { | ||
| 952 | enum { | ||
| 953 | NAME = 5, | ||
| 954 | } | ||
| 955 | } | ||
| 956 | |||
| 957 | # This are replaced with a definition of a class method | ||
| 958 | class MyClass { | ||
| 959 | static method NAME : int () { return 5; } | ||
| 960 | } | ||
| 961 | |||
| 962 | See also L</"Inline Expansion of Method Call to Get an Enuemration Value">. | ||
| 963 | |||
| 964 | Compilation Errors: | ||
| 965 | |||
| 966 | I<NAME> must be a L<method name|SPVM::Document::Language::Tokenization/"Method Name">. Otherwise, a compilation error occurs. | ||
| 967 | |||
| 968 | I<VALUE> must be an L<integer literal|/"Integer Literal"> within int type. Otherwise, a compilation error occurs. | ||
| 969 | |||
| 970 | Examples: | ||
| 971 | |||
| 972 | # Examples of enumerations | ||
| 973 | class MyClass { | ||
| 974 | enum { | ||
| 975 | FLAG1, | ||
| 976 | FLAG2, | ||
| 977 | FLAG3, | ||
| 978 | } | ||
| 979 | |||
| 980 | static method main : void () { | ||
| 981 | my $flag1 = MyClass->FLAG1; | ||
| 982 | } | ||
| 983 | } | ||
| 984 | |||
| 985 | class MyClass { | ||
| 986 | enum { | ||
| 987 | FLAG1, | ||
| 988 | FLAG2 = 2, | ||
| 989 | FLAG3, | ||
| 990 | } | ||
| 991 | } | ||
| 992 | |||
| 993 | =head2 Enumeration Attributes | ||
| 994 | |||
| 995 | The List of Enumeration Attributes: | ||
| 996 | |||
| 997 | =begin html | ||
| 998 | |||
| 999 | <table> | ||
| 1000 | <tr> | ||
| 1001 | <th> | ||
| 1002 | Attributes | ||
| 1003 | </th> | ||
| 1004 | <th> | ||
| 1005 | Descriptions | ||
| 1006 | </th> | ||
| 1007 | </tr> | ||
| 1008 | <tr> | ||
| 1009 | <td> | ||
| 1010 | <b>public</b> | ||
| 1011 | </td> | ||
| 1012 | <td> | ||
| 1013 | This enumeration is public. All classes can access all items of this enumeration. This is default. | ||
| 1014 | </td> | ||
| 1015 | </tr> | ||
| 1016 | <tr> | ||
| 1017 | <td> | ||
| 1018 | <b>private</b> | ||
| 1019 | </td> | ||
| 1020 | <td> | ||
| 1021 | This enumeration is private. All classes ohter than this class cannnot access all items of this enumeration. | ||
| 1022 | </td> | ||
| 1023 | </tr> | ||
| 1024 | <tr> | ||
| 1025 | <td> | ||
| 1026 | <b>protected</b> | ||
| 1027 | </td> | ||
| 1028 | <td> | ||
| 1029 | This enumeration is protected. All classes ohter than this class and its child classes cannot access all items of this enumeration. | ||
| 1030 | </td> | ||
| 1031 | </tr> | ||
| 1032 | </table> | ||
| 1033 | |||
| 1034 | =end html | ||
| 1035 | |||
| 1036 | Compilation Errors: | ||
| 1037 | |||
| 1038 | One of C<private>, C<protected> and C<public> must be specified. Otherwise, a compilation error occurs. | ||
| 1039 | |||
| 1040 | =head1 Class Variable | ||
| 1041 | |||
| 1042 | A class variable is a global variable that belongs to a L<class|/"Class">. | ||
| 1043 | |||
| 1044 | =head2 Class Variable Definition | ||
| 1045 | |||
| 1046 | C<our> keyword defines a class variable. | ||
| 1047 | |||
| 1048 | our CLASS_VARIABLE_NAME : OPT_ATTRIBUTES TYPE; | ||
| 1049 | |||
| 1050 | I<CLASS_VARIABLE_NAME> is a L<class variable name|SPVM::Document::Language::Tokenization/"Class Variable Name"> that does not contains C<::>. | ||
| 1051 | |||
| 1052 | I<TYPE> is a L<type|SPVM::Document::Language::Types/"Types">. | ||
| 1053 | |||
| 1054 | I<OPT_ATTRIBUTES> is one of | ||
| 1055 | |||
| 1056 | EMPTY | ||
| 1057 | ATTRIBUTES | ||
| 1058 | |||
| 1059 | I<EMPTY> means nothing exists. | ||
| 1060 | |||
| 1061 | I<ATTRIBUTES> is one of | ||
| 1062 | |||
| 1063 | ATTRIBUTES ATTRIBUTE | ||
| 1064 | ATTRIBUTE | ||
| 1065 | |||
| 1066 | I<ATTRIBUTE> is a L<class variable attribute|/"Class Variable Attributes">. | ||
| 1067 | |||
| 1068 | Each class variable is initialized by its L<type initial value|SPVM::Document::Language::Types/"Type Initial Value">. | ||
| 1069 | |||
| 1070 | The value of a class variable is got by the operation of L<getting a class variable|SPVM::Document::Language::Operators/"Class Variable Get Operation">. | ||
| 1071 | |||
| 1072 | The value of a class variable is set by the operation of L<setting a class variable|SPVM::Document::Language::Operators/"Class Variable Set Operation">. | ||
| 1073 | |||
| 1074 | Compilation Errors: | ||
| 1075 | |||
| 1076 | I<CLASS_VARIABLE_NAME> must a L<class variable name|SPVM::Document::Language::Tokenization/"Class Variable Name"> that does not contains C<::>. Otherwise, a compilation error occurs. | ||
| 1077 | |||
| 1078 | I<TYPE> must be a L<numeric type|SPVM::Document::Language::Types/"Numeric Types"> or an L<object type|SPVM::Document::Language::Types/"Object Types">. | ||
| 1079 | |||
| 1080 | I<ATTRIBUTE> must be a L<class variable attribute|/"Class Variable Attributes">. Otherwise, a compilation error occurs. | ||
| 1081 | |||
| 1082 | If two or more class variables that has a same name are defined, a compilation error occurs. | ||
| 1083 | |||
| 1084 | Examples: | ||
| 1085 | |||
| 1086 | class MyClass { | ||
| 1087 | our $NUM_BYTE : byte; | ||
| 1088 | our $NUM_SHORT : short; | ||
| 1089 | our $NUM_INT : int; | ||
| 1090 | our $NUM_LONG : long; | ||
| 1091 | our $NUM_FLOAT : float; | ||
| 1092 | our $NUM_DOUBLE : double; | ||
| 1093 | our $POINT : Point; | ||
| 1094 | |||
| 1095 | our $NUM_PUBLIC : public int; | ||
| 1096 | our $NUM_RO : ro int; | ||
| 1097 | our $NUM_RW : rw int; | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | =head2 Class Variable Attributes | ||
| 1101 | |||
| 1102 | The List of Class Variable Attributes: | ||
| 1103 | |||
| 1104 | =begin html | ||
| 1105 | |||
| 1106 | <table> | ||
| 1107 | <tr> | ||
| 1108 | <th> | ||
| 1109 | Class Variable Attributes | ||
| 1110 | </th> | ||
| 1111 | <th> | ||
| 1112 | Descriptions | ||
| 1113 | </th> | ||
| 1114 | </tr> | ||
| 1115 | <tr> | ||
| 1116 | <td> | ||
| 1117 | <b>public</b> | ||
| 1118 | </td> | ||
| 1119 | <td> | ||
| 1120 | This class variable is public. All classes can access this class variable. | ||
| 1121 | </td> | ||
| 1122 | </tr> | ||
| 1123 | <tr> | ||
| 1124 | <td> | ||
| 1125 | <b>private</b> | ||
| 1126 | </td> | ||
| 1127 | <td> | ||
| 1128 | This class variable is private. All classes ohter than this class cannnot access this class variable. | ||
| 1129 | </td> | ||
| 1130 | </tr> | ||
| 1131 | <tr> | ||
| 1132 | <td> | ||
| 1133 | <b>protected</b> | ||
| 1134 | </td> | ||
| 1135 | <td> | ||
| 1136 | This class variable is protected. All classes ohter than this class and its child classes cannot access this class variable. This is default. | ||
| 1137 | </td> | ||
| 1138 | </tr> | ||
| 1139 | <tr> | ||
| 1140 | <td> | ||
| 1141 | <b>ro</b> | ||
| 1142 | </td> | ||
| 1143 | <td> | ||
| 1144 | This class variable defines a <a href="#Class-Variable-Getter-Method">getter method</a>. | ||
| 1145 | </td> | ||
| 1146 | </tr> | ||
| 1147 | <tr> | ||
| 1148 | <td> | ||
| 1149 | <b>wo</b> | ||
| 1150 | </td> | ||
| 1151 | <td> | ||
| 1152 | This class variable defineds a <a href="#Class-Variable-Setter-Method">setter method</a>. | ||
| 1153 | </td> | ||
| 1154 | </tr> | ||
| 1155 | <tr> | ||
| 1156 | <td> | ||
| 1157 | <b>rw</b> | ||
| 1158 | </td> | ||
| 1159 | <td> | ||
| 1160 | This class variable defines a <a href="#Class-Variable-Getter-Method">getter method</a> and a <a href="#Class-Variable-Setter-Method">setter method</a>. | ||
| 1161 | </td> | ||
| 1162 | </tr> | ||
| 1163 | <tr> | ||
| 1164 | <td> | ||
| 1165 | <b>cache</b> | ||
| 1166 | </td> | ||
| 1167 | <td> | ||
| 1168 | This class variable is used for cache in runtime. | ||
| 1169 | </td> | ||
| 1170 | </tr> | ||
| 1171 | <tr> | ||
| 1172 | <td> | ||
| 1173 | <b>virtual</b> | ||
| 1174 | </td> | ||
| 1175 | <td> | ||
| 1176 | The class variable is not defined, but you can defined accessors. | ||
| 1177 | </td> | ||
| 1178 | </tr> | ||
| 1179 | </table> | ||
| 1180 | |||
| 1181 | =end html | ||
| 1182 | |||
| 1183 | Compilation Errors: | ||
| 1184 | |||
| 1185 | One of C<private>, C<protected> and C<public> must be specified. Otherwise, a compilation error occurs. | ||
| 1186 | |||
| 1187 | One of C<ro>, C<wo>, and C<rw> must be specified. Otherwise, a compilation error occurs. | ||
| 1188 | |||
| 1189 | =head3 Class Variable Getter Method | ||
| 1190 | |||
| 1191 | A class variable getter method is a method to perform the operation of L<getting a class variable|SPVM::Document::Language::Operators/"Class Variable Get Operation">. | ||
| 1192 | |||
| 1193 | This method is a class method that has no arguments. | ||
| 1194 | |||
| 1195 | If the type of the class variable is byte type or short type, the return type is int type. Otherwise, it is the same type as the class variable. | ||
| 1196 | |||
| 1197 | The method name is the same as the class variable name, but C<$> is removed. For example, if the class variable name is C<$FOO>, the method name is C<FOO>. | ||
| 1198 | |||
| 1199 | Examples: | ||
| 1200 | |||
| 1201 | # Examples of class variable getter methods | ||
| 1202 | class MyClass { | ||
| 1203 | our $VALUE : ro int; | ||
| 1204 | |||
| 1205 | static method main : void { | ||
| 1206 | my $num = Foo->VALUE; | ||
| 1207 | } | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | A user defined class variable getter can be defined with C<get> keyword. | ||
| 1211 | |||
| 1212 | class MyClass { | ||
| 1213 | our $VALUE : ro int | ||
| 1214 | get { | ||
| 1215 | return $VALUE; | ||
| 1216 | } | ||
| 1217 | ; | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | User defined getter and setter can be defined at once. | ||
| 1221 | |||
| 1222 | class MyClass { | ||
| 1223 | our $VALUE : rw int | ||
| 1224 | get { | ||
| 1225 | return $VALUE; | ||
| 1226 | } | ||
| 1227 | set { | ||
| 1228 | $VALUE = $_; | ||
| 1229 | } | ||
| 1230 | ; | ||
| 1231 | } | ||
| 1232 | |||
| 1233 | =head3 Class Variable Setter Method | ||
| 1234 | |||
| 1235 | A class variable setter method is a method to perform the operation of L<setting a class variable|SPVM::Document::Language::Operators/"Class Variable Set Operation">. | ||
| 1236 | |||
| 1237 | This method is a class method that has an argument. | ||
| 1238 | |||
| 1239 | If the type of the class variable is byte type or short type, the argument type is int type. Otherwise, it is the same type as the class variable. | ||
| 1240 | |||
| 1241 | The return type is void type. | ||
| 1242 | |||
| 1243 | The method name is the same as the class variable name, but C<$> is removed and C<SET_> is added to the beginning of it. For example, if the class variable name is C<$FOO>, the method name is C<SET_FOO>. | ||
| 1244 | |||
| 1245 | Examples: | ||
| 1246 | |||
| 1247 | # Examples of class variable setter methods | ||
| 1248 | class MyClass { | ||
| 1249 | our $NUM : wo int; | ||
| 1250 | |||
| 1251 | static method main : void { | ||
| 1252 | Foo->SET_NUM(3); | ||
| 1253 | } | ||
| 1254 | } | ||
| 1255 | |||
| 1256 | A user defined class variable setter can be defined with C<set> keyword. The passed value is assigned to C<$_>. | ||
| 1257 | |||
| 1258 | class MyClass { | ||
| 1259 | our $VALUE : wo int | ||
| 1260 | set { | ||
| 1261 | $VALUE = $_; | ||
| 1262 | } | ||
| 1263 | ; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | The argument can be replaced. | ||
| 1267 | |||
| 1268 | class MyClass { | ||
| 1269 | our $VALUE : wo int | ||
| 1270 | set ($VAL : object) { | ||
| 1271 | $VALUE = (int)$VAL; | ||
| 1272 | } | ||
| 1273 | ; | ||
| 1274 | } | ||
| 1275 | |||
| 1276 | =head1 Field | ||
| 1277 | |||
| 1278 | A field is a data member of a L<class|/"Class">. | ||
| 1279 | |||
| 1280 | =head2 Field Definition | ||
| 1281 | |||
| 1282 | The C<has> keyword defines a field. | ||
| 1283 | |||
| 1284 | has FIELD_NAME : OPT_ATTRIBUTES TYPE; | ||
| 1285 | |||
| 1286 | I<FIELD_NAME> is a L<field name|SPVM::Document::Language::Tokenization/"Field Name">. | ||
| 1287 | |||
| 1288 | I<TYPE> is a L<type|SPVM::Document::Language::Types/"Types">. | ||
| 1289 | |||
| 1290 | I<OPT_ATTRIBUTES> is one of | ||
| 1291 | |||
| 1292 | EMPTY | ||
| 1293 | ATTRIBUTES | ||
| 1294 | |||
| 1295 | I<EMPTY> means nothing exists. | ||
| 1296 | |||
| 1297 | I<ATTRIBUTES> is one of | ||
| 1298 | |||
| 1299 | ATTRIBUTES ATTRIBUTE | ||
| 1300 | ATTRIBUTE | ||
| 1301 | |||
| 1302 | I<ATTRIBUTE> is a L<field attribute|/"Field Attributes">. | ||
| 1303 | |||
| 1304 | Each field of an object is initialized by its L<type initial value|SPVM::Document::Language::Types/"Type Initial Value"> when the object is created. | ||
| 1305 | |||
| 1306 | The value of a field is got by the operation of L<getting a field|SPVM::Document::Language::Operators/"Field Get Operation">. | ||
| 1307 | |||
| 1308 | The value of a field is set by the operation of L<setting a field|SPVM::Document::Language::Operators/"Field Set Operation">. | ||
| 1309 | |||
| 1310 | Compilation Errors: | ||
| 1311 | |||
| 1312 | I<FIELD_NAME> must a L<field name|SPVM::Document::Language::Tokenization/"Field Name">. Otherwise, a compilation error occurs. | ||
| 1313 | |||
| 1314 | I<TYPE> must be a L<numeric type|SPVM::Document::Language::Types/"Numeric Types"> or an L<object type|SPVM::Document::Language::Types/"Object Types">. | ||
| 1315 | |||
| 1316 | I<ATTRIBUTE > must be a L<field attribute|/"Field Attributes">. Otherwise, a compilation error occurs. | ||
| 1317 | |||
| 1318 | If two or more fields that has a same name are defined, a compilation error occurs. | ||
| 1319 | |||
| 1320 | If the field is already defined with a different type in the super class, a compilation error occurs. | ||
| 1321 | |||
| 1322 | Examples: | ||
| 1323 | |||
| 1324 | class MyClass { | ||
| 1325 | has num_byte : byte; | ||
| 1326 | has num_short : short; | ||
| 1327 | has num_int : int; | ||
| 1328 | has num_long : long; | ||
| 1329 | has num_float : float; | ||
| 1330 | has num_double : double; | ||
| 1331 | |||
| 1332 | has num_public : public int; | ||
| 1333 | has num_ro : ro int; | ||
| 1334 | has num_rw : rw int; | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | =head2 Field Attributes | ||
| 1338 | |||
| 1339 | The List of Field Attributes: | ||
| 1340 | |||
| 1341 | =begin html | ||
| 1342 | |||
| 1343 | <table> | ||
| 1344 | <tr> | ||
| 1345 | <th> | ||
| 1346 | Field Attributes | ||
| 1347 | </th> | ||
| 1348 | <th> | ||
| 1349 | Descriptions | ||
| 1350 | </th> | ||
| 1351 | </tr> | ||
| 1352 | <tr> | ||
| 1353 | <td> | ||
| 1354 | <b>public</b> | ||
| 1355 | </td> | ||
| 1356 | <td> | ||
| 1357 | This field is public. All classes can access this field. | ||
| 1358 | </td> | ||
| 1359 | </tr> | ||
| 1360 | <tr> | ||
| 1361 | <td> | ||
| 1362 | <b>private</b> | ||
| 1363 | </td> | ||
| 1364 | <td> | ||
| 1365 | This field is private. All classes ohter than this class cannnot access this field. | ||
| 1366 | </td> | ||
| 1367 | </tr> | ||
| 1368 | <tr> | ||
| 1369 | <td> | ||
| 1370 | <b>protected</b> | ||
| 1371 | </td> | ||
| 1372 | <td> | ||
| 1373 | This field is protected. All classes ohter than this class and its child classes cannot access this field. This is default. | ||
| 1374 | </td> | ||
| 1375 | </tr> | ||
| 1376 | <tr> | ||
| 1377 | <td> | ||
| 1378 | <b>ro</b> | ||
| 1379 | </td> | ||
| 1380 | <td> | ||
| 1381 | This field defines a <a href="#Field-Getter-Method">getter method</a>. | ||
| 1382 | </td> | ||
| 1383 | </tr> | ||
| 1384 | <tr> | ||
| 1385 | <td> | ||
| 1386 | <b>wo</b> | ||
| 1387 | </td> | ||
| 1388 | <td> | ||
| 1389 | This field defineds a <a href="#Field-Setter-Method">setter method</a>. | ||
| 1390 | </td> | ||
| 1391 | </tr> | ||
| 1392 | <tr> | ||
| 1393 | <td> | ||
| 1394 | <b>rw</b> | ||
| 1395 | </td> | ||
| 1396 | <td> | ||
| 1397 | This field defines a <a href="#Field-Getter-Method">getter method</a> and a <a href="#Field-Setter-Method">setter method</a>. | ||
| 1398 | </td> | ||
| 1399 | </tr> | ||
| 1400 | <tr> | ||
| 1401 | <td> | ||
| 1402 | <b>virtual</b> | ||
| 1403 | </td> | ||
| 1404 | <td> | ||
| 1405 | The field is not defined, but you can defined accessors. | ||
| 1406 | </td> | ||
| 1407 | </tr> | ||
| 1408 | </table> | ||
| 1409 | |||
| 1410 | =end html | ||
| 1411 | |||
| 1412 | Compilation Errors: | ||
| 1413 | |||
| 1414 | One of C<private>, C<protected> and C<public> must be specified. Otherwise, a compilation error occurs. | ||
| 1415 | |||
| 1416 | One of C<ro>, C<wo>, and C<rw> must be specified. Otherwise, a compilation error occurs. | ||
| 1417 | |||
| 1418 | =head3 Field Getter Method | ||
| 1419 | |||
| 1420 | A field getter method is a method to perform the operation of L<getting a field|SPVM::Document::Language::Operators/"Field Get Operation">. | ||
| 1421 | |||
| 1422 | This method is an instance method that has no arguments. | ||
| 1423 | |||
| 1424 | If the type of the field is byte type or short type, the return type is int type. Otherwise, it is the same type as the field. | ||
| 1425 | |||
| 1426 | The method name is the same as the field name. For example, if the field name is C<foo>, the method name is C<foo>. | ||
| 1427 | |||
| 1428 | Examples: | ||
| 1429 | |||
| 1430 | # Examples of field getter methods | ||
| 1431 | class MyClass { | ||
| 1432 | has value : ro int; | ||
| 1433 | |||
| 1434 | static method new { | ||
| 1435 | return new Foo; | ||
| 1436 | } | ||
| 1437 | |||
| 1438 | static method main : void { | ||
| 1439 | my $foo = Foo->new; | ||
| 1440 | |||
| 1441 | my $value = $foo->value; | ||
| 1442 | } | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | A user defined field getter can be defined with C<get> keyword. | ||
| 1446 | |||
| 1447 | class MyClass { | ||
| 1448 | has value : ro int | ||
| 1449 | get { | ||
| 1450 | return $self->{value}; | ||
| 1451 | } | ||
| 1452 | ; | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | User defined getter and setter can be defined at once. | ||
| 1456 | |||
| 1457 | class MyClass { | ||
| 1458 | has value : rw int | ||
| 1459 | get { | ||
| 1460 | return $self->{value}; | ||
| 1461 | } | ||
| 1462 | set { | ||
| 1463 | $self->{value} = $_; | ||
| 1464 | } | ||
| 1465 | ; | ||
| 1466 | } | ||
| 1467 | |||
| 1468 | =head3 Field Setter Method | ||
| 1469 | |||
| 1470 | A field setter method is a method to perform the operation of L<setting a field|SPVM::Document::Language::Operators/"Field Set Operation">. | ||
| 1471 | |||
| 1472 | This method is an instance method that has an argument. | ||
| 1473 | |||
| 1474 | If the type of the field is byte type or short type, the argument type is int type. Otherwise, it is the same type as the field. | ||
| 1475 | |||
| 1476 | The return type is void type. | ||
| 1477 | |||
| 1478 | The method name is the same as the field name, but C<set_> is added to the beginning of it. For example, if the field name is C<foo>, the method name is C<set_foo>. | ||
| 1479 | |||
| 1480 | Examples: | ||
| 1481 | |||
| 1482 | # Examples of field setter methods | ||
| 1483 | class MyClass { | ||
| 1484 | has value : wo int; | ||
| 1485 | |||
| 1486 | static method new { | ||
| 1487 | return new Foo; | ||
| 1488 | } | ||
| 1489 | |||
| 1490 | static method main : void { | ||
| 1491 | my $foo = Foo->new; | ||
| 1492 | |||
| 1493 | $foo->set_value(3); | ||
| 1494 | } | ||
| 1495 | } | ||
| 1496 | |||
| 1497 | A user defined field setter can be defined with C<set> keyword. The passed value is assigned to C<$_>. | ||
| 1498 | |||
| 1499 | class MyClass { | ||
| 1500 | has value : wo int | ||
| 1501 | set { | ||
| 1502 | $self->{value} = $_; | ||
| 1503 | } | ||
| 1504 | ; | ||
| 1505 | } | ||
| 1506 | |||
| 1507 | The argument can be replaced. | ||
| 1508 | |||
| 1509 | class MyClass { | ||
| 1510 | has value : wo int | ||
| 1511 | set ($value : object) { | ||
| 1512 | $self->{value} = (int)$value; | ||
| 1513 | } | ||
| 1514 | ; | ||
| 1515 | } | ||
| 1516 | |||
| 1517 | The return type can be replaced. | ||
| 1518 | |||
| 1519 | class MyClass { | ||
| 1520 | has value : wo int | ||
| 1521 | set : MyClass ($value : object) { | ||
| 1522 | $self->{value} = (int)$value; | ||
| 1523 | return $self; | ||
| 1524 | } | ||
| 1525 | ; | ||
| 1526 | } | ||
| 1527 | |||
| 1528 | If there is no method block and a return type is specified, the default setter returns the invocant. | ||
| 1529 | |||
| 1530 | class MyClass { | ||
| 1531 | has value : wo int | ||
| 1532 | set : MyClass | ||
| 1533 | ; | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | =head2 Field Existence | ||
| 1537 | |||
| 1538 | An object has field existence flags. | ||
| 1539 | |||
| 1540 | When a new object is created, these flags are zero. | ||
| 1541 | |||
| 1542 | When one of field set operations is performed, this flag is set to 1. | ||
| 1543 | |||
| 1544 | # The field existence flag for "value" field is set to 1. | ||
| 1545 | $object->{value} = 1; | ||
| 1546 | |||
| 1547 | You can check if a field exists using L<exists operator|SPVM::Document::Language::Operators/"exists Operator">. | ||
| 1548 | |||
| 1549 | my $exists = exists $object->{value}; | ||
| 1550 | |||
| 1551 | You can delete a field using L<delete operator|SPVM::Document::Language::Operators/"delete Operator">. | ||
| 1552 | |||
| 1553 | delete $object->{value}; | ||
| 1554 | |||
| 1555 | The field existence flag is set to 0, and the value of the field is set to 0 in the case of numeric types or undef in the case of object types. | ||
| 1556 | |||
| 1557 | =head1 Method | ||
| 1558 | |||
| 1559 | =head2 Method Definition | ||
| 1560 | |||
| 1561 | The C<method> keyword defines a method. | ||
| 1562 | |||
| 1563 | OPT_ATTRIBUTES method METHOD_NAME : RETURN_TYPE (OPT_ARG_ITEMS) { } | ||
| 1564 | OPT_ATTRIBUTES method METHOD_NAME : RETURN_TYPE (OPT_ARG_ITEMS); | ||
| 1565 | |||
| 1566 | I<OPT_ATTRIBUTES> is one of | ||
| 1567 | |||
| 1568 | EMPTY | ||
| 1569 | ATTRIBUTES | ||
| 1570 | |||
| 1571 | I<EMPTY> means nothing exists. | ||
| 1572 | |||
| 1573 | I<ATTRIBUTES> is one of | ||
| 1574 | |||
| 1575 | ATTRIBUTES ATTRIBUTE | ||
| 1576 | ATTRIBUTE | ||
| 1577 | |||
| 1578 | I<ATTRIBUTE> is a L<method attribute|/"Method Attributes">. | ||
| 1579 | |||
| 1580 | I<METHOD_NAME> is a L<method name|SPVM::Document::Language::Tokenization/"Method Name">. | ||
| 1581 | |||
| 1582 | I<RETURN_TYPE> is a L<type|SPVM::Document::Language::Types/"Types">. | ||
| 1583 | |||
| 1584 | I<OPT_ARG_ITEMS> is one of | ||
| 1585 | |||
| 1586 | EMPTY | ||
| 1587 | ARG_ITEMS | ||
| 1588 | |||
| 1589 | I<EMPTY> means nothing exists. | ||
| 1590 | |||
| 1591 | I<ARG_ITEMS> is one of | ||
| 1592 | |||
| 1593 | ARG_ITEMS , ARG_ITEM | ||
| 1594 | ARG_ITEM | ||
| 1595 | |||
| 1596 | I<ARG_ITEM> is one of | ||
| 1597 | |||
| 1598 | ARG_NAME : ARG_TYPE | ||
| 1599 | ARG_NAME : ARG_TYPE = VALUE | ||
| 1600 | |||
| 1601 | I<ARG_NAME> is a L<local variable name|SPVM::Document::Language::Tokenization/"Local Variable Name">. | ||
| 1602 | |||
| 1603 | The local variable specified I<ARG_NAME> is declared at the beginning of L<method implementation|/"Method Implementation">. | ||
| 1604 | |||
| 1605 | I<ARG_TYPE> is a L<type|SPVM::Document::Language::Types/"Types">. | ||
| 1606 | |||
| 1607 | I<VALUE> is a L<literal|SPVM::Document::Language::Tokenization/"Literals"> or C<undef>. | ||
| 1608 | |||
| 1609 | A method with the C<static> L<method attribute|/"Method Attributes"> is a L<class method|/"Class Method">. | ||
| 1610 | |||
| 1611 | A method without the C<static> L<method attribute|/"Method Attributes"> is an L<instance method|/"Instance Method">. | ||
| 1612 | |||
| 1613 | An argument with I<VALUE> is an optional arguments. If a method call dose not give a value to an optional argument, I<VALUE> is given to the argument. | ||
| 1614 | |||
| 1615 | A interface method defined in an L<interface|/"Interface"> is an L<interface method|/"Interface Method">. | ||
| 1616 | |||
| 1617 | A method with the C<native> L<method attribute|/"Method Attributes"> is a L<native method|/"Native Method">. | ||
| 1618 | |||
| 1619 | Compilation Errors: | ||
| 1620 | |||
| 1621 | The count of I<ARGS> must be less than or equal to 255. Otherwise, a compilation error occurs. | ||
| 1622 | |||
| 1623 | I<ARG_TYPE> must be a L<numeric type|SPVM::Document::Language::Types/"Numeric Types">, a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Types">, an L<object type|SPVM::Document::Language::Types/"Object Types">, or a L<reference type|SPVM::Document::Language::Types/"Reference Types">. Otherwise, a compilation error occurs. | ||
| 1624 | |||
| 1625 | I<RETURN_TYPE> must be void type, a L<numeric type|SPVM::Document::Language::Types/"Numeric Types">, a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Types"> or an L<object type|SPVM::Document::Language::Types/"Object Types">. Otherwise, a compilation error occurs. | ||
| 1626 | |||
| 1627 | Methods other than interface methods and native methods must have a method block. Otherwise, a compilation error occurs. | ||
| 1628 | |||
| 1629 | I<VALUE> must satisfy L<assignment requirement|SPVM::Document::Language::Types/"Assignment Requirement"> to I<ARG_TYPE>. Otherwise, a compilation error occurs. | ||
| 1630 | |||
| 1631 | I<METHOD_NAME> must not be "INIT". Otherwise, a compilation error occurs. | ||
| 1632 | |||
| 1633 | A non-optional argument must not be placed after an optional argument. Otherwise, a compilation error occurs. | ||
| 1634 | |||
| 1635 | Examples: | ||
| 1636 | |||
| 1637 | class MyClass { | ||
| 1638 | # Class method | ||
| 1639 | static method substr : string ($string : string, $offset : int, $length : int = -1) { | ||
| 1640 | # ... | ||
| 1641 | } | ||
| 1642 | |||
| 1643 | # Instance method | ||
| 1644 | method clear : void () { | ||
| 1645 | |||
| 1646 | } | ||
| 1647 | } | ||
| 1648 | |||
| 1649 | =head2 Method Attributes | ||
| 1650 | |||
| 1651 | The List of Method Attributes: | ||
| 1652 | |||
| 1653 | =begin html | ||
| 1654 | |||
| 1655 | <table> | ||
| 1656 | <tr> | ||
| 1657 | <th> | ||
| 1658 | Method Attributes | ||
| 1659 | </th> | ||
| 1660 | <th> | ||
| 1661 | Descriptions | ||
| 1662 | </th> | ||
| 1663 | </tr> | ||
| 1664 | <tr> | ||
| 1665 | <td> | ||
| 1666 | <b>public</b> | ||
| 1667 | </td> | ||
| 1668 | <td> | ||
| 1669 | This method is public. All classes can call this method. This is default. | ||
| 1670 | </td> | ||
| 1671 | </tr> | ||
| 1672 | <tr> | ||
| 1673 | <td> | ||
| 1674 | <b>private</b> | ||
| 1675 | </td> | ||
| 1676 | <td> | ||
| 1677 | This method is private. All classes ohter than this class cannnot call this method. | ||
| 1678 | </td> | ||
| 1679 | </tr> | ||
| 1680 | <tr> | ||
| 1681 | <td> | ||
| 1682 | <b>protected</b> | ||
| 1683 | </td> | ||
| 1684 | <td> | ||
| 1685 | This method is protected. All classes ohter than this class and its child classes cannot call this method. | ||
| 1686 | </td> | ||
| 1687 | </tr> | ||
| 1688 | <tr> | ||
| 1689 | <td> | ||
| 1690 | <b>precompile</b> | ||
| 1691 | </td> | ||
| 1692 | <td> | ||
| 1693 | This method is a <a href="#Precompilation-Method">precompilation method</a>. | ||
| 1694 | </td> | ||
| 1695 | </tr> | ||
| 1696 | <tr> | ||
| 1697 | <td> | ||
| 1698 | <b>native</b> | ||
| 1699 | </td> | ||
| 1700 | <td> | ||
| 1701 | This method is a <a href="#Native-Method">native method</a>. | ||
| 1702 | </td> | ||
| 1703 | </tr> | ||
| 1704 | <tr> | ||
| 1705 | <td> | ||
| 1706 | <b>required</b> | ||
| 1707 | </td> | ||
| 1708 | <td> | ||
| 1709 | A method that implements this interface method must be defined. | ||
| 1710 | </td> | ||
| 1711 | </tr> | ||
| 1712 | </table> | ||
| 1713 | |||
| 1714 | =end html | ||
| 1715 | |||
| 1716 | Compilation Errors: | ||
| 1717 | |||
| 1718 | One of C<private>, C<protected> and C<public> must be specified. Otherwise, a compilation error occurs. | ||
| 1719 | |||
| 1720 | C<required> must be specified to an interface method. Otherwise, a compilation error occurs. | ||
| 1721 | |||
| 1722 | Examples: | ||
| 1723 | |||
| 1724 | # private method | ||
| 1725 | private method : int sum ($num1 : int, $num2 : int) { | ||
| 1726 | return $num1 + $num2; | ||
| 1727 | } | ||
| 1728 | |||
| 1729 | # precompile method | ||
| 1730 | precompile method : int sum ($num1 : int, $num2 : int) { | ||
| 1731 | return $num1 + $num2; | ||
| 1732 | } | ||
| 1733 | |||
| 1734 | # native method | ||
| 1735 | native method : int sum ($num1 : int, $num2 : int); | ||
| 1736 | |||
| 1737 | =head2 Class Method | ||
| 1738 | |||
| 1739 | A method defined with the C<static> L<method attribute|/"Method Attributes"> is a class method. | ||
| 1740 | |||
| 1741 | A class method can be called by a L<class method call|SPVM::Document::Language::Operators/"Class Method Call">. | ||
| 1742 | |||
| 1743 | =head2 Instance Method | ||
| 1744 | |||
| 1745 | A method defined without the C<static> L<method attribute|/"Method Attributes"> is an instance method. | ||
| 1746 | |||
| 1747 | An instance method can be called by an L<instance method call|SPVM::Document::Language::Operators/"Instance Method Call">. | ||
| 1748 | |||
| 1749 | =head2 Interface Method | ||
| 1750 | |||
| 1751 | An instance method defined in an L<interface|/"Interface"> is an interface method. | ||
| 1752 | |||
| 1753 | Normally, an interface method does not have a method block. | ||
| 1754 | |||
| 1755 | method my_method : int (); | ||
| 1756 | |||
| 1757 | But, an interface method can have a method block. | ||
| 1758 | |||
| 1759 | method my_method : int () { | ||
| 1760 | |||
| 1761 | } | ||
| 1762 | |||
| 1763 | An interface method can have the C<required> L<method attribute|/"Method Attributes"> that indicates classes must define a method with the same name that implements this interface method. | ||
| 1764 | |||
| 1765 | required method my_method : int (); | ||
| 1766 | |||
| 1767 | =head2 INIT Block | ||
| 1768 | |||
| 1769 | An C<INIT> block is a special scope block to initialize class variables or perform setup tasks. | ||
| 1770 | |||
| 1771 | our $FOO : int; | ||
| 1772 | |||
| 1773 | INIT { | ||
| 1774 | $FOO = 1; | ||
| 1775 | } | ||
| 1776 | |||
| 1777 | Multiple C<INIT> blocks can be defined: | ||
| 1778 | |||
| 1779 | INIT { | ||
| 1780 | # First setup | ||
| 1781 | } | ||
| 1782 | |||
| 1783 | INIT { | ||
| 1784 | # Second setup | ||
| 1785 | } | ||
| 1786 | |||
| 1787 | All C<INIT> blocks in a class are merged into a single L<INIT method|/"INIT Method"> at compile time. | ||
| 1788 | |||
| 1789 | =head2 INIT Method | ||
| 1790 | |||
| 1791 | An C<INIT> method is a special method that is automatically generated by the compiler. | ||
| 1792 | |||
| 1793 | It consists of all L<INIT blocks|/"INIT Block"> in a class. If no C<INIT> block exists, an empty C<INIT> method is generated. | ||
| 1794 | |||
| 1795 | This method has no arguments and its return type is the C<void> type. | ||
| 1796 | |||
| 1797 | The C<INIT> methods of the parent class and classes loaded via L<use statements|/"use Statement"> are called at the beginning of the C<INIT> method of the current class. | ||
| 1798 | |||
| 1799 | Note that the execution order of C<INIT> methods among different classes is not generally guaranteed. However, the C<INIT> methods of L<Default Loaded Classes|/"Default Loaded Classes"> are guaranteed to be called before any user-defined C<INIT> methods. | ||
| 1800 | |||
| 1801 | This method is automatically called after all classes are loaded, and before the first method in the program is called. It can also be called manually. | ||
| 1802 | |||
| 1803 | Examples: | ||
| 1804 | |||
| 1805 | # Examples of INIT blocks | ||
| 1806 | class MyClass { | ||
| 1807 | use Point; | ||
| 1808 | |||
| 1809 | our $NUM : int; | ||
| 1810 | our $STRING : string; | ||
| 1811 | our $POINT : Point; | ||
| 1812 | |||
| 1813 | INIT { | ||
| 1814 | $NUM = 3; | ||
| 1815 | $STRING = "abc"; | ||
| 1816 | } | ||
| 1817 | |||
| 1818 | INIT { | ||
| 1819 | $POINT = Point->new(1, 2); | ||
| 1820 | } | ||
| 1821 | |||
| 1822 | INIT { | ||
| 1823 | OtherClass->INIT; | ||
| 1824 | } | ||
| 1825 | } | ||
| 1826 | |||
| 1827 | =head2 END Block | ||
| 1828 | |||
| 1829 | An C<END> block is a special scope block to perform cleanup tasks. | ||
| 1830 | |||
| 1831 | END { | ||
| 1832 | # Cleanup tasks | ||
| 1833 | } | ||
| 1834 | |||
| 1835 | Multiple C<END> blocks can be defined: | ||
| 1836 | |||
| 1837 | END { | ||
| 1838 | # First cleanup | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | END { | ||
| 1842 | # Second cleanup | ||
| 1843 | } | ||
| 1844 | |||
| 1845 | All C<END> blocks in a class are merged into a single L<END method|/"END Method"> in reverse order (LIFO) at compile time. | ||
| 1846 | |||
| 1847 | =head2 END Method | ||
| 1848 | |||
| 1849 | An C<END> method is a special method that is automatically generated by the compiler. | ||
| 1850 | |||
| 1851 | It consists of all L<END blocks|/"END Block"> in a class. If no C<END> block exists, an empty C<END> method is generated. | ||
| 1852 | |||
| 1853 | This method has no arguments and its return type is the C<void> type. | ||
| 1854 | |||
| 1855 | Note that the execution order of C<END> methods among different classes is not generally guaranteed. | ||
| 1856 | |||
| 1857 | If an exception occurs in an C<END> method, it is converted to a warning message and printed to C<stderr>, and the program continues its termination process. | ||
| 1858 | |||
| 1859 | This method is automatically called when the program is about to terminate, just before class variables are destroyed. It can also be called manually. | ||
| 1860 | |||
| 1861 | =head2 Destructor | ||
| 1862 | |||
| 1863 | A destructor is a method called just before an object is destroyed. | ||
| 1864 | |||
| 1865 | method DESTROY : void () { | ||
| 1866 | |||
| 1867 | } | ||
| 1868 | |||
| 1869 | A destructor is an L<instance method|/"Instance Method">. | ||
| 1870 | |||
| 1871 | The name of a destructor is C<DESTROY>. | ||
| 1872 | |||
| 1873 | A destructor has no arguments. | ||
| 1874 | |||
| 1875 | The retrun type is void type. | ||
| 1876 | |||
| 1877 | An L<exception|SPVM::Document::Language::ExceptionHandling/"Exception Handling"> thrown in a destructor is converted to a warning message. | ||
| 1878 | |||
| 1879 | The warning message is printed to L<SPVM's standard error|SPVM::Document::Language::System/"Standard Streams">. | ||
| 1880 | |||
| 1881 | If the reference count of an object is 0, the destructor method in the class of the object is called. | ||
| 1882 | |||
| 1883 | In this time, the return value and an exception variable in the current L<runtime stack|SPVM::Document::NativeClass/"Runtime Stack"> is saved before the call to the destructor method, and restore it after the call. | ||
| 1884 | |||
| 1885 | So, the exception variable in the current runtime stack cannot be changed in a destructor. | ||
| 1886 | |||
| 1887 | See L<Garbage Collection|SPVM::Document::Language::GarbageCollection/"Garbage Collection"> about garbage collection. | ||
| 1888 | |||
| 1889 | If the current class does not have C<DESTROY> method and its super class have C<DESTROY> method, the C<DESTROY> in its super class is called just before an object is destroyed. | ||
| 1890 | |||
| 1891 | Compilation Errors: | ||
| 1892 | |||
| 1893 | A destructor must be an L<instance method|/"Instance Method">. Otherwise, a compilation error occurs. | ||
| 1894 | |||
| 1895 | The name of a destructor must be C<DESTROY>. Otherwise, a compilation error occurs. | ||
| 1896 | |||
| 1897 | If a destructor has arguments, a compilation error occurs. | ||
| 1898 | |||
| 1899 | The retrun type must be void type. Otherwise, a compilation error occurs. | ||
| 1900 | |||
| 1901 | If two or more destructors are defined, a compilation error occurs. | ||
| 1902 | |||
| 1903 | Examples: | ||
| 1904 | |||
| 1905 | # Destructor | ||
| 1906 | class MyClass { | ||
| 1907 | method DESTROY : void () { | ||
| 1908 | say "DESTROY"; | ||
| 1909 | } | ||
| 1910 | } | ||
| 1911 | |||
| 1912 | =head2 Method Override | ||
| 1913 | |||
| 1914 | An instance method in a parent class can be overridden by an instance method with the same name in a child class. | ||
| 1915 | |||
| 1916 | class ChildClass extends ParentClass { | ||
| 1917 | # Method Override | ||
| 1918 | method clear : void () { | ||
| 1919 | # ... | ||
| 1920 | } | ||
| 1921 | } | ||
| 1922 | |||
| 1923 | class ParentClass { | ||
| 1924 | |||
| 1925 | method clear : void () { | ||
| 1926 | # ... | ||
| 1927 | } | ||
| 1928 | } | ||
| 1929 | |||
| 1930 | The overridding method in the child class must satisfy L<interface method requirement|SPVM::Document::Language::Types/"Interface Method Requirement"> to the parent method interpretted as an interface method. | ||
| 1931 | |||
| 1932 | Compilation Errors: | ||
| 1933 | |||
| 1934 | The overridding method in the child class must satisfy L<interface requirement|SPVM::Document::Language::Types/"Interface Requirement"> to the parent method. Otherwise, a compilation error occurs. | ||
| 1935 | |||
| 1936 | =head2 Native Method | ||
| 1937 | |||
| 1938 | A method with the C<native> L<method attribute|/"Method Attributes"> is a native method. | ||
| 1939 | |||
| 1940 | native sum : int ($num1 : int, $num2 : int); | ||
| 1941 | |||
| 1942 | A native method has no L<method block|/"Method Block">. | ||
| 1943 | |||
| 1944 | The implementation of a native method is written by native languages such as the C language, C<C++>. | ||
| 1945 | |||
| 1946 | See L<SPVM::Document::NativeClass> about the way to write the implementation of a native method. | ||
| 1947 | |||
| 1948 | =head2 Precompilation Method | ||
| 1949 | |||
| 1950 | A method with the C<precompile> L<method attribute|/"Method Attributes"> is a precompilation method. | ||
| 1951 | |||
| 1952 | precompile sum : int ($num1 : int, $num2 : int) { | ||
| 1953 | |||
| 1954 | } | ||
| 1955 | |||
| 1956 | The source code of each precompilation method is converted to a C source code and it is compiled to the machine code. | ||
| 1957 | |||
| 1958 | And the machine codes of all precompilation methods in a class is linked to a dynamic link library such as C<MyClass.so> on Linux/Unix, C<MyClass.dll> on Windows, C<MyClass.dylib> on Mac. | ||
| 1959 | |||
| 1960 | The address of machine code of each precompilation method in the dynamic link library is bind to a precompilation method. | ||
| 1961 | |||
| 1962 | Precompilation methods need a L<build directory|SPVM::Document::EnvironmentVariables/"SPVM_BUILD_DIR"> such as C<~/.spvm_build> to compile and link them. | ||
| 1963 | |||
| 1964 | =head2 Bootstrap Method | ||
| 1965 | |||
| 1966 | A bootstrap method is a method where a program start. | ||
| 1967 | |||
| 1968 | void main : void () { | ||
| 1969 | |||
| 1970 | } | ||
| 1971 | |||
| 1972 | The method name is C<main>. | ||
| 1973 | |||
| 1974 | The return type is void type. | ||
| 1975 | |||
| 1976 | It has no arguments. | ||
| 1977 | |||
| 1978 | =head2 Method Implementation | ||
| 1979 | |||
| 1980 | Normally a method has a method block. L<Statements|SPVM::Document::Language::Statements/"Statements"> can be written in a method block. | ||
| 1981 | |||
| 1982 | static method foo : int ($num1 : int, $num2 : int) { | ||
| 1983 | |||
| 1984 | my $total = $num1 + $num2; | ||
| 1985 | |||
| 1986 | return $total; | ||
| 1987 | } | ||
| 1988 | |||
| 1989 | SPVM operation codes are generated from a method implementation. | ||
| 1990 | |||
| 1991 | =head2 Arguments Signature | ||
| 1992 | |||
| 1993 | An arguments signature uniquely identifies the sequence of argument types without the type of invocant. | ||
| 1994 | |||
| 1995 | TYPE1,TYPE2,TYPEn | ||
| 1996 | |||
| 1997 | The arguments signature of the following class method and instance method is C<string,mutable string,string[],Point[][],int*>. | ||
| 1998 | |||
| 1999 | static method args_signature_class_method : void ($args1 : string, $args2 : mutable string, $args3 : string[], $args4 : Point[][], $args5 : int*) { | ||
| 2000 | |||
| 2001 | } | ||
| 2002 | |||
| 2003 | method args_signature_instance_method : void ($args1 : string, $args2 : mutable string, $args3 : string[], $args4 : Point[][], $args5 : int*) { | ||
| 2004 | |||
| 2005 | } | ||
| 2006 | |||
| 2007 | =head2 Variable Length Arguments | ||
| 2008 | |||
| 2009 | The variable length arguments syntax C<object...> is a syntax suger to pass a C<object[]> object as an argument. | ||
| 2010 | |||
| 2011 | static method varargs_method : string ($args : object...) { | ||
| 2012 | |||
| 2013 | } | ||
| 2014 | |||
| 2015 | A C<object[]> object is created with the arguments. Each element is casted to any object type C<object>. | ||
| 2016 | |||
| 2017 | &varags_method("abc", 1); | ||
| 2018 | |||
| 2019 | If no arguments are specified, zero-length C<object[]> object is passed. | ||
| 2020 | |||
| 2021 | &varags_method(); | ||
| 2022 | |||
| 2023 | If you want to pass an C<object[]> object as one argument, you can use the type cast to C<object...>. | ||
| 2024 | |||
| 2025 | my $objects = [(object)"abc", 1]; | ||
| 2026 | |||
| 2027 | &varags_method((object...)$objects); | ||
| 2028 | |||
| 2029 | Compilation Errors: | ||
| 2030 | |||
| 2031 | The use of variable length arguments is restricted to object[] type. Otherwise a compilation error occurs. | ||
| 2032 | |||
| 2033 | The use of variable length arguments must be the last argument. Otherwise a compilation error occurs. | ||
| 2034 | |||
| 2035 | The use of variable length arguments cannot have a default value. If so, a compilation error occurs. | ||
| 2036 | |||
| 2037 | =head2 Generics | ||
| 2038 | |||
| 2039 | Generics are supported. | ||
| 2040 | |||
| 2041 | The type parameter is C<element>. | ||
| 2042 | |||
| 2043 | C<element> and C<element[]> are allowed as a type parameter based type. | ||
| 2044 | |||
| 2045 | element | ||
| 2046 | element[] | ||
| 2047 | |||
| 2048 | A type parameter based type must be the return type of an instance method. | ||
| 2049 | |||
| 2050 | The return type is casted to the element type of a L<generic type|SPVM::Document::Language::Types/"Generic Type">. | ||
| 2051 | |||
| 2052 | Examples: | ||
| 2053 | |||
| 2054 | class MyList { | ||
| 2055 | |||
| 2056 | has array : object[]; | ||
| 2057 | |||
| 2058 | static method new : MyList ($objects : object[]) { | ||
| 2059 | |||
| 2060 | my $self = new MyList; | ||
| 2061 | |||
| 2062 | $self->{array} = $objects; | ||
| 2063 | |||
| 2064 | return $self; | ||
| 2065 | } | ||
| 2066 | |||
| 2067 | method get : element ($index : int) { | ||
| 2068 | |||
| 2069 | return $self->{array}->[$index]; | ||
| 2070 | |||
| 2071 | } | ||
| 2072 | |||
| 2073 | method get_array : element[] () { | ||
| 2074 | |||
| 2075 | return $self->{array}; | ||
| 2076 | } | ||
| 2077 | } | ||
| 2078 | |||
| 2079 | # MyList of string is a generic type | ||
| 2080 | my $list = (MyList of string)MyList->new(["a", "b"]); | ||
| 2081 | |||
| 2082 | # $elem is casted to string type | ||
| 2083 | my $elem = $list->get(0); | ||
| 2084 | |||
| 2085 | # $array is casted to string[] type | ||
| 2086 | my $array = $list->get_array; | ||
| 2087 | |||
| 2088 | =head2 Union Mapping | ||
| 2089 | |||
| 2090 | If both the return type and an argument type of a method are L<union types|SPVM::Document::Language::Types/"Union Type">, the return type is automatically cast to the specific type corresponding to the index of the type of the passed argument at compile time. | ||
| 2091 | |||
| 2092 | # Mapping definition: | ||
| 2093 | # Index 0: Int[] -> int[] | ||
| 2094 | # Index 1: Long[] -> long[] | ||
| 2095 | static method union_mapping_method : int[]|long[] ($arg : Int[]|Long[]) { | ||
| 2096 | # ... | ||
| 2097 | } | ||
| 2098 | |||
| 2099 | # The return type is automatically cast to int[] | ||
| 2100 | my $int_array = &union_mapping_method(Int->new_array([1, 2])); | ||
| 2101 | |||
| 2102 | # The return type is automatically cast to long[] | ||
| 2103 | my $long_array = &union_mapping_method(Long->new_array([4L, 5L])); | ||
| 2104 | |||
| 2105 | This mapping is resolved based on the first argument that has a union type. | ||
| 2106 | |||
| 2107 | If the passed argument does not match any type in the argument's union type, or if no union type argument exists, no automatic casting is performed. In this case, the return type remains the defined union type (which is handled as the any object type C<object>). | ||
| 2108 | |||
| 2109 | =head1 Local Variable | ||
| 2110 | |||
| 2111 | A local variable is a variable that has a L<scope|SPVM::Document::Language::GarbageCollection/"Scope">. | ||
| 2112 | |||
| 2113 | =head2 Local Variable Declaration | ||
| 2114 | |||
| 2115 | A C<my> keyword declares a local variable. | ||
| 2116 | |||
| 2117 | my LOCAL_VAR_NAME | ||
| 2118 | my LOCAL_VAR_NAME : TYPE | ||
| 2119 | my LOCAL_VAR_NAME = VALUE | ||
| 2120 | my LOCAL_VAR_NAME : TYPE = VALUE | ||
| 2121 | |||
| 2122 | I<LOCAL_VAR_NAME> is a L<local variable name|SPVM::Document::Language::Tokenization/"Local Variable Name">. | ||
| 2123 | |||
| 2124 | I<TYPE> is a L<type|SPVM::Document::Language::Types/"Types">. | ||
| 2125 | |||
| 2126 | If I<TYPE> is ommited, I<TYPE> is set to the type of I<VALUE>. This is called L<type inference|/"Type Inference">. | ||
| 2127 | |||
| 2128 | I<VALUE> is an L<operator|SPVM::Document::Language::Operators/"Operators">. | ||
| 2129 | |||
| 2130 | A local variable is declared in a L<scope block|/"Scope Block">. | ||
| 2131 | |||
| 2132 | A local variable declaration performs the operation of L<pushing a local variable on the mortal stack|SPVM::Document::Language::GarbageCollection/"Pushing a Local Variable on the Mortal Stack">. | ||
| 2133 | |||
| 2134 | A local variable declaration is a L<local variable access|SPVM::Document::Language::Operators/"Local Variable Access">. | ||
| 2135 | |||
| 2136 | Compilation Errors: | ||
| 2137 | |||
| 2138 | I<LOCAL_VAR_NAME> must be a L<local variable name|SPVM::Document::Language::Tokenization/"Local Variable Name">. Otherwise a compilation error occurs. | ||
| 2139 | |||
| 2140 | I<TYPE> must be a L<numeric type|SPVM::Document::Language::Types/"Numeric Types">, an L<object type|SPVM::Document::Language::Types/"Object Types">, L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Types">, or a L<reference type|SPVM::Document::Language::Types/"Reference Types">. Otherwise a compilation error occurs. | ||
| 2141 | |||
| 2142 | If I<TYPE> is not resolved, a compilation error occurs. | ||
| 2143 | |||
| 2144 | Examples: | ||
| 2145 | |||
| 2146 | # Examples of local variable declarations | ||
| 2147 | my $var : int; | ||
| 2148 | my $var : Point; | ||
| 2149 | my $var : Complex_2d; | ||
| 2150 | my $var : int*; | ||
| 2151 | |||
| 2152 | my $num = 1; | ||
| 2153 | |||
| 2154 | my $num = 1.0; | ||
| 2155 | |||
| 2156 | my $ppp = my $bar = 4; | ||
| 2157 | |||
| 2158 | if (my $bar = 1) { | ||
| 2159 | |||
| 2160 | } | ||
| 2161 | |||
| 2162 | while (my $bar = 1) { | ||
| 2163 | |||
| 2164 | } | ||
| 2165 | |||
| 2166 | =head3 Type Inference | ||
| 2167 | |||
| 2168 | If the type of the local variable declaration is ommited, the type of the local variable is set to the right operand type of assignment operator. This is called type inference. | ||
| 2169 | |||
| 2170 | # int | ||
| 2171 | my $num = 1; | ||
| 2172 | |||
| 2173 | # double | ||
| 2174 | my $num = 1.0; | ||
| 2175 | |||
| 2176 | # Foo | ||
| 2177 | my $foo = new Foo; | ||
| 2178 | |||
| 2179 | =head2 Local Temporary Variable | ||
| 2180 | |||
| 2181 | The declaration of a local variable C<$_> is treated as a declaration of a local templrary variable. | ||
| 2182 | |||
| 2183 | my $_ : TYPE; | ||
| 2184 | |||
| 2185 | The internal variable name like C<$_.line_12_column_85>. | ||
| 2186 | |||
| 2187 | Local temporary variables can be declared repeatedly with different types in the same scope. | ||
| 2188 | |||
| 2189 | my $_ = 1; | ||
| 2190 | |||
| 2191 | my $_ = 2; | ||
| 2192 | |||
| 2193 | my $_ = Point->new; | ||
| 2194 | |||
| 2195 | Examples: | ||
| 2196 | |||
| 2197 | my $_ = 1; | ||
| 2198 | |||
| 2199 | say $_; | ||
| 2200 | |||
| 2201 | my $_ = 2; | ||
| 2202 | |||
| 2203 | say $_; | ||
| 2204 | |||
| 2205 | my $_ = Point->new; | ||
| 2206 | |||
| 2207 | say $_->x; | ||
| 2208 | |||
| 2209 | my $point = (my $_ = Point->new(1, 2), $_->clear, $_); | ||
| 2210 | |||
| 2211 | =head2 Monitor Variable | ||
| 2212 | |||
| 2213 | A monitor variable is a special local variable that is used for debugging or reflection. | ||
| 2214 | |||
| 2215 | The name of a monitor variable is C<$^MONITOR>. | ||
| 2216 | |||
| 2217 | Only one monitor variable can be declared in a class file. | ||
| 2218 | |||
| 2219 | A monitor variable must be declared as a local variable. | ||
| 2220 | |||
| 2221 | Example: | ||
| 2222 | |||
| 2223 | my $var = new int[3]; | ||
| 2224 | |||
| 2225 | # $^MONITOR is declared as a local variable and assigned a value | ||
| 2226 | my $^MONITOR = $var; | ||
| 2227 | |||
| 2228 | A monitor variable is used by L<Native::BasicType#has_monitor_var|SPVM::Native::BasicType/"has_monitor_var"> and L<Native::BasicType#get_monitor_var_type|SPVM::Native::BasicType/"get_monitor_var_type"> in the native API. | ||
| 2229 | |||
| 2230 | Compilation Errors: | ||
| 2231 | |||
| 2232 | If more than one monitor variable is declared in a class file, a compilation error occurs. | ||
| 2233 | |||
| 2234 | =head1 Symbol Name Resolution | ||
| 2235 | |||
| 2236 | This section describes resolutions of symbol names, such as variable names, class names, field names, method names in the current L<method implementation|/"Method Implementation">. | ||
| 2237 | |||
| 2238 | |||
| 2239 | =head2 Variable Name Resolution | ||
| 2240 | |||
| 2241 | A variable name resolves to a L<class variable access|SPVM::Document::Language::Operators/"Class Variable Access"> or a L<local variable access|SPVM::Document::Language::Operators/"Local Variable Access">. | ||
| 2242 | |||
| 2243 | If C<::> is contained in a variable name, a class variable definition as the same name as I<VAR_NAME> in I<CLASS_TYPE> is searched. | ||
| 2244 | |||
| 2245 | CLASS_TYPE::VAR_NAME | ||
| 2246 | |||
| 2247 | If found, a variable name resloves to a L<class variable access|SPVM::Document::Language::Operators/"Class Variable Access">. | ||
| 2248 | |||
| 2249 | If C<::> is not contained in a variable name, a local variable declaration as the same name as I<VAR_NAME> is searched upwards from the posotion of I<VAR_NAME>. | ||
| 2250 | |||
| 2251 | VAR_NAME | ||
| 2252 | |||
| 2253 | If found, a variable name resloves to a L<local variable access|SPVM::Document::Language::Operators/"Local Variable Access">. | ||
| 2254 | |||
| 2255 | If not found, a class variable definition as the same name as I<VAR_NAME> in L<outmost class|/"Outmost Class"> is searched. | ||
| 2256 | |||
| 2257 | If found, a variable name resloves to a L<class variable access|SPVM::Document::Language::Operators/"Class Variable Access">. | ||
| 2258 | |||
| 2259 | Compilation Errors: | ||
| 2260 | |||
| 2261 | I<VAR_NAME> must be a valid variable name. Otherwise, a compilation error occurs. | ||
| 2262 | |||
| 2263 | The class specified by I<CLASS_TYPE> must be loaded. Otherwise, a compilation error occurs. | ||
| 2264 | |||
| 2265 | The class variable relative name specified by I<VAR_NAME> must be defined in the class specified by I<VAR_NAME>. Otherwise, a compilation error occurs. | ||
| 2266 | |||
| 2267 | If it resolves to a class variable, L<outmost class|/"Outmost Class"> must has the access control to I<VAR_NAME> in the I<CLASS_TYPE>. Otherwise, a compilation error occurs. | ||
| 2268 | |||
| 2269 | =head2 Field Access Resolution | ||
| 2270 | |||
| 2271 | The following syntax resolves to a L<field access|SPVM::Document::Language::Operators/"Field Access">. | ||
| 2272 | |||
| 2273 | INVOCANT->{FIELD_NAME} | ||
| 2274 | |||
| 2275 | The type of I<INVOCANT> is a L<class type|SPVM::Document::Language::Types/"Class Type">, a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Type">, or a L<multi-numeric reference type|SPVM::Document::Language::Types/"Multi-Numeric Reference Type">. | ||
| 2276 | |||
| 2277 | I<FIELD_NAME> is a L<field name|SPVM::Document::Language::Tokenization/"Field Name">. | ||
| 2278 | |||
| 2279 | A field specified by I<FIELD_NAME> is searched in the type of I<INVOCANT>. | ||
| 2280 | |||
| 2281 | If it is found and the type of I<INVOCANT> is a L<class type|SPVM::Document::Language::Types/"Class Type">, it resolves to a field access for class types. | ||
| 2282 | |||
| 2283 | If it is found and the type of I<INVOCANT> is a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Type">, it resolves to a field access for multi-numeric types. | ||
| 2284 | |||
| 2285 | If it is found and the type of I<INVOCANT> is a L<multi-numeric reference type|SPVM::Document::Language::Types/"Multi-Numeric Reference Type">, it resolves to a field access for multi-numeric reference types. | ||
| 2286 | |||
| 2287 | Compilation Errors: | ||
| 2288 | |||
| 2289 | I<INVOCANT> must be an object of a L<class type|SPVM::Document::Language::Types/"Class Type">, a multi-numeric number of a L<multi-numeric type|SPVM::Document::Language::Types/"Multi-Numeric Type">, a multi-numeric number referenced by a L<multi-numeric reference type|SPVM::Document::Language::Types/"Multi-Numeric Reference Type">. Otherwise, a compilation error occurs. | ||
| 2290 | |||
| 2291 | If the type of I<INVOCANT> is a class type, the field specified by I<FIELD_NAME> must be defined in the class, or its super classes. Otherwise, a compilation error occurs. | ||
| 2292 | |||
| 2293 | If the type of I<INVOCANT> is a multi-numeric type, the field specified by I<FIELD_NAME> must be defined in the multi-numeric type. Otherwise, a compilation error occurs. | ||
| 2294 | |||
| 2295 | If the type of I<INVOCANT> is a multi-numeric reference type, the field specified by I<FIELD_NAME> must be defined in the multi-numeric type referenced by the multi-numeric reference type. Otherwise, a compilation error occurs. | ||
| 2296 | |||
| 2297 | The L<outmost class|/"Outmost Class"> must has the access control to I<FIELD_NAME> in the type of I<INVOCANT>. Otherwise, a compilation error occurs. | ||
| 2298 | |||
| 2299 | =head2 Method Call Resolution | ||
| 2300 | |||
| 2301 | A L<method call|SPVM::Document::Language::Operators/"Method Call"> is an operation to call a method. | ||
| 2302 | |||
| 2303 | A method call resolves to one of the three types of method calls, a class method call, a static instance method call, and an instance method call. | ||
| 2304 | |||
| 2305 | =head3 Class Method Call Resolution | ||
| 2306 | |||
| 2307 | A class method call calls a class method. | ||
| 2308 | |||
| 2309 | CLASS_TYPE->METHOD_NAME | ||
| 2310 | CLASS_TYPE->METHOD_NAME(OPT_ARGS) | ||
| 2311 | &METHOD_NAME | ||
| 2312 | &METHOD_NAME(OPT_ARGS) | ||
| 2313 | |||
| 2314 | I<CLASS_TYPE> is a L<class type|SPVM::Document::Language::Types/"Class Types"> or a class alias name created by an L<alias statement|/"alias Statement">. | ||
| 2315 | |||
| 2316 | C<&> is converted to L<outmost class|/"Outmost Class">. This becomes C<I<CLASS_TYPE-E<gt>>>. | ||
| 2317 | |||
| 2318 | I<METHOD_NAME> is a L<method name|SPVM::Document::Language::Tokenization/"Method Name">. | ||
| 2319 | |||
| 2320 | I<OPT_ARGS> is one of | ||
| 2321 | |||
| 2322 | EMPTY | ||
| 2323 | ARGS | ||
| 2324 | |||
| 2325 | I<EMPTY> means nothing exists. | ||
| 2326 | |||
| 2327 | I<ARGS> is one of | ||
| 2328 | |||
| 2329 | ARGS , ARG | ||
| 2330 | ARG | ||
| 2331 | |||
| 2332 | I<ARG> is an L<operator|SPVM::Document::Language::Operators/"Operators">. | ||
| 2333 | |||
| 2334 | If a method specified I<METHOD_NAME> is searched in I<CLASS_TYPE>. | ||
| 2335 | |||
| 2336 | If found, it resolves to a L<class method call|SPVM::Document::Language::Operators/"Class Method Call">. | ||
| 2337 | |||
| 2338 | The return type is the type of the found method. | ||
| 2339 | |||
| 2340 | Compilation Errors: | ||
| 2341 | |||
| 2342 | If the method is not found, a compilation error occurs. | ||
| 2343 | |||
| 2344 | If the found method is an instance method, a compilation error occurs. | ||
| 2345 | |||
| 2346 | If the type of I<ARG> does not satisfy L<assignment requirement|SPVM::Document::Language::Types/"Assignment Requirement">, a compilation error occurs. | ||
| 2347 | |||
| 2348 | If the length of I<ARGS> is too many, a compilation error occurs. | ||
| 2349 | |||
| 2350 | If the length of I<ARGS> is too few, a compilation error occurs. | ||
| 2351 | |||
| 2352 | The L<outmost class|/"Outmost Class"> must has the access control to the found method. Otherwise, a compilation error occurs. | ||
| 2353 | |||
| 2354 | Examples: | ||
| 2355 | |||
| 2356 | # Examples of class method calls | ||
| 2357 | class MyClass { | ||
| 2358 | |||
| 2359 | static method main : void () { | ||
| 2360 | my $ret = Fn->INT_MAX; | ||
| 2361 | |||
| 2362 | my $ret = Fn->abs(-5); | ||
| 2363 | |||
| 2364 | my $ret = &sum(1, 2); | ||
| 2365 | } | ||
| 2366 | |||
| 2367 | static method sum : int ($num1 : int, $num2 : int) { | ||
| 2368 | return $num1 + $num2; | ||
| 2369 | } | ||
| 2370 | } | ||
| 2371 | |||
| 2372 | =head4 Inline Expansion of Method Call to Get an Enuemration Value | ||
| 2373 | |||
| 2374 | An inline expansion is performed on every class method call to get an enumeration value, and it is replaced with the return value. | ||
| 2375 | |||
| 2376 | Examples: | ||
| 2377 | |||
| 2378 | class MyClass { | ||
| 2379 | enum { | ||
| 2380 | FLAG1 = 5; | ||
| 2381 | } | ||
| 2382 | |||
| 2383 | method main : void () { | ||
| 2384 | |||
| 2385 | # This is replaced with 5. | ||
| 2386 | MyClass->FLAG1; | ||
| 2387 | } | ||
| 2388 | } | ||
| 2389 | |||
| 2390 | =head3 Static Instance Method Call Resolution | ||
| 2391 | |||
| 2392 | A static instance method call calls an instance method specifying a class. | ||
| 2393 | |||
| 2394 | INVOCANT->CLASS_TYPE::METHOD_NAME | ||
| 2395 | INVOCANT->CLASS_TYPE::METHOD_NAME(OPT_ARGS) | ||
| 2396 | |||
| 2397 | I<INVOCANT> is an object of a L<class type|SPVM::Document::Language::Types/"Class Types"> or an L<interface type|SPVM::Document::Language::Types/"Interface Types">. | ||
| 2398 | |||
| 2399 | I<CLASS_TYPE> is a L<class type|SPVM::Document::Language::Types/"Class Types">, an L<interface type|SPVM::Document::Language::Types/"Interface Types">, or C<SUPER>. | ||
| 2400 | |||
| 2401 | If C<SUPER> is specified, a method specified by I<METHOD_NAME> is searched in the super classes of the current class. If it is found and it is an instance method, C<SUPER> is replaced to the class of the found method. This becomes I<CLASS_TYPE>. | ||
| 2402 | |||
| 2403 | I<METHOD_NAME> is a L<method name|SPVM::Document::Language::Tokenization/"Method Name">. | ||
| 2404 | |||
| 2405 | I<OPT_ARGS> is one of | ||
| 2406 | |||
| 2407 | EMPTY | ||
| 2408 | ARGS | ||
| 2409 | |||
| 2410 | I<EMPTY> means nothing exists. | ||
| 2411 | |||
| 2412 | I<ARGS> is one of | ||
| 2413 | |||
| 2414 | ARGS , ARG | ||
| 2415 | ARG | ||
| 2416 | |||
| 2417 | I<ARG> is an L<operator|SPVM::Document::Language::Operators/"Operators">. | ||
| 2418 | |||
| 2419 | A method specified I<METHOD_NAME> is searched in I<CLASS_TYPE>. | ||
| 2420 | |||
| 2421 | If found, it resolves to a L<static instance method call|SPVM::Document::Language::Operators/"Static Instance Method Call">. | ||
| 2422 | |||
| 2423 | The return type is the type of the found method. | ||
| 2424 | |||
| 2425 | Compilation Errors: | ||
| 2426 | |||
| 2427 | I<CLASS_TYPE> must be a L<class type|SPVM::Document::Language::Types/"Class Types">, an L<interface type|SPVM::Document::Language::Types/"Interface Types">, or C<SUPER>. Ohterwise a compilation error occurs. | ||
| 2428 | |||
| 2429 | The type of I<INVOCANT> must satisfies L<assignment requirement|SPVM::Document::Language::Types/"Assignment Requirement"> to I<CLASS_TYPE>. Ohterwise a compilation error occurs. | ||
| 2430 | |||
| 2431 | If C<SUPER> is specified and the found method is a class method, a compilation error occurs. | ||
| 2432 | |||
| 2433 | If the method is not found, a compilation error occurs. | ||
| 2434 | |||
| 2435 | If the found method is a class method, a compilation error occurs. | ||
| 2436 | |||
| 2437 | If the type of I<ARG> does not satisfy L<assignment requirement|SPVM::Document::Language::Types/"Assignment Requirement">, a compilation error occurs. | ||
| 2438 | |||
| 2439 | If the length of I<ARGS> is too many, a compilation error occurs. | ||
| 2440 | |||
| 2441 | If the length of I<ARGS> is too few, a compilation error occurs. | ||
| 2442 | |||
| 2443 | The L<outmost class|/"Outmost Class"> must has the access control to the found method. Otherwise, a compilation error occurs. | ||
| 2444 | |||
| 2445 | Examples: | ||
| 2446 | |||
| 2447 | # Examples of static instance method calls | ||
| 2448 | my $point3d = Point3D->new; | ||
| 2449 | |||
| 2450 | $point3d->Point::clear; | ||
| 2451 | |||
| 2452 | $point3d->SUPER::clear; | ||
| 2453 | |||
| 2454 | =head3 Instance Method Call Resolution | ||
| 2455 | |||
| 2456 | An instance method call calls an instance method. | ||
| 2457 | |||
| 2458 | INVOCANT->METHOD_NAME | ||
| 2459 | INVOCANT->METHOD_NAME(OPT_ARGS) | ||
| 2460 | |||
| 2461 | I<INVOCANT> is an object of a L<class type|SPVM::Document::Language::Types/"Class Types"> or an L<interface type|SPVM::Document::Language::Types/"Interface Types">. | ||
| 2462 | |||
| 2463 | I<METHOD_NAME> is a L<method name|SPVM::Document::Language::Tokenization/"Method Name">. | ||
| 2464 | |||
| 2465 | I<OPT_ARGS> is one of | ||
| 2466 | |||
| 2467 | EMPTY | ||
| 2468 | ARGS | ||
| 2469 | |||
| 2470 | I<EMPTY> means nothing exists. | ||
| 2471 | |||
| 2472 | I<ARGS> is one of | ||
| 2473 | |||
| 2474 | ARGS , ARG | ||
| 2475 | ARG | ||
| 2476 | |||
| 2477 | I<ARG> is an L<operator|SPVM::Document::Language::Operators/"Operators">. | ||
| 2478 | |||
| 2479 | If the type of I<INVOCANT> is a L<class type|SPVM::Document::Language::Types/"Class Types">, a method specified I<METHOD_NAME> is searched in the class and its super classes. | ||
| 2480 | |||
| 2481 | If found, it resolves to an L<instance method call|SPVM::Document::Language::Operators/"Instance Method Call">. | ||
| 2482 | |||
| 2483 | If the type of I<INVOCANT> is an L<interface type|SPVM::Document::Language::Types/"Interface Types">, a method specified I<METHOD_NAME> is searched in the interface. | ||
| 2484 | |||
| 2485 | If found, it resolves to an L<instance method call|SPVM::Document::Language::Operators/"Instance Method Call">. | ||
| 2486 | |||
| 2487 | The return type is the type of the found method. | ||
| 2488 | |||
| 2489 | Compilation Errors: | ||
| 2490 | |||
| 2491 | The type of I<INVOCANT> must be a L<class type|SPVM::Document::Language::Types/"Class Types"> or an L<interface type|SPVM::Document::Language::Types/"Interface Types">. | ||
| 2492 | |||
| 2493 | If the method specified by I<METHOD_NAME> is not found, a compilation error occurs. | ||
| 2494 | |||
| 2495 | If the found method is a class method, a compilation error occurs. | ||
| 2496 | |||
| 2497 | If the type of I<ARG> does not satisfy L<assignment requirement|SPVM::Document::Language::Types/"Assignment Requirement">, a compilation error occurs. | ||
| 2498 | |||
| 2499 | If the length of I<ARGS> is too many, a compilation error occurs. | ||
| 2500 | |||
| 2501 | If the length of I<ARGS> is too few, a compilation error occurs. | ||
| 2502 | |||
| 2503 | The L<outmost class|/"Outmost Class"> must has the access control to the found method. Otherwise, a compilation error occurs. | ||
| 2504 | |||
| 2505 | Examples: | ||
| 2506 | |||
| 2507 | # Examples of instance method calls | ||
| 2508 | |||
| 2509 | my $point = Point->new; | ||
| 2510 | |||
| 2511 | $point->clear; | ||
| 2512 | |||
| 2513 | my $stringable = (Stringable)$point; | ||
| 2514 | |||
| 2515 | my $string = $strinble->to_string; | ||
| 2516 | |||
| 2517 | =head1 Block | ||
| 2518 | |||
| 2519 | A block is the part enclosed by C<{> and C<}>. | ||
| 2520 | |||
| 2521 | =head2 Class Block | ||
| 2522 | |||
| 2523 | A class block is a block used in a class definition. | ||
| 2524 | |||
| 2525 | class MyClass { | ||
| 2526 | |||
| 2527 | } | ||
| 2528 | |||
| 2529 | =head2 Enumeration Block | ||
| 2530 | |||
| 2531 | An enumeration block is a block used in an enumeration definition. | ||
| 2532 | |||
| 2533 | enum { | ||
| 2534 | ONE, | ||
| 2535 | TWO, | ||
| 2536 | } | ||
| 2537 | |||
| 2538 | =head2 Scope Block | ||
| 2539 | |||
| 2540 | A scope block is a block that has its L<scope|SPVM::Document::Language::GarbageCollection/"Scope">. | ||
| 2541 | |||
| 2542 | Zero or more L<statements|SPVM::Document::Language::Statements/"Statements"> are written in a scope block. | ||
| 2543 | |||
| 2544 | =head3 Simple Block | ||
| 2545 | |||
| 2546 | A simple block is a scope block. | ||
| 2547 | |||
| 2548 | { | ||
| 2549 | 1; | ||
| 2550 | } | ||
| 2551 | |||
| 2552 | A simple block must have at least one statements. Otherwise, it is intepreted as a L<key-value array initialization|SPVM::Document::Language::Operators/"Key-Value Array Initialization">. | ||
| 2553 | |||
| 2554 | =head3 Method Block | ||
| 2555 | |||
| 2556 | A method block is a scope block. | ||
| 2557 | |||
| 2558 | static method foo : int () { | ||
| 2559 | |||
| 2560 | } | ||
| 2561 | |||
| 2562 | =head3 INIT Block | ||
| 2563 | |||
| 2564 | An C<INIT> block is a scope block. | ||
| 2565 | |||
| 2566 | INIT { | ||
| 2567 | |||
| 2568 | } | ||
| 2569 | |||
| 2570 | =head3 eval Block | ||
| 2571 | |||
| 2572 | An C<eval> block is a scope block. | ||
| 2573 | |||
| 2574 | eval { | ||
| 2575 | |||
| 2576 | } | ||
| 2577 | |||
| 2578 | =head3 if Block | ||
| 2579 | |||
| 2580 | An C<if> block is a scope block. | ||
| 2581 | |||
| 2582 | if (CONDITION) { | ||
| 2583 | |||
| 2584 | } | ||
| 2585 | |||
| 2586 | =head3 elsif Block | ||
| 2587 | |||
| 2588 | An C<elsif> block is a scope block. | ||
| 2589 | |||
| 2590 | elsif (CONDITION) { | ||
| 2591 | |||
| 2592 | } | ||
| 2593 | |||
| 2594 | =head3 else Block | ||
| 2595 | |||
| 2596 | An C<else> block is a scope block. | ||
| 2597 | |||
| 2598 | else { | ||
| 2599 | |||
| 2600 | } | ||
| 2601 | |||
| 2602 | =head3 for Block | ||
| 2603 | |||
| 2604 | A C<for> block is a scope block. | ||
| 2605 | |||
| 2606 | for (my $i = 0; $i < 3; $i++) { | ||
| 2607 | |||
| 2608 | } | ||
| 2609 | |||
| 2610 | =head3 while Block | ||
| 2611 | |||
| 2612 | A C<while> block is a scope block. | ||
| 2613 | |||
| 2614 | while (CONDITION) { | ||
| 2615 | |||
| 2616 | } | ||
| 2617 | |||
| 2618 | =head3 switch Block | ||
| 2619 | |||
| 2620 | A C<switch> block is a scope block. | ||
| 2621 | |||
| 2622 | switch (CONDITION) { | ||
| 2623 | |||
| 2624 | } | ||
| 2625 | |||
| 2626 | =head3 case Block | ||
| 2627 | |||
| 2628 | A C<case> block is a scope block. | ||
| 2629 | |||
| 2630 | case CASE_VALUE1: { | ||
| 2631 | # ... | ||
| 2632 | } | ||
| 2633 | |||
| 2634 | =head3 default Block | ||
| 2635 | |||
| 2636 | A C<default> block is a scope block. | ||
| 2637 | |||
| 2638 | default: { | ||
| 2639 | # ... | ||
| 2640 | } | ||
| 2641 | |||
| 2642 | =head1 See Also | ||
| 2643 | |||
| 2644 | =over 2 | ||
| 2645 | |||
| 2646 | =item * L<SPVM::Document::Language::SyntaxParsing> | ||
| 2647 | |||
| 2648 | =item * L<SPVM::Document::Language::Operators> | ||
| 2649 | |||
| 2650 | =item * L<SPVM::Document::Language::Statements> | ||
| 2651 | |||
| 2652 | =item * L<SPVM::Document::Language> | ||
| 2653 | |||
| 2654 | =item * L<SPVM::Document> | ||
| 2655 | |||
| 2656 | =back | ||
| 2657 | |||
| 2658 | =head1 Copyright & License | ||
| 2659 | |||
| 2660 | Copyright (c) 2023 Yuki Kimoto | ||
| 2661 | |||
| 2662 | MIT License |