Coding Conventions - yuki-kimoto/SPVM GitHub Wiki

This topic describes coding convension for the SPVM programming language.

If you follow these coding convension, you receive the benefits of common consciousness.

Method Names

Lower case and snake case.

method foo_bar : void ();

Field Names

Lower case and snake case.

has foo_bar : int;

Class Variable Names

Upper case and snake case.

our $FOO_BAR : int;

If you use a class variable only in a method or a few methods and it is private one, it is good to use lower case.

our $my_re : Regex;

INIT {
  $my_re = Regex->new("[a-z]");
}

method foo : void () {
  
  my $string = "a";

  if ($my_re->match($string)) {

  }
}

Constructor Names and Its Arguments

A constructor name is new. The arguments are a value of object[] type or any type of arguments depending on the frequency of use.

class IO::Socket::IP {
  static method new : IO::Socket::IP ($options : object[] = undef) {
  
  }
}
class Point {
  static method new : Point ($x : int = 0, $y : int = 0) {
  
  }
}
class Regex {
  static method new : Regex ($pattern : string) {
  
  }
}

If you want to create a constructor method that has different arguments, use _with_ suffix.

class Regex {
  static method new_with_options : Regex ($options : object[] = undef) {
  
  }
}
class Native::MethodCall {
  static method new_with_env_stack : Regex ($env : Native::Env, $stack : Native::Stack) {
  
  }
}

Native Method Implementation

Declare error_id variable setting to 0..

And get arguments from stack at the beginning. This is because the values on stack are changed for method calls.


int32_t SPVM__Native__MethodCall__new_method_with_env_stack_common(SPVM_ENV* env, SPVM_VALUE* stack, int32_t method_call_type) {
  
  int32_t error_id = 0;
  
  void* obj_self_env = stack[0].oval;
  
  void* obj_self_stack = stack[1].oval;
  
  // ...

  return 0;
}