| 1 | package SPVM::Native::Compiler; | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | 1; | ||
| 6 | |||
| 7 | =head1 Name | ||
| 8 | |||
| 9 | SPVM::Native::Compiler - Native::Compiler | ||
| 10 | |||
| 11 | =head1 Description | ||
| 12 | |||
| 13 | C<SPVM::Native::Compiler> is C<Native::Compiler> class in L<SPVM> language. It compiles SPVM source codes and builds the runtime. | ||
| 14 | |||
| 15 | The instance of L<Native::Runtime|SPVM::Native::Runtime> class is build by L</"get_runtime"> method in this class. | ||
| 16 | |||
| 17 | =head1 Usage | ||
| 18 | |||
| 19 | use Native::Compiler; | ||
| 20 | |||
| 21 | my $compiler = Native::Compiler->new; | ||
| 22 | |||
| 23 | $compiler->add_class_search_dir("lib"); | ||
| 24 | |||
| 25 | $compiler->set_start_file(__FILE__); | ||
| 26 | |||
| 27 | { | ||
| 28 | my $basic_type_name = "Foo"; | ||
| 29 | $compiler->set_start_line(__LINE__ + 1); | ||
| 30 | $compiler->compile($basic_type_name); | ||
| 31 | } | ||
| 32 | |||
| 33 | { | ||
| 34 | my $basic_type_name = "Bar"; | ||
| 35 | $compiler->set_start_line(__LINE__ + 1); | ||
| 36 | $compiler->compile($basic_type_name); | ||
| 37 | } | ||
| 38 | |||
| 39 | my $runtime = $compiler->get_runtime; | ||
| 40 | |||
| 41 | =head1 Pointer | ||
| 42 | |||
| 43 | The C<Native::Compiler> class is a L<pointer class|SPVM::Document::Language/"Pointer Class">. | ||
| 44 | |||
| 45 | Its insntace has a pointer to a L<compiler|SPVM::Document::NativeAPI::Native::Compiler> object. | ||
| 46 | |||
| 47 | =head1 Class Methods | ||
| 48 | |||
| 49 | =head2 new | ||
| 50 | |||
| 51 | C<static method new : L<Native::Compiler|SPVM::Native::Compiler> ();> | ||
| 52 | |||
| 53 | Creates a new C<Native::Compiler> object and returns it. | ||
| 54 | |||
| 55 | =head1 Instance Methods | ||
| 56 | |||
| 57 | =head2 get_class_search_dirs_length | ||
| 58 | |||
| 59 | C<method get_class_search_dirs_length : int ();> | ||
| 60 | |||
| 61 | Returns the length of the class search directories. | ||
| 62 | |||
| 63 | =head2 get_class_search_dir | ||
| 64 | |||
| 65 | C<method get_class_search_dir : string ($index : int);> | ||
| 66 | |||
| 67 | Returns a class search directory at the index $index. | ||
| 68 | |||
| 69 | =head2 add_class_search_dir | ||
| 70 | |||
| 71 | C<method add_class_search_dir : void ($class_search_dir : string);> | ||
| 72 | |||
| 73 | Adds a class search directory at the tail of the current class search directories. | ||
| 74 | |||
| 75 | =head2 prepend_class_search_dir | ||
| 76 | |||
| 77 | C<method prepend_class_search_dir : void ($class_search_dir : string);> | ||
| 78 | |||
| 79 | Adds a class search directory at the head of the current class search directories. | ||
| 80 | |||
| 81 | =head2 clear_class_search_dirs | ||
| 82 | |||
| 83 | C<method clear_class_search_dirs : void ();> | ||
| 84 | |||
| 85 | Removes all class search directories. | ||
| 86 | |||
| 87 | =head2 set_start_file | ||
| 88 | |||
| 89 | C<method set_start_file : void ($start_file : string);> | ||
| 90 | |||
| 91 | Sets the name of the file to start the compiling by L</"compile"> method. | ||
| 92 | |||
| 93 | =head2 set_start_line | ||
| 94 | |||
| 95 | C<method set_start_line : void ($start_line : int);> | ||
| 96 | |||
| 97 | Sets the line to start compiling by L</"compile"> method. | ||
| 98 | |||
| 99 | =head2 compile | ||
| 100 | |||
| 101 | C<method compile : void ($class_name : string);> | ||
| 102 | |||
| 103 | Compiles a class given by the class name $class_name. | ||
| 104 | |||
| 105 | This method can be called multiple times. | ||
| 106 | |||
| 107 | Exceptions: | ||
| 108 | |||
| 109 | If compilation errors occurred, an exception is thrown set eval_errro_id to the basic type ID of L<Error::Compile|SPVM::Error::Compile> class. | ||
| 110 | |||
| 111 | =head2 get_error_messages | ||
| 112 | |||
| 113 | C<method get_error_messages : string[] ();> | ||
| 114 | |||
| 115 | Returns compilation error messages in this compiling by L</"compile"> method. | ||
| 116 | |||
| 117 | =head2 get_runtime | ||
| 118 | |||
| 119 | C<method get_runtime : L<Native::Runtime|SPVM::Native::Runtime> ();> | ||
| 120 | |||
| 121 | Returns the runtime. | ||
| 122 | |||
| 123 | The return value is a L<Native::Runtime|SPVM::Native::Runtime> object. | ||
| 124 | |||
| 125 | =head2 get_class_file | ||
| 126 | |||
| 127 | C<method get_class_file : L<Native::ClassFile|SPVM::Native::ClassFile> ($class_name : string);> | ||
| 128 | |||
| 129 | Gets a L<Native::ClassFile> object by a class name, and returns it. | ||
| 130 | |||
| 131 | =head2 compile_anon_class | ||
| 132 | |||
| 133 | C<native method compile_anon_class : string ($source : string);> | ||
| 134 | |||
| 135 | Compiles a anon class, and return the generated anon class name. | ||
| 136 | |||
| 137 | |||
| 138 | This method can be called multiple times. | ||
| 139 | Exceptions: | ||
| 140 | |||
| 141 | If compilation errors occurred, an exception is thrown set eval_errro_id to the basic type ID of L<Error::Compile|SPVM::Error::Compile> class. | ||
| 142 | |||
| 143 | Examples: | ||
| 144 | |||
| 145 | use Native; | ||
| 146 | use Native::Compiler; | ||
| 147 | use Native::MethodCall; | ||
| 148 | |||
| 149 | my $compiler = Native->get_current_compiler; | ||
| 150 | |||
| 151 | my $source = <<'EOS'; | ||
| 152 | class { | ||
| 153 | use Fn; | ||
| 154 | static method sum : int ($num1 : int, $num2 : int) { | ||
| 155 | return $num1 + $num2; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | EOS | ||
| 159 | $compiler->set_start_file(__FILE__); | ||
| 160 | $compiler->set_start_line(__LINE__ + 1); | ||
| 161 | my $anon_class_name = $compiler->compile_anon_class($source);; | ||
| 162 | |||
| 163 | my $ret = Native::MethodCall->call_class_method($anon_class_name, "sum", [(object)1, 2]);; | ||
| 164 | |||
| 165 | say $ret->(Int)->value; | ||
| 166 | |||
| 167 | =head2 compile_script | ||
| 168 | |||
| 169 | C<native method compile_script : string ($source : string);> | ||
| 170 | |||
| 171 | Same as L</"compile_anon_class">. | ||
| 172 | |||
| 173 | Eamples: | ||
| 174 | |||
| 175 | my $source = <<'EOS'; | ||
| 176 | use Fn; | ||
| 177 | my $var = 1; | ||
| 178 | say $var; | ||
| 179 | EOS | ||
| 180 | |||
| 181 | =head1 See Also | ||
| 182 | |||
| 183 | =head2 Native::Runtime | ||
| 184 | |||
| 185 | The instance of L<Native::Runtime|SPVM::Native::Runtime> class is build by L</"get_runtime"> method in this class. | ||
| 186 | |||
| 187 | =head1 Copyright & License | ||
| 188 | |||
| 189 | Copyright 2023-2023 Yuki Kimoto, all rights reserved. | ||
| 190 | |||
| 191 | This program is free software; you can redistribute it and/or modify it | ||
| 192 | under the same terms as Perl itself. | ||
| 193 | |||
| 194 | =head1 Copyright & License | ||
| 195 | |||
| 196 | Copyright (c) 2023 Yuki Kimoto | ||
| 197 | |||
| 198 | MIT License |