The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

SPVM::Native::Compiler - Native::Compiler

Description

SPVM::Native::Compiler is the Native::Compiler class in the SPVM language. It compiles SPVM source codes and builds the runtime.

The instance of the Native::Runtime class is build by the "get_runtime" method in this class.

Usage

  use Native::Compiler;
  
  my $compiler = Native::Compiler->new;
  
  $compiler->add_include_dir("lib");
  
  $compiler->set_start_file(__FILE__);
  
  {
    my $basic_type_name = "Foo";
    $compiler->set_start_line(__LINE__ + 1);
    $compiler->compile($basic_type_name);
  }
  
  {
    my $basic_type_name = "Bar";
    $compiler->set_start_line(__LINE__ + 1);
    $compiler->compile($basic_type_name);
  }
  
  my $runtime = $compiler->get_runtime;

Pointer

The Native::Compiler class is a pointer class.

Its insntace has a pointer to a compiler object.

Class Methods

new

static method new : Native::Compiler ();

Creates a new Native::Compiler object and returns it.

Instance Methods

add_include_dir

method add_include_dir : void ($include_dir : string);

Adds a class directory to search for classes.

set_start_file

method set_start_file : void ($start_file : string);

Sets the name of the file to start the compiling by the "compile" method.

set_start_line

method set_start_line : void ($start_line : int);

Sets the line to start compiling by the "compile" method.

compile

method compile : void ($class_name : string);

Compiles a class given by the class name $class_name.

This method can be called multiple times.

Exceptions:

If compilation errors occurred, an exception is thrown set eval_errro_id to the basic type ID of the Error::Compile class.

get_error_messages

method get_error_messages : string[] ();

Returns compilation error messages in this compiling by the "compile" method.

get_runtime

method get_runtime : Native::Runtime ();

Returns the runtime.

The return value is a Native::Runtime object.

get_class_file

method get_class_file : Native::ClassFile ($class_name : string);

Gets a Native::ClassFile object by a class name, and returns it.

compile_anon_class

native method compile_anon_class : string ($source : string);

Compiles a anon class, and return the generated anon class name.

This method can be called multiple times. Exceptions:

If compilation errors occurred, an exception is thrown set eval_errro_id to the basic type ID of the Error::Compile class.

Examples:

  use Native;
  use Native::Compiler;
  use Native::MethodCall;
  
  my $compiler = Native->get_current_compiler;
  
  my $source = <<'EOS';
class {
  static method sum : int ($num1 : int, $num2 : int) {
    return $num1 + $num2;
  }
}
EOS
  $compiler->set_start_file(__FILE__);
  $compiler->set_start_line(__LINE__ + 1);
  my $anon_class_name = $compiler->compile_anon_class($source);;
  
  my $ret = Native::MethodCall->call_class_method($anon_class_name, "sum", [(object)1, 2]);;
  
  say $ret->(Int)->value;

eval_string

  method eval_string : string ($main_source : string);

This method emulates Perl's string eval.

This method creates the following source code.

  "
  class {
  static method main : void () {
  #line 1
  $main_source
  }
  }
  "

And calls "compile_anon_class" given this source code and gets the anon class name.

And calls call_class_method in the Native::MethodCall class.

  Native::MethodCall->call_class_method($anon_class_name, "main");

Examples:

  Compiler->eval_string("my $total = 1 + 2; say $total;");

See Also

Native::Runtime

The instance of the Native::Runtime class is build by the "get_runtime" method in this class.

Copyright & License

Copyright 2023-2023 Yuki Kimoto, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License