| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::Java::Runtime; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Perlito5::Java::CORE; | ||
| 7 | use Perlito5::Java::Crypt; | ||
| 8 | use Perlito5::Java::JavaCompiler; | ||
| 9 | use Perlito5::Runtime::Formline; | ||
| 10 | use Perlito5::Runtime::Sprintf; | ||
| 11 | |||
| 12 | sub perl5_to_java { | ||
| 13 | my ($source, $namespace, $want, $scope_java) = @_; | ||
| 14 | |||
| 15 | # say "source: [" . $source . "]"; | ||
| 16 | |||
| 17 | local $_; | ||
| 18 | local ${^GLOBAL_PHASE}; | ||
| 19 | local @Perlito5::BASE_SCOPE = ($scope_java); # ->[0]; | ||
| 20 | local @Perlito5::SCOPE_STMT; | ||
| 21 | local $Perlito5::CLOSURE_SCOPE = 0; | ||
| 22 | local $Perlito5::PKG_NAME = $namespace; | ||
| 23 | local @Perlito5::UNITCHECK_BLOCK; | ||
| 24 | local @Perlito5::Java::Java_constants; | ||
| 25 | local %Perlito5::Java::Java_constant_seen; | ||
| 26 | |||
| 27 | # warn "in eval enter\n"; | ||
| 28 | # warn "External scope ", Perlito5::Dumper::Dumper($scope_java); | ||
| 29 | # warn "BASE_SCOPE ", Perlito5::Dumper::Dumper($Perlito5::BASE_SCOPE); | ||
| 30 | # warn "SCOPE_STMT ", Perlito5::Dumper::Dumper(\@Perlito5::SCOPE_STMT); | ||
| 31 | |||
| 32 | my $match = Perlito5::Grammar::exp_stmts( $source, 0 ); | ||
| 33 | |||
| 34 | if ( !$match || $match->{to} < length($source) ) { | ||
| 35 | die "Syntax error in eval near pos ", $match->{to}; | ||
| 36 | } | ||
| 37 | |||
| 38 | # TODO - process type annotations like: | ||
| 39 | # package Java::Object { import => 'java.lang.Object' } | ||
| 40 | # | ||
| 41 | while ( @Perlito::ANNOTATION ) { | ||
| 42 | my $ann = shift(@Perlito::ANNOTATION); | ||
| 43 | my $str = Perlito5::AST::CompUnit::process_java_import_statement(@$ann); | ||
| 44 | # warn "ANNOTATION: [[[\n$str\n]]]\n"; | ||
| 45 | } | ||
| 46 | |||
| 47 | my $ast = | ||
| 48 | Perlito5::AST::Call->new( | ||
| 49 | method => "postcircumfix:<( )>", | ||
| 50 | arguments => [], | ||
| 51 | invocant => Perlito5::AST::Sub->new( | ||
| 52 | block => Perlito5::AST::Block->new( | ||
| 53 | stmts => $match->{capture}, | ||
| 54 | ), | ||
| 55 | ), | ||
| 56 | ); | ||
| 57 | |||
| 58 | # use lexicals from BEGIN scratchpad | ||
| 59 | $ast = $ast->emit_begin_scratchpad(); | ||
| 60 | |||
| 61 | # warn "perl_to_java: ", Perlito5::Dumper::Dumper( $ast ); | ||
| 62 | my $java_code = $ast->emit_java(2, $want); | ||
| 63 | |||
| 64 | # say "java-source: [" . $java_code . "]"; | ||
| 65 | |||
| 66 | # warn "in perl_to_java: ", Perlito5::Dumper::Dumper( \@Perlito5::Java::Java_constants ); | ||
| 67 | |||
| 68 | my $java_classes = Perlito5::Java::get_java_class_info() // {}; | ||
| 69 | my $className = "PlEval" . $Perlito5::ID++; | ||
| 70 | my $constants = ""; | ||
| 71 | $constants .= | ||
| 72 | "import org.perlito.Perlito5.*;\n" | ||
| 73 | . "import java.util.regex.Pattern;\n" | ||
| 74 | # . join("", | ||
| 75 | # # import the Java classes | ||
| 76 | # # that were declared with | ||
| 77 | # # | ||
| 78 | # # package My::Java { import => "org.My.Java", ... } | ||
| 79 | # # | ||
| 80 | # map { | ||
| 81 | # my $class = $java_classes->{$_}; | ||
| 82 | # $class->{import} ? "import $class->{import};\n" : () | ||
| 83 | # } | ||
| 84 | # sort keys %$java_classes | ||
| 85 | # ) | ||
| 86 | . "public class " . $className . " {\n"; | ||
| 87 | for my $s ( @Perlito5::Java::Java_constants ) { | ||
| 88 | # say "s: [[$s]] ", ref($s), "\n"; | ||
| 89 | $constants .= " " . $s . ";\n"; | ||
| 90 | } | ||
| 91 | $constants .= | ||
| 92 | " public " . $className . "() {\n" | ||
| 93 | . " }\n"; | ||
| 94 | |||
| 95 | Perlito5::set_global_phase("UNITCHECK"); | ||
| 96 | $_->() while $_ = shift @Perlito5::UNITCHECK_BLOCK; | ||
| 97 | |||
| 98 | # warn "in eval BASE_SCOPE exit: ", Perlito5::Dumper::Dumper($Perlito5::BASE_SCOPE); | ||
| 99 | |||
| 100 | return ($className, $java_code, $constants); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub eval_ast { | ||
| 104 | my ($ast) = @_; | ||
| 105 | my $want = 0; | ||
| 106 | |||
| 107 | # TODO - process type annotations like: | ||
| 108 | # package Java::Object { import => 'java.lang.Object' } | ||
| 109 | # | ||
| 110 | while ( @Perlito::ANNOTATION ) { | ||
| 111 | my $ann = shift(@Perlito::ANNOTATION); | ||
| 112 | my $str = Perlito5::AST::CompUnit::process_java_import_statement(@$ann); | ||
| 113 | # warn "ANNOTATION: [[[\n$str\n]]]\n"; | ||
| 114 | } | ||
| 115 | |||
| 116 | # warn "AST:\n" . Perlito5::Dumper::Dumper($ast); | ||
| 117 | |||
| 118 | # use lexicals from BEGIN scratchpad | ||
| 119 | $ast = $ast->emit_begin_scratchpad(); | ||
| 120 | # warn "eval_ast: ", Perlito5::Dumper::Dumper( $ast ); | ||
| 121 | |||
| 122 | my $java_code = $ast->emit_java(2, $want); | ||
| 123 | # say STDERR "java-source: [" . $java_code . "]"; | ||
| 124 | Perlito5::set_global_phase("UNITCHECK"); | ||
| 125 | $_->() while $_ = shift @Perlito5::UNITCHECK_BLOCK; | ||
| 126 | # warn "in eval BASE_SCOPE exit: ", Perlito5::Dumper::Dumper($Perlito5::BASE_SCOPE); | ||
| 127 | |||
| 128 | my $java_classes = Perlito5::Java::get_java_class_info() // {}; | ||
| 129 | my $className = "PlEval" . $Perlito5::ID++; | ||
| 130 | my $constants = ""; | ||
| 131 | $constants .= | ||
| 132 | "import org.perlito.Perlito5.*;\n" | ||
| 133 | . "import java.util.regex.Pattern;\n" | ||
| 134 | # . join("", | ||
| 135 | # # import the Java classes | ||
| 136 | # # that were declared with | ||
| 137 | # # | ||
| 138 | # # package My::Java { import => "org.My.Java", ... } | ||
| 139 | # # | ||
| 140 | # map { | ||
| 141 | # my $class = $java_classes->{$_}; | ||
| 142 | # $class->{import} ? "import $class->{import};\n" : () | ||
| 143 | # } | ||
| 144 | # sort keys %$java_classes | ||
| 145 | # ) | ||
| 146 | . "public class " . $className . " {\n"; | ||
| 147 | for my $s ( @Perlito5::Java::Java_constants ) { | ||
| 148 | # say "s: [[$s]] ", ref($s), "\n"; | ||
| 149 | $constants .= " " . $s . ";\n"; | ||
| 150 | } | ||
| 151 | $constants .= | ||
| 152 | " public " . $className . "() {\n" | ||
| 153 | . " }\n"; | ||
| 154 | |||
| 155 | # warn "constants [[\n$constants ]]\n"; | ||
| 156 | |||
| 157 | @_ = ($className, $java_code, $constants); | ||
| 158 | return Java::inline('PlJavaCompiler.eval_java_string(List__)'); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub emit_java_extends { | ||
| 162 | my ($class, $java_classes) = @_; | ||
| 163 | # extends the imported Java classes | ||
| 164 | # that were declared with | ||
| 165 | # | ||
| 166 | # package My::X { extends => "My::Object" } | ||
| 167 | # | ||
| 168 | |||
| 169 | # 'extends' => 'My::Object', | ||
| 170 | # 'extends_java_type' => 'Object', | ||
| 171 | # 'java_native_to_perl' => 'pMyX', | ||
| 172 | # 'java_type' => 'MyX', | ||
| 173 | # 'perl_package' => 'My::X', | ||
| 174 | # 'perl_to_java' => 'to_MyX', | ||
| 175 | # 'Java::inline' => " // ... Java code ... \n", | ||
| 176 | # 'methods' => [ | ||
| 177 | # instance_meth => { | ||
| 178 | # decl => [ "public" ], | ||
| 179 | # return => "Int", | ||
| 180 | # args => [ "Int" ], # this/$self is added to the Perl method arguments | ||
| 181 | # code => "MyClass::instance_meth", | ||
| 182 | # }, | ||
| 183 | # class_meth => { | ||
| 184 | # decl => [ "public", "static" ], | ||
| 185 | # return => "Int", | ||
| 186 | # throws => [ "IOException" ], | ||
| 187 | # args => [ "Int" ], # class name is added to the Perl method arguments | ||
| 188 | # code => "MyClass::class_meth", | ||
| 189 | # }, | ||
| 190 | # | ||
| 191 | # TODO: constructors, variables | ||
| 192 | # | ||
| 193 | # MyX => { | ||
| 194 | # decl => [ "public" ], | ||
| 195 | # return => undef, # a constructor doesn't return anything | ||
| 196 | # args => [], | ||
| 197 | # Java::inline => '{ super(123) }', | ||
| 198 | # }, | ||
| 199 | # ], | ||
| 200 | # 'variables' => [ | ||
| 201 | # myName => { | ||
| 202 | # decl => [ "public" ], | ||
| 203 | # type => "String", | ||
| 204 | # }, | ||
| 205 | # ], | ||
| 206 | |||
| 207 | my @out; | ||
| 208 | my $java_decl = $class->{decl} // []; | ||
| 209 | if ($class->{extends}) { | ||
| 210 | push @out, "@$java_decl class $class->{java_type} extends $class->{extends_java_type} {"; | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | push @out, "@$java_decl class $class->{java_type} implements $class->{implements_java_type} {"; | ||
| 214 | } | ||
| 215 | push @out, $class->{'Java::inline'} if $class->{'Java::inline'}; | ||
| 216 | while ( @{ $class->{variables} } ) { | ||
| 217 | my $method = shift @{ $class->{variables} }; | ||
| 218 | my $data = shift @{ $class->{variables} }; | ||
| 219 | # TODO | ||
| 220 | # | ||
| 221 | } | ||
| 222 | while ( @{ $class->{methods} } ) { | ||
| 223 | my $method = shift @{ $class->{methods} }; | ||
| 224 | my $data = shift @{ $class->{methods} }; | ||
| 225 | my $decl = $data->{decl}; | ||
| 226 | my $code = $data->{code} or die "Java extends: missing 'code' argument in method '$method'"; | ||
| 227 | my $return = $data->{return} or die "Java extends: missing 'return' argument in method '$method'"; | ||
| 228 | my @args; | ||
| 229 | my $var = 0; | ||
| 230 | for my $arg ( @{ $data->{args} } ) { | ||
| 231 | my $type = $java_classes->{$arg}; | ||
| 232 | push @args, "$type->{java_type} param$var"; | ||
| 233 | $var++; | ||
| 234 | } | ||
| 235 | my @java_decl = @$decl; | ||
| 236 | my $return_type = $return; | ||
| 237 | if ( $return ne "void" ) { | ||
| 238 | my $type = $java_classes->{$return}; | ||
| 239 | $return_type = $type->{java_type}; | ||
| 240 | } | ||
| 241 | my $throws = ''; | ||
| 242 | if ( $data->{throws} ) { | ||
| 243 | $throws = "throws @{ $data->{throws} }"; | ||
| 244 | } | ||
| 245 | push @out, " @java_decl $return_type $method(" . join(", ", @args) . ") $throws {"; | ||
| 246 | |||
| 247 | @args = (); | ||
| 248 | if ( grep { $_ eq "static" } @$decl ) { | ||
| 249 | # class method | ||
| 250 | push @args, "new PlString(\"$class->{perl_package}\")"; | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | # instance method | ||
| 254 | push @args, "new $class->{java_native_to_perl}(this)"; | ||
| 255 | } | ||
| 256 | $var = 0; | ||
| 257 | for my $arg ( @{ $data->{args} } ) { | ||
| 258 | my $type = $java_classes->{$arg}; | ||
| 259 | push @args, "new $type->{java_native_to_perl}(param$var)"; | ||
| 260 | $var++; | ||
| 261 | } | ||
| 262 | push @out, " PlObject[] res = Main.apply(\"$code\", " . join(", ", @args) . ");"; | ||
| 263 | |||
| 264 | if ( $return eq "void" ) { | ||
| 265 | # void method | ||
| 266 | push @out, " return;"; | ||
| 267 | } | ||
| 268 | else { | ||
| 269 | my $type = $java_classes->{$return} | ||
| 270 | or die "Java class '$return' is not imported"; | ||
| 271 | push @out, " return res[0].$type->{perl_to_java}();"; | ||
| 272 | } | ||
| 273 | |||
| 274 | # public Int instance_meth(Int param1) { | ||
| 275 | # PlInt p1 = new PlInt(param1); | ||
| 276 | # PlObject[] res = Main.apply("MyClass::instance_meth", this, p1); | ||
| 277 | # return res[0].to_Int(); | ||
| 278 | # } | ||
| 279 | # public Int class_meth(Int param1) { | ||
| 280 | # PlObject[] res = Main.apply("MyClass::class_meth", param1); | ||
| 281 | # return res[0].to_Int(); | ||
| 282 | # } | ||
| 283 | |||
| 284 | push @out, " }"; | ||
| 285 | } | ||
| 286 | push @out, "}\n"; | ||
| 287 | return join("\n", @out); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub emit_java { | ||
| 291 | my ($self, %args) = @_; | ||
| 292 | |||
| 293 | # This sub returns a list, to avoid the Java error: "constant string too long" | ||
| 294 | |||
| 295 | my %java_classes = %{ $args{java_classes} // {} }; | ||
| 296 | |||
| 297 | if ($Perlito5::JAVA_EVAL) { | ||
| 298 | return ( | ||
| 299 | <<'EOT' | ||
| 300 | |||
| 301 | // use perlito5-lib.jar | ||
| 302 | import org.perlito.Perlito5.*; | ||
| 303 | import java.util.regex.Pattern; | ||
| 304 | import java.time.*; | ||
| 305 | import java.time.format.*; | ||
| 306 | import java.lang.management.*; | ||
| 307 | EOT | ||
| 308 | # import the Java classes | ||
| 309 | # that were declared with | ||
| 310 | # | ||
| 311 | # package My::Java { import => "org.My.Java", ... } | ||
| 312 | # | ||
| 313 | # , (( map { | ||
| 314 | # my $class = $java_classes{$_}; | ||
| 315 | # $class->{import} ? "import $class->{import};\n" : () | ||
| 316 | # } | ||
| 317 | # sort keys %java_classes | ||
| 318 | # )) | ||
| 319 | # , "\n" | ||
| 320 | ); | ||
| 321 | } | ||
| 322 | |||
| 323 | my @number_unary = qw/ op_int neg complement abs sqrt cos sin exp log /; | ||
| 324 | |||
| 325 | my @boolean_unary = ( | ||
| 326 | 'is_int', | ||
| 327 | 'is_num', | ||
| 328 | 'is_string', | ||
| 329 | 'is_bool', | ||
| 330 | 'is_undef', | ||
| 331 | 'is_regex', | ||
| 332 | 'is_filehandle', | ||
| 333 | 'is_ref', | ||
| 334 | 'is_arrayref', | ||
| 335 | 'is_coderef', | ||
| 336 | 'is_hashref', | ||
| 337 | 'is_scalarref', | ||
| 338 | 'is_typeglobref', | ||
| 339 | ); | ||
| 340 | |||
| 341 | my %number_binop = ( | ||
| 342 | add => { op => '+', returns => 'PlInt', num_returns => 'PlDouble'}, | ||
| 343 | sub => { op => '-', returns => 'PlInt', num_returns => 'PlDouble'}, | ||
| 344 | mul => { op => '*', returns => 'PlInt', num_returns => 'PlDouble'}, | ||
| 345 | div => { op => '/', returns => 'PlDouble', num_returns => 'PlDouble'}, | ||
| 346 | num_eq => { op => '==', returns => 'PlBool', num_returns => 'PlBool' }, | ||
| 347 | num_ne => { op => '!=', returns => 'PlBool', num_returns => 'PlBool' }, | ||
| 348 | num_lt => { op => '<', returns => 'PlBool', num_returns => 'PlBool' }, | ||
| 349 | num_le => { op => '<=', returns => 'PlBool', num_returns => 'PlBool' }, | ||
| 350 | num_gt => { op => '>', returns => 'PlBool', num_returns => 'PlBool' }, | ||
| 351 | num_ge => { op => '>=', returns => 'PlBool', num_returns => 'PlBool' }, | ||
| 352 | int_and => { op => '&', returns => 'PlInt', num_returns => 'PlInt' }, | ||
| 353 | int_or => { op => '|', returns => 'PlInt', num_returns => 'PlInt' }, | ||
| 354 | int_xor => { op => '^', returns => 'PlInt', num_returns => 'PlInt' }, | ||
| 355 | int_shr => { op => '>>>', returns => 'PlInt', num_returns => 'PlInt' }, | ||
| 356 | int_shl => { op => '<<', returns => 'PlInt', num_returns => 'PlInt' }, | ||
| 357 | ); | ||
| 358 | my %string_binop = ( | ||
| 359 | str_eq => { op => '==', str_op => 'eq', returns => 'PlBool' }, | ||
| 360 | str_ne => { op => '!=', str_op => 'ne', returns => 'PlBool' }, | ||
| 361 | str_lt => { op => '<', str_op => 'lt', returns => 'PlBool' }, | ||
| 362 | str_le => { op => '<=', str_op => 'le', returns => 'PlBool' }, | ||
| 363 | str_gt => { op => '>', str_op => 'gt', returns => 'PlBool' }, | ||
| 364 | str_ge => { op => '>=', str_op => 'ge', returns => 'PlBool' }, | ||
| 365 | ); | ||
| 366 | |||
| 367 | my %native_to_perl = ( | ||
| 368 | long => 'PlInt', | ||
| 369 | double => 'PlDouble', | ||
| 370 | boolean => 'PlBool', | ||
| 371 | String => 'PlString', | ||
| 372 | ); | ||
| 373 | for (values %java_classes) { | ||
| 374 | if ( $_->{perl_to_java} ) { | ||
| 375 | if ( $_->{import} || $_->{extends} || $_->{implements} ) { | ||
| 376 | $native_to_perl{$_->{java_type}} = $_->{java_native_to_perl}; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | my %self_assign_number_binop = ( | ||
| 382 | add => { op => '+=', }, | ||
| 383 | sub => { op => '-=', }, | ||
| 384 | mul => { op => '*=', }, | ||
| 385 | div => { op => '/=', }, | ||
| 386 | mod => { op => '%=', }, | ||
| 387 | int_or => { op => '|=', }, | ||
| 388 | int_and => { op => '&=', }, | ||
| 389 | int_xor => { op => '^=', }, | ||
| 390 | int_shr => { op => '>>=', }, | ||
| 391 | int_shl => { op => '<<=', }, | ||
| 392 | pow => { op => '**=' }, | ||
| 393 | ); | ||
| 394 | # TODO self_assign: | ||
| 395 | # or || | ||
| 396 | # and && | ||
| 397 | # defined_or // | ||
| 398 | # str_concat . | ||
| 399 | # string_replicate x. | ||
| 400 | # str_or |. | ||
| 401 | # str_and &. | ||
| 402 | # str_xor ^. | ||
| 403 | |||
| 404 | my %special_var_index = ( | ||
| 405 | # this is used by the variable localization methods | ||
| 406 | '_' => [ 4, 'PlLvalue' ], | ||
| 407 | '\\' => [ 5, 'PlLvalue' ], | ||
| 408 | '|' => [ 6, 'PlLvalueSpecialVarAutoflush' ], | ||
| 409 | '@' => [ 7, 'PlLvalue' ], | ||
| 410 | ); | ||
| 411 | |||
| 412 | return ( | ||
| 413 | <<'EOT' | ||
| 414 | // start Perl-Java runtime | ||
| 415 | // this is generated code - see: lib/Perlito5/Java/Runtime.pm | ||
| 416 | |||
| 417 | import java.lang.Math; | ||
| 418 | import java.lang.System; | ||
| 419 | import java.util.*; | ||
| 420 | import java.io.*; | ||
| 421 | import java.lang.management.*; | ||
| 422 | import java.nio.file.*; | ||
| 423 | import java.nio.file.attribute.*; | ||
| 424 | import java.nio.charset.*; | ||
| 425 | import java.nio.ByteBuffer; | ||
| 426 | import static java.nio.file.attribute.PosixFilePermission.*; | ||
| 427 | import java.time.*; | ||
| 428 | import java.time.format.*; | ||
| 429 | import java.util.regex.Pattern; | ||
| 430 | import java.util.regex.Matcher; | ||
| 431 | import java.util.concurrent.TimeUnit; | ||
| 432 | import java.lang.reflect.*; | ||
| 433 | EOT | ||
| 434 | , ( $Perlito5::BOOTSTRAP_JAVA_EVAL ? Perlito5::Java::JavaCompiler->emit_java_imports() : () ) | ||
| 435 | |||
| 436 | # import the Java classes | ||
| 437 | # that were declared with | ||
| 438 | # | ||
| 439 | # package My::Java { import => "org.My.Java", ... } | ||
| 440 | # | ||
| 441 | , # (( map { | ||
| 442 | # my $class = $java_classes{$_}; | ||
| 443 | # $class->{import} ? "import $class->{import};\n" : () | ||
| 444 | # } | ||
| 445 | # sort keys %java_classes | ||
| 446 | # )) | ||
| 447 | # extends the imported Java classes | ||
| 448 | # that were declared with | ||
| 449 | # | ||
| 450 | # package My::Java { extends => "My::Java", ... } | ||
| 451 | # | ||
| 452 | , (( map { | ||
| 453 | my $class = $java_classes{$_}; | ||
| 454 | $class->{extends} || $class->{implements} ? emit_java_extends($class, \%java_classes) : () | ||
| 455 | } | ||
| 456 | sort keys %java_classes | ||
| 457 | )) | ||
| 458 | |||
| 459 | , Perlito5::Java::CORE->emit_java() | ||
| 460 | |||
| 461 | # Perl-Java exceptions | ||
| 462 | , <<'EOT' | ||
| 463 | class PlControlException extends RuntimeException { | ||
| 464 | } | ||
| 465 | class PlNextException extends PlControlException { | ||
| 466 | public int label_id; | ||
| 467 | |||
| 468 | public PlNextException(int i) { | ||
| 469 | this.label_id = i; | ||
| 470 | } | ||
| 471 | } | ||
| 472 | class PlLastException extends PlControlException { | ||
| 473 | public int label_id; | ||
| 474 | |||
| 475 | public PlLastException(int i) { | ||
| 476 | this.label_id = i; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | class PlRedoException extends PlControlException { | ||
| 480 | public int label_id; | ||
| 481 | |||
| 482 | public PlRedoException(int i) { | ||
| 483 | this.label_id = i; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | class PlReturnException extends PlControlException { | ||
| 487 | public PlObject ret; | ||
| 488 | |||
| 489 | public PlReturnException(PlObject ret) { | ||
| 490 | this.ret = ret; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | class PlDieException extends PlControlException { | ||
| 494 | public PlObject ret; | ||
| 495 | |||
| 496 | public PlDieException(PlObject ret) { | ||
| 497 | this.ret = ret; | ||
| 498 | } | ||
| 499 | public String getMessage() { | ||
| 500 | return this.ret.toString(); | ||
| 501 | } | ||
| 502 | } | ||
| 503 | class PlCx { | ||
| 504 | public static final Thread mainThread = Thread.currentThread(); | ||
| 505 | public static final int VOID = 0; | ||
| 506 | public static final int SCALAR = 1; | ||
| 507 | public static final int LIST = 2; | ||
| 508 | public static final PlUndef UNDEF = new PlUndef(); | ||
| 509 | public static final PlBool TRUE = new PlBool(true); | ||
| 510 | public static final PlBool FALSE = new PlBool(false); | ||
| 511 | public static final Charset UTF8 = Charset.forName("UTF-8"); | ||
| 512 | public static final PlString EMPTY = new PlString(""); | ||
| 513 | public static final PlNextException NEXT = new PlNextException(0); | ||
| 514 | public static final PlLastException LAST = new PlLastException(0); | ||
| 515 | public static final String OVERLOAD_STRING = "(\"\""; // ("" | ||
| 516 | public static final String OVERLOAD_NUM = "(0+"; | ||
| 517 | public static final String OVERLOAD_BOOL = "(bool"; | ||
| 518 | public static final String OVERLOAD_NOMETHOD = "(nomethod"; | ||
| 519 | public static final PlRegex SPLIT_SPACE = new PlRegex("\\s+", Pattern.MULTILINE, false); | ||
| 520 | EOT | ||
| 521 | , " " . join("\n ", | ||
| 522 | map { "public static final PlInt " . ($_ < 0 ? "MIN" : "INT") . abs($_) . " = new PlInt($_);" } | ||
| 523 | (-2 .. 9) ) . "\n" | ||
| 524 | , " " . join("\n ", @{ $args{java_constants} // [] } ) . "\n" | ||
| 525 | , <<'EOT' | ||
| 526 | } | ||
| 527 | EOT | ||
| 528 | |||
| 529 | , Perlito5::Java::Crypt->emit_java() | ||
| 530 | , ( $Perlito5::BOOTSTRAP_JAVA_EVAL ? Perlito5::Java::JavaCompiler->emit_java() : () ) | ||
| 531 | |||
| 532 | , <<'EOT' | ||
| 533 | class PerlCompare implements Comparator<PlObject> { | ||
| 534 | public PlClosure sorter; | ||
| 535 | public PlLvalue v_a; | ||
| 536 | public PlLvalue v_b; | ||
| 537 | public PlArray list__; | ||
| 538 | public PerlCompare (PlClosure sorter, PlLvalue a, PlLvalue b, PlArray list__) { | ||
| 539 | this.sorter = sorter; | ||
| 540 | this.v_a = a; | ||
| 541 | this.v_b = b; | ||
| 542 | this.list__ = list__; | ||
| 543 | } | ||
| 544 | public int compare (PlObject a, PlObject b) { | ||
| 545 | v_a.set(a); | ||
| 546 | v_b.set(b); | ||
| 547 | return this.sorter.apply( PlCx.SCALAR, list__ ).to_int(); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | class PerlRangeString implements Iterator<PlObject> { | ||
| 551 | public PlString v_start; | ||
| 552 | public String v_end; | ||
| 553 | public PerlRangeString(PlString v_start, String v_end) { | ||
| 554 | this.v_start = v_start; | ||
| 555 | this.v_end = v_end; | ||
| 556 | } | ||
| 557 | public PlObject next() { | ||
| 558 | PlString ret = v_start; | ||
| 559 | PlObject incr = v_start._incr(); | ||
| 560 | if (incr.is_string()) { | ||
| 561 | v_start = (PlString)incr; | ||
| 562 | } | ||
| 563 | else { | ||
| 564 | v_start = new PlString(incr.toString()); | ||
| 565 | } | ||
| 566 | return new PlLvalue(ret); | ||
| 567 | } | ||
| 568 | public boolean hasNext() { | ||
| 569 | return ( (v_start.int_length() < v_end.length()) | ||
| 570 | || (v_start.int_length() == v_end.length() && v_start.boolean_str_le(v_end)) ); | ||
| 571 | } | ||
| 572 | } | ||
| 573 | class PerlRangeInt implements Iterator<PlObject> { | ||
| 574 | public long v_start; | ||
| 575 | public long v_end; | ||
| 576 | public PerlRangeInt(long v_start, long v_end) { | ||
| 577 | this.v_start = v_start; | ||
| 578 | this.v_end = v_end; | ||
| 579 | } | ||
| 580 | public PlObject next() { | ||
| 581 | PlInt ret = new PlInt(v_start); | ||
| 582 | v_start++; | ||
| 583 | return new PlLvalue(ret); | ||
| 584 | } | ||
| 585 | public boolean hasNext() { | ||
| 586 | return v_start <= v_end; | ||
| 587 | } | ||
| 588 | } | ||
| 589 | class PlLvalueIterator implements Iterator<PlObject> { | ||
| 590 | public PlLvalue v_start; | ||
| 591 | public PlLvalueIterator(PlLvalue v_start) { | ||
| 592 | this.v_start = v_start; | ||
| 593 | } | ||
| 594 | public PlObject next() { | ||
| 595 | PlObject ret = v_start; | ||
| 596 | v_start = null; | ||
| 597 | return ret; | ||
| 598 | } | ||
| 599 | public boolean hasNext() { | ||
| 600 | return (v_start != null); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | class PerlRange0 implements Iterator<PlObject> { | ||
| 604 | public PerlRange0() { | ||
| 605 | } | ||
| 606 | public PlObject next() { | ||
| 607 | return new PlObject(); | ||
| 608 | } | ||
| 609 | public boolean hasNext() { | ||
| 610 | return false; | ||
| 611 | } | ||
| 612 | } | ||
| 613 | class PerlRange implements Iterable<PlObject> { | ||
| 614 | public PlObject v_start; | ||
| 615 | public PlObject v_end; | ||
| 616 | private static HashMap<String, Integer> flip_flop = new HashMap<String, Integer>(); | ||
| 617 | public PerlRange(PlObject v_start, PlObject v_end) { | ||
| 618 | this.v_start = v_start; | ||
| 619 | this.v_end = v_end; | ||
| 620 | } | ||
| 621 | public Iterator<PlObject> iterator() { | ||
| 622 | if (this.v_start.is_string() && this.v_end.is_string()) { | ||
| 623 | String s = v_start.toString(); | ||
| 624 | final int length = s.length(); | ||
| 625 | if (length > 0) { | ||
| 626 | boolean is_num_start = PerlOp.looks_like_number(s); | ||
| 627 | boolean is_num_end = PerlOp.looks_like_number(this.v_end.toString()); | ||
| 628 | if (is_num_start && is_num_end && s.codePointAt(0) != '0') { | ||
| 629 | if (!this.v_start.is_integer_range() || !this.v_end.is_integer_range()) { | ||
| 630 | PlCORE.die("Range iterator outside integer range"); | ||
| 631 | } | ||
| 632 | return new PerlRangeInt(this.v_start.to_long(), this.v_end.to_long()); | ||
| 633 | } | ||
| 634 | // If the initial value specified isn't part of a magical increment sequence | ||
| 635 | // (that is, a non-empty string matching /^[a-zA-Z]*[0-9]*\z/ ), | ||
| 636 | // only the initial value will be returned. | ||
| 637 | boolean is_incrementable = true; | ||
| 638 | for (int offset = 0; offset < length; offset++) { | ||
| 639 | int c = s.codePointAt(offset); | ||
| 640 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { | ||
| 641 | // good | ||
| 642 | } | ||
| 643 | else { | ||
| 644 | for ( ; offset < length; offset++) { | ||
| 645 | c = s.codePointAt(offset); | ||
| 646 | if (c >= '0' && c <= '9') { | ||
| 647 | // good | ||
| 648 | } | ||
| 649 | else { | ||
| 650 | is_incrementable = false; | ||
| 651 | offset = length; // exit loop | ||
| 652 | } | ||
| 653 | } | ||
| 654 | } | ||
| 655 | } | ||
| 656 | if (is_incrementable) { | ||
| 657 | return new PerlRangeString(new PlString(s), this.v_end.toString()); | ||
| 658 | } | ||
| 659 | } | ||
| 660 | if (length > this.v_end.toString().length()) { | ||
| 661 | return new PerlRange0(); | ||
| 662 | } | ||
| 663 | return new PlLvalueIterator(new PlLvalue(new PlString(s))); | ||
| 664 | } | ||
| 665 | |||
| 666 | if (!this.v_start.is_integer_range() || !this.v_end.is_integer_range()) { | ||
| 667 | PlCORE.die("Range iterator outside integer range"); | ||
| 668 | } | ||
| 669 | return new PerlRangeInt(this.v_start.to_long(), this.v_end.to_long()); | ||
| 670 | } | ||
| 671 | public final PlObject range(int want, String id, int three_dots) { | ||
| 672 | if (want == PlCx.LIST) { | ||
| 673 | PlArray ret = new PlArray(); | ||
| 674 | for (PlObject i : this) { | ||
| 675 | ret.a.add(i); | ||
| 676 | } | ||
| 677 | return ret; | ||
| 678 | } | ||
| 679 | // http://perldoc.perl.org/perlop.html#Range-Operators | ||
| 680 | Integer v = flip_flop.get(id); | ||
| 681 | if (v != null && v != 0) { | ||
| 682 | v++; | ||
| 683 | if (v_end.to_boolean()) { | ||
| 684 | flip_flop.put(id, 0); | ||
| 685 | return new PlString("" + v + "E0"); | ||
| 686 | } | ||
| 687 | else { | ||
| 688 | flip_flop.put(id, v); | ||
| 689 | return new PlInt(v); | ||
| 690 | } | ||
| 691 | } | ||
| 692 | else { | ||
| 693 | if (v_start.to_boolean()) { | ||
| 694 | v = 1; | ||
| 695 | } | ||
| 696 | else { | ||
| 697 | v = 0; | ||
| 698 | } | ||
| 699 | if (v != 0 && three_dots == 0 && v_end.to_boolean()) { | ||
| 700 | flip_flop.put(id, 0); | ||
| 701 | return new PlString("" + v + "E0"); | ||
| 702 | } | ||
| 703 | else { | ||
| 704 | flip_flop.put(id, v); | ||
| 705 | if (v == 0) { | ||
| 706 | return PlCx.EMPTY; | ||
| 707 | } | ||
| 708 | return new PlInt(v); | ||
| 709 | } | ||
| 710 | } | ||
| 711 | } | ||
| 712 | } | ||
| 713 | class PerlArgumentLookupResult { | ||
| 714 | // public ArrayList<Class[]> params; | ||
| 715 | public Object arg; | ||
| 716 | public Class cl; | ||
| 717 | |||
| 718 | public PerlArgumentLookupResult(Object arg, Class cl) { | ||
| 719 | // , ArrayList<Class[]> params) { | ||
| 720 | this.arg = arg; | ||
| 721 | this.cl = cl; | ||
| 722 | // this.params = params; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | class PerlOp { | ||
| 726 | // PerlOp implements operators: && || | ||
| 727 | // and auxiliary functions | ||
| 728 | // | ||
| 729 | // note: '+' add() and '-' sub() are PlObject methods, not implemented here. | ||
| 730 | // | ||
| 731 | // TODO - see Perlito5/JavaScript2/Runtime.pm for more operator implementations | ||
| 732 | |||
| 733 | // symbol tables | ||
| 734 | // like %Module:: | ||
| 735 | public static final PlObject getSymbolTable(String nameSpace) { | ||
| 736 | // TODO - create the typeglobs that link to "inner" namespaces, like *Java:: in %Perlito5::Java:: | ||
| 737 | int pos = nameSpace.lastIndexOf("::"); | ||
| 738 | boolean isMain = nameSpace.equals("main::"); | ||
| 739 | PlHash out = new PlHash(); | ||
| 740 | getSymbolTableScan(out, PlStringConstant.constants, nameSpace, pos, isMain); | ||
| 741 | return out; | ||
| 742 | } | ||
| 743 | |||
| 744 | private static final void getSymbolTableScan(PlHash out, HashMap<String, PlStringConstant> vars, String nameSpace, int pos, boolean isMain) { | ||
| 745 | String[] keys = vars.keySet().toArray(new String[0]); | ||
| 746 | if (isMain) { | ||
| 747 | for (String name : keys) { | ||
| 748 | if (name.length() > pos + 2 && name.indexOf(nameSpace) == 0 && name.lastIndexOf("::") == pos) { | ||
| 749 | // normal variable like "ARGV" in $main::ARGV | ||
| 750 | out.hset(name.substring(pos+2), PlV.fget(name)); | ||
| 751 | } | ||
| 752 | else { | ||
| 753 | // "inner" namespace | ||
| 754 | String inner = name.substring(0, name.indexOf("::")+2); | ||
| 755 | out.hset(inner, PlV.fget(inner)); | ||
| 756 | } | ||
| 757 | } | ||
| 758 | } | ||
| 759 | else { | ||
| 760 | for (String name : keys) { | ||
| 761 | if (name.length() > pos + 2 && name.indexOf(nameSpace) == 0) { | ||
| 762 | if (name.lastIndexOf("::") == pos) { | ||
| 763 | // normal variable like "ARGV" in $main::ARGV | ||
| 764 | out.hset(name.substring(pos+2), PlV.fget(name)); | ||
| 765 | } | ||
| 766 | else { | ||
| 767 | // "inner" namespace | ||
| 768 | String inner = name.substring(pos+2, name.indexOf("::", pos+2)+2); | ||
| 769 | out.hset(inner, PlV.fget(name.substring(0, name.indexOf("::", pos+2)+2))); | ||
| 770 | } | ||
| 771 | } | ||
| 772 | } | ||
| 773 | } | ||
| 774 | } | ||
| 775 | |||
| 776 | public static final PlObject deleteSymbolTable(String nameSpace, PlObject index) { | ||
| 777 | // delete $Foo::{foo} | ||
| 778 | String sname = nameSpace + index.toString(); | ||
| 779 | PlString name = new PlString(sname); | ||
| 780 | PlStringConstant glob = PlStringConstant.getConstant(sname); | ||
| 781 | glob.codeRef.set(PlCx.UNDEF); | ||
| 782 | glob.scalarRef.set(PlCx.UNDEF); | ||
| 783 | glob.arrayRef.set(new PlArrayRef()); | ||
| 784 | glob.hashRef.set(new PlHashRef()); | ||
| 785 | glob.fileRef.set(new PlFileHandle(sname)); | ||
| 786 | return PlCx.UNDEF; | ||
| 787 | } | ||
| 788 | |||
| 789 | // filehandles | ||
| 790 | public static final PlFileHandle get_filehandle(PlObject fh, String nameSpace) { | ||
| 791 | if (fh.is_lvalue()) { | ||
| 792 | if (fh.is_undef()) { | ||
| 793 | // $fh autovivification to filehandle | ||
| 794 | fh.set(new PlFileHandle(nameSpace)); | ||
| 795 | } | ||
| 796 | fh = fh.get(); | ||
| 797 | } | ||
| 798 | if (fh.is_filehandle()) { | ||
| 799 | // *FILE | ||
| 800 | return (PlFileHandle)fh; | ||
| 801 | } | ||
| 802 | if (fh.is_typeglobref()) { | ||
| 803 | // \*FILE | ||
| 804 | return ((PlGlobRef)fh).filehandle; | ||
| 805 | } | ||
| 806 | return get_filehandle(fh.toString(), nameSpace); // get "GLOB" by name | ||
| 807 | } | ||
| 808 | public static final PlFileHandle get_filehandle(String s, String nameSpace) { | ||
| 809 | int pos = s.indexOf("::"); | ||
| 810 | if (pos == 0) { | ||
| 811 | // ::x | ||
| 812 | s = "main" + s; | ||
| 813 | } | ||
| 814 | if (pos == -1) { | ||
| 815 | if (s.equals("STDOUT")) { | ||
| 816 | s = "main::STDOUT"; | ||
| 817 | } | ||
| 818 | else if (s.equals("STDERR")) { | ||
| 819 | s = "main::STDERR"; | ||
| 820 | } | ||
| 821 | else if (s.equals("STDIN")) { | ||
| 822 | s = "main::STDIN"; | ||
| 823 | } | ||
| 824 | else if (s.equals("ARGV")) { | ||
| 825 | s = "main::ARGV"; | ||
| 826 | } | ||
| 827 | else { | ||
| 828 | s = nameSpace + "::" + s; | ||
| 829 | } | ||
| 830 | } | ||
| 831 | while (s.startsWith("main::main::")) { | ||
| 832 | s = s.substring(6); | ||
| 833 | } | ||
| 834 | PlObject fh = PlV.fget(s); // get "GLOB" by name | ||
| 835 | return (PlFileHandle)(fh.get()); | ||
| 836 | } | ||
| 837 | public static final Set<PosixFilePermission> MaskToPermissions(int mask) { | ||
| 838 | final Set<PosixFilePermission> perm = new HashSet<PosixFilePermission>(); | ||
| 839 | // TODO - provide a workaround | ||
| 840 | // if ((mask & 04000)==0) PlCORE.die("setuid bit not implemented"); | ||
| 841 | // if ((mask & 02000)==0) PlCORE.die("setgid bit not implemented"); | ||
| 842 | // if ((mask & 01000)==0) PlCORE.die("sticky bit not implemented"); | ||
| 843 | if ((mask & 00400)==0) perm.add(OWNER_READ); | ||
| 844 | if ((mask & 00200)==0) perm.add(OWNER_WRITE); | ||
| 845 | if ((mask & 00100)==0) perm.add(OWNER_EXECUTE); | ||
| 846 | if ((mask & 00040)==0) perm.add(GROUP_READ); | ||
| 847 | if ((mask & 00020)==0) perm.add(GROUP_WRITE); | ||
| 848 | if ((mask & 00010)==0) perm.add(GROUP_EXECUTE); | ||
| 849 | if ((mask & 00004)==0) perm.add(OTHERS_READ); | ||
| 850 | if ((mask & 00002)==0) perm.add(OTHERS_WRITE); | ||
| 851 | if ((mask & 00001)==0) perm.add(OTHERS_EXECUTE); | ||
| 852 | return perm; | ||
| 853 | } | ||
| 854 | |||
| 855 | // objects | ||
| 856 | |||
| 857 | public static final PlObject callSuper( String method, String packageName, PlArray args, int context ) { | ||
| 858 | // SUPER calls: $v->SUPER::x; | ||
| 859 | PlObject methodCode = PlCx.UNDEF; | ||
| 860 | ISA: | ||
| 861 | for (PlObject className : PlV.array_get(packageName + "::ISA")) { | ||
| 862 | methodCode = PlClass.getInstance(className).method_lookup(method, 0); | ||
| 863 | if (!methodCode.is_undef()) { | ||
| 864 | break ISA; | ||
| 865 | } | ||
| 866 | } | ||
| 867 | if (methodCode.is_undef()) { | ||
| 868 | PlCORE.die( "Can't locate object method \"" + method | ||
| 869 | + "\" via package \"" + packageName + "\""); | ||
| 870 | } | ||
| 871 | return PerlOp.call(methodCode, args, context); | ||
| 872 | } | ||
| 873 | |||
| 874 | // coderef methods can be called on ANY invocant | ||
| 875 | // $m = sub {...}; | ||
| 876 | // $a->$m | ||
| 877 | public static final PlObject call( PlObject method, PlArray args, int context ) { | ||
| 878 | if ( method.is_coderef() ) { | ||
| 879 | return method.apply(context, args); | ||
| 880 | } | ||
| 881 | else if ( method.is_lvalue() ) { | ||
| 882 | return call( method.get(), args, context ); | ||
| 883 | } | ||
| 884 | else { | ||
| 885 | return call( method.toString(), args, context ); | ||
| 886 | } | ||
| 887 | } | ||
| 888 | public static final PlObject call( PlStringConstant method, PlArray args, int context ) { | ||
| 889 | return call( method.toString(), args, context ); | ||
| 890 | } | ||
| 891 | public static final PlObject call( String method, PlArray args, int context ) { | ||
| 892 | PlObject invocant = args.aget(0); | ||
| 893 | |||
| 894 | PlClass pClass = invocant.blessed_class(); | ||
| 895 | |||
| 896 | if ( pClass != null ) { | ||
| 897 | // invocant is a normal Perl object instance | ||
| 898 | PlObject methodCode = pClass.method_lookup(method, 0); | ||
| 899 | if (methodCode.is_undef()) { | ||
| 900 | String className = pClass.className(); | ||
| 901 | return PlCORE.die( "Can't locate object method \"" + method | ||
| 902 | + "\" via package \"" + className | ||
| 903 | + "\" (perhaps you forgot to load \"" + className + "\"?)" ); | ||
| 904 | } | ||
| 905 | return methodCode.apply(context, args); | ||
| 906 | } | ||
| 907 | |||
| 908 | if ( invocant.is_lvalue() ) { | ||
| 909 | invocant = invocant.get(); | ||
| 910 | } | ||
| 911 | if ( invocant.is_JavaObject() ) { | ||
| 912 | args.shift(); // remove invocant | ||
| 913 | Object obj = ((PlJavaObject)invocant).toJava(); | ||
| 914 | PlLvalue ret = new PlLvalue(); | ||
| 915 | Class cl; | ||
| 916 | if (obj instanceof Class) { | ||
| 917 | cl = (Class)obj; | ||
| 918 | } | ||
| 919 | else { | ||
| 920 | cl = obj.getClass(); | ||
| 921 | } | ||
| 922 | |||
| 923 | int argCount = args.to_int(); | ||
| 924 | if (argCount == 0) { | ||
| 925 | // no arguments; this may be a "field" | ||
| 926 | try { | ||
| 927 | Field fi = cl.getField(method); | ||
| 928 | if (obj instanceof Class) { | ||
| 929 | // obj is Class; method is "field" | ||
| 930 | ret.set( fi.get(null) ); | ||
| 931 | } | ||
| 932 | else { | ||
| 933 | // obj is instance; method is "field" | ||
| 934 | ret.set( fi.get(obj) ); | ||
| 935 | } | ||
| 936 | return ret; | ||
| 937 | } | ||
| 938 | catch (NoSuchFieldException e) { | ||
| 939 | // Array "length" is special | ||
| 940 | if (method.equals("length")) { | ||
| 941 | try { | ||
| 942 | ret.set(Array.getLength(obj)); | ||
| 943 | return ret; | ||
| 944 | } | ||
| 945 | catch (Exception e2) { | ||
| 946 | } | ||
| 947 | } | ||
| 948 | } | ||
| 949 | catch (Exception e) { | ||
| 950 | return PlCORE.die(new PlStringLazyError(e)); | ||
| 951 | } | ||
| 952 | } | ||
| 953 | |||
| 954 | ArrayList<Class[]> params = new ArrayList<Class[]>(); | ||
| 955 | for (Method m : cl.getMethods()) { | ||
| 956 | if (m.getName().equals(method)) { | ||
| 957 | Class[] mArgs = m.getParameterTypes(); | ||
| 958 | if (mArgs.length > 0 && mArgs.length <= argCount) { | ||
| 959 | params.add(mArgs); | ||
| 960 | } | ||
| 961 | } | ||
| 962 | } | ||
| 963 | if (method.equals("new")) { | ||
| 964 | |||
| 965 | if (cl.isArray() && args.to_int() == 1) { | ||
| 966 | // $ArrayClass->new(10) | ||
| 967 | ret.set( java.lang.reflect.Array.newInstance(cl.getComponentType(), args.aget(0).to_int()) ); | ||
| 968 | return ret; | ||
| 969 | } | ||
| 970 | |||
| 971 | for (Constructor m : cl.getConstructors()) { | ||
| 972 | Class[] mArgs = m.getParameterTypes(); | ||
| 973 | if (mArgs.length > 0 && mArgs.length <= argCount) { | ||
| 974 | params.add(mArgs); | ||
| 975 | } | ||
| 976 | } | ||
| 977 | } | ||
| 978 | |||
| 979 | // TODO - overloading | ||
| 980 | // - sort constructors by specificity | ||
| 981 | // - See: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2 | ||
| 982 | // http://www.xyzws.com/javafaq/what-is-a-mostspecific-method/8 | ||
| 983 | // - type promotion: byte -> short -> int -> long -> float -> double | ||
| 984 | // - boxing / unboxing | ||
| 985 | // - method arity: consider variable arity (Object...) "[Ljava.lang.Object;" | ||
| 986 | // | ||
| 987 | // perl> eval { $aa = Java::inline q{ Class.forName("java.lang.String") }} | ||
| 988 | // Class(0xf3fcd59) | ||
| 989 | // perl> $aa->format("aaa", 123) | ||
| 990 | // java.lang.String - param #1 | ||
| 991 | // [Ljava.lang.Object; - param #2, ... | ||
| 992 | // | ||
| 993 | // - check Class.getSuperclass() | ||
| 994 | // - special case when arglist is empty, but (Object...) | ||
| 995 | // - Creating arrays via Java Reflection is done using the java.lang.reflect.Array: | ||
| 996 | // See: http://tutorials.jenkov.com/java-reflection/arrays.html | ||
| 997 | // | ||
| 998 | // cl.isArray() // true | ||
| 999 | // cl.getComponentType() // int.class | ||
| 1000 | // int[] intArray = (int[]) Array.newInstance(int.class, 3); | ||
| 1001 | // | ||
| 1002 | |||
| 1003 | // System.out.println("Candidate methods " + method); | ||
| 1004 | // for(int i = 0; i < params.size(); i++) { | ||
| 1005 | // System.out.println(" params:"); | ||
| 1006 | // for(Class c : params.get(i)) { | ||
| 1007 | // System.out.println(" " + c.getName()); | ||
| 1008 | // } | ||
| 1009 | // } | ||
| 1010 | |||
| 1011 | int paramPos = 0; | ||
| 1012 | ArrayList<Class[]> param2; | ||
| 1013 | ArrayList<Class> classArgs = new ArrayList<Class>(); | ||
| 1014 | ArrayList<Object> objArgs = new ArrayList<Object>(); | ||
| 1015 | PerlArgumentLookupResult newArg; | ||
| 1016 | |||
| 1017 | ARGS: | ||
| 1018 | while (args.to_int() > 0) { | ||
| 1019 | PlObject v = args.shift(); | ||
| 1020 | newArg = v.castToClass( params, paramPos ); | ||
| 1021 | // System.out.println("Closest class " + newArg.cl.getName()); | ||
| 1022 | |||
| 1023 | if (newArg.arg == null && newArg.cl.isArray()) { | ||
| 1024 | // varargs | ||
| 1025 | // System.out.println("Start varargs"); | ||
| 1026 | args.unshift(v); // cast the argument again | ||
| 1027 | Class varargsArrayClass = newArg.cl; | ||
| 1028 | Class varargsClass = varargsArrayClass.getComponentType(); | ||
| 1029 | Object[] varargs = (Object[])java.lang.reflect.Array.newInstance(varargsClass, args.to_int()); | ||
| 1030 | params = new ArrayList<Class[]>(); | ||
| 1031 | params.add( new Class[]{ varargsClass } ); | ||
| 1032 | paramPos = 0; | ||
| 1033 | int varargsPos = 0; | ||
| 1034 | while (args.to_int() > 0) { | ||
| 1035 | v = args.shift(); | ||
| 1036 | newArg = v.castToClass( params, paramPos ); | ||
| 1037 | // System.out.println("varargs: Closest class " + newArg.cl.getName()); | ||
| 1038 | varargs[varargsPos++] = newArg.arg; | ||
| 1039 | } | ||
| 1040 | classArgs.add(varargsArrayClass); | ||
| 1041 | objArgs.add(varargs); | ||
| 1042 | break ARGS; | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | classArgs.add(newArg.cl); | ||
| 1046 | objArgs.add(newArg.arg); | ||
| 1047 | |||
| 1048 | // prune candidates | ||
| 1049 | param2 = new ArrayList<Class[]>(); | ||
| 1050 | for (Class[] mArgs : params) { | ||
| 1051 | if (mArgs[paramPos].equals(newArg.cl)) { | ||
| 1052 | param2.add(mArgs); | ||
| 1053 | } | ||
| 1054 | } | ||
| 1055 | params = param2; | ||
| 1056 | paramPos++; | ||
| 1057 | |||
| 1058 | // System.out.println("Candidate methods " + method); | ||
| 1059 | // for(int i = 0; i < params.size(); i++) { | ||
| 1060 | // System.out.println(" params:"); | ||
| 1061 | // for(Class c : params.get(i)) { | ||
| 1062 | // System.out.println(" " + c.getName()); | ||
| 1063 | // } | ||
| 1064 | // } | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | try { | ||
| 1068 | Method meth = ((Class<?>)cl).getMethod(method, classArgs.toArray(new Class[classArgs.size()])); | ||
| 1069 | ret.set( meth.invoke(obj, objArgs.toArray(new Object[objArgs.size()]))); | ||
| 1070 | return ret; | ||
| 1071 | } | ||
| 1072 | catch (NoSuchMethodException e) { | ||
| 1073 | } | ||
| 1074 | catch (Exception e) { | ||
| 1075 | return PlCORE.die(new PlStringLazyError(e)); | ||
| 1076 | } | ||
| 1077 | if (method.equals("new")) { | ||
| 1078 | try { | ||
| 1079 | Constructor co = ((Class<?>)cl).getConstructor(classArgs.toArray(new Class[classArgs.size()])); | ||
| 1080 | ret.set( co.newInstance(objArgs.toArray(new Object[objArgs.size()])) ); | ||
| 1081 | return ret; | ||
| 1082 | } | ||
| 1083 | catch (NoSuchMethodException e) { | ||
| 1084 | } | ||
| 1085 | catch (Exception e) { | ||
| 1086 | return PlCORE.die(new PlStringLazyError(e)); | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | return PlCORE.die( "Can't locate object method \"" + method | ||
| 1090 | + "\" via Java class \"" + cl.getCanonicalName() | ||
| 1091 | + "\" (perhaps the parameter types don\'t match?)" ); | ||
| 1092 | } | ||
| 1093 | if ( invocant.is_undef() ) { | ||
| 1094 | return PlCORE.die( "Can't call method \"" + method + "\" on an undefined value" ); | ||
| 1095 | } | ||
| 1096 | if (invocant.is_typeglobref()) { | ||
| 1097 | // \*FILE | ||
| 1098 | invocant = ((PlGlobRef)invocant).filehandle; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | if (invocant.is_ref()) { | ||
| 1102 | return PlCORE.die( "Can't call method \"" + method + "\" on unblessed reference" ); | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | if (invocant.is_filehandle()) { | ||
| 1106 | // invocant can be a nonref-typeglob | ||
| 1107 | // *FILE | ||
| 1108 | // $fh->print() is allowed, even if $fh is unblessed | ||
| 1109 | if (method.equals("print")) { | ||
| 1110 | args.shift(); // TODO - keep filehandle in arg list | ||
| 1111 | return PlCORE.print(context, (PlFileHandle)invocant, args.toString()); | ||
| 1112 | } | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | // invocant can be a package name | ||
| 1116 | String invocant_str = invocant.toString(); | ||
| 1117 | if ( invocant_str.equals("") ) { | ||
| 1118 | return PlCORE.die( "Can't call method \"" + method + "\" on an undefined value" ); | ||
| 1119 | } | ||
| 1120 | PlObject methodCode = PlClass.getInstance(invocant_str).method_lookup(method, 0); | ||
| 1121 | if (methodCode.is_undef()) { | ||
| 1122 | return PlCORE.die( "Can't locate object method \"" + method | ||
| 1123 | + "\" via package \"" + invocant_str | ||
| 1124 | + "\" (perhaps you forgot to load \"" + invocant_str + "\"?)" ); | ||
| 1125 | } | ||
| 1126 | return methodCode.apply(context, args); | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | // local() | ||
| 1130 | public static final PlObject push_local(PlHash container, String index) { | ||
| 1131 | PlV.local_stack.a.add(container); | ||
| 1132 | PlV.local_stack.a.add(new PlString(index)); | ||
| 1133 | PlLvalue empty = new PlLvalue(); | ||
| 1134 | PlV.local_stack.a.add(container.hget_lvalue(index)); | ||
| 1135 | PlV.local_stack.a.add(PlCx.INT0); | ||
| 1136 | container.hset_alias(index, empty); | ||
| 1137 | return empty; | ||
| 1138 | } | ||
| 1139 | public static final PlObject push_local(PlArray container, int index) { | ||
| 1140 | PlV.local_stack.a.add(container); | ||
| 1141 | PlV.local_stack.a.add(new PlInt(index)); | ||
| 1142 | PlLvalue empty = new PlLvalue(); | ||
| 1143 | PlV.local_stack.a.add(container.aget_lvalue(index)); | ||
| 1144 | PlV.local_stack.a.add(PlCx.INT1); | ||
| 1145 | container.aset_alias(index, empty); | ||
| 1146 | return empty; | ||
| 1147 | } | ||
| 1148 | public static final void push_local_regex_result() { | ||
| 1149 | PlRegexResult match = PlV.regex_result; | ||
| 1150 | PlV.local_stack.a.add(match); | ||
| 1151 | PlV.local_stack.a.add(PlCx.INT2); | ||
| 1152 | PlRegexResult new_match = new PlRegexResult(); | ||
| 1153 | new_match.matcher = match.matcher; | ||
| 1154 | new_match.regex_string = match.regex_string; | ||
| 1155 | PlV.regex_result = new_match; | ||
| 1156 | } | ||
| 1157 | public static final PlObject push_local_named_sub(PlObject value, String name) { | ||
| 1158 | PlStringConstant glob = PlStringConstant.getConstant(name); | ||
| 1159 | PlV.local_stack.a.add(new PlString(name)); | ||
| 1160 | PlV.local_stack.a.add(glob.codeRef); | ||
| 1161 | PlV.local_stack.a.add(PlCx.INT3); | ||
| 1162 | PlLvalue newValue = new PlLvalue(); | ||
| 1163 | newValue.set(value); | ||
| 1164 | glob.cset_alias( newValue ); | ||
| 1165 | return newValue; | ||
| 1166 | } | ||
| 1167 | public static final PlObject push_local_scalar(PlObject value, String name) { | ||
| 1168 | PlStringConstant glob = PlStringConstant.getConstant(name); | ||
| 1169 | PlV.local_stack.a.add(new PlString(name)); | ||
| 1170 | PlV.local_stack.a.add(glob.scalarRef); | ||
| 1171 | PlV.local_stack.a.add(new PlInt(20)); // XXX magic number | ||
| 1172 | PlLvalue newValue = new PlLvalue(); | ||
| 1173 | newValue.set(value); | ||
| 1174 | glob.scalarRef = newValue; | ||
| 1175 | return newValue; | ||
| 1176 | } | ||
| 1177 | public static final PlObject push_local_array(PlObject value, String name) { | ||
| 1178 | PlStringConstant glob = PlStringConstant.getConstant(name); | ||
| 1179 | PlV.local_stack.a.add(new PlString(name)); | ||
| 1180 | PlV.local_stack.a.add(glob.arrayRef); | ||
| 1181 | PlV.local_stack.a.add(new PlInt(21)); // XXX magic number | ||
| 1182 | PlLvalue newValue = new PlLvalue(); | ||
| 1183 | newValue.set(value); | ||
| 1184 | glob.arrayRef = newValue; | ||
| 1185 | return newValue; | ||
| 1186 | } | ||
| 1187 | public static final PlObject push_local_hash(PlObject value, String name) { | ||
| 1188 | PlStringConstant glob = PlStringConstant.getConstant(name); | ||
| 1189 | PlV.local_stack.a.add(new PlString(name)); | ||
| 1190 | PlV.local_stack.a.add(glob.hashRef); | ||
| 1191 | PlV.local_stack.a.add(new PlInt(22)); // XXX magic number | ||
| 1192 | PlLvalue newValue = new PlLvalue(); | ||
| 1193 | newValue.set(value); | ||
| 1194 | glob.hashRef = newValue; | ||
| 1195 | return newValue; | ||
| 1196 | } | ||
| 1197 | public static final PlObject push_local_file(PlObject value, String name) { | ||
| 1198 | PlStringConstant glob = PlStringConstant.getConstant(name); | ||
| 1199 | PlV.local_stack.a.add(new PlString(name)); | ||
| 1200 | PlV.local_stack.a.add(glob.fileRef); | ||
| 1201 | PlV.local_stack.a.add(new PlInt(23)); // XXX magic number | ||
| 1202 | PlLvalue newValue = new PlLvalue(); | ||
| 1203 | newValue.set(value); | ||
| 1204 | glob.fileRef = newValue; | ||
| 1205 | return newValue; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | // localizers for special variables like $_ $\ | ||
| 1209 | EOT | ||
| 1210 | , (( map { | ||
| 1211 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 1212 | my $index = $special_var_index{$_}[0]; | ||
| 1213 | my $class = $special_var_index{$_}[1]; | ||
| 1214 | " | ||
| 1215 | public static final void push_local_special_var_${java_name}() { | ||
| 1216 | PlV.local_stack.a.add(PlV.${java_name}); | ||
| 1217 | PlV.local_stack.a.add(PlCx.INT${index}); | ||
| 1218 | PlV.${java_name} = new $class(); | ||
| 1219 | } | ||
| 1220 | " | ||
| 1221 | } | ||
| 1222 | sort keys %Perlito5::Java::special_scalar | ||
| 1223 | )) | ||
| 1224 | , <<'EOT' | ||
| 1225 | |||
| 1226 | public static final int local_length() { | ||
| 1227 | return PlV.local_stack.to_int(); | ||
| 1228 | } | ||
| 1229 | public static final PlObject cleanup_local(int pos, PlObject ret) { | ||
| 1230 | while (PlV.local_stack.to_int() > pos) { | ||
| 1231 | int t = PlV.local_stack.pop().to_int(); | ||
| 1232 | PlObject v = PlV.local_stack.pop(); | ||
| 1233 | PlObject index; | ||
| 1234 | PlObject container; | ||
| 1235 | switch (t) { | ||
| 1236 | case 0: | ||
| 1237 | index = PlV.local_stack.pop(); | ||
| 1238 | container = PlV.local_stack.pop(); | ||
| 1239 | ((PlHash)container).hset_alias(index.toString(), (PlLvalue)v); | ||
| 1240 | break; | ||
| 1241 | case 1: | ||
| 1242 | index = PlV.local_stack.pop(); | ||
| 1243 | container = PlV.local_stack.pop(); | ||
| 1244 | ((PlArray)container).aset_alias(index.to_int(), (PlLvalue)v); | ||
| 1245 | break; | ||
| 1246 | case 2: | ||
| 1247 | PlV.regex_result = (PlRegexResult)v; | ||
| 1248 | break; | ||
| 1249 | case 3: | ||
| 1250 | index = PlV.local_stack.pop(); | ||
| 1251 | PlStringConstant.getConstant(index.toString()).cset_alias( (PlLvalue)v ); | ||
| 1252 | break; | ||
| 1253 | case 20: // XXX magic number | ||
| 1254 | index = PlV.local_stack.pop(); | ||
| 1255 | PlStringConstant.getConstant(index.toString()).scalarRef = (PlLvalue)v; | ||
| 1256 | break; | ||
| 1257 | case 21: // XXX magic number | ||
| 1258 | index = PlV.local_stack.pop(); | ||
| 1259 | PlStringConstant.getConstant(index.toString()).arrayRef = (PlLvalue)v; | ||
| 1260 | break; | ||
| 1261 | case 22: // XXX magic number | ||
| 1262 | index = PlV.local_stack.pop(); | ||
| 1263 | PlStringConstant.getConstant(index.toString()).hashRef = (PlLvalue)v; | ||
| 1264 | break; | ||
| 1265 | case 23: // XXX magic number | ||
| 1266 | index = PlV.local_stack.pop(); | ||
| 1267 | PlStringConstant.getConstant(index.toString()).fileRef = (PlLvalue)v; | ||
| 1268 | break; | ||
| 1269 | EOT | ||
| 1270 | , (( map { | ||
| 1271 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 1272 | my $index = $special_var_index{$_}[0]; | ||
| 1273 | my $class = $special_var_index{$_}[1]; | ||
| 1274 | " | ||
| 1275 | case ${index}: | ||
| 1276 | PlV.${java_name} = ($class)v; | ||
| 1277 | break; | ||
| 1278 | " | ||
| 1279 | } | ||
| 1280 | sort keys %Perlito5::Java::special_scalar | ||
| 1281 | )) | ||
| 1282 | , <<'EOT' | ||
| 1283 | |||
| 1284 | } | ||
| 1285 | } | ||
| 1286 | return ret; | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | // context() | ||
| 1290 | // - handles run-time scalar/list/void context in expression results | ||
| 1291 | public static final PlObject context(int want, PlObject arg) { | ||
| 1292 | if (want == PlCx.LIST) { | ||
| 1293 | return arg; | ||
| 1294 | } | ||
| 1295 | return arg.scalar(); | ||
| 1296 | } | ||
| 1297 | public static final PlObject context(int want, Object o) { | ||
| 1298 | PlObject arg = PlJavaObject.fromObject(o); | ||
| 1299 | if (want == PlCx.LIST) { | ||
| 1300 | return arg; | ||
| 1301 | } | ||
| 1302 | return arg.scalar(); | ||
| 1303 | } | ||
| 1304 | public static final PlObject context(int want) { | ||
| 1305 | if (want == PlCx.LIST) { | ||
| 1306 | return new PlArray(); | ||
| 1307 | } | ||
| 1308 | return PlCx.UNDEF; | ||
| 1309 | } | ||
| 1310 | public static final PlObject context(int want, PlObject... args) { | ||
| 1311 | if (want == PlCx.LIST) { | ||
| 1312 | return new PlArray(args); | ||
| 1313 | } | ||
| 1314 | return args[args.length-1].scalar(); | ||
| 1315 | } | ||
| 1316 | public static final PlObject context(int want, String arg) { | ||
| 1317 | if (want == PlCx.LIST) { | ||
| 1318 | return new PlArray(new PlString(arg)); | ||
| 1319 | } | ||
| 1320 | return new PlString(arg); | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | // process id | ||
| 1324 | public static PlObject getPID() { | ||
| 1325 | String processName = | ||
| 1326 | java.lang.management.ManagementFactory.getRuntimeMXBean().getName(); | ||
| 1327 | return new PlString(processName.split("@")[0]); | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | // statement() | ||
| 1331 | // - workaround for "Error: not a statement" | ||
| 1332 | // - this is the compile-time version of context(null, arg) | ||
| 1333 | public static final void statement(PlObject... args) { } | ||
| 1334 | public static final void statement() { } | ||
| 1335 | |||
| 1336 | // control-flow exceptions | ||
| 1337 | public static final PlObject next() { | ||
| 1338 | throw PlCx.NEXT; | ||
| 1339 | } | ||
| 1340 | public static final PlObject next(int label_id) { | ||
| 1341 | throw new PlNextException(label_id); | ||
| 1342 | } | ||
| 1343 | public static final PlObject last() { | ||
| 1344 | throw PlCx.LAST; | ||
| 1345 | } | ||
| 1346 | public static final PlObject last(int label_id) { | ||
| 1347 | throw new PlLastException(label_id); | ||
| 1348 | } | ||
| 1349 | public static final PlObject redo(int label_id) { | ||
| 1350 | throw new PlRedoException(label_id); | ||
| 1351 | } | ||
| 1352 | public static final PlObject ret(PlObject ret) { | ||
| 1353 | throw new PlReturnException(ret); | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | public static final PlObject gotoOp(int ctx, PlObject s, PlArray List__) { | ||
| 1357 | if (s.is_coderef()) { | ||
| 1358 | // goto &subr; | ||
| 1359 | throw new PlReturnException(s.apply(ctx, List__)); | ||
| 1360 | } | ||
| 1361 | return PlCORE.die("goto() not implemented"); | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | EOT | ||
| 1365 | , # list break | ||
| 1366 | <<'EOT' | ||
| 1367 | |||
| 1368 | public static final PlObject mod(PlInt aa, PlObject bb) { | ||
| 1369 | long a = aa.to_long(); | ||
| 1370 | long b = bb.to_long(); | ||
| 1371 | long res = Math.abs(a) % Math.abs(b); | ||
| 1372 | // PlCORE.say("mod " + a + " % " + b + " = " + res); | ||
| 1373 | if (a < 0 && b > 0) { | ||
| 1374 | return new PlInt(b - res); | ||
| 1375 | } | ||
| 1376 | if (a > 0 && b < 0) { | ||
| 1377 | return new PlInt(b + res); | ||
| 1378 | } | ||
| 1379 | if (a < 0 && b < 0) { | ||
| 1380 | return new PlInt(- res); | ||
| 1381 | } | ||
| 1382 | return new PlInt(res); | ||
| 1383 | } | ||
| 1384 | public static final PlObject mod(PlDouble aa, PlObject bb) { | ||
| 1385 | double a = aa.to_double(); | ||
| 1386 | double b = bb.to_double(); | ||
| 1387 | double res = Math.abs(a) % Math.abs(b); | ||
| 1388 | // PlCORE.say("mod " + a + " % " + b + " = " + res); | ||
| 1389 | if (a < 0.0 && b > 0.0) { | ||
| 1390 | return new PlDouble(b - res); | ||
| 1391 | } | ||
| 1392 | if (a > 0.0 && b < 0.0) { | ||
| 1393 | return new PlDouble(b + res); | ||
| 1394 | } | ||
| 1395 | if (a < 0.0 && b < 0.0) { | ||
| 1396 | return new PlDouble(- res); | ||
| 1397 | } | ||
| 1398 | return new PlDouble(res); | ||
| 1399 | } | ||
| 1400 | |||
| 1401 | public static final PlObject srand() { | ||
| 1402 | PlV.random = new Random(); | ||
| 1403 | return PlCx.UNDEF; | ||
| 1404 | } | ||
| 1405 | public static final PlObject srand(PlObject o) { | ||
| 1406 | if (!o.is_integer_range()) { | ||
| 1407 | PlCORE.warn(PlCx.VOID, new PlArray(new PlString("Integer overflow in srand"))); | ||
| 1408 | } | ||
| 1409 | long s = o.to_long(); | ||
| 1410 | PlV.random = new Random(s); | ||
| 1411 | if (s == 0) { | ||
| 1412 | return new PlString("0E0"); | ||
| 1413 | } | ||
| 1414 | return new PlInt(s); | ||
| 1415 | } | ||
| 1416 | |||
| 1417 | public static final PlObject rand(double s) { | ||
| 1418 | if (s == 0.0) { | ||
| 1419 | s = 1.0; | ||
| 1420 | } | ||
| 1421 | return new PlDouble(s * PlV.random.nextDouble()); | ||
| 1422 | } | ||
| 1423 | |||
| 1424 | // and1(x) ? y : and3() | ||
| 1425 | public static final boolean and1(PlObject arg1) { | ||
| 1426 | if (arg1.to_boolean()) { | ||
| 1427 | return true; | ||
| 1428 | } | ||
| 1429 | else { | ||
| 1430 | PlV.boolean_stack = arg1; | ||
| 1431 | return false; | ||
| 1432 | } | ||
| 1433 | } | ||
| 1434 | public static final PlObject and3() { | ||
| 1435 | return PlV.boolean_stack; | ||
| 1436 | } | ||
| 1437 | |||
| 1438 | // or1(x) ? or2() : y | ||
| 1439 | public static final boolean or1(PlObject arg1) { | ||
| 1440 | if (arg1.to_boolean()) { | ||
| 1441 | PlV.boolean_stack = arg1; | ||
| 1442 | return true; | ||
| 1443 | } | ||
| 1444 | else { | ||
| 1445 | return false; | ||
| 1446 | } | ||
| 1447 | } | ||
| 1448 | public static final PlObject or2() { | ||
| 1449 | return PlV.boolean_stack; | ||
| 1450 | } | ||
| 1451 | |||
| 1452 | // defined_or1(x) ? defined_or2() : y | ||
| 1453 | public static final boolean defined_or1(PlObject arg1) { | ||
| 1454 | if (!arg1.is_undef()) { | ||
| 1455 | PlV.boolean_stack = arg1; | ||
| 1456 | return true; | ||
| 1457 | } | ||
| 1458 | else { | ||
| 1459 | return false; | ||
| 1460 | } | ||
| 1461 | } | ||
| 1462 | public static final PlObject defined_or2() { | ||
| 1463 | return PlV.boolean_stack; | ||
| 1464 | } | ||
| 1465 | |||
| 1466 | public static final PlInt ord(PlObject s) { | ||
| 1467 | String item = s.toString(); | ||
| 1468 | return new PlInt(item.length() > 0 ? Character.codePointAt(item, 0) : 0); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | // 'prefix:<-A>' => 'PerlOp.p5atime', | ||
| 1472 | // 'prefix:<-C>' => 'PerlOp.p5ctime', | ||
| 1473 | // 'prefix:<-M>' => 'PerlOp.p5mtime', | ||
| 1474 | // 'prefix:<-d>' => 'PerlOp.p5is_directory', | ||
| 1475 | // 'prefix:<-e>' => 'PerlOp.p5file_exists', | ||
| 1476 | // 'prefix:<-f>' => 'PerlOp.p5is_file', | ||
| 1477 | // 'prefix:<-s>' => 'PerlOp.p5size', | ||
| 1478 | |||
| 1479 | public static final Path resolve_file(PlObject s) throws IOException { | ||
| 1480 | String name = s.toString(); | ||
| 1481 | if (name.equals("_") && PlV.lastStat != null) { | ||
| 1482 | return PlV.path.resolve(PlV.lastStat).toRealPath(); | ||
| 1483 | } | ||
| 1484 | PlV.lastStat = name; | ||
| 1485 | return PlV.path.resolve(name).toRealPath(); | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | public static final PlObject p5atime(PlObject s) { | ||
| 1489 | return PlCORE.die("-A not implemented"); | ||
| 1490 | } | ||
| 1491 | public static final PlObject p5ctime(PlObject s) { | ||
| 1492 | return PlCORE.die("-C not implemented"); | ||
| 1493 | } | ||
| 1494 | public static final PlObject p5mtime(PlObject s) { | ||
| 1495 | try { | ||
| 1496 | // TODO - "Script start time minus file modification time, in days" | ||
| 1497 | return new PlDouble(new File(resolve_file(s).toString()).lastModified() / 86400.0); | ||
| 1498 | } | ||
| 1499 | catch(IOException e) { | ||
| 1500 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1501 | return PlCx.UNDEF; | ||
| 1502 | } | ||
| 1503 | catch(RuntimeException e) { | ||
| 1504 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1505 | return PlCx.UNDEF; | ||
| 1506 | } | ||
| 1507 | } | ||
| 1508 | public static final PlObject p5is_directory(PlObject s) { | ||
| 1509 | try { | ||
| 1510 | return new PlBool(new File(resolve_file(s).toString()).isDirectory()); | ||
| 1511 | } | ||
| 1512 | catch(IOException e) { | ||
| 1513 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1514 | return PlCx.UNDEF; | ||
| 1515 | } | ||
| 1516 | catch(RuntimeException e) { | ||
| 1517 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1518 | return PlCx.UNDEF; | ||
| 1519 | } | ||
| 1520 | } | ||
| 1521 | public static final PlObject p5file_exists(PlObject s) { | ||
| 1522 | try { | ||
| 1523 | return new PlBool(new File(resolve_file(s).toString()).exists()); | ||
| 1524 | } | ||
| 1525 | catch(IOException e) { | ||
| 1526 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1527 | return PlCx.UNDEF; | ||
| 1528 | } | ||
| 1529 | catch(RuntimeException e) { | ||
| 1530 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1531 | return PlCx.UNDEF; | ||
| 1532 | } | ||
| 1533 | } | ||
| 1534 | public static final PlObject p5is_file(PlObject s) { | ||
| 1535 | try { | ||
| 1536 | return new PlBool(new File(resolve_file(s).toString()).isFile()); | ||
| 1537 | } | ||
| 1538 | catch(IOException e) { | ||
| 1539 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1540 | return PlCx.UNDEF; | ||
| 1541 | } | ||
| 1542 | catch(RuntimeException e) { | ||
| 1543 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1544 | return PlCx.UNDEF; | ||
| 1545 | } | ||
| 1546 | } | ||
| 1547 | public static final PlObject p5size(PlObject s) { | ||
| 1548 | try { | ||
| 1549 | return new PlInt(new File(resolve_file(s).toString()).length()); | ||
| 1550 | } | ||
| 1551 | catch(IOException e) { | ||
| 1552 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1553 | return PlCx.UNDEF; | ||
| 1554 | } | ||
| 1555 | catch(RuntimeException e) { | ||
| 1556 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1557 | return PlCx.UNDEF; | ||
| 1558 | } | ||
| 1559 | } | ||
| 1560 | public static final PlObject p5is_symbolic_link(PlObject s) { | ||
| 1561 | try { | ||
| 1562 | return new PlBool(Files.isSymbolicLink(resolve_file(s))); | ||
| 1563 | } | ||
| 1564 | catch(IOException e) { | ||
| 1565 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1566 | return PlCx.UNDEF; | ||
| 1567 | } | ||
| 1568 | catch(RuntimeException e) { | ||
| 1569 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1570 | return PlCx.UNDEF; | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | public static final PlObject p5is_writable(PlObject s) { | ||
| 1574 | try { | ||
| 1575 | return new PlBool(Files.isWritable(resolve_file(s))); | ||
| 1576 | } | ||
| 1577 | catch(IOException e) { | ||
| 1578 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1579 | return PlCx.UNDEF; | ||
| 1580 | } | ||
| 1581 | catch(RuntimeException e) { | ||
| 1582 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1583 | return PlCx.UNDEF; | ||
| 1584 | } | ||
| 1585 | } | ||
| 1586 | public static final PlObject p5is_readable(PlObject s) { | ||
| 1587 | try { | ||
| 1588 | return new PlBool(Files.isReadable(resolve_file(s))); | ||
| 1589 | } | ||
| 1590 | catch(IOException e) { | ||
| 1591 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1592 | return PlCx.UNDEF; | ||
| 1593 | } | ||
| 1594 | catch(RuntimeException e) { | ||
| 1595 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1596 | return PlCx.UNDEF; | ||
| 1597 | } | ||
| 1598 | } | ||
| 1599 | public static final PlObject p5is_executable(PlObject s) { | ||
| 1600 | try { | ||
| 1601 | return new PlBool(Files.isExecutable(resolve_file(s))); | ||
| 1602 | } | ||
| 1603 | catch(IOException e) { | ||
| 1604 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1605 | return PlCx.UNDEF; | ||
| 1606 | } | ||
| 1607 | catch(RuntimeException e) { | ||
| 1608 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 1609 | return PlCx.UNDEF; | ||
| 1610 | } | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | public static final PlObject p5is_named_pipe(PlObject s) { | ||
| 1614 | // TODO "-p" | ||
| 1615 | return new PlBool(false); | ||
| 1616 | } | ||
| 1617 | |||
| 1618 | public static final PlObject list_replicate(PlArray o, PlObject c, int wantarray) { | ||
| 1619 | if (wantarray == PlCx.SCALAR) { | ||
| 1620 | return o.aget(-1).string_replicate(c); | ||
| 1621 | } | ||
| 1622 | int count = c.to_int(); | ||
| 1623 | PlArray arr = new PlArray(); | ||
| 1624 | int olength = o.to_int(); | ||
| 1625 | if (count > 0) { | ||
| 1626 | for (int ci = 0; ci < count; ci++) { | ||
| 1627 | for (int oi = 0; oi < olength; oi++) { | ||
| 1628 | // this pushes an alias to the original value, as discussed in | ||
| 1629 | // https://beta.nntp.perl.org/group/perl.perl5.porters/2013/06/msg203177.html | ||
| 1630 | arr.a.add( o.aget_lvalue(oi) ); | ||
| 1631 | } | ||
| 1632 | } | ||
| 1633 | } | ||
| 1634 | return arr; | ||
| 1635 | } | ||
| 1636 | public static final PlObject grep(PlClosure c, PlArray a, PlArray list__, int wantarray) { | ||
| 1637 | PlArray ret = new PlArray(); | ||
| 1638 | int size = a.to_int(); | ||
| 1639 | PlLvalue v__ref = PlV.Scalar_ARG; | ||
| 1640 | PlObject v__val = v__ref.get(); | ||
| 1641 | for (int i = 0; i < size; i++) { | ||
| 1642 | boolean result; | ||
| 1643 | PlObject temp = a.aget(i); | ||
| 1644 | v__ref.set(temp); | ||
| 1645 | result = c.apply(PlCx.SCALAR, list__).to_boolean(); | ||
| 1646 | if (result) { | ||
| 1647 | ret.push_void(temp); | ||
| 1648 | } | ||
| 1649 | } | ||
| 1650 | v__ref.set(v__val); | ||
| 1651 | return (wantarray == PlCx.LIST ) ? ret : ret.length_of_array(); | ||
| 1652 | } | ||
| 1653 | public static final PlObject map(PlClosure c, PlArray a, PlArray list__, int wantarray) { | ||
| 1654 | if (wantarray == PlCx.LIST ) { | ||
| 1655 | PlArray ret = new PlArray(); | ||
| 1656 | int size = a.to_int(); | ||
| 1657 | PlLvalue v__ref = PlV.Scalar_ARG; | ||
| 1658 | PlObject v__val = v__ref.get(); | ||
| 1659 | for (int i = 0; i < size; i++) { | ||
| 1660 | v__ref.set(a.aget(i)); | ||
| 1661 | ret.push_void(c.apply(PlCx.LIST, list__)); | ||
| 1662 | } | ||
| 1663 | v__ref.set(v__val); | ||
| 1664 | return ret; | ||
| 1665 | } | ||
| 1666 | else { | ||
| 1667 | int ret = 0; | ||
| 1668 | int size = a.to_int(); | ||
| 1669 | PlLvalue v__ref = PlV.Scalar_ARG; | ||
| 1670 | PlObject v__val = v__ref.get(); | ||
| 1671 | for (int i = 0; i < size; i++) { | ||
| 1672 | v__ref.set(a.aget(i)); | ||
| 1673 | ret += c.apply(PlCx.LIST, new PlArray()).length_of_array_int(); | ||
| 1674 | } | ||
| 1675 | v__ref.set(v__val); | ||
| 1676 | return new PlInt(ret); | ||
| 1677 | } | ||
| 1678 | } | ||
| 1679 | public static final PlObject sort(PlClosure c, PlArray a, PlArray list__, int wantarray) { | ||
| 1680 | String pkg = c.pkg_name; | ||
| 1681 | PlArray ret = new PlArray(a); | ||
| 1682 | PlLvalue v_a_ref = (PlLvalue)PlV.sget(pkg + "::a"); | ||
| 1683 | PlLvalue v_b_ref = (PlLvalue)PlV.sget(pkg + "::b"); | ||
| 1684 | PerlCompare comp = new PerlCompare(c, v_a_ref, v_b_ref, list__); | ||
| 1685 | PlObject v_a_val = v_a_ref.get(); | ||
| 1686 | PlObject v_b_val = v_b_ref.get(); | ||
| 1687 | Collections.sort(ret.a, comp); | ||
| 1688 | v_a_ref.set(v_a_val); | ||
| 1689 | v_b_ref.set(v_b_val); | ||
| 1690 | return (wantarray == PlCx.LIST ) ? ret : ret.length_of_array(); | ||
| 1691 | } | ||
| 1692 | public static final PlObject sort(PlObject c, PlArray a, PlArray list__, int wantarray) { | ||
| 1693 | if (c.is_coderef()) { | ||
| 1694 | if (c.is_lvalue()) { | ||
| 1695 | c = c.get(); | ||
| 1696 | } | ||
| 1697 | return sort((PlClosure)c, a, list__, wantarray); | ||
| 1698 | } | ||
| 1699 | return PlCORE.die("Not implemented: first argument to sort() is not a closure"); | ||
| 1700 | } | ||
| 1701 | |||
| 1702 | public static PlObject prototype(PlObject arg, String packageName) { | ||
| 1703 | if (arg.is_coderef()) { | ||
| 1704 | if (arg.is_lvalue()) { | ||
| 1705 | arg = arg.get(); | ||
| 1706 | } | ||
| 1707 | return ((PlClosure)arg).prototype(); | ||
| 1708 | } | ||
| 1709 | String method = arg.toString(); | ||
| 1710 | PlObject methodCode; | ||
| 1711 | if (method.indexOf("::") == -1) { | ||
| 1712 | methodCode = PlV.cget(packageName + "::" + method); | ||
| 1713 | } | ||
| 1714 | else { | ||
| 1715 | // fully qualified name | ||
| 1716 | methodCode = PlV.cget(method); | ||
| 1717 | } | ||
| 1718 | if (methodCode.is_coderef()) { | ||
| 1719 | return prototype(methodCode, packageName); | ||
| 1720 | } | ||
| 1721 | return PlCx.UNDEF; | ||
| 1722 | } | ||
| 1723 | |||
| 1724 | private static int _regex_character_class_escape(int offset, String s, StringBuilder sb, int length, boolean flag_xx, | ||
| 1725 | StringBuilder rejected) | ||
| 1726 | { | ||
| 1727 | // inside [ ... ] | ||
| 1728 | // space becomes: "\ " unless the /xx flag is used (flag_xx) | ||
| 1729 | // \120 becomes: \0120 - Java requires octal sequences to start with zero | ||
| 1730 | // \0 becomes: \00 - Java requires the extra zero | ||
| 1731 | // \b is rejected, Java doesn't support \b inside [...] | ||
| 1732 | boolean first = true; | ||
| 1733 | for ( ; offset < length; ) { | ||
| 1734 | final int c = s.codePointAt(offset); | ||
| 1735 | switch (c) { | ||
| 1736 | case ']': | ||
| 1737 | if (first) { | ||
| 1738 | sb.append("\\]"); | ||
| 1739 | break; | ||
| 1740 | } | ||
| 1741 | else { | ||
| 1742 | sb.append(Character.toChars(c)); | ||
| 1743 | return offset; | ||
| 1744 | } | ||
| 1745 | case '[': | ||
| 1746 | sb.append("\\["); | ||
| 1747 | break; | ||
| 1748 | case '\\': // escape - \[ \120 | ||
| 1749 | |||
| 1750 | if (offset < length) { | ||
| 1751 | if (s.codePointAt(offset + 1) == 'b') { | ||
| 1752 | rejected.append("\\b"); // Java doesn't support \b inside [...] | ||
| 1753 | offset++; | ||
| 1754 | break; | ||
| 1755 | } | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | sb.append(Character.toChars(c)); | ||
| 1759 | if (offset < length) { | ||
| 1760 | offset++; | ||
| 1761 | int c2 = s.codePointAt(offset); | ||
| 1762 | if (c2 >= '1' && c2 <= '3') { | ||
| 1763 | if (offset < length+1) { | ||
| 1764 | int off = offset; | ||
| 1765 | int c3 = s.codePointAt(off++); | ||
| 1766 | int c4 = s.codePointAt(off++); | ||
| 1767 | if ((c3 >= '0' && c3 <= '7') && (c4 >= '0' && c4 <= '7')) { | ||
| 1768 | // a \000 octal sequence | ||
| 1769 | sb.append('0'); | ||
| 1770 | } | ||
| 1771 | } | ||
| 1772 | } | ||
| 1773 | else if (c2 == '0') { | ||
| 1774 | // rewrite \0 to \00 | ||
| 1775 | sb.append('0'); | ||
| 1776 | } | ||
| 1777 | sb.append(Character.toChars(c2)); | ||
| 1778 | } | ||
| 1779 | break; | ||
| 1780 | case ' ': | ||
| 1781 | if (flag_xx) { | ||
| 1782 | sb.append(Character.toChars(c)); | ||
| 1783 | } | ||
| 1784 | else { | ||
| 1785 | sb.append("\\ "); // make this space a "token", even inside /x | ||
| 1786 | } | ||
| 1787 | break; | ||
| 1788 | default: | ||
| 1789 | sb.append(Character.toChars(c)); | ||
| 1790 | break; | ||
| 1791 | } | ||
| 1792 | first = false; | ||
| 1793 | offset++; | ||
| 1794 | } | ||
| 1795 | return offset; | ||
| 1796 | } | ||
| 1797 | private static int _regex_skip_comment(int offset, String s, int length) { | ||
| 1798 | // [ ... ] | ||
| 1799 | int offset3 = offset; | ||
| 1800 | for ( ; offset3 < length; ) { | ||
| 1801 | final int c3 = s.codePointAt(offset3); | ||
| 1802 | switch (c3) { | ||
| 1803 | case ')': | ||
| 1804 | return offset3; | ||
| 1805 | case '\\': | ||
| 1806 | offset3++; | ||
| 1807 | break; | ||
| 1808 | default: | ||
| 1809 | break; | ||
| 1810 | } | ||
| 1811 | offset3++; | ||
| 1812 | } | ||
| 1813 | return offset; // possible error - end of comment not found | ||
| 1814 | } | ||
| 1815 | |||
| 1816 | // regex escape rules: | ||
| 1817 | // | ||
| 1818 | // \[ as-is | ||
| 1819 | // [xx xx] becomes: [xx\ xx] - this will make sure space is a token, even when /x modifier is set | ||
| 1820 | // \120 becomes: \0120 - Java requires octal sequences to start with zero | ||
| 1821 | // \0 becomes: \00 - Java requires the extra zero | ||
| 1822 | // (?#...) inline comment is removed | ||
| 1823 | // [xx \b xx] becomes: (?:[xx xx]|\b) - java doesn't support \b as a character | ||
| 1824 | // | ||
| 1825 | public static String regex_escape(String s, boolean flag_xx) { | ||
| 1826 | // escape spaces in character classes | ||
| 1827 | final int length = s.length(); | ||
| 1828 | StringBuilder sb = new StringBuilder(); | ||
| 1829 | StringBuilder rejected = new StringBuilder(); | ||
| 1830 | for (int offset = 0; offset < length; ) { | ||
| 1831 | final int c = s.codePointAt(offset); | ||
| 1832 | switch (c) { | ||
| 1833 | case '\\': // escape - \[ \120 | ||
| 1834 | sb.append(Character.toChars(c)); | ||
| 1835 | if (offset < length) { | ||
| 1836 | offset++; | ||
| 1837 | int c2 = s.codePointAt(offset); | ||
| 1838 | if (c2 >= '1' && c2 <= '3') { | ||
| 1839 | if (offset < length+1) { | ||
| 1840 | int off = offset; | ||
| 1841 | int c3 = s.codePointAt(off++); | ||
| 1842 | int c4 = s.codePointAt(off++); | ||
| 1843 | if ((c3 >= '0' && c3 <= '7') && (c4 >= '0' && c4 <= '7')) { | ||
| 1844 | // a \000 octal sequence | ||
| 1845 | sb.append('0'); | ||
| 1846 | } | ||
| 1847 | } | ||
| 1848 | } | ||
| 1849 | else if (c2 == '0') { | ||
| 1850 | // rewrite \0 to \00 | ||
| 1851 | sb.append('0'); | ||
| 1852 | } | ||
| 1853 | sb.append(Character.toChars(c2)); | ||
| 1854 | } | ||
| 1855 | break; | ||
| 1856 | case '[': // character class | ||
| 1857 | int len = sb.length(); | ||
| 1858 | sb.append(Character.toChars(c)); | ||
| 1859 | offset++; | ||
| 1860 | offset = _regex_character_class_escape(offset, s, sb, length, flag_xx, rejected); | ||
| 1861 | if (rejected.length() > 0) { | ||
| 1862 | // process \b inside character class | ||
| 1863 | String subseq; | ||
| 1864 | if ((sb.length() - len) == 2) { | ||
| 1865 | subseq = "(?:" + rejected.toString() + ")"; | ||
| 1866 | } | ||
| 1867 | else { | ||
| 1868 | subseq = "(?:" + sb.substring(len) + "|" + rejected.toString() + ")"; | ||
| 1869 | } | ||
| 1870 | // PlCORE.warn(PlCx.VOID, new PlArray(new PlString("Rejected: " + subseq))); | ||
| 1871 | rejected.setLength(0); | ||
| 1872 | sb.setLength(len); | ||
| 1873 | sb.append(subseq); | ||
| 1874 | } | ||
| 1875 | break; | ||
| 1876 | case '(': | ||
| 1877 | boolean append = true; | ||
| 1878 | if (offset < length - 3) { | ||
| 1879 | int c2 = s.codePointAt(offset+1); | ||
| 1880 | int c3 = s.codePointAt(offset+2); | ||
| 1881 | int c4 = s.codePointAt(offset+3); | ||
| 1882 | if (c2 == '?' && c3 == '#') { | ||
| 1883 | // comment (?# ... ) | ||
| 1884 | offset = _regex_skip_comment(offset, s, length); | ||
| 1885 | append = false; | ||
| 1886 | } | ||
| 1887 | else if (c2 == '?' && c3 == '<' && | ||
| 1888 | ((c4 >= 'A' && c4 <= 'F') || (c4 >= 'a' && c4 <= 'f') || (c4 == '_')) | ||
| 1889 | ) | ||
| 1890 | { | ||
| 1891 | // named capture (?<one> ... ) | ||
| 1892 | // TODO - replace underscore in name | ||
| 1893 | int endName = s.indexOf(">", offset+3); | ||
| 1894 | if (endName > offset) { | ||
| 1895 | String name = s.substring(offset+3, endName); | ||
| 1896 | // PlCORE.say("name [" + name + "]"); | ||
| 1897 | name = name.replace("_", "UnderScore"); // See: regex_named_capture() | ||
| 1898 | sb.append("(?<"); | ||
| 1899 | sb.append(name); | ||
| 1900 | sb.append(">"); | ||
| 1901 | offset = endName; | ||
| 1902 | append = false; | ||
| 1903 | } | ||
| 1904 | } | ||
| 1905 | } | ||
| 1906 | if (append) { | ||
| 1907 | sb.append(Character.toChars(c)); | ||
| 1908 | } | ||
| 1909 | break; | ||
| 1910 | default: // normal char | ||
| 1911 | sb.append(Character.toChars(c)); | ||
| 1912 | break; | ||
| 1913 | } | ||
| 1914 | offset++; | ||
| 1915 | } | ||
| 1916 | return sb.toString(); | ||
| 1917 | } | ||
| 1918 | |||
| 1919 | public static final PlObject set_pos(PlObject vv, PlObject value, PlRegexResult matcher, String str) { | ||
| 1920 | if (!vv.is_lvalue()) { | ||
| 1921 | return value; | ||
| 1922 | } | ||
| 1923 | PlLvalue var = (PlLvalue)vv; | ||
| 1924 | var.addMagic(); | ||
| 1925 | |||
| 1926 | if (value.is_undef()) { | ||
| 1927 | var.magic.pos = null; | ||
| 1928 | var.magic.regex_zero_length_flag = false; | ||
| 1929 | return value; | ||
| 1930 | } | ||
| 1931 | |||
| 1932 | int pos = value.to_int(); | ||
| 1933 | |||
| 1934 | // check for zero-length match | ||
| 1935 | int old_pos = var.pos().to_int(); | ||
| 1936 | |||
| 1937 | if (old_pos == pos) { | ||
| 1938 | // PlCORE.say("zero length match"); | ||
| 1939 | if (var.magic.regex_zero_length_flag) { | ||
| 1940 | if (matcher.matcher.find()) { | ||
| 1941 | matcher.regex_string = str; | ||
| 1942 | pos = matcher.matcher.end(); | ||
| 1943 | |||
| 1944 | // TODO - $& | ||
| 1945 | // String cap1 = str.substring(old_pos, pos); | ||
| 1946 | // String cap = str.substring(matcher.start(), matcher.end()); | ||
| 1947 | // PlCORE.say("zero length match [true]: [" + cap + "] ["+ cap1+"] pos=" + pos + " start="+matcher.start() + " end="+matcher.end()); | ||
| 1948 | |||
| 1949 | var.magic.regex_zero_length_flag = false; | ||
| 1950 | } | ||
| 1951 | else { | ||
| 1952 | reset_match(); | ||
| 1953 | var.magic.pos = null; | ||
| 1954 | return PlCx.UNDEF; | ||
| 1955 | } | ||
| 1956 | } | ||
| 1957 | else { | ||
| 1958 | var.magic.regex_zero_length_flag = true; | ||
| 1959 | } | ||
| 1960 | } | ||
| 1961 | |||
| 1962 | // TODO - test that pos < string length | ||
| 1963 | value = new PlInt(pos); | ||
| 1964 | var.magic.pos = pos; | ||
| 1965 | return value; | ||
| 1966 | } | ||
| 1967 | |||
| 1968 | // ****** regex variables | ||
| 1969 | // class PlRegexResult | ||
| 1970 | // public Matcher matcher; // regex captures | ||
| 1971 | // public String regex_string; // last string used in a regex | ||
| 1972 | |||
| 1973 | public static final PlRegexResult set_match(Matcher m, String s) { | ||
| 1974 | PlRegexResult match = PlV.regex_result; | ||
| 1975 | match.matcher = m; | ||
| 1976 | match.regex_string = s; | ||
| 1977 | return match; | ||
| 1978 | } | ||
| 1979 | public static final void reset_match() { | ||
| 1980 | PlRegexResult match = PlV.regex_result; | ||
| 1981 | match.matcher = null; | ||
| 1982 | match.regex_string = null; | ||
| 1983 | } | ||
| 1984 | public static final PlObject regex_var(int var_number) { | ||
| 1985 | if (var_number == 0) { | ||
| 1986 | return PlV.sget("main::0"); | ||
| 1987 | } | ||
| 1988 | Matcher matcher = PlV.regex_result.matcher; | ||
| 1989 | if (matcher == null || var_number > matcher.groupCount() || var_number < 1) { | ||
| 1990 | return PlCx.UNDEF; | ||
| 1991 | } | ||
| 1992 | String cap = matcher.group(var_number); | ||
| 1993 | if (cap == null) { | ||
| 1994 | return PlCx.UNDEF; | ||
| 1995 | } | ||
| 1996 | return new PlString(cap); | ||
| 1997 | } | ||
| 1998 | public static final PlObject regex_var(String var_name) { | ||
| 1999 | PlRegexResult match = PlV.regex_result; | ||
| 2000 | Matcher matcher = match.matcher; | ||
| 2001 | String str = match.regex_string; | ||
| 2002 | if (matcher == null || str == null) { | ||
| 2003 | return PlCx.UNDEF; | ||
| 2004 | } | ||
| 2005 | if (var_name.equals("&")) { // $& | ||
| 2006 | return new PlString( str.substring(matcher.start(), matcher.end()) ); | ||
| 2007 | } | ||
| 2008 | if (var_name.equals("`")) { // $` | ||
| 2009 | return new PlString( str.substring(0, matcher.start()) ); | ||
| 2010 | } | ||
| 2011 | if (var_name.equals("'")) { // $' | ||
| 2012 | return new PlString( str.substring(matcher.end()) ); | ||
| 2013 | } | ||
| 2014 | return PlCx.UNDEF; | ||
| 2015 | } | ||
| 2016 | public static final PlObject regex_named_capture(String var_name) { | ||
| 2017 | if (var_name == null) { | ||
| 2018 | return PlCx.UNDEF; | ||
| 2019 | } | ||
| 2020 | Matcher matcher = PlV.regex_result.matcher; | ||
| 2021 | if (matcher == null) { | ||
| 2022 | return PlCx.UNDEF; | ||
| 2023 | } | ||
| 2024 | try { | ||
| 2025 | var_name = var_name.replace("_", "UnderScore"); // See: regex_escape() | ||
| 2026 | String cap = matcher.group(var_name); | ||
| 2027 | if (cap == null) { | ||
| 2028 | return PlCx.UNDEF; | ||
| 2029 | } | ||
| 2030 | return new PlString(cap); | ||
| 2031 | } catch (Exception e) { | ||
| 2032 | } | ||
| 2033 | return PlCx.UNDEF; | ||
| 2034 | } | ||
| 2035 | |||
| 2036 | // ****** end regex variables | ||
| 2037 | |||
| 2038 | public static final PlObject match(PlObject input, PlRegex pat, int want, boolean global, boolean c_flag) { | ||
| 2039 | // 'want' context (PlCx.LIST, PlCx.SCALAR, PlCx.VOID) | ||
| 2040 | // 'global' g - globally match the pattern repeatedly in the string | ||
| 2041 | // 'c_flag' c - keep the current position during repeated matching | ||
| 2042 | |||
| 2043 | String str = input.toString(); | ||
| 2044 | if (want != PlCx.LIST) { | ||
| 2045 | Matcher matcher = pat.p.matcher(str); | ||
| 2046 | if (global) { | ||
| 2047 | // scalar context, global match | ||
| 2048 | PlObject pos = input.pos(); | ||
| 2049 | boolean find; | ||
| 2050 | if (pos.is_undef()) { | ||
| 2051 | find = matcher.find(); | ||
| 2052 | } | ||
| 2053 | else { | ||
| 2054 | find = matcher.find(pos.to_int()); | ||
| 2055 | } | ||
| 2056 | if (find) { | ||
| 2057 | PlRegexResult match = set_match(matcher, str); | ||
| 2058 | set_pos(input, new PlInt(matcher.end()), match, str); | ||
| 2059 | return PlCx.TRUE; | ||
| 2060 | } | ||
| 2061 | else { | ||
| 2062 | // reset_match(); | ||
| 2063 | if (!c_flag) { | ||
| 2064 | set_pos(input, PlCx.UNDEF, null, null); | ||
| 2065 | } | ||
| 2066 | return PlCx.FALSE; | ||
| 2067 | } | ||
| 2068 | } | ||
| 2069 | else { | ||
| 2070 | // scalar context, non-global match | ||
| 2071 | if (matcher.find()) { | ||
| 2072 | set_match(matcher, str); | ||
| 2073 | return PlCx.TRUE; | ||
| 2074 | } | ||
| 2075 | else { | ||
| 2076 | // reset_match(); | ||
| 2077 | return PlCx.FALSE; | ||
| 2078 | } | ||
| 2079 | } | ||
| 2080 | } | ||
| 2081 | // list context | ||
| 2082 | Matcher matcher = pat.p.matcher(str); | ||
| 2083 | PlArray ret = new PlArray(); | ||
| 2084 | if (global) { | ||
| 2085 | // list context, global match | ||
| 2086 | // Note: if there are no captures, then return the matched substrings | ||
| 2087 | boolean found = false; | ||
| 2088 | while (matcher.find()) { | ||
| 2089 | found = true; | ||
| 2090 | int count = matcher.groupCount(); | ||
| 2091 | if (count > 0) { | ||
| 2092 | for (int i = 1; i <= count; i++) { | ||
| 2093 | String cap = matcher.group(i); | ||
| 2094 | if (cap == null) { | ||
| 2095 | ret.push_void(PlCx.UNDEF); | ||
| 2096 | } | ||
| 2097 | else { | ||
| 2098 | ret.push_void(cap); | ||
| 2099 | } | ||
| 2100 | } | ||
| 2101 | } | ||
| 2102 | else { | ||
| 2103 | String cap = matcher.group(); | ||
| 2104 | if (cap == null) { | ||
| 2105 | ret.push_void(PlCx.UNDEF); | ||
| 2106 | } | ||
| 2107 | else { | ||
| 2108 | ret.push_void(cap); | ||
| 2109 | } | ||
| 2110 | } | ||
| 2111 | } | ||
| 2112 | if (found) { | ||
| 2113 | set_match(matcher, str); | ||
| 2114 | } | ||
| 2115 | else { | ||
| 2116 | // reset_match(); | ||
| 2117 | } | ||
| 2118 | set_pos(input, PlCx.UNDEF, null, null); | ||
| 2119 | return ret; | ||
| 2120 | } | ||
| 2121 | else { | ||
| 2122 | // list context, non-global match | ||
| 2123 | if (matcher.find()) { | ||
| 2124 | set_match(matcher, str); | ||
| 2125 | for (int i = 1; i <= matcher.groupCount(); i++) { | ||
| 2126 | String cap = matcher.group(i); | ||
| 2127 | if (cap == null) { | ||
| 2128 | ret.push_void(PlCx.UNDEF); | ||
| 2129 | } | ||
| 2130 | else { | ||
| 2131 | ret.push_void(cap); | ||
| 2132 | } | ||
| 2133 | } | ||
| 2134 | } | ||
| 2135 | else { | ||
| 2136 | // reset_match(); | ||
| 2137 | } | ||
| 2138 | return ret; | ||
| 2139 | } | ||
| 2140 | } | ||
| 2141 | public static final PlObject match(PlObject s, PlLvalue pat, int want, boolean global, boolean c_flag) { | ||
| 2142 | return match(s, pat.get(), want, global, c_flag); | ||
| 2143 | } | ||
| 2144 | public static final PlObject match(PlObject s, PlObject pat, int want, boolean global, boolean c_flag) { | ||
| 2145 | // TODO - cache the compiled pattern | ||
| 2146 | return match(s, new PlRegex(pat, 0, false), want, global, c_flag); | ||
| 2147 | } | ||
| 2148 | |||
| 2149 | public static final PlObject replace(PlLvalue s, PlRegex pat, PlClosure rep, int want, boolean global, boolean replace_flag) { | ||
| 2150 | String str = s.toString(); | ||
| 2151 | int count = 0; | ||
| 2152 | Matcher matcher = pat.p.matcher(str); | ||
| 2153 | if (global) { | ||
| 2154 | final StringBuilder buf = new StringBuilder(str.length() + 256); | ||
| 2155 | int pos = 0; | ||
| 2156 | while (matcher.find()) { | ||
| 2157 | count++; | ||
| 2158 | set_match(matcher, str); | ||
| 2159 | int start = matcher.start(); | ||
| 2160 | int end = matcher.end(); | ||
| 2161 | String replace = rep.apply_do_block(PlCx.SCALAR, want, new PlArray()).toString(); | ||
| 2162 | if (start > pos) { | ||
| 2163 | buf.append( str.substring(pos, start) ); | ||
| 2164 | } | ||
| 2165 | if (replace.length() > 0) { | ||
| 2166 | buf.append( replace ); | ||
| 2167 | } | ||
| 2168 | pos = end; | ||
| 2169 | } | ||
| 2170 | if (count > 0) { | ||
| 2171 | if (pos <= str.length()) { | ||
| 2172 | buf.append( str.substring(pos) ); | ||
| 2173 | } | ||
| 2174 | if (replace_flag) { | ||
| 2175 | s.set(new PlString(buf.toString())); | ||
| 2176 | } | ||
| 2177 | } | ||
| 2178 | } | ||
| 2179 | else { | ||
| 2180 | if (matcher.find()) { | ||
| 2181 | count++; | ||
| 2182 | set_match(matcher, str); | ||
| 2183 | int start = matcher.start(); | ||
| 2184 | int end = matcher.end(); | ||
| 2185 | String replace = rep.apply_do_block(PlCx.SCALAR, want, new PlArray()).toString(); | ||
| 2186 | final StringBuilder buf = new StringBuilder(str.length() + replace.length()); | ||
| 2187 | if (start > 0) { | ||
| 2188 | buf.append( str.substring(0, start) ); | ||
| 2189 | } | ||
| 2190 | if (replace.length() > 0) { | ||
| 2191 | buf.append( replace ); | ||
| 2192 | } | ||
| 2193 | if (end <= str.length()) { | ||
| 2194 | buf.append( str.substring(end) ); | ||
| 2195 | } | ||
| 2196 | if (replace_flag) { | ||
| 2197 | s.set(new PlString(buf.toString())); | ||
| 2198 | } | ||
| 2199 | } | ||
| 2200 | } | ||
| 2201 | if (count == 0) { | ||
| 2202 | // no match | ||
| 2203 | return PlCx.FALSE; | ||
| 2204 | } | ||
| 2205 | return new PlInt(count); | ||
| 2206 | } | ||
| 2207 | public static final PlObject replace(PlLvalue s, PlRegex pat, String replace, int want, boolean global, boolean replace_flag) { | ||
| 2208 | String str = s.toString(); | ||
| 2209 | int count = 0; | ||
| 2210 | Matcher matcher = pat.p.matcher(str); | ||
| 2211 | if (global) { | ||
| 2212 | final StringBuilder buf = new StringBuilder(str.length() + 256); | ||
| 2213 | int pos = 0; | ||
| 2214 | while (matcher.find()) { | ||
| 2215 | count++; | ||
| 2216 | set_match(matcher, str); | ||
| 2217 | int start = matcher.start(); | ||
| 2218 | int end = matcher.end(); | ||
| 2219 | if (start > pos) { | ||
| 2220 | buf.append( str.substring(pos, start) ); | ||
| 2221 | } | ||
| 2222 | if (replace.length() > 0) { | ||
| 2223 | buf.append( replace ); | ||
| 2224 | } | ||
| 2225 | pos = end; | ||
| 2226 | } | ||
| 2227 | if (count > 0) { | ||
| 2228 | if (pos <= str.length()) { | ||
| 2229 | buf.append( str.substring(pos) ); | ||
| 2230 | } | ||
| 2231 | if (replace_flag) { | ||
| 2232 | s.set(new PlString(buf.toString())); | ||
| 2233 | } | ||
| 2234 | } | ||
| 2235 | } | ||
| 2236 | else { | ||
| 2237 | if (matcher.find()) { | ||
| 2238 | count++; | ||
| 2239 | set_match(matcher, str); | ||
| 2240 | int start = matcher.start(); | ||
| 2241 | int end = matcher.end(); | ||
| 2242 | final StringBuilder buf = new StringBuilder(str.length() + replace.length()); | ||
| 2243 | if (start > 0) { | ||
| 2244 | buf.append( str.substring(0, start) ); | ||
| 2245 | } | ||
| 2246 | if (replace.length() > 0) { | ||
| 2247 | buf.append( replace ); | ||
| 2248 | } | ||
| 2249 | if (end <= str.length()) { | ||
| 2250 | buf.append( str.substring(end) ); | ||
| 2251 | } | ||
| 2252 | if (replace_flag) { | ||
| 2253 | s.set(new PlString(buf.toString())); | ||
| 2254 | } | ||
| 2255 | } | ||
| 2256 | } | ||
| 2257 | if (count == 0) { | ||
| 2258 | // no match | ||
| 2259 | return PlCx.FALSE; | ||
| 2260 | } | ||
| 2261 | return new PlInt(count); | ||
| 2262 | } | ||
| 2263 | |||
| 2264 | public static final PlObject replace(PlLvalue s, PlRegex pat, PlObject rep, int want, boolean global, boolean replace_flag) { | ||
| 2265 | if (rep.is_coderef()) { | ||
| 2266 | return replace(s, pat, (PlClosure)rep, want, global, replace_flag); | ||
| 2267 | } | ||
| 2268 | return replace(s, pat, rep.toString(), want, global, replace_flag); | ||
| 2269 | } | ||
| 2270 | public static final PlObject replace(PlObject s, PlObject pat, PlObject rep, int want, boolean global, boolean replace_flag) { | ||
| 2271 | if (!s.is_lvalue()) { | ||
| 2272 | if (!replace_flag) { | ||
| 2273 | PlLvalue lv = new PlLvalue(s); | ||
| 2274 | return replace(lv, new PlRegex(pat, 0, false), rep, want, global, replace_flag); | ||
| 2275 | } | ||
| 2276 | PlCORE.die("Can't modify constant item in substitution (s///)"); | ||
| 2277 | } | ||
| 2278 | // TODO - cache the compiled pattern | ||
| 2279 | return replace((PlLvalue)s, new PlRegex(pat, 0, false), rep, want, global, replace_flag); | ||
| 2280 | } | ||
| 2281 | public static final PlObject replace(PlObject s, PlObject pat, String rep, int want, boolean global, boolean replace_flag) { | ||
| 2282 | if (!s.is_lvalue()) { | ||
| 2283 | if (!replace_flag) { | ||
| 2284 | PlLvalue lv = new PlLvalue(s); | ||
| 2285 | return replace(lv, new PlRegex(pat, 0, false), rep, want, global, replace_flag); | ||
| 2286 | } | ||
| 2287 | PlCORE.die("Can't modify constant item in substitution (s///)"); | ||
| 2288 | } | ||
| 2289 | // TODO - cache the compiled pattern | ||
| 2290 | return replace((PlLvalue)s, new PlRegex(pat, 0, false), rep, want, global, replace_flag); | ||
| 2291 | } | ||
| 2292 | |||
| 2293 | // $v =~ tr/xyz/abc/i | ||
| 2294 | // PerlOp.tr(v_v_100, new PlString("xyz"), new PlString("abc"), "", PlCx.VOID) | ||
| 2295 | public static final PlObject tr(PlObject pstr, PlObject psearchChars, PlObject preplaceChars, String modifier, int want) { | ||
| 2296 | String str = pstr.toString(); | ||
| 2297 | String searchChars = psearchChars.toString(); | ||
| 2298 | String replaceChars = preplaceChars.toString(); | ||
| 2299 | boolean complement = modifier.indexOf("c") < 0 ? false : true; // c = complement | ||
| 2300 | boolean replace_flag = modifier.indexOf("r") < 0 ? true : false; // r = replace flag | ||
| 2301 | int modified = 0; | ||
| 2302 | final int replaceCharsLength = replaceChars.length(); | ||
| 2303 | final int strLength = str.length(); | ||
| 2304 | final StringBuilder buf = new StringBuilder(strLength); | ||
| 2305 | |||
| 2306 | if (complement) { | ||
| 2307 | for (int i = 0; i < strLength; i++) { | ||
| 2308 | final char ch = str.charAt(i); | ||
| 2309 | final int index = searchChars.indexOf(ch); | ||
| 2310 | if (index < 0) { | ||
| 2311 | // not found | ||
| 2312 | modified++; | ||
| 2313 | if (replaceCharsLength > 0) { | ||
| 2314 | buf.append(replaceChars.charAt(replaceCharsLength - 1)); | ||
| 2315 | } | ||
| 2316 | } else { | ||
| 2317 | buf.append(ch); | ||
| 2318 | } | ||
| 2319 | } | ||
| 2320 | } | ||
| 2321 | else { | ||
| 2322 | for (int i = 0; i < strLength; i++) { | ||
| 2323 | final char ch = str.charAt(i); | ||
| 2324 | final int index = searchChars.indexOf(ch); | ||
| 2325 | if (index >= 0) { | ||
| 2326 | modified++; | ||
| 2327 | if (index < replaceCharsLength) { | ||
| 2328 | buf.append(replaceChars.charAt(index)); | ||
| 2329 | } | ||
| 2330 | } else { | ||
| 2331 | buf.append(ch); | ||
| 2332 | } | ||
| 2333 | } | ||
| 2334 | } | ||
| 2335 | |||
| 2336 | if (modified > 0 && replace_flag) { | ||
| 2337 | pstr.set(new PlString(buf.toString())); | ||
| 2338 | } | ||
| 2339 | return new PlInt(modified); | ||
| 2340 | } | ||
| 2341 | |||
| 2342 | |||
| 2343 | // looks_like_number | ||
| 2344 | private static int _parse_space(String s, int length, int offset) { | ||
| 2345 | for ( ; offset < length; offset++ ) { | ||
| 2346 | final int c3 = s.codePointAt(offset); | ||
| 2347 | switch (c3) { | ||
| 2348 | case ' ': case '\t': case '\n': case '\r': | ||
| 2349 | break; | ||
| 2350 | default: | ||
| 2351 | return offset; | ||
| 2352 | } | ||
| 2353 | } | ||
| 2354 | return offset; | ||
| 2355 | } | ||
| 2356 | private static boolean _parse_space_to_end(String s, int length, int offset) { | ||
| 2357 | for ( ; offset < length; offset++ ) { | ||
| 2358 | final int c3 = s.codePointAt(offset); | ||
| 2359 | switch (c3) { | ||
| 2360 | case ' ': case '\t': case '\n': case '\r': | ||
| 2361 | break; | ||
| 2362 | default: | ||
| 2363 | return false; | ||
| 2364 | } | ||
| 2365 | } | ||
| 2366 | return true; | ||
| 2367 | } | ||
| 2368 | private static boolean _parse_exp(String s, int length, int offset) { | ||
| 2369 | // 123.45E^^^ | ||
| 2370 | final int c = s.codePointAt(offset); | ||
| 2371 | if (c == '+' || c == '-') { | ||
| 2372 | offset++; | ||
| 2373 | if (offset >= length) { | ||
| 2374 | return false; | ||
| 2375 | } | ||
| 2376 | } | ||
| 2377 | for ( ; offset < length; offset++ ) { | ||
| 2378 | final int c3 = s.codePointAt(offset); | ||
| 2379 | switch (c3) { | ||
| 2380 | case '0': case '1': case '2': case '3': case '4': | ||
| 2381 | case '5': case '6': case '7': case '8': case '9': | ||
| 2382 | break; | ||
| 2383 | default: | ||
| 2384 | return _parse_space_to_end(s, length, offset); | ||
| 2385 | } | ||
| 2386 | } | ||
| 2387 | return true; | ||
| 2388 | } | ||
| 2389 | private static boolean _parse_dot(String s, int length, int offset) { | ||
| 2390 | // 123.^^^ | ||
| 2391 | for ( ; offset < length; offset++ ) { | ||
| 2392 | final int c3 = s.codePointAt(offset); | ||
| 2393 | switch (c3) { | ||
| 2394 | case '0': case '1': case '2': case '3': case '4': | ||
| 2395 | case '5': case '6': case '7': case '8': case '9': | ||
| 2396 | break; | ||
| 2397 | case 'E': case 'e': | ||
| 2398 | return _parse_exp(s, length, offset+1); | ||
| 2399 | default: | ||
| 2400 | return _parse_space_to_end(s, length, offset); | ||
| 2401 | } | ||
| 2402 | } | ||
| 2403 | return true; | ||
| 2404 | } | ||
| 2405 | private static boolean _parse_int(String s, int length, int offset) { | ||
| 2406 | // 123 | ||
| 2407 | for ( ; offset < length; offset++ ) { | ||
| 2408 | final int c3 = s.codePointAt(offset); | ||
| 2409 | switch (c3) { | ||
| 2410 | case '0': case '1': case '2': case '3': case '4': | ||
| 2411 | case '5': case '6': case '7': case '8': case '9': | ||
| 2412 | break; | ||
| 2413 | case '.': | ||
| 2414 | return _parse_dot(s, length, offset+1); | ||
| 2415 | case 'E': case 'e': | ||
| 2416 | return _parse_exp(s, length, offset+1); | ||
| 2417 | default: | ||
| 2418 | return _parse_space_to_end(s, length, offset); | ||
| 2419 | } | ||
| 2420 | } | ||
| 2421 | return true; | ||
| 2422 | } | ||
| 2423 | public static boolean looks_like_number(String s) { | ||
| 2424 | final int length = s.length(); | ||
| 2425 | int offset = _parse_space(s, length, 0); | ||
| 2426 | if (offset >= length) { | ||
| 2427 | return false; | ||
| 2428 | } | ||
| 2429 | int c = s.codePointAt(offset); | ||
| 2430 | if (c == '+' || c == '-') { | ||
| 2431 | offset++; | ||
| 2432 | if (offset >= length) { | ||
| 2433 | return false; | ||
| 2434 | } | ||
| 2435 | c = s.codePointAt(offset); | ||
| 2436 | } | ||
| 2437 | switch (c) { | ||
| 2438 | case 'i': case 'I': | ||
| 2439 | return s.substring(offset, offset+3).equalsIgnoreCase("inf"); | ||
| 2440 | case 'n': case 'N': | ||
| 2441 | return s.substring(offset, offset+3).equalsIgnoreCase("nan"); | ||
| 2442 | case '.': | ||
| 2443 | offset++; | ||
| 2444 | if (offset >= length) { | ||
| 2445 | return false; | ||
| 2446 | } | ||
| 2447 | final int c3 = s.codePointAt(offset); | ||
| 2448 | switch (c3) { | ||
| 2449 | case '0': case '1': case '2': case '3': case '4': | ||
| 2450 | case '5': case '6': case '7': case '8': case '9': | ||
| 2451 | return _parse_dot(s, length, offset+1); | ||
| 2452 | } | ||
| 2453 | case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': | ||
| 2454 | return _parse_int(s, length, offset+1); | ||
| 2455 | } | ||
| 2456 | return false; | ||
| 2457 | } | ||
| 2458 | public static boolean looks_like_number(PlObject arg) { | ||
| 2459 | if (arg.is_num() || arg.is_int()) { | ||
| 2460 | return true; | ||
| 2461 | } | ||
| 2462 | return looks_like_number(arg.toString()); | ||
| 2463 | } | ||
| 2464 | public static long _parse_oct(String s, int base) { | ||
| 2465 | try { | ||
| 2466 | s = s.replace("_",""); | ||
| 2467 | return Long.parseLong(s, base); | ||
| 2468 | } catch (NumberFormatException n) { | ||
| 2469 | } catch (Exception e) { | ||
| 2470 | } | ||
| 2471 | return 0; | ||
| 2472 | } | ||
| 2473 | public static long oct(String s) { | ||
| 2474 | final int length = s.length(); | ||
| 2475 | int c; | ||
| 2476 | |||
| 2477 | for (int i = 0; i < length; i++ ) { | ||
| 2478 | c = s.codePointAt(i); | ||
| 2479 | if (c > 254) { | ||
| 2480 | PlCORE.die("Wide character in oct"); | ||
| 2481 | } | ||
| 2482 | } | ||
| 2483 | |||
| 2484 | int offset = _parse_space(s, length, 0); | ||
| 2485 | if (offset >= length) { | ||
| 2486 | return 0; | ||
| 2487 | } | ||
| 2488 | int start = offset; | ||
| 2489 | c = s.codePointAt(offset); | ||
| 2490 | if (c == '0') { | ||
| 2491 | start++; | ||
| 2492 | offset++; | ||
| 2493 | if (offset >= length) { | ||
| 2494 | return 0; | ||
| 2495 | } | ||
| 2496 | c = s.codePointAt(offset); | ||
| 2497 | } | ||
| 2498 | boolean ul = false; | ||
| 2499 | switch (c) { | ||
| 2500 | case 'x': case 'X': | ||
| 2501 | start++; | ||
| 2502 | offset++; | ||
| 2503 | for ( ; offset < length; offset++ ) { | ||
| 2504 | c = s.codePointAt(offset); | ||
| 2505 | if (c == '_') { | ||
| 2506 | if (ul) { | ||
| 2507 | return _parse_oct(s.substring(start, offset), 16); | ||
| 2508 | } | ||
| 2509 | ul = true; | ||
| 2510 | } | ||
| 2511 | else if ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9')) { | ||
| 2512 | ul = false; | ||
| 2513 | } | ||
| 2514 | else { | ||
| 2515 | return _parse_oct(s.substring(start, offset), 16); | ||
| 2516 | } | ||
| 2517 | } | ||
| 2518 | return _parse_oct(s.substring(start, offset), 16); | ||
| 2519 | case 'b': case 'B': | ||
| 2520 | start++; | ||
| 2521 | offset++; | ||
| 2522 | for ( ; offset < length; offset++ ) { | ||
| 2523 | c = s.codePointAt(offset); | ||
| 2524 | switch (c) { | ||
| 2525 | case '_': | ||
| 2526 | if (ul) { | ||
| 2527 | return _parse_oct(s.substring(start, offset), 2); | ||
| 2528 | } | ||
| 2529 | ul = true; | ||
| 2530 | break; | ||
| 2531 | case '0': case '1': | ||
| 2532 | ul = false; | ||
| 2533 | break; | ||
| 2534 | default: | ||
| 2535 | return _parse_oct(s.substring(start, offset), 2); | ||
| 2536 | } | ||
| 2537 | } | ||
| 2538 | return _parse_oct(s.substring(start, offset), 2); | ||
| 2539 | case '0': case '1': case '2': case '3': case '4': | ||
| 2540 | case '5': case '6': case '7': case '_': | ||
| 2541 | for ( ; offset < length; offset++ ) { | ||
| 2542 | c = s.codePointAt(offset); | ||
| 2543 | switch (c) { | ||
| 2544 | case '_': | ||
| 2545 | if (ul) { | ||
| 2546 | return _parse_oct(s.substring(start, offset), 8); | ||
| 2547 | } | ||
| 2548 | ul = true; | ||
| 2549 | break; | ||
| 2550 | case '0': case '1': case '2': case '3': case '4': | ||
| 2551 | case '5': case '6': case '7': | ||
| 2552 | ul = false; | ||
| 2553 | break; | ||
| 2554 | default: | ||
| 2555 | return _parse_oct(s.substring(start, offset), 8); | ||
| 2556 | } | ||
| 2557 | } | ||
| 2558 | return _parse_oct(s.substring(start, offset), 8); | ||
| 2559 | } | ||
| 2560 | return 0; | ||
| 2561 | } | ||
| 2562 | |||
| 2563 | } | ||
| 2564 | |||
| 2565 | EOT | ||
| 2566 | , # list break | ||
| 2567 | <<'EOT' | ||
| 2568 | |||
| 2569 | class PlV { | ||
| 2570 | // PlV implements namespaces and global variables | ||
| 2571 | // | ||
| 2572 | // TODO - import CORE subroutines in new namespaces, if needed | ||
| 2573 | |||
| 2574 | public static PlRegexResult regex_result = new PlRegexResult(); | ||
| 2575 | public static Path path; | ||
| 2576 | public static PlFileHandle STDIN = (PlFileHandle)PlStringConstant.getConstant("main::STDIN").fileRef.o; | ||
| 2577 | public static PlFileHandle STDOUT = (PlFileHandle)PlStringConstant.getConstant("main::STDOUT").fileRef.o; | ||
| 2578 | public static PlFileHandle STDERR = (PlFileHandle)PlStringConstant.getConstant("main::STDERR").fileRef.o; | ||
| 2579 | public static PlFileHandle selectedFileHandle = STDOUT; | ||
| 2580 | public static final PlStringConstant sub_sprintf_vd = PlStringConstant.getConstant("Perlito5::Runtime::Sprintf::sprintf_vd"); | ||
| 2581 | |||
| 2582 | // initialize special variables like $_ $\ | ||
| 2583 | EOT | ||
| 2584 | , (( map { | ||
| 2585 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 2586 | my $index = $special_var_index{$_}[0]; | ||
| 2587 | my $class = $special_var_index{$_}[1]; | ||
| 2588 | " | ||
| 2589 | public static $class $java_name = new $class(); // \$$_ | ||
| 2590 | " | ||
| 2591 | } | ||
| 2592 | sort keys %Perlito5::Java::special_scalar | ||
| 2593 | )) | ||
| 2594 | , <<'EOT' | ||
| 2595 | |||
| 2596 | public static PlObject boolean_stack; | ||
| 2597 | public static PlArray local_stack = new PlArray(); | ||
| 2598 | public static Random random = new Random(); | ||
| 2599 | public static String lastStat = null; | ||
| 2600 | |||
| 2601 | public static final void init(String[] args) { | ||
| 2602 | // @ARGV | ||
| 2603 | PlV.array_set("main::ARGV", new PlArray(args)); // args is String[] | ||
| 2604 | // %ENV | ||
| 2605 | PlV.hash_set("main::ENV", new PlArray(System.getenv())); // env is Map<String, String> | ||
| 2606 | // $" = " " | ||
| 2607 | PlV.sset("main::" + (char)34, new PlString(" ")); | ||
| 2608 | // $^O = "Unix"; default = "perlito5" | ||
| 2609 | PlV.sset("main::" + (char)15, new PlString( System.getProperty("os.name", "perlito5") )); | ||
| 2610 | // $/ = "\n" | ||
| 2611 | PlV.sset("main::/", new PlString( System.getProperty("line.separator", "\n") )); | ||
| 2612 | // $; = \034 | ||
| 2613 | PlV.sset("main::;", new PlString((char)28)); | ||
| 2614 | // $^T = time at start | ||
| 2615 | PlV.sset("main::" + (char)20, PlCORE.time(PlCx.SCALAR, new PlArray())); | ||
| 2616 | |||
| 2617 | PlV.STDIN.inputStream = System.in; | ||
| 2618 | PlV.STDIN.reader = new BufferedReader(new InputStreamReader(System.in)); | ||
| 2619 | PlV.STDIN.eof = false; | ||
| 2620 | PlV.STDIN.typeglob_name = "main::STDIN"; | ||
| 2621 | PlV.STDIN.charset = "UTF-8"; | ||
| 2622 | PlFileHandle.allOpenFiles.add(PlV.STDIN); | ||
| 2623 | |||
| 2624 | PlV.STDOUT.outputStream = System.out; | ||
| 2625 | PlV.STDOUT.typeglob_name = "main::STDOUT"; | ||
| 2626 | PlV.STDOUT.charset = "UTF-8"; | ||
| 2627 | PlFileHandle.allOpenFiles.add(PlV.STDOUT); | ||
| 2628 | |||
| 2629 | PlV.STDERR.outputStream = System.err; | ||
| 2630 | PlV.STDERR.typeglob_name = "main::STDERR"; | ||
| 2631 | PlV.STDERR.charset = "UTF-8"; | ||
| 2632 | PlFileHandle.allOpenFiles.add(PlV.STDERR); | ||
| 2633 | |||
| 2634 | PlCORE.select(PlV.STDOUT); | ||
| 2635 | |||
| 2636 | try { | ||
| 2637 | PlV.path = Paths.get(".").toRealPath(); | ||
| 2638 | } | ||
| 2639 | catch (IOException e) { | ||
| 2640 | // don't know what to do | ||
| 2641 | } | ||
| 2642 | |||
| 2643 | PlV.fset("main::STDIN", PlV.STDIN); // "GLOB" | ||
| 2644 | PlV.fset("main::STDOUT", PlV.STDOUT); | ||
| 2645 | PlV.fset("main::STDERR", PlV.STDERR); | ||
| 2646 | |||
| 2647 | PlV.cset("UNIVERSAL::can", new PlClosure(PlCx.UNDEF, new PlObject[]{ }, "UNIVERSAL", true) { | ||
| 2648 | public PlObject apply(int want, PlArray List__) { | ||
| 2649 | PlObject self = List__.shift(); | ||
| 2650 | String method_name = List__.shift().toString(); | ||
| 2651 | PlClass bless = self.blessed_class(); | ||
| 2652 | if ( bless != null ) { | ||
| 2653 | PlObject methodCode = bless.method_lookup(method_name, 0); | ||
| 2654 | if (methodCode.is_coderef()) { | ||
| 2655 | return methodCode; | ||
| 2656 | } | ||
| 2657 | return PlCx.UNDEF; | ||
| 2658 | } | ||
| 2659 | |||
| 2660 | // calling can() as a class method | ||
| 2661 | PlObject methodCode = PlClass.getInstance(self).method_lookup(method_name, 0); | ||
| 2662 | if (methodCode.is_coderef()) { | ||
| 2663 | return methodCode; | ||
| 2664 | } | ||
| 2665 | |||
| 2666 | return PlCx.UNDEF; | ||
| 2667 | } | ||
| 2668 | }); | ||
| 2669 | PlV.cset("UNIVERSAL::isa", new PlClosure(PlCx.UNDEF, new PlObject[]{ }, "UNIVERSAL", true) { | ||
| 2670 | public PlObject apply(int want, PlArray List__) { | ||
| 2671 | PlObject self = List__.shift(); | ||
| 2672 | String class_name = List__.shift().toString(); | ||
| 2673 | PlClass bless = self.blessed_class(); | ||
| 2674 | if ( bless != null ) { | ||
| 2675 | return bless.isa(class_name, 0); | ||
| 2676 | } | ||
| 2677 | |||
| 2678 | // reftype == "ARRAY" | ||
| 2679 | if (self.reftype().toString().equals(class_name)) { | ||
| 2680 | return PlCx.INT1; | ||
| 2681 | } | ||
| 2682 | |||
| 2683 | // calling isa() as a class method | ||
| 2684 | bless = PlClass.getInstance(self); | ||
| 2685 | if ( bless != null ) { | ||
| 2686 | return bless.isa(class_name, 0); | ||
| 2687 | } | ||
| 2688 | |||
| 2689 | return PlCx.UNDEF; | ||
| 2690 | } | ||
| 2691 | }); | ||
| 2692 | |||
| 2693 | PerlOp.reset_match(); | ||
| 2694 | } | ||
| 2695 | public static final void teardown() { | ||
| 2696 | |||
| 2697 | // TODO - catch error in END | ||
| 2698 | // Perlito5::set_global_phase("END"); | ||
| 2699 | // eval { | ||
| 2700 | // $_->() for @Perlito5::END_BLOCK; | ||
| 2701 | // 1; | ||
| 2702 | // } | ||
| 2703 | // or warn "$@\nEND failed--call queue aborted.\n" | ||
| 2704 | |||
| 2705 | // Perlito5::set_global_phase("END"); | ||
| 2706 | PlV.sset("main::" + (char)7 + "LOBAL_PHASE", new PlStringConstant("END")); | ||
| 2707 | |||
| 2708 | // $_->() for @Perlito5::END_BLOCK; | ||
| 2709 | for (PlObject tmp : PlArray.construct_list_of_aliases(PlV.array_get("Perlito5::END_BLOCK"))) { | ||
| 2710 | tmp.apply(PlCx.VOID, new PlArray()); | ||
| 2711 | } | ||
| 2712 | |||
| 2713 | PlFileHandle.close_all_files(); | ||
| 2714 | } | ||
| 2715 | |||
| 2716 | // scalar | ||
| 2717 | public static final PlLvalue sget(String name) { | ||
| 2718 | return PlStringConstant.getConstant(name).scalarRef; | ||
| 2719 | } | ||
| 2720 | public static final PlLvalue sget_local(String name) { | ||
| 2721 | return (PlLvalue)PerlOp.push_local_scalar(PlCx.UNDEF, name); | ||
| 2722 | } | ||
| 2723 | public static final PlObject sset(String name, PlObject v) { | ||
| 2724 | return PlStringConstant.getConstant(name).scalarRef.set(v); | ||
| 2725 | } | ||
| 2726 | public static final PlObject sset(String name, Object o) { | ||
| 2727 | return PlStringConstant.getConstant(name).scalarRef.set(PlJavaObject.fromObject(o)); | ||
| 2728 | } | ||
| 2729 | public static final PlObject sset_local(String name, PlObject v) { | ||
| 2730 | return PerlOp.push_local_scalar(v, name); | ||
| 2731 | } | ||
| 2732 | public static final void sset_alias(String name, PlLvalue v) { | ||
| 2733 | PlStringConstant.getConstant(name).scalarRef = v; | ||
| 2734 | } | ||
| 2735 | |||
| 2736 | // accessors for special variables like $_ $\ | ||
| 2737 | EOT | ||
| 2738 | , (( map { | ||
| 2739 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 2740 | my $index = $special_var_index{$_}[0]; | ||
| 2741 | my $class = $special_var_index{$_}[1]; | ||
| 2742 | " | ||
| 2743 | public static final PlLvalue sget_local_${java_name}() { | ||
| 2744 | PerlOp.push_local_special_var_${java_name}(); | ||
| 2745 | return ${java_name}; | ||
| 2746 | } | ||
| 2747 | public static final PlObject sset_local_${java_name}(PlObject v) { | ||
| 2748 | PerlOp.push_local_special_var_${java_name}(); | ||
| 2749 | return ${java_name}.set(v); | ||
| 2750 | } | ||
| 2751 | public static final void sset_alias_${java_name}(${class} v) { | ||
| 2752 | ${java_name} = v; | ||
| 2753 | } | ||
| 2754 | " | ||
| 2755 | } | ||
| 2756 | sort keys %Perlito5::Java::special_scalar | ||
| 2757 | )) | ||
| 2758 | , <<'EOT' | ||
| 2759 | |||
| 2760 | public static final PlLvalue cget(String name) { | ||
| 2761 | // this implements " \&name " | ||
| 2762 | PlLvalue code = PlStringConstant.getConstant(name).codeRef; | ||
| 2763 | if ( code.is_coderef() ) { | ||
| 2764 | return code; | ||
| 2765 | } | ||
| 2766 | int pos = name.lastIndexOf("::"); | ||
| 2767 | if (pos == -1) { | ||
| 2768 | return code; | ||
| 2769 | } | ||
| 2770 | String namespace = name.substring(0, pos); | ||
| 2771 | PlLvalue autoload = PlV.cget_no_autoload(namespace + "::AUTOLOAD"); | ||
| 2772 | if ( autoload.is_coderef() ) { | ||
| 2773 | PlV.sset(namespace + "::AUTOLOAD", new PlString(name)); | ||
| 2774 | return autoload; | ||
| 2775 | } | ||
| 2776 | return code; | ||
| 2777 | } | ||
| 2778 | public static final PlLvalue cget_local(String name) { | ||
| 2779 | return (PlLvalue)PerlOp.push_local_named_sub(PlCx.UNDEF, name); | ||
| 2780 | } | ||
| 2781 | public static final PlLvalue cget_no_autoload(String name) { | ||
| 2782 | return PlStringConstant.getConstant(name).codeRef; | ||
| 2783 | } | ||
| 2784 | public static final PlObject cset(String name, PlObject v) { | ||
| 2785 | return PlStringConstant.getConstant(name).cset(v); | ||
| 2786 | } | ||
| 2787 | public static final PlObject cset_local(String name, PlObject v) { | ||
| 2788 | return PerlOp.push_local_named_sub(v, name); | ||
| 2789 | } | ||
| 2790 | public static final void cset_alias(String name, PlLvalue v) { | ||
| 2791 | PlStringConstant.getConstant(name).cset_alias(v); | ||
| 2792 | } | ||
| 2793 | |||
| 2794 | // hash | ||
| 2795 | public static final PlHash hash_get(String name) { | ||
| 2796 | return PlStringConstant.getConstant(name).hashRef.hash_deref_strict(); | ||
| 2797 | } | ||
| 2798 | public static final PlHash hash_get_local(String name) { | ||
| 2799 | PlLvalue o = (PlLvalue)PerlOp.push_local_hash(new PlHashRef(), name); | ||
| 2800 | return o.hash_deref_strict(); | ||
| 2801 | } | ||
| 2802 | public static final PlObject hash_set(String name, PlObject v) { | ||
| 2803 | PlLvalue o = PlStringConstant.getConstant(name).hashRef; | ||
| 2804 | return o.hash_deref_set(v); | ||
| 2805 | } | ||
| 2806 | public static final PlObject hash_set_local(String name, PlObject v) { | ||
| 2807 | PlLvalue o = (PlLvalue)PerlOp.push_local_hash(new PlHashRef(), name); | ||
| 2808 | return o.hash_deref_set(v); | ||
| 2809 | } | ||
| 2810 | public static final PlLvalue hget(String name) { | ||
| 2811 | return PlStringConstant.getConstant(name).hashRef; | ||
| 2812 | } | ||
| 2813 | public static final PlLvalue hget_local(String name) { | ||
| 2814 | return (PlLvalue)PerlOp.push_local_hash(new PlHashRef(), name); | ||
| 2815 | } | ||
| 2816 | public static final PlObject hset(String name, PlObject v) { | ||
| 2817 | return PlStringConstant.getConstant(name).hashRef.set(v); | ||
| 2818 | } | ||
| 2819 | public static final PlObject hset(String name, PlLvalue v) { | ||
| 2820 | return PlStringConstant.getConstant(name).hashRef.set(v); | ||
| 2821 | } | ||
| 2822 | public static final PlObject hset_local(String name, PlObject v) { | ||
| 2823 | return (PlLvalue)PerlOp.push_local_hash(v, name); | ||
| 2824 | } | ||
| 2825 | public static final void hset_alias(String name, PlHash v) { | ||
| 2826 | PlStringConstant.getConstant(name).hashRef = new PlLvalue(v); | ||
| 2827 | } | ||
| 2828 | |||
| 2829 | // array | ||
| 2830 | public static final PlArray array_get(String name) { | ||
| 2831 | return PlStringConstant.getConstant(name).arrayRef.array_deref_strict(); | ||
| 2832 | } | ||
| 2833 | public static final PlArray array_get_local(String name) { | ||
| 2834 | PlLvalue o = (PlLvalue)PerlOp.push_local_array(new PlArrayRef(), name); | ||
| 2835 | return o.array_deref_strict(); | ||
| 2836 | } | ||
| 2837 | public static final PlObject array_set(String name, PlObject v) { | ||
| 2838 | PlLvalue o = PlStringConstant.getConstant(name).arrayRef; | ||
| 2839 | return o.array_deref_set(v); | ||
| 2840 | } | ||
| 2841 | public static final PlObject array_set_local(String name, PlObject v) { | ||
| 2842 | PlLvalue o = (PlLvalue)PerlOp.push_local_array(new PlArrayRef(), name); | ||
| 2843 | return o.array_deref_set(v); | ||
| 2844 | } | ||
| 2845 | public static final PlLvalue aget(String name) { | ||
| 2846 | return PlStringConstant.getConstant(name).arrayRef; | ||
| 2847 | } | ||
| 2848 | public static final PlLvalue aget_local(String name) { | ||
| 2849 | return (PlLvalue)PerlOp.push_local_array(new PlArrayRef(), name); | ||
| 2850 | } | ||
| 2851 | public static final PlObject aset(String name, PlObject v) { | ||
| 2852 | return PlStringConstant.getConstant(name).arrayRef.array_deref_set(v); | ||
| 2853 | } | ||
| 2854 | public static final PlObject aset_local(String name, PlObject v) { | ||
| 2855 | return (PlLvalue)PerlOp.push_local_array(v, name); | ||
| 2856 | } | ||
| 2857 | public static final void aset_alias(String name, PlArray v) { | ||
| 2858 | PlStringConstant.getConstant(name).arrayRef = new PlLvalue(v); | ||
| 2859 | } | ||
| 2860 | |||
| 2861 | // filehandle | ||
| 2862 | public static final PlLvalue fget(String name) { | ||
| 2863 | return PlStringConstant.getConstant(name).fileRef; | ||
| 2864 | } | ||
| 2865 | public static final PlLvalue fget_local(String name) { | ||
| 2866 | return (PlLvalue)PerlOp.push_local_array(new PlFileHandle(name), name); | ||
| 2867 | } | ||
| 2868 | public static final PlObject fset(String name, PlObject v) { | ||
| 2869 | return PlStringConstant.getConstant(name).fileRef.set(v); | ||
| 2870 | } | ||
| 2871 | public static final PlObject fset_local(String name, PlObject v) { | ||
| 2872 | return (PlLvalue)PerlOp.push_local_array(v, name); | ||
| 2873 | } | ||
| 2874 | |||
| 2875 | // code | ||
| 2876 | public static final PlObject code_lookup_by_name(String nameSpace, PlObject name) { | ||
| 2877 | if (name.is_coderef()) { | ||
| 2878 | return name; | ||
| 2879 | } | ||
| 2880 | if (name.is_bool() && name.to_boolean()) { | ||
| 2881 | // RT #63790: calling PL_sv_yes as a sub is special-cased to silently | ||
| 2882 | // return (so Foo->import() silently fails if import() doesn't exist), | ||
| 2883 | return name; | ||
| 2884 | } | ||
| 2885 | String s = name.toString(); | ||
| 2886 | if (s.indexOf("::") == -1) { | ||
| 2887 | s = nameSpace + "::" + s; | ||
| 2888 | } | ||
| 2889 | return PlV.cget(s); | ||
| 2890 | } | ||
| 2891 | public static final PlObject code_lookup_by_name_no_autoload(String nameSpace, PlObject name) { | ||
| 2892 | if (name.is_coderef()) { | ||
| 2893 | return name; | ||
| 2894 | } | ||
| 2895 | String s = name.toString(); | ||
| 2896 | if (s.indexOf("::") == -1) { | ||
| 2897 | s = nameSpace + "::" + s; | ||
| 2898 | } | ||
| 2899 | return PlV.cget_no_autoload(s); | ||
| 2900 | } | ||
| 2901 | |||
| 2902 | // glob | ||
| 2903 | public static final PlObject glob_set(PlObject name, PlObject value, String nameSpace) { | ||
| 2904 | return glob_set(name.toString(), value, nameSpace); | ||
| 2905 | } | ||
| 2906 | public static final PlObject glob_set(String name, PlObject value, String nameSpace) { | ||
| 2907 | if (value.is_lvalue()) { | ||
| 2908 | value = value.get(); | ||
| 2909 | } | ||
| 2910 | if (value.is_coderef()) { | ||
| 2911 | PlV.cset(name, value); | ||
| 2912 | } | ||
| 2913 | else if (value.is_hashref()) { | ||
| 2914 | PlV.hset(name, value); | ||
| 2915 | } | ||
| 2916 | else if (value.is_arrayref()) { | ||
| 2917 | PlStringConstant.getConstant(name).arrayRef.set(value); | ||
| 2918 | } | ||
| 2919 | else if (value.is_scalarref()) { | ||
| 2920 | PlV.sset(name, value.scalar_deref(nameSpace)); | ||
| 2921 | } | ||
| 2922 | else if (value.is_typeglobref()) { | ||
| 2923 | // *x = \*y | ||
| 2924 | PlGlobRef gl = (PlGlobRef)value; | ||
| 2925 | return glob_set(name, gl.filehandle, nameSpace); | ||
| 2926 | } | ||
| 2927 | else if (value.is_filehandle()) { | ||
| 2928 | // *x = *y | ||
| 2929 | PlFileHandle fh = (PlFileHandle)value; | ||
| 2930 | String typeglob_name = fh.typeglob_name; | ||
| 2931 | if (typeglob_name == null) { | ||
| 2932 | PlCORE.die("not implemented assign anonymous typeglob to typeglob"); | ||
| 2933 | } | ||
| 2934 | return glob_set(name, new PlString(typeglob_name), nameSpace); | ||
| 2935 | } | ||
| 2936 | else if (!value.is_ref()) { | ||
| 2937 | String typeglob_name = value.toString(); | ||
| 2938 | if (typeglob_name.indexOf("::") == -1) { | ||
| 2939 | typeglob_name = nameSpace + "::" + typeglob_name; | ||
| 2940 | } | ||
| 2941 | // TODO - share lvalue containers (alias) | ||
| 2942 | PlV.fset(name, PlV.fget(typeglob_name)); | ||
| 2943 | PlV.cset_alias(name, PlV.cget(typeglob_name)); | ||
| 2944 | PlV.sset_alias(name, PlV.sget(typeglob_name)); | ||
| 2945 | PlV.aset(name, PlV.aget(typeglob_name)); | ||
| 2946 | PlV.hset(name, PlV.hget(typeglob_name)); | ||
| 2947 | } | ||
| 2948 | else { | ||
| 2949 | PlCORE.die("not implemented assign " + value.ref() + " to typeglob"); | ||
| 2950 | } | ||
| 2951 | return value; | ||
| 2952 | } | ||
| 2953 | public static final PlObject glob_set_local(PlString name, PlObject value, String nameSpace) { | ||
| 2954 | return glob_set_local(name.toString(), value, nameSpace); | ||
| 2955 | } | ||
| 2956 | public static final PlObject glob_set_local(String name, PlObject value, String nameSpace) { | ||
| 2957 | if (value.is_coderef()) { | ||
| 2958 | PlV.cset_local(name, value); | ||
| 2959 | } | ||
| 2960 | else if (value.is_hashref()) { | ||
| 2961 | PlV.hset_local(name, value); | ||
| 2962 | } | ||
| 2963 | else if (value.is_arrayref()) { | ||
| 2964 | PlV.aset_local(name, value); | ||
| 2965 | } | ||
| 2966 | else if (value.is_scalarref()) { | ||
| 2967 | PlV.sset_local(name, value); | ||
| 2968 | } | ||
| 2969 | else if (value.is_typeglobref()) { | ||
| 2970 | // local *x = \*y | ||
| 2971 | PlGlobRef gl = (PlGlobRef)value; | ||
| 2972 | return glob_set_local(name, gl.filehandle, nameSpace); | ||
| 2973 | } | ||
| 2974 | else if (value.is_filehandle()) { | ||
| 2975 | // local *x = *y | ||
| 2976 | PlFileHandle fh = (PlFileHandle)value; | ||
| 2977 | String typeglob_name = fh.typeglob_name; | ||
| 2978 | if (typeglob_name == null) { | ||
| 2979 | PlCORE.die("not implemented assign anonymous typeglob to typeglob"); | ||
| 2980 | } | ||
| 2981 | return glob_set_local(name, new PlString(typeglob_name), nameSpace); | ||
| 2982 | } | ||
| 2983 | else if (!value.is_ref()) { | ||
| 2984 | String typeglob_name = value.toString(); | ||
| 2985 | if (typeglob_name.indexOf("::") == -1) { | ||
| 2986 | typeglob_name = nameSpace + "::" + typeglob_name; | ||
| 2987 | } | ||
| 2988 | // TODO - share lvalue containers (alias) | ||
| 2989 | PlV.fset_local(name, PlV.fget(typeglob_name)); | ||
| 2990 | PlV.cset_local(name, PlCx.UNDEF); | ||
| 2991 | PlV.cset_alias(name, PlV.cget(typeglob_name)); | ||
| 2992 | PlV.sset_local(name, PlCx.UNDEF); | ||
| 2993 | PlV.sset_alias(name, PlV.sget(typeglob_name)); | ||
| 2994 | PlV.aset_local(name, PlV.aget(typeglob_name)); | ||
| 2995 | PlV.hset_local(name, PlV.hget(typeglob_name)); | ||
| 2996 | } | ||
| 2997 | else { | ||
| 2998 | PlCORE.die("not implemented assign " + value.ref() + " to typeglob"); | ||
| 2999 | } | ||
| 3000 | return value; | ||
| 3001 | } | ||
| 3002 | public static final PlObject make_reference(PlObject value) { | ||
| 3003 | if (value.is_filehandle()) { | ||
| 3004 | return new PlGlobRef(value); | ||
| 3005 | } | ||
| 3006 | return new PlLvalueRef(value); | ||
| 3007 | } | ||
| 3008 | public static final PlObject make_reference(PlLvalue value) { | ||
| 3009 | if (value.is_filehandle()) { | ||
| 3010 | return new PlGlobRef(value); | ||
| 3011 | } | ||
| 3012 | return new PlLvalueRef(value); | ||
| 3013 | } | ||
| 3014 | |||
| 3015 | } | ||
| 3016 | class PlObject implements Cloneable, Iterable<PlObject> { | ||
| 3017 | public static final String REF_str = new String(""); | ||
| 3018 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 3019 | |||
| 3020 | public Iterator<PlObject> iterator() { | ||
| 3021 | // if (this.is_array()) { | ||
| 3022 | // return ((PlArray)this).iterator(); | ||
| 3023 | // } | ||
| 3024 | // if (this.is_hash()) { | ||
| 3025 | // return ((PlHash)this).iterator(); | ||
| 3026 | // } | ||
| 3027 | // if (this.is_lvalue()) { | ||
| 3028 | // return ((PlLvalue)this).iterator(); | ||
| 3029 | // } | ||
| 3030 | return new PlLvalueIterator(new PlROvalue(this)); | ||
| 3031 | } | ||
| 3032 | |||
| 3033 | public PlObject() { | ||
| 3034 | } | ||
| 3035 | EOT | ||
| 3036 | # add interfaces to Java classes | ||
| 3037 | # that were declared with | ||
| 3038 | # | ||
| 3039 | # package MyJavaClass { Java } | ||
| 3040 | # | ||
| 3041 | , (( map { | ||
| 3042 | my $class = $java_classes{$_}; | ||
| 3043 | my $java_class_name = $class->{java_type}; | ||
| 3044 | my $perl_to_java = $class->{perl_to_java}; | ||
| 3045 | $class->{import} || $class->{extends} || $class->{implements} ? | ||
| 3046 | " public ${java_class_name} ${perl_to_java}() {\n" | ||
| 3047 | . " PlCORE.die(\"error .${perl_to_java}!\");\n" | ||
| 3048 | . " return null;\n" | ||
| 3049 | . " }\n" : () | ||
| 3050 | } | ||
| 3051 | grep { $java_classes{$_}{perl_to_java} } | ||
| 3052 | sort keys %java_classes | ||
| 3053 | )) | ||
| 3054 | , <<'EOT' | ||
| 3055 | // public String toString() { | ||
| 3056 | // return this.toString(); | ||
| 3057 | // } | ||
| 3058 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 3059 | // want String | ||
| 3060 | for (Class[] cl : params) { | ||
| 3061 | if (cl[pos].equals( "".getClass() )) { | ||
| 3062 | return new PerlArgumentLookupResult( this.toString(), cl[pos] ); | ||
| 3063 | } | ||
| 3064 | } | ||
| 3065 | // want int | ||
| 3066 | for (Class[] cl : params) { | ||
| 3067 | if (cl[pos].equals( java.lang.Integer.TYPE )) { | ||
| 3068 | return new PerlArgumentLookupResult( this.to_int(), cl[pos] ); | ||
| 3069 | } | ||
| 3070 | } | ||
| 3071 | // want long | ||
| 3072 | for (Class[] cl : params) { | ||
| 3073 | if (cl[pos].equals( java.lang.Long.TYPE )) { | ||
| 3074 | return new PerlArgumentLookupResult( this.to_long(), cl[pos] ); | ||
| 3075 | } | ||
| 3076 | } | ||
| 3077 | // want boolean | ||
| 3078 | for (Class[] cl : params) { | ||
| 3079 | if (cl[pos].equals( java.lang.Boolean.TYPE )) { | ||
| 3080 | return new PerlArgumentLookupResult( this.to_boolean(), cl[pos] ); | ||
| 3081 | } | ||
| 3082 | } | ||
| 3083 | // want double | ||
| 3084 | for (Class[] cl : params) { | ||
| 3085 | if (cl[pos].equals( java.lang.Double.TYPE )) { | ||
| 3086 | return new PerlArgumentLookupResult( this.to_double(), cl[pos] ); | ||
| 3087 | } | ||
| 3088 | } | ||
| 3089 | // want float | ||
| 3090 | for (Class[] cl : params) { | ||
| 3091 | if (cl[pos].equals( java.lang.Float.TYPE )) { | ||
| 3092 | return new PerlArgumentLookupResult( this.to_float(), cl[pos] ); | ||
| 3093 | } | ||
| 3094 | } | ||
| 3095 | // want array | ||
| 3096 | for (Class[] cl : params) { | ||
| 3097 | if (cl[pos].isArray() && pos == (cl.length - 1)) { | ||
| 3098 | // varargs | ||
| 3099 | return new PerlArgumentLookupResult( null, cl[pos] ); | ||
| 3100 | } | ||
| 3101 | } | ||
| 3102 | |||
| 3103 | // default: return the Perl class | ||
| 3104 | return new PerlArgumentLookupResult( this, this.getClass() ); | ||
| 3105 | } | ||
| 3106 | public int to_int() { | ||
| 3107 | long v = this.to_long(); | ||
| 3108 | if (v > Integer.MAX_VALUE || v < Integer.MIN_VALUE) { | ||
| 3109 | PlCORE.die("numeric overflow converting to int"); | ||
| 3110 | } | ||
| 3111 | return (int)v; | ||
| 3112 | } | ||
| 3113 | public byte to_byte() { | ||
| 3114 | long v = this.to_long(); | ||
| 3115 | if (v > Byte.MAX_VALUE || v < Byte.MIN_VALUE) { | ||
| 3116 | PlCORE.die("numeric overflow converting to byte"); | ||
| 3117 | } | ||
| 3118 | return (byte)v; | ||
| 3119 | } | ||
| 3120 | public short to_short() { | ||
| 3121 | long v = this.to_long(); | ||
| 3122 | if (v > Short.MAX_VALUE || v < Short.MIN_VALUE) { | ||
| 3123 | PlCORE.die("numeric overflow converting to short"); | ||
| 3124 | } | ||
| 3125 | return (short)v; | ||
| 3126 | } | ||
| 3127 | public float to_float() { | ||
| 3128 | double v = this.to_double(); | ||
| 3129 | if (v > Float.MAX_VALUE || v < Float.MIN_VALUE) { | ||
| 3130 | PlCORE.die("numeric overflow converting to float"); | ||
| 3131 | } | ||
| 3132 | return (float)v; | ||
| 3133 | } | ||
| 3134 | public long to_long() { | ||
| 3135 | return 0; | ||
| 3136 | } | ||
| 3137 | public double to_double() { | ||
| 3138 | return 0.0; | ||
| 3139 | } | ||
| 3140 | public boolean to_boolean() { | ||
| 3141 | return false; | ||
| 3142 | } | ||
| 3143 | public PlObject to_num() { | ||
| 3144 | return PlCx.INT0; | ||
| 3145 | } | ||
| 3146 | public char to_char() { | ||
| 3147 | return (char)(this.to_int()); | ||
| 3148 | } | ||
| 3149 | public PlObject pos() { | ||
| 3150 | return PlCx.UNDEF; | ||
| 3151 | } | ||
| 3152 | public PlObject set_pos(PlObject value) { | ||
| 3153 | return PlCORE.die("Can't modify constant item in match position"); | ||
| 3154 | } | ||
| 3155 | |||
| 3156 | public PlObject end_of_array_index() { | ||
| 3157 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3158 | } | ||
| 3159 | public PlObject set_end_of_array_index(PlObject o) { | ||
| 3160 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3161 | } | ||
| 3162 | public PlObject apply(int want, PlArray List__) { | ||
| 3163 | // $ perl -e ' $a = 5; $a->() ' | ||
| 3164 | // Undefined subroutine &main::5 called | ||
| 3165 | String name = this.toString(); | ||
| 3166 | PlObject code = PlStringConstant.getConstant(name).codeRef; | ||
| 3167 | if ( code.is_coderef() ) { | ||
| 3168 | return code.apply(want, List__); | ||
| 3169 | } | ||
| 3170 | int pos = name.lastIndexOf("::"); | ||
| 3171 | if (pos != -1) { | ||
| 3172 | String namespace = name.substring(0, pos); | ||
| 3173 | PlLvalue autoload = PlV.cget_no_autoload(namespace + "::AUTOLOAD"); | ||
| 3174 | if ( autoload.is_coderef() ) { | ||
| 3175 | PlV.sset(namespace + "::AUTOLOAD", new PlString(name)); | ||
| 3176 | return autoload.apply(want, List__); | ||
| 3177 | } | ||
| 3178 | } | ||
| 3179 | return PlCORE.die("Undefined subroutine &" + name + " called"); | ||
| 3180 | } | ||
| 3181 | |||
| 3182 | public PlScalarImmutable length() { | ||
| 3183 | return new PlInt(this.toString().length()); | ||
| 3184 | } | ||
| 3185 | public PlObject get_arrayref() { | ||
| 3186 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3187 | } | ||
| 3188 | |||
| 3189 | public PlObject shift() { | ||
| 3190 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3191 | } | ||
| 3192 | |||
| 3193 | public PlObject get_hashref() { | ||
| 3194 | return PlCORE.die("Not a HASH reference"); | ||
| 3195 | } | ||
| 3196 | |||
| 3197 | public PlObject hget_scalarref(String i) { | ||
| 3198 | PlCORE.die("Not a SCALAR reference"); | ||
| 3199 | return this; | ||
| 3200 | } | ||
| 3201 | public PlObject scalar_deref_lvalue(String namespace) { | ||
| 3202 | return PlCORE.die("Not a SCALAR reference"); | ||
| 3203 | } | ||
| 3204 | public PlObject scalar_deref(String namespace) { | ||
| 3205 | return PlCORE.die("Not a SCALAR reference"); | ||
| 3206 | } | ||
| 3207 | public PlObject scalar_deref_strict() { | ||
| 3208 | return PlCORE.die("Not a SCALAR reference"); | ||
| 3209 | } | ||
| 3210 | public PlObject scalar_deref_set(String namespace, PlObject v) { | ||
| 3211 | return PlCORE.die("Not a SCALAR reference"); | ||
| 3212 | } | ||
| 3213 | public PlObject aget_list_of_aliases(int want, PlArray a) { | ||
| 3214 | if (this.is_array()) { | ||
| 3215 | return ((PlArray)this).aget_list_of_aliases(want, a); | ||
| 3216 | } | ||
| 3217 | return PlCORE.die("Not an ARRAY"); | ||
| 3218 | } | ||
| 3219 | public PlObject aget_lvalue(int i) { | ||
| 3220 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3221 | } | ||
| 3222 | public PlObject aget_scalarref(int i) { | ||
| 3223 | PlCORE.die("Not a SCALAR reference"); | ||
| 3224 | return this; | ||
| 3225 | } | ||
| 3226 | |||
| 3227 | public PlArray array_deref_lvalue() { | ||
| 3228 | PlCORE.die("Not an ARRAY reference"); | ||
| 3229 | return (PlArray)this; | ||
| 3230 | } | ||
| 3231 | public PlArray array_deref(String namespace) { | ||
| 3232 | return (PlArray)PlCORE.die("Not an ARRAY reference"); | ||
| 3233 | } | ||
| 3234 | public PlArray array_deref_strict() { | ||
| 3235 | return (PlArray)PlCORE.die("Not an ARRAY reference"); | ||
| 3236 | } | ||
| 3237 | public PlObject array_deref_set(PlObject i) { | ||
| 3238 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3239 | } | ||
| 3240 | |||
| 3241 | public PlObject hget_arrayref(String i) { | ||
| 3242 | return PlCORE.die("Not a HASH reference"); | ||
| 3243 | } | ||
| 3244 | public PlObject hget_hashref(String i) { | ||
| 3245 | PlCORE.die("Not a HASH reference"); | ||
| 3246 | return this; | ||
| 3247 | } | ||
| 3248 | |||
| 3249 | public PlObject aget_arrayref(int i) { | ||
| 3250 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3251 | } | ||
| 3252 | public PlObject aget_hashref(int i) { | ||
| 3253 | return PlCORE.die("Not an ARRAY reference"); | ||
| 3254 | } | ||
| 3255 | |||
| 3256 | public PlObject hash_deref(String namespace) { | ||
| 3257 | return PlCORE.die("Not a HASH reference"); | ||
| 3258 | } | ||
| 3259 | public PlHash hash_deref_strict() { | ||
| 3260 | return (PlHash)PlCORE.die("Not a HASH reference"); | ||
| 3261 | } | ||
| 3262 | public PlObject hash_deref_set(PlObject i) { | ||
| 3263 | return PlCORE.die("Not a HASH reference"); | ||
| 3264 | } | ||
| 3265 | |||
| 3266 | public PlObject hget(String i) { | ||
| 3267 | return PlCORE.die("Not a HASH reference"); | ||
| 3268 | } | ||
| 3269 | public PlObject hget_lvalue(String i) { | ||
| 3270 | return PlCORE.die("Not a HASH reference"); | ||
| 3271 | } | ||
| 3272 | public PlObject hget_lvalue_local(String i) { | ||
| 3273 | return PlCORE.die("Not a HASH reference"); | ||
| 3274 | } | ||
| 3275 | |||
| 3276 | public PlObject hset(String s, PlObject v) { | ||
| 3277 | PlCORE.die("Not a HASH reference"); | ||
| 3278 | return this; | ||
| 3279 | } | ||
| 3280 | public PlObject hset(String s, PlLvalue v) { | ||
| 3281 | PlCORE.die("Not a HASH reference"); | ||
| 3282 | return this; | ||
| 3283 | } | ||
| 3284 | |||
| 3285 | public PlObject aget(int i) { | ||
| 3286 | PlCORE.die("Not an ARRAY reference"); | ||
| 3287 | return this; | ||
| 3288 | } | ||
| 3289 | public PlObject aset(int i, PlObject v) { | ||
| 3290 | PlCORE.die("Not an ARRAY reference"); | ||
| 3291 | return this; | ||
| 3292 | } | ||
| 3293 | |||
| 3294 | public PlObject to_array() { | ||
| 3295 | PlCORE.die("Not an ARRAY reference"); | ||
| 3296 | return this; | ||
| 3297 | } | ||
| 3298 | public PlScalarImmutable length_of_array() { | ||
| 3299 | PlCORE.die("Not an ARRAY reference"); | ||
| 3300 | return PlCx.UNDEF; | ||
| 3301 | } | ||
| 3302 | public boolean length_of_array_boolean() { | ||
| 3303 | PlCORE.die("Not an ARRAY reference"); | ||
| 3304 | return false; | ||
| 3305 | } | ||
| 3306 | public int length_of_array_int() { | ||
| 3307 | return 1; | ||
| 3308 | } | ||
| 3309 | public PlObject values() { | ||
| 3310 | PlCORE.die("Type of argument to values on reference must be unblessed hashref or arrayref"); | ||
| 3311 | return this; | ||
| 3312 | } | ||
| 3313 | public PlObject keys() { | ||
| 3314 | PlCORE.die("Type of argument to keys on reference must be unblessed hashref or arrayref"); | ||
| 3315 | return this; | ||
| 3316 | } | ||
| 3317 | public PlObject each() { | ||
| 3318 | PlCORE.die("Type of argument to each on reference must be unblessed hashref or arrayref"); | ||
| 3319 | return this; | ||
| 3320 | } | ||
| 3321 | public PlObject aexists(PlObject i) { | ||
| 3322 | PlCORE.die("exists argument is not a HASH or ARRAY element or a subroutine"); | ||
| 3323 | return this; | ||
| 3324 | } | ||
| 3325 | public PlObject hexists(String i) { | ||
| 3326 | PlCORE.die("exists argument is not a HASH or ARRAY element or a subroutine"); | ||
| 3327 | return this; | ||
| 3328 | } | ||
| 3329 | public PlObject hexists(PlObject i) { | ||
| 3330 | return this.hexists(i.toString()); | ||
| 3331 | } | ||
| 3332 | public PlObject adelete(int want, PlObject i) { | ||
| 3333 | PlCORE.die("delete argument is not a HASH or ARRAY element or slice"); | ||
| 3334 | return this; | ||
| 3335 | } | ||
| 3336 | public PlObject hdelete(int want, PlObject i) { | ||
| 3337 | PlCORE.die("delete argument is not a HASH or ARRAY element or slice"); | ||
| 3338 | return this; | ||
| 3339 | } | ||
| 3340 | public PlObject set(PlObject o) { | ||
| 3341 | PlCORE.die("Modification of a read-only value attempted"); | ||
| 3342 | return this; | ||
| 3343 | } | ||
| 3344 | public PlObject vecSet(PlObject pOffset, PlObject pBits, PlObject pValue) { | ||
| 3345 | PlCORE.die("Can't modify constant item in scalar assignment"); | ||
| 3346 | return this; | ||
| 3347 | } | ||
| 3348 | public PlObject vec(PlObject pOffset, PlObject pBits) { | ||
| 3349 | // vec($i, 0, 32) | ||
| 3350 | String sb = this.toString(); | ||
| 3351 | int offset = pOffset.to_int(); | ||
| 3352 | if (offset < 0) { | ||
| 3353 | return PlCORE.die("Negative offset to vec in lvalue context: " + offset); | ||
| 3354 | } | ||
| 3355 | int bits = pBits.to_int(); | ||
| 3356 | if (bits == 1) { | ||
| 3357 | int byteOfs = offset / 8; | ||
| 3358 | int bitOfs = offset - 8 * byteOfs; | ||
| 3359 | long mask = 0b0001; | ||
| 3360 | if (byteOfs < sb.length()) { | ||
| 3361 | return new PlInt((sb.charAt(byteOfs) >> bitOfs) & mask); | ||
| 3362 | } | ||
| 3363 | else { | ||
| 3364 | return PlCx.INT0; | ||
| 3365 | } | ||
| 3366 | } | ||
| 3367 | if (bits == 2) { | ||
| 3368 | int byteOfs = offset / 4; | ||
| 3369 | int bitOfs = 2 * (offset - 4 * byteOfs); | ||
| 3370 | long mask = 0b0011; | ||
| 3371 | if (byteOfs < sb.length()) { | ||
| 3372 | return new PlInt((sb.charAt(byteOfs) >> bitOfs) & mask); | ||
| 3373 | } | ||
| 3374 | else { | ||
| 3375 | return PlCx.INT0; | ||
| 3376 | } | ||
| 3377 | } | ||
| 3378 | if (bits == 4) { | ||
| 3379 | int byteOfs = offset / 2; | ||
| 3380 | int bitOfs = 4 * (offset - 2 * byteOfs); | ||
| 3381 | long mask = 0b1111; | ||
| 3382 | if (byteOfs < sb.length()) { | ||
| 3383 | return new PlInt((sb.charAt(byteOfs) >> bitOfs) & mask); | ||
| 3384 | } | ||
| 3385 | else { | ||
| 3386 | return PlCx.INT0; | ||
| 3387 | } | ||
| 3388 | } | ||
| 3389 | if (bits == 8) { | ||
| 3390 | if (offset < sb.length()) { | ||
| 3391 | return new PlInt(sb.charAt(offset) & 0xFF); | ||
| 3392 | } | ||
| 3393 | else { | ||
| 3394 | return PlCx.INT0; | ||
| 3395 | } | ||
| 3396 | } | ||
| 3397 | if (bits == 16) { | ||
| 3398 | long b0 = offset < sb.length() ? (sb.charAt(offset) & 0xFF) << 8 : 0; | ||
| 3399 | offset++; | ||
| 3400 | long b1 = offset < sb.length() ? sb.charAt(offset) & 0xFF : 0; | ||
| 3401 | long res = b0 + b1; | ||
| 3402 | if (res < 0) { | ||
| 3403 | res = 4294967296L + res; | ||
| 3404 | } | ||
| 3405 | return new PlInt(res); | ||
| 3406 | } | ||
| 3407 | if (bits == 32) { | ||
| 3408 | long b0 = offset < sb.length() ? (sb.charAt(offset) & 0xFF) << 24 : 0; | ||
| 3409 | offset++; | ||
| 3410 | long b1 = offset < sb.length() ? (sb.charAt(offset) & 0xFF) << 16 : 0; | ||
| 3411 | offset++; | ||
| 3412 | long b2 = offset < sb.length() ? (sb.charAt(offset) & 0xFF) << 8 : 0; | ||
| 3413 | offset++; | ||
| 3414 | long b3 = offset < sb.length() ? sb.charAt(offset) & 0xFF : 0; | ||
| 3415 | long res = b0 + b1 + b2 + b3; | ||
| 3416 | if (res < 0) { | ||
| 3417 | res = 4294967296L + res; | ||
| 3418 | } | ||
| 3419 | return new PlInt(res); | ||
| 3420 | } | ||
| 3421 | return PlCORE.die("Illegal number of bits in vec: " + bits); | ||
| 3422 | } | ||
| 3423 | public PlScalarImmutable get() { | ||
| 3424 | return (PlScalarImmutable)this; | ||
| 3425 | } | ||
| 3426 | public PlObject mod(PlObject o) { | ||
| 3427 | return this.to_num().mod(o); | ||
| 3428 | } | ||
| 3429 | |||
| 3430 | EOT | ||
| 3431 | , ((map { | ||
| 3432 | my $op = $_; | ||
| 3433 | " public boolean $op() { | ||
| 3434 | return false; | ||
| 3435 | } | ||
| 3436 | " | ||
| 3437 | } | ||
| 3438 | sort @boolean_unary )) | ||
| 3439 | |||
| 3440 | , <<'EOT' | ||
| 3441 | |||
| 3442 | public boolean is_JavaObject() { | ||
| 3443 | return false; | ||
| 3444 | } | ||
| 3445 | public Object toJava() { | ||
| 3446 | return this; | ||
| 3447 | } | ||
| 3448 | |||
| 3449 | public boolean is_hash() { | ||
| 3450 | return false; | ||
| 3451 | } | ||
| 3452 | public boolean is_slice() { | ||
| 3453 | return false; | ||
| 3454 | } | ||
| 3455 | public boolean is_array() { | ||
| 3456 | return false; | ||
| 3457 | } | ||
| 3458 | public boolean is_lvalue() { | ||
| 3459 | return false; | ||
| 3460 | } | ||
| 3461 | public boolean is_regex_result() { | ||
| 3462 | return false; | ||
| 3463 | } | ||
| 3464 | public boolean is_integer_range() { | ||
| 3465 | return new PlDouble(this.to_double()).is_integer_range(); | ||
| 3466 | } | ||
| 3467 | public PlObject tie(PlArray args) { | ||
| 3468 | if (this.is_lvalue()) { | ||
| 3469 | return ((PlLvalue)this).tie(args); | ||
| 3470 | } | ||
| 3471 | return PlCORE.die("Can't modify constant item in tie"); | ||
| 3472 | } | ||
| 3473 | public PlString ref() { | ||
| 3474 | return REF; | ||
| 3475 | } | ||
| 3476 | public String ref_str() { | ||
| 3477 | return REF_str; | ||
| 3478 | } | ||
| 3479 | public boolean ref_boolean() { | ||
| 3480 | return false; | ||
| 3481 | } | ||
| 3482 | public PlObject refaddr() { | ||
| 3483 | // Scalar::Util::refaddr() | ||
| 3484 | return PlCx.UNDEF; | ||
| 3485 | } | ||
| 3486 | public PlObject reftype() { | ||
| 3487 | // Scalar::Util::reftype() | ||
| 3488 | return PlCx.UNDEF; | ||
| 3489 | } | ||
| 3490 | public PlObject refstring() { | ||
| 3491 | if (this.is_ref()) { | ||
| 3492 | StringBuilder sb = new StringBuilder(); | ||
| 3493 | PlClass bless = this.blessed_class(); | ||
| 3494 | if ( bless != null ) { | ||
| 3495 | sb.append(this.ref_str()); | ||
| 3496 | sb.append("="); | ||
| 3497 | } | ||
| 3498 | sb.append(this.reftype().toString()); | ||
| 3499 | sb.append("(0x"); | ||
| 3500 | sb.append(Integer.toHexString(this.refaddr().to_int())); | ||
| 3501 | sb.append(")"); | ||
| 3502 | return new PlString(sb.toString()); | ||
| 3503 | } | ||
| 3504 | return PlCx.EMPTY; | ||
| 3505 | } | ||
| 3506 | public PlObject blessed() { | ||
| 3507 | // Scalar::Util::blessed() | ||
| 3508 | return PlCx.UNDEF; | ||
| 3509 | } | ||
| 3510 | public PlObject tied() { | ||
| 3511 | return PlCx.UNDEF; | ||
| 3512 | } | ||
| 3513 | public PlObject _decr() { | ||
| 3514 | // --$x | ||
| 3515 | return PlCx.MIN1; | ||
| 3516 | } | ||
| 3517 | public PlObject _incr() { | ||
| 3518 | // ++$x | ||
| 3519 | return PlCx.INT1; | ||
| 3520 | } | ||
| 3521 | |||
| 3522 | public PlObject op_int() { | ||
| 3523 | return new PlInt(this.to_long()); | ||
| 3524 | } | ||
| 3525 | public PlObject neg() { | ||
| 3526 | return new PlInt(-this.to_long()); | ||
| 3527 | } | ||
| 3528 | public PlObject complement() { | ||
| 3529 | long v = this.to_long(); | ||
| 3530 | return new PlInt(v < 0 ? ~v : 4294967295L - v); | ||
| 3531 | } | ||
| 3532 | public PlObject str_complement() { | ||
| 3533 | return PlCORE.die("~. not implemented"); | ||
| 3534 | } | ||
| 3535 | public PlObject abs() { | ||
| 3536 | long c = this.to_long(); | ||
| 3537 | return new PlInt(c < 0 ? -c : c); | ||
| 3538 | } | ||
| 3539 | |||
| 3540 | public PlObject sqrt() { return new PlDouble(Math.sqrt(this.to_double())); } | ||
| 3541 | public PlObject cos() { return new PlDouble(Math.cos(this.to_double())); } | ||
| 3542 | public PlObject sin() { return new PlDouble(Math.sin(this.to_double())); } | ||
| 3543 | public PlObject exp() { return new PlDouble(Math.exp(this.to_double())); } | ||
| 3544 | public PlObject log() { return new PlDouble(Math.log(this.to_double())); } | ||
| 3545 | public PlObject pow(PlObject arg) { return new PlDouble(Math.pow(this.to_double(), arg.to_double())); } | ||
| 3546 | public PlObject atan2(PlObject arg) { return new PlDouble(Math.atan2(this.to_double(), arg.to_double())); } | ||
| 3547 | |||
| 3548 | public PlObject pre_decr() { | ||
| 3549 | // --$x | ||
| 3550 | PlCORE.die("Can't modify constant item in predecrement (--)"); | ||
| 3551 | return this; | ||
| 3552 | } | ||
| 3553 | public PlObject post_decr() { | ||
| 3554 | // $x-- | ||
| 3555 | PlCORE.die("Can't modify constant item in postdecrement (--)"); | ||
| 3556 | return this; | ||
| 3557 | } | ||
| 3558 | public PlObject pre_incr() { | ||
| 3559 | // ++$x | ||
| 3560 | PlCORE.die("Can't modify constant item in preincrement (++)"); | ||
| 3561 | return this; | ||
| 3562 | } | ||
| 3563 | public PlObject post_incr() { | ||
| 3564 | // $x++ | ||
| 3565 | PlCORE.die("Can't modify constant item in postincrement (++)"); | ||
| 3566 | return this; | ||
| 3567 | } | ||
| 3568 | |||
| 3569 | public PlObject lcfirst() { | ||
| 3570 | String s = this.toString(); | ||
| 3571 | int len = s.length(); | ||
| 3572 | if (len == 0) { | ||
| 3573 | return new PlString(s); | ||
| 3574 | } | ||
| 3575 | if (len == 1) { | ||
| 3576 | return new PlString(s.toLowerCase()); | ||
| 3577 | } | ||
| 3578 | return new PlString( s.substring(0,1).toLowerCase() + s.substring(1) ); | ||
| 3579 | } | ||
| 3580 | public PlObject ucfirst() { | ||
| 3581 | String s = this.toString(); | ||
| 3582 | int len = s.length(); | ||
| 3583 | if (len == 0) { | ||
| 3584 | return new PlString(s); | ||
| 3585 | } | ||
| 3586 | if (len == 1) { | ||
| 3587 | return new PlString(s.toUpperCase()); | ||
| 3588 | } | ||
| 3589 | return new PlString( s.substring(0,1).toUpperCase() + s.substring(1) ); | ||
| 3590 | } | ||
| 3591 | public PlObject quotemeta() { | ||
| 3592 | String s = this.toString(); | ||
| 3593 | final int length = s.length(); | ||
| 3594 | StringBuilder sb = new StringBuilder(); | ||
| 3595 | for (int offset = 0; offset < length; offset++) { | ||
| 3596 | final int c = s.codePointAt(offset); | ||
| 3597 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { | ||
| 3598 | // good | ||
| 3599 | } | ||
| 3600 | else { | ||
| 3601 | sb.append("\\"); | ||
| 3602 | } | ||
| 3603 | sb.append(Character.toChars(c)); | ||
| 3604 | } | ||
| 3605 | return new PlString(sb.toString()); | ||
| 3606 | } | ||
| 3607 | public PlInt index(PlObject substr) { | ||
| 3608 | String s = this.toString(); | ||
| 3609 | String s1 = substr.toString(); | ||
| 3610 | return new PlInt(s.indexOf(s1)); | ||
| 3611 | } | ||
| 3612 | public PlInt index(PlObject substr, PlObject position) { | ||
| 3613 | String s = this.toString(); | ||
| 3614 | String s1 = substr.toString(); | ||
| 3615 | int i = position.to_int(); | ||
| 3616 | if (i < 0) { | ||
| 3617 | i = 0; | ||
| 3618 | } | ||
| 3619 | return new PlInt(s.indexOf(s1, i)); | ||
| 3620 | } | ||
| 3621 | public PlInt rindex(PlObject substr) { | ||
| 3622 | String s = this.toString(); | ||
| 3623 | String s1 = substr.toString(); | ||
| 3624 | return new PlInt(s.lastIndexOf(s1)); | ||
| 3625 | } | ||
| 3626 | public PlInt rindex(PlObject substr, PlObject position) { | ||
| 3627 | String s = this.toString(); | ||
| 3628 | String s1 = substr.toString(); | ||
| 3629 | int i = position.to_int(); | ||
| 3630 | if (i < 0) { | ||
| 3631 | if (s1.length() == 0) { | ||
| 3632 | return PlCx.INT0; | ||
| 3633 | } | ||
| 3634 | return PlCx.MIN1; | ||
| 3635 | } | ||
| 3636 | return new PlInt(s.lastIndexOf(s1, i)); | ||
| 3637 | } | ||
| 3638 | public PlObject substr(int offset) { | ||
| 3639 | // substr EXPR,OFFSET | ||
| 3640 | String s = this.toString(); | ||
| 3641 | int ofs = offset; | ||
| 3642 | if (ofs < 0) { | ||
| 3643 | ofs = s.length() + ofs; | ||
| 3644 | } | ||
| 3645 | if (ofs < 0) { | ||
| 3646 | ofs = 0; | ||
| 3647 | } | ||
| 3648 | if (ofs >= s.length()) { | ||
| 3649 | return PlCx.UNDEF; | ||
| 3650 | } | ||
| 3651 | return new PlString(s.substring(ofs)); | ||
| 3652 | } | ||
| 3653 | public PlObject substr(int offset, int length) { | ||
| 3654 | // substr EXPR,OFFSET,LENGTH | ||
| 3655 | String s = this.toString(); | ||
| 3656 | int ofs = offset; | ||
| 3657 | int len = length; | ||
| 3658 | if (ofs < 0) { | ||
| 3659 | ofs = s.length() + ofs; | ||
| 3660 | } | ||
| 3661 | if (ofs >= s.length()) { | ||
| 3662 | return PlCx.UNDEF; | ||
| 3663 | } | ||
| 3664 | |||
| 3665 | if (len < 0) { | ||
| 3666 | len = s.length() + len; | ||
| 3667 | } | ||
| 3668 | else { | ||
| 3669 | len = ofs + len; | ||
| 3670 | } | ||
| 3671 | |||
| 3672 | if (len >= s.length()) { | ||
| 3673 | len = s.length(); | ||
| 3674 | } | ||
| 3675 | if (len <= 0) { | ||
| 3676 | return PlCx.UNDEF; | ||
| 3677 | } | ||
| 3678 | if (ofs < 0) { | ||
| 3679 | ofs = 0; | ||
| 3680 | } | ||
| 3681 | return new PlString(s.substring(ofs, len)); | ||
| 3682 | } | ||
| 3683 | public PlObject substr(int offset, int length, PlObject replacement) { | ||
| 3684 | // substr EXPR,OFFSET,LENGTH,REPLACEMENT | ||
| 3685 | String s = this.toString(); | ||
| 3686 | int ofs = offset; | ||
| 3687 | int len = length; | ||
| 3688 | if (ofs < 0) { | ||
| 3689 | ofs = s.length() + ofs; | ||
| 3690 | } | ||
| 3691 | if (ofs >= s.length()) { | ||
| 3692 | return PlCORE.die("substr outside of string"); | ||
| 3693 | } | ||
| 3694 | |||
| 3695 | if (len < 0) { | ||
| 3696 | len = s.length() + len; | ||
| 3697 | } | ||
| 3698 | else { | ||
| 3699 | len = ofs + len; | ||
| 3700 | } | ||
| 3701 | |||
| 3702 | if (len >= s.length()) { | ||
| 3703 | len = s.length(); | ||
| 3704 | } | ||
| 3705 | if (len <= 0) { | ||
| 3706 | return PlCx.UNDEF; | ||
| 3707 | } | ||
| 3708 | if (ofs < 0) { | ||
| 3709 | ofs = 0; | ||
| 3710 | } | ||
| 3711 | |||
| 3712 | PlObject ret = new PlString(s.substring(ofs, len)); | ||
| 3713 | String start = ""; | ||
| 3714 | String end = ""; | ||
| 3715 | |||
| 3716 | if (ofs > 0) { | ||
| 3717 | start = s.substring(0, ofs); | ||
| 3718 | } | ||
| 3719 | if (len < s.length()) { | ||
| 3720 | end = s.substring(len); | ||
| 3721 | } | ||
| 3722 | this.set( new PlString( start + replacement.toString() + end ) ); | ||
| 3723 | return ret; | ||
| 3724 | } | ||
| 3725 | public PlObject lvalue_substr(int offset) { | ||
| 3726 | return this.lvalue_substr(offset, this.toString().length()); | ||
| 3727 | } | ||
| 3728 | public PlObject lvalue_substr(int offset, int length) { | ||
| 3729 | if (this.is_lvalue()) { | ||
| 3730 | return new PlLvalueSubstring(this, offset, length); | ||
| 3731 | } | ||
| 3732 | return this.substr(offset, length); | ||
| 3733 | } | ||
| 3734 | public PlObject bless(String className) { | ||
| 3735 | PlCORE.die("Can't bless non-reference value"); | ||
| 3736 | return this; | ||
| 3737 | } | ||
| 3738 | public PlClass blessed_class() { | ||
| 3739 | return null; | ||
| 3740 | } | ||
| 3741 | public PlObject set_prototype(PlObject v) { | ||
| 3742 | return PlCORE.die("Type of arg 1 to Scalar::Util::set_prototype must be block or sub {}"); | ||
| 3743 | } | ||
| 3744 | public PlScalarImmutable scalar() { | ||
| 3745 | return (PlScalarImmutable)this; | ||
| 3746 | } | ||
| 3747 | public PlObject clone() throws CloneNotSupportedException { | ||
| 3748 | return this; | ||
| 3749 | } | ||
| 3750 | public PlObject string_replicate(PlObject c) { | ||
| 3751 | int count = c.to_int(); | ||
| 3752 | if ( count < 1 ) { | ||
| 3753 | return new PlString(""); | ||
| 3754 | } | ||
| 3755 | else { | ||
| 3756 | String raw_s = this.toString(); | ||
| 3757 | StringBuilder sb = new StringBuilder(); | ||
| 3758 | for (int i = 0; i < count; i++) { | ||
| 3759 | sb.append(raw_s); | ||
| 3760 | } | ||
| 3761 | return new PlString(sb.toString()); | ||
| 3762 | } | ||
| 3763 | } | ||
| 3764 | public PlObject str_cmp(PlObject b) { | ||
| 3765 | int c = this.toString().compareTo(b.toString()); | ||
| 3766 | return (c == 0 ? PlCx.INT0 : c < 0 ? PlCx.MIN1 : PlCx.INT1); | ||
| 3767 | } | ||
| 3768 | public PlObject num_cmp(PlObject b) { | ||
| 3769 | return b.num_cmp2(this); | ||
| 3770 | } | ||
| 3771 | public PlObject num_cmp2(PlObject b) { | ||
| 3772 | if (b.is_num()) { | ||
| 3773 | return ((PlDouble)b).num_cmp(this); | ||
| 3774 | } | ||
| 3775 | Long blong = b.to_long(); | ||
| 3776 | int c = blong.compareTo(this.to_long()); | ||
| 3777 | return (c == 0 ? PlCx.INT0 : c < 0 ? PlCx.MIN1 : PlCx.INT1); | ||
| 3778 | } | ||
| 3779 | public PlObject str_xor(PlObject b) { | ||
| 3780 | return PlCORE.die("^. not implemented"); | ||
| 3781 | } | ||
| 3782 | public PlObject str_and(PlObject b) { | ||
| 3783 | return PlCORE.die("&. not implemented"); | ||
| 3784 | } | ||
| 3785 | public PlObject str_or(PlObject b) { | ||
| 3786 | return PlCORE.die("|. not implemented"); | ||
| 3787 | } | ||
| 3788 | public PlObject smartmatch(PlObject arg2) { | ||
| 3789 | if (arg2.is_undef()) { | ||
| 3790 | return this.is_undef() ? PlCx.TRUE : PlCx.FALSE; | ||
| 3791 | } | ||
| 3792 | if (arg2.is_string()) { | ||
| 3793 | return this.str_eq(arg2); | ||
| 3794 | } | ||
| 3795 | if (arg2.is_num() || arg2.is_int()) { | ||
| 3796 | return this.num_eq(arg2); | ||
| 3797 | } | ||
| 3798 | if (arg2.is_ref()) { | ||
| 3799 | PlClass bless = arg2.blessed_class(); | ||
| 3800 | if ( bless != null ) { | ||
| 3801 | // TODO - right side is object: invoke ~~ overloading on Object, or die | ||
| 3802 | } | ||
| 3803 | } | ||
| 3804 | if (arg2.is_array()) { | ||
| 3805 | // TODO - right side is array | ||
| 3806 | PlArray ret = new PlArray(); | ||
| 3807 | for (PlObject aa : arg2) { | ||
| 3808 | PlObject rr = this.smartmatch(aa); | ||
| 3809 | if (rr.to_boolean()) { | ||
| 3810 | ret.push_void(rr); | ||
| 3811 | } | ||
| 3812 | } | ||
| 3813 | return ret; | ||
| 3814 | } | ||
| 3815 | if (arg2.is_hash()) { | ||
| 3816 | // TODO - right side is hash | ||
| 3817 | } | ||
| 3818 | if (arg2.is_coderef()) { | ||
| 3819 | // TODO - right side is code | ||
| 3820 | } | ||
| 3821 | if (arg2.is_regex()) { | ||
| 3822 | // TODO - right side is regex | ||
| 3823 | } | ||
| 3824 | return PlCORE.die(PlCx.VOID, new PlArray(new PlString("Not implemented: smartmatch operator with argument type '"), arg2.ref(), new PlString("'"))); | ||
| 3825 | } | ||
| 3826 | |||
| 3827 | EOT | ||
| 3828 | , ((map { | ||
| 3829 | my $perl = $_; | ||
| 3830 | my $native = $number_binop{$perl}{op}; | ||
| 3831 | my $returns = $number_binop{$perl}{returns}; | ||
| 3832 | " public PlObject ${perl}(PlObject s) { | ||
| 3833 | return s.${perl}2(this); | ||
| 3834 | } | ||
| 3835 | " | ||
| 3836 | . ( $returns eq 'PlDouble' ? | ||
| 3837 | " public PlObject ${perl}2(PlObject s) { | ||
| 3838 | return new ${returns}( s.to_double() ${native} this.to_double() ); | ||
| 3839 | } | ||
| 3840 | " | ||
| 3841 | : | ||
| 3842 | " public PlObject ${perl}2(PlObject s) { | ||
| 3843 | return new ${returns}( s.to_long() ${native} this.to_long() ); | ||
| 3844 | } | ||
| 3845 | " ) | ||
| 3846 | } | ||
| 3847 | sort keys %number_binop )) | ||
| 3848 | |||
| 3849 | , ((map { | ||
| 3850 | my $perl = $_; | ||
| 3851 | my $native = $string_binop{$perl}{op}; | ||
| 3852 | my $returns = $string_binop{$perl}{returns}; | ||
| 3853 | " public PlObject ${perl}(PlObject b) { | ||
| 3854 | return new ${returns}(this.toString().compareTo(b.toString()) ${native} 0); | ||
| 3855 | } | ||
| 3856 | " | ||
| 3857 | } | ||
| 3858 | sort keys %string_binop )) | ||
| 3859 | |||
| 3860 | , ((map { | ||
| 3861 | my $perl = $_; | ||
| 3862 | " public PlObject self_assign_${perl}(PlObject s) { | ||
| 3863 | return PlCORE.die(\"Can't modify constant item\"); | ||
| 3864 | } | ||
| 3865 | " | ||
| 3866 | } | ||
| 3867 | sort ( 'string_replicate', | ||
| 3868 | 'and', | ||
| 3869 | 'or', | ||
| 3870 | 'str_and', | ||
| 3871 | 'str_or', | ||
| 3872 | 'str_xor', | ||
| 3873 | keys %self_assign_number_binop, | ||
| 3874 | ), | ||
| 3875 | )) | ||
| 3876 | |||
| 3877 | , ((map { | ||
| 3878 | my $perl = $_; | ||
| 3879 | " public PlObject _self_${perl}(PlObject s) { | ||
| 3880 | return this.${perl}(s); | ||
| 3881 | } | ||
| 3882 | " | ||
| 3883 | } | ||
| 3884 | sort ( 'string_replicate', | ||
| 3885 | keys %self_assign_number_binop, | ||
| 3886 | ), | ||
| 3887 | )) | ||
| 3888 | |||
| 3889 | , <<'EOT' | ||
| 3890 | } | ||
| 3891 | class PlScalarObject extends PlObject { | ||
| 3892 | public PlScalarObject() { | ||
| 3893 | } | ||
| 3894 | } | ||
| 3895 | class PlScalarImmutable extends PlScalarObject { | ||
| 3896 | public PlScalarImmutable() { | ||
| 3897 | } | ||
| 3898 | |||
| 3899 | public PlScalarImmutable get() { | ||
| 3900 | return this; | ||
| 3901 | } | ||
| 3902 | public PlScalarImmutable scalar() { | ||
| 3903 | return this; | ||
| 3904 | } | ||
| 3905 | } | ||
| 3906 | class PlReference extends PlScalarImmutable { | ||
| 3907 | public static final String REF_str = new String("REF"); | ||
| 3908 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 3909 | public PlClass bless; | ||
| 3910 | |||
| 3911 | public boolean is_ref() { | ||
| 3912 | return true; | ||
| 3913 | } | ||
| 3914 | public PlReference bless(String className) { | ||
| 3915 | this.bless = PlClass.getInstance(className); | ||
| 3916 | return this; | ||
| 3917 | } | ||
| 3918 | public PlClass blessed_class() { | ||
| 3919 | return this.bless; | ||
| 3920 | } | ||
| 3921 | |||
| 3922 | public PlString ref() { | ||
| 3923 | if ( this.bless == null ) { | ||
| 3924 | return this.REF; | ||
| 3925 | } | ||
| 3926 | else { | ||
| 3927 | return this.bless.plClassName(); | ||
| 3928 | } | ||
| 3929 | } | ||
| 3930 | public String ref_str() { | ||
| 3931 | if ( this.bless == null ) { | ||
| 3932 | return this.REF_str; | ||
| 3933 | } | ||
| 3934 | else { | ||
| 3935 | return this.bless.className(); | ||
| 3936 | } | ||
| 3937 | } | ||
| 3938 | public boolean ref_boolean() { | ||
| 3939 | return true; | ||
| 3940 | } | ||
| 3941 | |||
| 3942 | public PlInt refaddr() { | ||
| 3943 | // Scalar::Util::refaddr() | ||
| 3944 | return new PlInt(this.hashCode()); | ||
| 3945 | } | ||
| 3946 | public PlObject blessed() { | ||
| 3947 | // Scalar::Util::blessed() | ||
| 3948 | if ( this.bless == null ) { | ||
| 3949 | return PlCx.UNDEF; | ||
| 3950 | } | ||
| 3951 | else { | ||
| 3952 | return this.bless.plClassName(); | ||
| 3953 | } | ||
| 3954 | } | ||
| 3955 | public PlObject reftype() { | ||
| 3956 | // Scalar::Util::reftype() | ||
| 3957 | return REF; | ||
| 3958 | } | ||
| 3959 | |||
| 3960 | // overload | ||
| 3961 | public String toString() { | ||
| 3962 | return PlClass.overload_to_string(this).toString(); | ||
| 3963 | } | ||
| 3964 | public boolean to_boolean() { | ||
| 3965 | return PlClass.overload_to_boolean(this).to_boolean(); | ||
| 3966 | } | ||
| 3967 | public double to_double() { | ||
| 3968 | return PlClass.overload_to_number(this).to_double(); | ||
| 3969 | } | ||
| 3970 | public PlObject to_num() { | ||
| 3971 | return PlClass.overload_to_number(this); | ||
| 3972 | } | ||
| 3973 | public int to_int() { | ||
| 3974 | return PlClass.overload_to_number(this).to_int(); | ||
| 3975 | } | ||
| 3976 | public long to_long() { | ||
| 3977 | return PlClass.overload_to_number(this).to_long(); | ||
| 3978 | } | ||
| 3979 | EOT | ||
| 3980 | , ((map { | ||
| 3981 | my $perl = $_; | ||
| 3982 | " public PlObject ${perl}2(PlObject s) { | ||
| 3983 | return PlClass.overload_${perl}(this, s, PlCx.INT1); | ||
| 3984 | } | ||
| 3985 | " | ||
| 3986 | } | ||
| 3987 | sort ( | ||
| 3988 | 'num_cmp', | ||
| 3989 | keys(%number_binop), | ||
| 3990 | ) | ||
| 3991 | )) | ||
| 3992 | |||
| 3993 | , ((map { | ||
| 3994 | my $op = $_; | ||
| 3995 | " public PlObject $op() { | ||
| 3996 | return PlClass.overload_$op(this); | ||
| 3997 | } | ||
| 3998 | " | ||
| 3999 | } | ||
| 4000 | sort ( | ||
| 4001 | '_decr', | ||
| 4002 | '_incr', | ||
| 4003 | @number_unary, | ||
| 4004 | ) | ||
| 4005 | )) | ||
| 4006 | |||
| 4007 | , ((map { | ||
| 4008 | my $perl = $_; | ||
| 4009 | " public PlObject ${perl}(PlObject s) { | ||
| 4010 | return PlClass.overload_$perl(this, s, PlCx.UNDEF); | ||
| 4011 | } | ||
| 4012 | " | ||
| 4013 | } | ||
| 4014 | sort ( | ||
| 4015 | 'str_cmp', | ||
| 4016 | 'pow', | ||
| 4017 | 'atan2', | ||
| 4018 | 'mod', | ||
| 4019 | 'num_cmp', | ||
| 4020 | 'string_replicate', | ||
| 4021 | 'smartmatch', | ||
| 4022 | keys(%string_binop), | ||
| 4023 | keys(%number_binop), | ||
| 4024 | ( map { "_self_$_" } keys %self_assign_number_binop ), | ||
| 4025 | ) | ||
| 4026 | )) | ||
| 4027 | |||
| 4028 | , <<'EOT' | ||
| 4029 | } | ||
| 4030 | class PlGlobRef extends PlReference { | ||
| 4031 | public static final String REF_str = new String("GLOB"); | ||
| 4032 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4033 | public PlFileHandle filehandle; | ||
| 4034 | |||
| 4035 | public PlGlobRef(PlFileHandle filehandle) { | ||
| 4036 | this.filehandle = filehandle; | ||
| 4037 | } | ||
| 4038 | public PlGlobRef(PlLvalue v) { | ||
| 4039 | PlObject o = v.get(); | ||
| 4040 | this.filehandle = (PlFileHandle)o; | ||
| 4041 | } | ||
| 4042 | public PlGlobRef(PlObject o) { | ||
| 4043 | this.filehandle = (PlFileHandle)o; | ||
| 4044 | } | ||
| 4045 | public boolean is_typeglobref() { | ||
| 4046 | return true; | ||
| 4047 | } | ||
| 4048 | public PlString ref() { | ||
| 4049 | if ( this.bless == null ) { | ||
| 4050 | return REF; | ||
| 4051 | } | ||
| 4052 | else { | ||
| 4053 | return this.bless.plClassName(); | ||
| 4054 | } | ||
| 4055 | } | ||
| 4056 | public String ref_str() { | ||
| 4057 | if ( this.bless == null ) { | ||
| 4058 | return REF_str; | ||
| 4059 | } | ||
| 4060 | else { | ||
| 4061 | return this.bless.className(); | ||
| 4062 | } | ||
| 4063 | } | ||
| 4064 | public boolean ref_boolean() { | ||
| 4065 | return true; | ||
| 4066 | } | ||
| 4067 | |||
| 4068 | public PlObject reftype() { | ||
| 4069 | return REF; | ||
| 4070 | } | ||
| 4071 | public PlObject hget(String i) { | ||
| 4072 | // *{ $name }{CODE}->() | ||
| 4073 | // PlCORE.say( "PlGlobRef.hget " + i + " " + this.filehandle.hget(i) ); | ||
| 4074 | return this.filehandle.hget(i); | ||
| 4075 | } | ||
| 4076 | public PlObject hset(String s, PlObject v) { | ||
| 4077 | return this.filehandle.hset(s, v); | ||
| 4078 | } | ||
| 4079 | public PlObject hset(String s, PlLvalue v) { | ||
| 4080 | return this.filehandle.hset(s, v.get()); | ||
| 4081 | } | ||
| 4082 | } | ||
| 4083 | |||
| 4084 | class PlStringReader extends Reader{ | ||
| 4085 | // read from string | ||
| 4086 | String s; | ||
| 4087 | int pos; | ||
| 4088 | int mark; | ||
| 4089 | |||
| 4090 | PlStringReader(PlObject o) { | ||
| 4091 | this.s = o.toString(); | ||
| 4092 | this.pos = 0; | ||
| 4093 | this.mark = 0; | ||
| 4094 | } | ||
| 4095 | public int read(char[] cbuf, int off, int len) { | ||
| 4096 | if (pos >= s.length()) { | ||
| 4097 | return -1; | ||
| 4098 | } | ||
| 4099 | len = Math.min(len, s.length() - pos); | ||
| 4100 | int count = 0; | ||
| 4101 | while (count < len) { | ||
| 4102 | cbuf[off++] = s.charAt(pos++); | ||
| 4103 | count++; | ||
| 4104 | } | ||
| 4105 | return count; | ||
| 4106 | } | ||
| 4107 | |||
| 4108 | public boolean markSupported() { | ||
| 4109 | return true; | ||
| 4110 | } | ||
| 4111 | public void mark(int readlimit) { | ||
| 4112 | mark = pos; | ||
| 4113 | return; | ||
| 4114 | } | ||
| 4115 | public void reset() { | ||
| 4116 | pos = mark; | ||
| 4117 | } | ||
| 4118 | public void close() { | ||
| 4119 | return; | ||
| 4120 | } | ||
| 4121 | } | ||
| 4122 | |||
| 4123 | class PlFileHandle extends PlScalarImmutable { | ||
| 4124 | // public static final String REF_str = new String("GLOB"); | ||
| 4125 | // public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4126 | public String typeglob_name; | ||
| 4127 | public OutputStream outputStream; // System.out, System.err | ||
| 4128 | public InputStream inputStream; // System.in | ||
| 4129 | public Iterator<Path> directoryIterator; | ||
| 4130 | public DirectoryStream<Path> directoryStream; | ||
| 4131 | public Reader reader; // Console.reader | ||
| 4132 | public StringBuilder readlineBuffer; | ||
| 4133 | public StringBuilder printBuffer; | ||
| 4134 | public boolean eof; | ||
| 4135 | public boolean is_argv; | ||
| 4136 | public Path path; // filename, can be null | ||
| 4137 | public String mode; // ">", "+<" | ||
| 4138 | public String charset; // "UTF-8" | ||
| 4139 | public boolean binmode; | ||
| 4140 | public boolean output_autoflush; | ||
| 4141 | public PlObject tied; | ||
| 4142 | |||
| 4143 | // "format" variables | ||
| 4144 | public PlLvalue format_formfeed; // $^L | ||
| 4145 | public PlLvalue format_page_number; // $% | ||
| 4146 | public PlLvalue format_lines_left; // $- | ||
| 4147 | public PlLvalue format_line_break_characters; // $: | ||
| 4148 | public PlLvalue format_lines_per_page; // $= | ||
| 4149 | public PlLvalue format_top_name; // $^ | ||
| 4150 | public PlLvalue format_name; // $~ | ||
| 4151 | |||
| 4152 | public static final int BUFFER_SIZE = 4096; | ||
| 4153 | public static final int BUFFER_THRESHOLD = BUFFER_SIZE - 256; | ||
| 4154 | public static HashSet<PlFileHandle> allOpenFiles = new HashSet<PlFileHandle>(); | ||
| 4155 | |||
| 4156 | public PlFileHandle() { | ||
| 4157 | this.readlineBuffer = new StringBuilder(); | ||
| 4158 | this.printBuffer = new StringBuilder(PlFileHandle.BUFFER_SIZE); | ||
| 4159 | this.eof = true; | ||
| 4160 | this.is_argv = false; | ||
| 4161 | this.binmode = false; | ||
| 4162 | this.output_autoflush = false; | ||
| 4163 | |||
| 4164 | // "format" variables | ||
| 4165 | this.format_formfeed = new PlLvalue( new PlString("" + (char)12) ); // $^L | ||
| 4166 | this.format_page_number = new PlLvalue( new PlInt(0) ); // $% | ||
| 4167 | this.format_lines_left = new PlLvalue( new PlInt(0) ); // $- | ||
| 4168 | this.format_line_break_characters = new PlLvalue( new PlString(" " + (char)10 + "-") ); // $: | ||
| 4169 | this.format_lines_per_page = new PlLvalue( new PlInt(60) ); // $= | ||
| 4170 | this.format_top_name = new PlLvalue(); // $^ | ||
| 4171 | this.format_name = new PlLvalue(); // $~ | ||
| 4172 | } | ||
| 4173 | |||
| 4174 | public PlFileHandle(String name) { | ||
| 4175 | this(); // call the base constructor | ||
| 4176 | if (name.equals("main::ARGV")) { | ||
| 4177 | this.is_argv = true; | ||
| 4178 | } | ||
| 4179 | this.typeglob_name = name; | ||
| 4180 | this.format_top_name.set(this.typeglob_name + "_TOP"); | ||
| 4181 | this.format_name.set(this.typeglob_name); | ||
| 4182 | } | ||
| 4183 | |||
| 4184 | public void dupFileHandle(PlFileHandle o) { | ||
| 4185 | this.typeglob_name = o.typeglob_name; | ||
| 4186 | this.outputStream = o.outputStream; // System.out, System.err | ||
| 4187 | this.inputStream = o.inputStream; // System.in | ||
| 4188 | this.directoryIterator = o.directoryIterator; | ||
| 4189 | this.directoryStream = o.directoryStream; | ||
| 4190 | this.reader = o.reader; // Console.reader | ||
| 4191 | this.readlineBuffer = o.readlineBuffer; | ||
| 4192 | this.printBuffer = o.printBuffer; | ||
| 4193 | this.eof = o.eof; | ||
| 4194 | this.is_argv = o.is_argv; | ||
| 4195 | this.path = o.path; // filename | ||
| 4196 | this.mode = o.mode; // ">", "+<" | ||
| 4197 | this.charset = o.charset; // "UTF-8" | ||
| 4198 | this.binmode = o.binmode; | ||
| 4199 | this.output_autoflush = o.output_autoflush; | ||
| 4200 | this.tied = o.tied; | ||
| 4201 | |||
| 4202 | // "format" variables | ||
| 4203 | this.format_formfeed.set(o.format_formfeed); | ||
| 4204 | this.format_page_number.set(o.format_page_number); | ||
| 4205 | this.format_lines_left.set(o.format_lines_left); | ||
| 4206 | this.format_line_break_characters.set(o.format_line_break_characters); | ||
| 4207 | this.format_lines_per_page.set(o.format_lines_per_page); | ||
| 4208 | this.format_top_name.set(o.format_top_name); | ||
| 4209 | this.format_name.set(o.format_name); | ||
| 4210 | } | ||
| 4211 | |||
| 4212 | public boolean is_filehandle() { | ||
| 4213 | return true; | ||
| 4214 | } | ||
| 4215 | |||
| 4216 | // tie "handle" | ||
| 4217 | public PlObject tie(PlArray args) { | ||
| 4218 | if (this.tied != null) { | ||
| 4219 | this.untie(); | ||
| 4220 | } | ||
| 4221 | PlObject self = PerlOp.call("TIEHANDLE", args, PlCx.VOID); | ||
| 4222 | this.tied = self; | ||
| 4223 | return self; | ||
| 4224 | } | ||
| 4225 | |||
| 4226 | public PlObject untie() { | ||
| 4227 | if (this.tied != null) { | ||
| 4228 | PlObject tied = this.tied; | ||
| 4229 | PlObject untie = PerlOp.call("can", new PlArray(tied, new PlString("UNTIE")), PlCx.SCALAR); | ||
| 4230 | if (untie.to_boolean()) { | ||
| 4231 | untie.apply(PlCx.VOID, new PlArray(tied)); | ||
| 4232 | }; | ||
| 4233 | this.tied = null; | ||
| 4234 | return tied; | ||
| 4235 | } | ||
| 4236 | return this; | ||
| 4237 | } | ||
| 4238 | public PlObject tied() { | ||
| 4239 | if (this.tied != null) { | ||
| 4240 | return this.tied; | ||
| 4241 | } | ||
| 4242 | return PlCx.UNDEF; | ||
| 4243 | } | ||
| 4244 | |||
| 4245 | public static void close_all_files() { | ||
| 4246 | // called at teardown - program finish, die() or exit() | ||
| 4247 | // Note: this is also called when the compiler finishes loading (the compiler is a Perl program) | ||
| 4248 | // System.out.println("PlFileHandle.close_all_files"); | ||
| 4249 | for ( PlFileHandle fh : allOpenFiles ) { | ||
| 4250 | try { | ||
| 4251 | fh.flush(); // not sure if we should close STDOUT and STDERR | ||
| 4252 | } | ||
| 4253 | catch(Exception e) { | ||
| 4254 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 4255 | } | ||
| 4256 | } | ||
| 4257 | } | ||
| 4258 | public PlObject set_autoflush(PlObject o) { | ||
| 4259 | this.output_autoflush = o.to_boolean(); | ||
| 4260 | if (this.output_autoflush) { | ||
| 4261 | try { | ||
| 4262 | this.flush(); | ||
| 4263 | } | ||
| 4264 | catch(Exception e) { | ||
| 4265 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 4266 | } | ||
| 4267 | } | ||
| 4268 | return o; | ||
| 4269 | } | ||
| 4270 | public void flush() throws IOException, UnsupportedEncodingException { | ||
| 4271 | // System.out.println("PlFileHandle.flush " + typeglob_name); | ||
| 4272 | if (this.outputStream == null) { | ||
| 4273 | return; | ||
| 4274 | } | ||
| 4275 | if (this.binmode) { | ||
| 4276 | StringBuilder s = this.printBuffer; | ||
| 4277 | int ll = s.length(); | ||
| 4278 | byte[] bytes = new byte[ll]; | ||
| 4279 | for (int i2 = 0; i2 < ll; i2++) { | ||
| 4280 | bytes[i2] = (byte)(s.charAt(i2)); | ||
| 4281 | } | ||
| 4282 | this.outputStream.write(bytes); | ||
| 4283 | } | ||
| 4284 | else { | ||
| 4285 | String s = this.printBuffer.toString(); | ||
| 4286 | byte[] bytes = s.getBytes(this.charset); | ||
| 4287 | this.outputStream.write(bytes); | ||
| 4288 | } | ||
| 4289 | // this.printBuffer = new StringBuilder(PlFileHandle.BUFFER_SIZE); | ||
| 4290 | // this.outputStream.flush(); // no need to flush when using write() | ||
| 4291 | this.printBuffer.setLength(0); | ||
| 4292 | } | ||
| 4293 | public void close() throws IOException { | ||
| 4294 | this.readlineBuffer = new StringBuilder(); | ||
| 4295 | this.eof = true; | ||
| 4296 | if (this.outputStream != null) { | ||
| 4297 | this.flush(); | ||
| 4298 | this.outputStream.close(); | ||
| 4299 | } | ||
| 4300 | if (this.reader != null) { | ||
| 4301 | this.reader.close(); | ||
| 4302 | } | ||
| 4303 | PlFileHandle.allOpenFiles.remove(this); | ||
| 4304 | } | ||
| 4305 | public String toString() { | ||
| 4306 | if (this.typeglob_name == null) { | ||
| 4307 | return ""; | ||
| 4308 | } | ||
| 4309 | if (this.typeglob_name.startsWith("main::")) { | ||
| 4310 | return "*" + this.typeglob_name.substring(4); | ||
| 4311 | } | ||
| 4312 | return "*" + this.typeglob_name; | ||
| 4313 | } | ||
| 4314 | public PlObject setUndef() { | ||
| 4315 | // undef *{$foo} | ||
| 4316 | // PlCORE.say( "PlFileHandle.setUndef " + typeglob_name); | ||
| 4317 | |||
| 4318 | PlString name = new PlString(typeglob_name); | ||
| 4319 | |||
| 4320 | try { | ||
| 4321 | // close filehandle if needed | ||
| 4322 | this.close(); | ||
| 4323 | } | ||
| 4324 | catch(Exception e) { | ||
| 4325 | PlV.sset("main::!", new PlStringLazyError(e)); | ||
| 4326 | } | ||
| 4327 | |||
| 4328 | PlStringConstant glob = PlStringConstant.getConstant(typeglob_name); | ||
| 4329 | glob.cset(PlCx.UNDEF); | ||
| 4330 | glob.scalarRef.set(PlCx.UNDEF); | ||
| 4331 | glob.arrayRef.set(new PlArrayRef()); | ||
| 4332 | glob.hashRef.set(new PlHashRef()); | ||
| 4333 | glob.fileRef.set(new PlFileHandle(typeglob_name)); | ||
| 4334 | if (typeglob_name.endsWith("::")) { | ||
| 4335 | // TODO - undefine inner symbol table | ||
| 4336 | } | ||
| 4337 | return PlCx.UNDEF; | ||
| 4338 | } | ||
| 4339 | public PlObject hget(String i) { | ||
| 4340 | // *{ $name }{CODE}->() | ||
| 4341 | // PlCORE.say( "PlFileHandle.hget " + i ); | ||
| 4342 | |||
| 4343 | if (i.equals("SCALAR")) { | ||
| 4344 | return PlV.make_reference(PlV.sget(typeglob_name)); | ||
| 4345 | } | ||
| 4346 | else if (i.equals("ARRAY")) { | ||
| 4347 | return PlV.aget(typeglob_name); | ||
| 4348 | } | ||
| 4349 | else if (i.equals("HASH")) { | ||
| 4350 | if (typeglob_name.endsWith("::")) { | ||
| 4351 | // %{"Module::"} | ||
| 4352 | return new PlHashRef(PerlOp.getSymbolTable(typeglob_name)); | ||
| 4353 | } | ||
| 4354 | return PlV.hget(typeglob_name); | ||
| 4355 | } | ||
| 4356 | else if (i.equals("CODE")) { | ||
| 4357 | return PlV.cget(typeglob_name); | ||
| 4358 | } | ||
| 4359 | else if (i.equals("IO")) { | ||
| 4360 | return this; // close enough | ||
| 4361 | } | ||
| 4362 | else if (i.equals("GLOB")) { | ||
| 4363 | return new PlGlobRef(this); | ||
| 4364 | } | ||
| 4365 | else if (i.equals("FORMAT")) { | ||
| 4366 | // TODO | ||
| 4367 | } | ||
| 4368 | else if (i.equals("NAME")) { | ||
| 4369 | int pos = typeglob_name.lastIndexOf("::"); | ||
| 4370 | if (pos != -1) { | ||
| 4371 | String name = typeglob_name.substring(pos+2); | ||
| 4372 | return new PlString(name); | ||
| 4373 | } | ||
| 4374 | } | ||
| 4375 | else if (i.equals("PACKAGE")) { | ||
| 4376 | int pos = typeglob_name.lastIndexOf("::"); | ||
| 4377 | if (pos != -1) { | ||
| 4378 | String namespace = typeglob_name.substring(0, pos); | ||
| 4379 | return new PlString(namespace); | ||
| 4380 | } | ||
| 4381 | } | ||
| 4382 | return PlCx.UNDEF; | ||
| 4383 | } | ||
| 4384 | public PlObject hset(String s, PlObject v) { | ||
| 4385 | return PlCORE.die("Can't modify glob elem in scalar assignment"); | ||
| 4386 | } | ||
| 4387 | public PlObject hset(String s, PlLvalue v) { | ||
| 4388 | return PlCORE.die("Can't modify glob elem in scalar assignment"); | ||
| 4389 | } | ||
| 4390 | public PlObject hget_scalarref(String i) { | ||
| 4391 | return this.hget(i); | ||
| 4392 | } | ||
| 4393 | public PlObject scalar_deref_set(String namespace, PlObject v) { | ||
| 4394 | return PlV.sset(typeglob_name, v); | ||
| 4395 | } | ||
| 4396 | } | ||
| 4397 | |||
| 4398 | EOT | ||
| 4399 | , # list break | ||
| 4400 | <<'EOT' | ||
| 4401 | |||
| 4402 | class PlLvalueSpecialVarAutoflush extends PlLvalue { | ||
| 4403 | // the $| variable | ||
| 4404 | public PlObject set(PlObject o) { | ||
| 4405 | // System.out.println("Set autoflush " + (o.to_boolean() ? "1" : "0")); | ||
| 4406 | PlV.selectedFileHandle.set_autoflush(o); | ||
| 4407 | return super.set(o); | ||
| 4408 | } | ||
| 4409 | public PlObject set(PlScalarImmutable o) { | ||
| 4410 | return this.set((PlObject)o); | ||
| 4411 | } | ||
| 4412 | public PlObject set(PlLvalue o) { | ||
| 4413 | return this.set((PlObject)o.get()); | ||
| 4414 | } | ||
| 4415 | } | ||
| 4416 | |||
| 4417 | EOT | ||
| 4418 | , # list break | ||
| 4419 | <<'EOT' | ||
| 4420 | |||
| 4421 | class PlRegex extends PlReference { | ||
| 4422 | public Pattern p; | ||
| 4423 | public String original_string; | ||
| 4424 | // public Matcher m; | ||
| 4425 | public boolean flag_xx; | ||
| 4426 | public static final String REF_str = new String("Regexp"); | ||
| 4427 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4428 | |||
| 4429 | public PlRegex(String p, int flags, boolean flag_xx) { | ||
| 4430 | this.flag_xx = flag_xx; | ||
| 4431 | this.p = Pattern.compile(PerlOp.regex_escape(p, flag_xx), flags); | ||
| 4432 | } | ||
| 4433 | public PlRegex(PlObject p, int flags, boolean flag_xx) { | ||
| 4434 | if (p.is_lvalue()) { | ||
| 4435 | p = p.get(); | ||
| 4436 | } | ||
| 4437 | if (p.is_regex()) { | ||
| 4438 | this.p = ((PlRegex)p).p; // reuse compiled regex; ignore any difference in flags | ||
| 4439 | } | ||
| 4440 | else { | ||
| 4441 | this.flag_xx = flag_xx; | ||
| 4442 | this.p = Pattern.compile(PerlOp.regex_escape(p.toString(), flag_xx), flags); | ||
| 4443 | } | ||
| 4444 | } | ||
| 4445 | public PlString ref() { | ||
| 4446 | if ( this.bless == null ) { | ||
| 4447 | return REF; | ||
| 4448 | } | ||
| 4449 | else { | ||
| 4450 | return this.bless.plClassName(); | ||
| 4451 | } | ||
| 4452 | } | ||
| 4453 | public String ref_str() { | ||
| 4454 | if ( this.bless == null ) { | ||
| 4455 | return REF_str; | ||
| 4456 | } | ||
| 4457 | else { | ||
| 4458 | return this.bless.className(); | ||
| 4459 | } | ||
| 4460 | } | ||
| 4461 | public boolean ref_boolean() { | ||
| 4462 | return true; | ||
| 4463 | } | ||
| 4464 | |||
| 4465 | public PlObject reftype() { | ||
| 4466 | return REF; | ||
| 4467 | } | ||
| 4468 | public String toString() { | ||
| 4469 | if (original_string == null) { | ||
| 4470 | |||
| 4471 | int flags = p.flags(); | ||
| 4472 | StringBuilder sb = new StringBuilder(); | ||
| 4473 | sb.append("(?"); | ||
| 4474 | |||
| 4475 | if ((flags & Pattern.CASE_INSENSITIVE) != 0) | ||
| 4476 | sb.append("i"); | ||
| 4477 | if ((flags & Pattern.COMMENTS) != 0) | ||
| 4478 | sb.append("x"); | ||
| 4479 | if (flag_xx) | ||
| 4480 | sb.append("x"); | ||
| 4481 | if ((flags & Pattern.DOTALL) != 0) | ||
| 4482 | sb.append("s"); | ||
| 4483 | if ((flags & Pattern.MULTILINE) != 0) | ||
| 4484 | sb.append("m"); | ||
| 4485 | |||
| 4486 | sb.append("-"); | ||
| 4487 | |||
| 4488 | if ((flags & Pattern.CASE_INSENSITIVE) == 0) | ||
| 4489 | sb.append("i"); | ||
| 4490 | if ((flags & Pattern.COMMENTS) == 0) | ||
| 4491 | sb.append("x"); | ||
| 4492 | if ((flags & Pattern.DOTALL) == 0) | ||
| 4493 | sb.append("s"); | ||
| 4494 | if ((flags & Pattern.MULTILINE) == 0) | ||
| 4495 | sb.append("m"); | ||
| 4496 | |||
| 4497 | sb.append(":"); | ||
| 4498 | sb.append(p.toString()); | ||
| 4499 | sb.append(")"); | ||
| 4500 | original_string = sb.toString(); | ||
| 4501 | |||
| 4502 | // TODO - show flags | ||
| 4503 | // Pattern.CANON_EQ | ||
| 4504 | // Pattern.LITERAL | ||
| 4505 | // Pattern.UNICODE_CASE | ||
| 4506 | // Pattern.UNICODE_CHARACTER_CLASS | ||
| 4507 | // Pattern.UNIX_LINES | ||
| 4508 | } | ||
| 4509 | return this.original_string; | ||
| 4510 | } | ||
| 4511 | public boolean is_regex() { | ||
| 4512 | return true; | ||
| 4513 | } | ||
| 4514 | } | ||
| 4515 | class PlRegexResult extends PlScalarImmutable { | ||
| 4516 | public Matcher matcher; // regex captures | ||
| 4517 | public String regex_string; // last string used in a regex | ||
| 4518 | |||
| 4519 | public boolean is_regex_result() { | ||
| 4520 | return true; | ||
| 4521 | } | ||
| 4522 | } | ||
| 4523 | class PlClosure extends PlReference implements Runnable { | ||
| 4524 | public PlObject[] env; // new PlObject[]{ v1, v2, v3 } | ||
| 4525 | public PlObject prototype; // '$$$' | ||
| 4526 | public String pkg_name; // 'main' | ||
| 4527 | public static final String REF_str = new String("CODE"); | ||
| 4528 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4529 | public PlClosure currentSub; | ||
| 4530 | public boolean is_defined; | ||
| 4531 | // metadata for caller() | ||
| 4532 | public String _javaClassName; | ||
| 4533 | public Integer _firstLineNumber; | ||
| 4534 | public Integer _lastLineNumber; | ||
| 4535 | |||
| 4536 | public PlClosure(PlObject prototype, PlObject[] env, String pkg_name, boolean is_defined) { | ||
| 4537 | this.prototype = prototype; | ||
| 4538 | this.env = env; | ||
| 4539 | this.pkg_name = pkg_name; | ||
| 4540 | this.currentSub = this; | ||
| 4541 | this.is_defined = is_defined; | ||
| 4542 | } | ||
| 4543 | public PlClosure(PlObject prototype, PlObject[] env, String pkg_name, boolean is_defined, PlClosure currentSub) { | ||
| 4544 | // this is the constructor for do-BLOCK; currentSub points to the "sub" outside | ||
| 4545 | this.prototype = prototype; | ||
| 4546 | this.env = env; | ||
| 4547 | this.pkg_name = pkg_name; | ||
| 4548 | this.currentSub = currentSub; | ||
| 4549 | this.is_defined = is_defined; | ||
| 4550 | } | ||
| 4551 | |||
| 4552 | public String javaClassName() { | ||
| 4553 | if (_javaClassName == null) { | ||
| 4554 | StackTraceElement firstStack = this.firstLine(); | ||
| 4555 | if ( firstStack != null ) { | ||
| 4556 | _javaClassName = firstStack.getClassName(); | ||
| 4557 | } | ||
| 4558 | } | ||
| 4559 | return _javaClassName; | ||
| 4560 | } | ||
| 4561 | |||
| 4562 | public Integer firstLineNumber() { | ||
| 4563 | if (_firstLineNumber == null) { | ||
| 4564 | StackTraceElement firstStack = this.firstLine(); | ||
| 4565 | if ( firstStack != null ) { | ||
| 4566 | _firstLineNumber = firstStack.getLineNumber(); | ||
| 4567 | } | ||
| 4568 | } | ||
| 4569 | return _firstLineNumber; | ||
| 4570 | } | ||
| 4571 | |||
| 4572 | public Integer lastLineNumber() { | ||
| 4573 | if (_lastLineNumber == null) { | ||
| 4574 | StackTraceElement lastStack = this.lastLine(); | ||
| 4575 | if ( lastStack != null ) { | ||
| 4576 | _lastLineNumber = lastStack.getLineNumber(); | ||
| 4577 | } | ||
| 4578 | } | ||
| 4579 | return _lastLineNumber; | ||
| 4580 | } | ||
| 4581 | |||
| 4582 | // subclasses override perlFileName() and perlLineNumber() | ||
| 4583 | public String perlFileName() { | ||
| 4584 | return null; | ||
| 4585 | } | ||
| 4586 | public Integer perlLineNumber() { | ||
| 4587 | return null; | ||
| 4588 | } | ||
| 4589 | |||
| 4590 | // subclasses override firstLine() and lastLine() | ||
| 4591 | // these methods are used by caller() to identify where the sub is implemented in the source code | ||
| 4592 | public StackTraceElement firstLine() { | ||
| 4593 | return null; | ||
| 4594 | } | ||
| 4595 | // Note: apply() overrides PlObject.apply(), which throws an error | ||
| 4596 | public PlObject apply(int want, PlArray List__) { | ||
| 4597 | PlCORE.die("it looks like you have a closure without a block"); | ||
| 4598 | return this; | ||
| 4599 | } | ||
| 4600 | public PlObject apply_do_block(int want, int return_context, PlArray List__) { | ||
| 4601 | PlCORE.die("it looks like you have a do-block without a block"); | ||
| 4602 | return this; | ||
| 4603 | } | ||
| 4604 | public StackTraceElement lastLine() { | ||
| 4605 | return null; | ||
| 4606 | } | ||
| 4607 | public void run() { | ||
| 4608 | // run as a thread | ||
| 4609 | this.apply(PlCx.VOID, new PlArray()); | ||
| 4610 | } | ||
| 4611 | |||
| 4612 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 4613 | // want java.lang.Runnable | ||
| 4614 | for (Class[] cl : params) { | ||
| 4615 | if (cl[pos].equals( java.lang.Runnable.class )) { | ||
| 4616 | return new PerlArgumentLookupResult( this, cl[pos] ); | ||
| 4617 | } | ||
| 4618 | } | ||
| 4619 | // try other things | ||
| 4620 | return super.castToClass(params, pos); | ||
| 4621 | } | ||
| 4622 | |||
| 4623 | public PlString ref() { | ||
| 4624 | if ( this.bless == null ) { | ||
| 4625 | return REF; | ||
| 4626 | } | ||
| 4627 | else { | ||
| 4628 | return this.bless.plClassName(); | ||
| 4629 | } | ||
| 4630 | } | ||
| 4631 | public String ref_str() { | ||
| 4632 | if ( this.bless == null ) { | ||
| 4633 | return REF_str; | ||
| 4634 | } | ||
| 4635 | else { | ||
| 4636 | return this.bless.className(); | ||
| 4637 | } | ||
| 4638 | } | ||
| 4639 | public boolean ref_boolean() { | ||
| 4640 | return true; | ||
| 4641 | } | ||
| 4642 | |||
| 4643 | public PlObject reftype() { | ||
| 4644 | return REF; | ||
| 4645 | } | ||
| 4646 | public boolean is_coderef() { | ||
| 4647 | return true; | ||
| 4648 | } | ||
| 4649 | public boolean is_undef() { | ||
| 4650 | return !is_defined; | ||
| 4651 | } | ||
| 4652 | public PlObject prototype() { | ||
| 4653 | return this.prototype; | ||
| 4654 | } | ||
| 4655 | public PlObject set_prototype(PlObject v) { | ||
| 4656 | return this.prototype = new PlString(v.toString()); | ||
| 4657 | } | ||
| 4658 | } | ||
| 4659 | class PlLvalueRef extends PlReference { | ||
| 4660 | private PlObject o; | ||
| 4661 | public static final String REF_str = new String("SCALAR"); | ||
| 4662 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4663 | public static final PlString REF_REF = new PlString("REF"); | ||
| 4664 | |||
| 4665 | public PlString ref() { | ||
| 4666 | if ( this.bless == null ) { | ||
| 4667 | if ( this.o.is_ref() ) { | ||
| 4668 | return REF_REF; | ||
| 4669 | } | ||
| 4670 | return REF; | ||
| 4671 | } | ||
| 4672 | else { | ||
| 4673 | return this.bless.plClassName(); | ||
| 4674 | } | ||
| 4675 | } | ||
| 4676 | public String ref_str() { | ||
| 4677 | if ( this.bless == null ) { | ||
| 4678 | if ( this.o.is_ref() ) { | ||
| 4679 | return REF_REF.toString(); | ||
| 4680 | } | ||
| 4681 | return REF_str; | ||
| 4682 | } | ||
| 4683 | else { | ||
| 4684 | return this.bless.className(); | ||
| 4685 | } | ||
| 4686 | } | ||
| 4687 | public boolean ref_boolean() { | ||
| 4688 | return true; | ||
| 4689 | } | ||
| 4690 | |||
| 4691 | public PlInt refaddr() { | ||
| 4692 | // Scalar::Util::refaddr() | ||
| 4693 | int id = System.identityHashCode(this.o); | ||
| 4694 | return new PlInt(id); | ||
| 4695 | } | ||
| 4696 | public PlLvalueRef(PlLvalue o) { | ||
| 4697 | this.o = o; | ||
| 4698 | } | ||
| 4699 | public PlLvalueRef(PlObject o) { | ||
| 4700 | this.o = o; | ||
| 4701 | } | ||
| 4702 | public PlLvalueRef(String o) { | ||
| 4703 | this.o = new PlString(o); | ||
| 4704 | } | ||
| 4705 | public PlObject scalar_deref_lvalue(String namespace) { | ||
| 4706 | return this.o; | ||
| 4707 | } | ||
| 4708 | public PlObject scalar_deref(String namespace) { | ||
| 4709 | return this.o; | ||
| 4710 | } | ||
| 4711 | public PlObject scalar_deref_strict() { | ||
| 4712 | return this.o; | ||
| 4713 | } | ||
| 4714 | public PlObject scalar_deref_set(String namespace, PlObject v) { | ||
| 4715 | return this.o.set(v); | ||
| 4716 | } | ||
| 4717 | public boolean is_scalarref() { | ||
| 4718 | return true; | ||
| 4719 | } | ||
| 4720 | } | ||
| 4721 | class PlArrayRef extends PlReference { | ||
| 4722 | public static final String REF_str = new String("ARRAY"); | ||
| 4723 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4724 | public PlArray ar; | ||
| 4725 | |||
| 4726 | public PlArrayRef() { | ||
| 4727 | this.ar = new PlArray(); | ||
| 4728 | } | ||
| 4729 | public PlArrayRef(PlArray o) { | ||
| 4730 | this.ar = o; | ||
| 4731 | } | ||
| 4732 | public PlArrayRef(PlObject o) { | ||
| 4733 | this.ar = (PlArray)o; | ||
| 4734 | } | ||
| 4735 | public PlInt refaddr() { | ||
| 4736 | // Scalar::Util::refaddr() | ||
| 4737 | int id = System.identityHashCode(this.ar); | ||
| 4738 | return new PlInt(id); | ||
| 4739 | } | ||
| 4740 | public PlObject set(PlArray o) { | ||
| 4741 | this.ar = o; | ||
| 4742 | return o; | ||
| 4743 | } | ||
| 4744 | public PlArray array_deref_lvalue() { | ||
| 4745 | return this.ar; | ||
| 4746 | } | ||
| 4747 | public PlArray array_deref(String namespace) { | ||
| 4748 | return this.ar; | ||
| 4749 | } | ||
| 4750 | public PlArray array_deref_strict() { | ||
| 4751 | return this.ar; | ||
| 4752 | } | ||
| 4753 | public PlObject array_deref_set(PlObject v) { | ||
| 4754 | this.ar.set(v); | ||
| 4755 | return v; | ||
| 4756 | } | ||
| 4757 | public boolean is_arrayref() { | ||
| 4758 | return true; | ||
| 4759 | } | ||
| 4760 | public PlString ref() { | ||
| 4761 | if ( this.bless == null ) { | ||
| 4762 | return REF; | ||
| 4763 | } | ||
| 4764 | else { | ||
| 4765 | return this.bless.plClassName(); | ||
| 4766 | } | ||
| 4767 | } | ||
| 4768 | public String ref_str() { | ||
| 4769 | if ( this.bless == null ) { | ||
| 4770 | return REF_str; | ||
| 4771 | } | ||
| 4772 | else { | ||
| 4773 | return this.bless.className(); | ||
| 4774 | } | ||
| 4775 | } | ||
| 4776 | public boolean ref_boolean() { | ||
| 4777 | return true; | ||
| 4778 | } | ||
| 4779 | |||
| 4780 | public PlObject reftype() { | ||
| 4781 | return REF; | ||
| 4782 | } | ||
| 4783 | |||
| 4784 | public PlObject aget(int i) { | ||
| 4785 | return this.ar.a.aget(i); | ||
| 4786 | } | ||
| 4787 | public PlObject aget_lvalue(int i) { | ||
| 4788 | return this.ar.aget_lvalue(i); | ||
| 4789 | } | ||
| 4790 | public PlObject aget_scalarref(int i) { | ||
| 4791 | return this.ar.aget_scalarref(i); | ||
| 4792 | } | ||
| 4793 | public PlObject aget_hashref(int i) { | ||
| 4794 | return this.ar.aget_hashref(i); | ||
| 4795 | } | ||
| 4796 | public PlObject aget_arrayref(int i) { | ||
| 4797 | return this.ar.aget_arrayref(i); | ||
| 4798 | } | ||
| 4799 | public PlObject aset(int i, PlObject v) { | ||
| 4800 | return this.ar.aset(i, v); | ||
| 4801 | } | ||
| 4802 | |||
| 4803 | public PlObject aexists(PlObject i) { | ||
| 4804 | return this.ar.aexists(i); | ||
| 4805 | } | ||
| 4806 | public PlObject adelete(int want, PlObject i) { | ||
| 4807 | return this.ar.adelete(want, i); | ||
| 4808 | } | ||
| 4809 | public PlObject values() { | ||
| 4810 | return this.ar.values(); | ||
| 4811 | } | ||
| 4812 | public PlObject keys() { | ||
| 4813 | return this.ar.keys(); | ||
| 4814 | } | ||
| 4815 | public PlObject each() { | ||
| 4816 | return this.ar.each(); | ||
| 4817 | } | ||
| 4818 | } | ||
| 4819 | |||
| 4820 | class PlHashRef extends PlReference { | ||
| 4821 | public static final String REF_str = new String("HASH"); | ||
| 4822 | public static final PlStringConstant REF = new PlStringConstant(REF_str); | ||
| 4823 | public PlHash ha; | ||
| 4824 | |||
| 4825 | public PlHashRef() { | ||
| 4826 | this.ha = new PlHash(); | ||
| 4827 | } | ||
| 4828 | public PlHashRef(PlHash o) { | ||
| 4829 | this.ha = o; | ||
| 4830 | } | ||
| 4831 | public PlHashRef(PlObject o) { | ||
| 4832 | this.ha = (PlHash)o; | ||
| 4833 | } | ||
| 4834 | public PlInt refaddr() { | ||
| 4835 | // Scalar::Util::refaddr() | ||
| 4836 | int id = System.identityHashCode(this.ha); | ||
| 4837 | return new PlInt(id); | ||
| 4838 | } | ||
| 4839 | public boolean is_hashref() { | ||
| 4840 | return true; | ||
| 4841 | } | ||
| 4842 | public PlString ref() { | ||
| 4843 | if ( this.bless == null ) { | ||
| 4844 | return REF; | ||
| 4845 | } | ||
| 4846 | else { | ||
| 4847 | return this.bless.plClassName(); | ||
| 4848 | } | ||
| 4849 | } | ||
| 4850 | public String ref_str() { | ||
| 4851 | if ( this.bless == null ) { | ||
| 4852 | return REF_str; | ||
| 4853 | } | ||
| 4854 | else { | ||
| 4855 | return this.bless.className(); | ||
| 4856 | } | ||
| 4857 | } | ||
| 4858 | public boolean ref_boolean() { | ||
| 4859 | return true; | ||
| 4860 | } | ||
| 4861 | |||
| 4862 | public PlObject reftype() { | ||
| 4863 | return REF; | ||
| 4864 | } | ||
| 4865 | |||
| 4866 | public PlObject set(PlHash o) { | ||
| 4867 | this.ha = o; | ||
| 4868 | return o; | ||
| 4869 | } | ||
| 4870 | public PlHash hash_deref(String namespace) { | ||
| 4871 | return this.ha; | ||
| 4872 | } | ||
| 4873 | public PlHash hash_deref_strict() { | ||
| 4874 | return this.ha; | ||
| 4875 | } | ||
| 4876 | public PlObject hash_deref_set(PlObject v) { | ||
| 4877 | this.ha.set(PlCx.VOID, v); | ||
| 4878 | return v; | ||
| 4879 | } | ||
| 4880 | |||
| 4881 | public PlObject hget(String i) { | ||
| 4882 | PlObject o = this.ha.h.get(i); | ||
| 4883 | if (o == null) { | ||
| 4884 | return PlCx.UNDEF; | ||
| 4885 | } | ||
| 4886 | return o; | ||
| 4887 | } | ||
| 4888 | |||
| 4889 | public PlObject hget_lvalue(String i) { | ||
| 4890 | return this.ha.hget_lvalue(i); | ||
| 4891 | } | ||
| 4892 | public PlObject hget_lvalue_local(String i) { | ||
| 4893 | return this.ha.hget_lvalue_local(i); | ||
| 4894 | } | ||
| 4895 | |||
| 4896 | public PlObject hget_scalarref(String i) { | ||
| 4897 | return this.ha.hget_scalarref(i); | ||
| 4898 | } | ||
| 4899 | |||
| 4900 | public PlObject hget_arrayref(String i) { | ||
| 4901 | return this.ha.hget_arrayref(i); | ||
| 4902 | } | ||
| 4903 | |||
| 4904 | public PlObject hget_hashref(String i) { | ||
| 4905 | return this.ha.hget_hashref(i); | ||
| 4906 | } | ||
| 4907 | |||
| 4908 | public PlObject hset(String i, PlObject v) { | ||
| 4909 | return this.ha.hset(i, v); | ||
| 4910 | } | ||
| 4911 | public PlObject hset(String i, PlLvalue v) { | ||
| 4912 | return this.ha.hset(i, v.get()); | ||
| 4913 | } | ||
| 4914 | public PlObject hset(int want, PlArray i, PlArray v) { | ||
| 4915 | return this.ha.hset(want, i, v); | ||
| 4916 | } | ||
| 4917 | public PlObject hset_alias(String i, PlObject v) { | ||
| 4918 | return this.ha.hset_alias(i, v); | ||
| 4919 | } | ||
| 4920 | public PlObject hexists(String i) { | ||
| 4921 | return this.ha.hexists(i); | ||
| 4922 | } | ||
| 4923 | public PlObject hexists(PlObject i) { | ||
| 4924 | return this.ha.hexists(i); | ||
| 4925 | } | ||
| 4926 | public PlObject hdelete(int want, PlObject a) { | ||
| 4927 | return this.ha.hdelete(want, a); | ||
| 4928 | } | ||
| 4929 | public PlObject hdelete(int want, PlArray a) { | ||
| 4930 | return this.ha.hdelete(want, a); | ||
| 4931 | } | ||
| 4932 | public PlObject hdelete(int want, PlString a) { | ||
| 4933 | return this.ha.hdelete(want, a); | ||
| 4934 | } | ||
| 4935 | public PlObject hdelete(int want, PlLvalue a) { | ||
| 4936 | return this.ha.hdelete(want, a); | ||
| 4937 | } | ||
| 4938 | public PlObject values() { | ||
| 4939 | return this.ha.values(); | ||
| 4940 | } | ||
| 4941 | public PlObject keys() { | ||
| 4942 | return this.ha.keys(); | ||
| 4943 | } | ||
| 4944 | public PlObject each() { | ||
| 4945 | return this.ha.each(); | ||
| 4946 | } | ||
| 4947 | } | ||
| 4948 | class PlClass { | ||
| 4949 | public static HashMap<String, PlClass> classes = new HashMap<String, PlClass>(); | ||
| 4950 | public String className; | ||
| 4951 | public PlString plClassName; | ||
| 4952 | public Boolean overload_flag; | ||
| 4953 | public Boolean overload_fallback_flag; | ||
| 4954 | public HashMap<String, PlObject> methodCache; | ||
| 4955 | |||
| 4956 | protected PlClass(String s) { | ||
| 4957 | this.className = s; | ||
| 4958 | this.plClassName = new PlString(s); | ||
| 4959 | this.overload_flag = null; | ||
| 4960 | this.overload_fallback_flag = null; | ||
| 4961 | this.methodCache = new HashMap<String, PlObject>(); | ||
| 4962 | } | ||
| 4963 | public static PlClass getInstance(PlObject s) { | ||
| 4964 | return PlClass.getInstance(s.toString()); | ||
| 4965 | } | ||
| 4966 | public static PlClass getInstance(String s) { | ||
| 4967 | PlClass c = classes.get(s); | ||
| 4968 | if (c == null) { | ||
| 4969 | c = new PlClass(s); | ||
| 4970 | classes.put(s, c); | ||
| 4971 | } | ||
| 4972 | return c; | ||
| 4973 | } | ||
| 4974 | public String className() { | ||
| 4975 | return this.className; | ||
| 4976 | } | ||
| 4977 | public PlString plClassName() { | ||
| 4978 | return this.plClassName; | ||
| 4979 | } | ||
| 4980 | public boolean is_undef() { | ||
| 4981 | return this.className == null; | ||
| 4982 | } | ||
| 4983 | public Boolean is_overloaded() { | ||
| 4984 | if (this.overload_flag == null) { | ||
| 4985 | PlObject methodCode1 = this.method_lookup("((", 0); | ||
| 4986 | PlObject methodCode2 = this.method_lookup("()", 0); | ||
| 4987 | this.overload_flag = methodCode1.is_coderef() || methodCode2.is_coderef(); | ||
| 4988 | } | ||
| 4989 | return this.overload_flag; | ||
| 4990 | } | ||
| 4991 | public Boolean is_overload_fallback() { | ||
| 4992 | if (this.overload_fallback_flag == null) { | ||
| 4993 | PlObject methodCode = this.method_lookup("()", 0); | ||
| 4994 | this.overload_fallback_flag = methodCode.is_coderef(); | ||
| 4995 | } | ||
| 4996 | return this.overload_fallback_flag; | ||
| 4997 | } | ||
| 4998 | |||
| 4999 | public PlObject overload_lookup(String method, int level) { | ||
| 5000 | // method is like (*= | ||
| 5001 | PlObject methodCode; | ||
| 5002 | String methodName = className + "::" + method; | ||
| 5003 | methodCode = PlV.cget_no_autoload(methodName); | ||
| 5004 | if (methodCode.is_undef()) { | ||
| 5005 | // method not found | ||
| 5006 | // "overload" methods have no AUTOLOAD | ||
| 5007 | // lookup in @ISA | ||
| 5008 | search: | ||
| 5009 | for (PlObject className : PlV.array_get(className + "::ISA")) { | ||
| 5010 | // prevent infinite loop | ||
| 5011 | if (level >= 100) { | ||
| 5012 | PlCORE.die("Recursive inheritance detected in package '" + className + "'"); | ||
| 5013 | } | ||
| 5014 | methodCode = PlClass.getInstance(className).overload_lookup(method, level+1); | ||
| 5015 | if (!methodCode.is_undef()) { | ||
| 5016 | return methodCode; | ||
| 5017 | } | ||
| 5018 | } | ||
| 5019 | } | ||
| 5020 | if (methodCode.is_undef()) { | ||
| 5021 | // overload does lookup in UNIVERSAL | ||
| 5022 | methodCode = PlV.cget_no_autoload("UNIVERSAL::" + method); | ||
| 5023 | } | ||
| 5024 | // method found | ||
| 5025 | |||
| 5026 | PlObject nameLookup = PlV.sget(methodName); | ||
| 5027 | if (!nameLookup.is_undef()) { | ||
| 5028 | // PlCORE.say("overload_lookup " + methodName + " scalar [" + nameLookup.toString() + "]"); | ||
| 5029 | return nameLookup; | ||
| 5030 | } | ||
| 5031 | |||
| 5032 | return methodCode; | ||
| 5033 | } | ||
| 5034 | |||
| 5035 | public void invalidate_method_cache(String method, int level) { | ||
| 5036 | if (this.methodCache.containsKey(method)) { | ||
| 5037 | this.methodCache.remove(method); | ||
| 5038 | } | ||
| 5039 | // TODO - lookup in all classes that inherit from us | ||
| 5040 | // for (PlObject className : PlV.array_get(className + "::ISA")) { | ||
| 5041 | // // prevent infinite loop | ||
| 5042 | // if (level >= 100) { | ||
| 5043 | // PlCORE.die("Recursive inheritance detected in package '" + className + "'"); | ||
| 5044 | // } | ||
| 5045 | // PlClass.getInstance(className).invalidate_method_cache(method, level+1); | ||
| 5046 | // } | ||
| 5047 | } | ||
| 5048 | |||
| 5049 | public PlObject method_lookup(String method, int level) { | ||
| 5050 | if (this.methodCache.containsKey(method)) { | ||
| 5051 | // retrieve from method cache | ||
| 5052 | // PlCORE.warn("match " + method); | ||
| 5053 | return this.methodCache.get(method); | ||
| 5054 | } | ||
| 5055 | // PlCORE.warn("miss " + method); | ||
| 5056 | |||
| 5057 | PlObject methodCode; | ||
| 5058 | int pos = method.indexOf("::"); | ||
| 5059 | if (pos != -1) { | ||
| 5060 | // fully qualified method name | ||
| 5061 | if (method.startsWith("SUPER::")) { | ||
| 5062 | PlCORE.die("not implemented: " + method); | ||
| 5063 | } | ||
| 5064 | return PlV.cget(method); | ||
| 5065 | } | ||
| 5066 | methodCode = PlV.cget_no_autoload(className + "::" + method); | ||
| 5067 | if (methodCode.is_undef()) { | ||
| 5068 | // method not found | ||
| 5069 | |||
| 5070 | // lookup in AUTOLOAD | ||
| 5071 | methodCode = PlV.cget_no_autoload(className + "::AUTOLOAD"); | ||
| 5072 | if (!methodCode.is_undef()) { | ||
| 5073 | if (method.charAt(0) == '(' // "overload" methods | ||
| 5074 | || method.equals("import") | ||
| 5075 | || method.equals("unimport") | ||
| 5076 | || method.equals("isa") | ||
| 5077 | || method.equals("can") | ||
| 5078 | ) { | ||
| 5079 | // overload method - TODO | ||
| 5080 | } | ||
| 5081 | else { | ||
| 5082 | PlV.sset(className + "::AUTOLOAD", new PlString(className + "::" + method)); | ||
| 5083 | // this.methodCache.put(method, methodCode); | ||
| 5084 | return methodCode; | ||
| 5085 | } | ||
| 5086 | } | ||
| 5087 | |||
| 5088 | // lookup in @ISA | ||
| 5089 | for (PlObject className : PlV.array_get(className + "::ISA")) { | ||
| 5090 | // prevent infinite loop | ||
| 5091 | if (level >= 100) { | ||
| 5092 | PlCORE.die("Recursive inheritance detected in package '" + className + "'"); | ||
| 5093 | } | ||
| 5094 | methodCode = PlClass.getInstance(className).method_lookup(method, level+1); | ||
| 5095 | if (!methodCode.is_undef()) { | ||
| 5096 | // found | ||
| 5097 | this.methodCache.put(method, methodCode); | ||
| 5098 | return methodCode; | ||
| 5099 | } | ||
| 5100 | } | ||
| 5101 | |||
| 5102 | // lookup in UNIVERSAL | ||
| 5103 | methodCode = PlV.cget_no_autoload("UNIVERSAL::" + method); | ||
| 5104 | } | ||
| 5105 | this.methodCache.put(method, methodCode); | ||
| 5106 | return methodCode; | ||
| 5107 | } | ||
| 5108 | public PlObject isa(String s, int level) { | ||
| 5109 | if (className.equals(s)) { | ||
| 5110 | return PlCx.INT1; | ||
| 5111 | } | ||
| 5112 | |||
| 5113 | // lookup in @ISA | ||
| 5114 | for (PlObject isa_item : PlV.array_get(className + "::ISA")) { | ||
| 5115 | String className = isa_item.toString(); | ||
| 5116 | // prevent infinite loop | ||
| 5117 | if (level >= 100) { | ||
| 5118 | PlCORE.die("Recursive inheritance detected in package '" + className.toString() + "'"); | ||
| 5119 | } | ||
| 5120 | PlObject is = PlClass.getInstance(className).isa(s, level+1); | ||
| 5121 | if (is.to_boolean()) { | ||
| 5122 | return is; | ||
| 5123 | } | ||
| 5124 | } | ||
| 5125 | |||
| 5126 | // lookup in UNIVERSAL | ||
| 5127 | if (s.equals("UNIVERSAL")) { | ||
| 5128 | return PlCx.INT1; | ||
| 5129 | } | ||
| 5130 | return PlCx.UNDEF; | ||
| 5131 | } | ||
| 5132 | EOT | ||
| 5133 | |||
| 5134 | # overload | ||
| 5135 | # TODO: "nomethod" | ||
| 5136 | # TODO: dispatch on indirect reference (method name instead of coderef); coderef = \&nil - See overload.pm | ||
| 5137 | # TODO: missing operators | ||
| 5138 | # with_assign => "+ - * / % ** << >> x .", | ||
| 5139 | # assign => "+= -= *= /= %= **= <<= >>= x= .=", | ||
| 5140 | # num_comparison => "< <= > >= == !=", | ||
| 5141 | # '3way_comparison' => "<=> cmp", | ||
| 5142 | # str_comparison => "lt le gt ge eq ne", | ||
| 5143 | # binary => '& &= | |= ^ ^=', | ||
| 5144 | # unary => "neg ! ~", | ||
| 5145 | # mutators => '++ --', | ||
| 5146 | # func => "atan2 cos sin exp abs log sqrt int", | ||
| 5147 | # conversion => 'bool "" 0+ qr', | ||
| 5148 | # iterators => '<>', | ||
| 5149 | # filetest => "-X", | ||
| 5150 | # dereferencing => '${} @{} %{} &{} *{}', | ||
| 5151 | # matching => '~~', | ||
| 5152 | # special => 'nomethod fallback =', | ||
| 5153 | # | ||
| 5154 | # TODO: new error message: | ||
| 5155 | # Operation "*": no method found, | ||
| 5156 | # left argument has no overloaded magic, | ||
| 5157 | # right argument in overloaded package XYZ | ||
| 5158 | # Operation "*": no method found, | ||
| 5159 | # left argument in overloaded package XYZ, | ||
| 5160 | # right argument has no overloaded magic | ||
| 5161 | |||
| 5162 | , <<'EOT' | ||
| 5163 | public static PlObject overload_to_string(PlObject o) { | ||
| 5164 | PlClass bless = o.blessed_class(); | ||
| 5165 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5166 | // PlCORE.say("PlReference toString() " + bless.className); | ||
| 5167 | for (String ovl : new String[] { PlCx.OVERLOAD_STRING, PlCx.OVERLOAD_NUM, PlCx.OVERLOAD_BOOL }) { | ||
| 5168 | PlObject methodCode = bless.overload_lookup(ovl, 0); | ||
| 5169 | if (!methodCode.is_undef()) { | ||
| 5170 | return PerlOp.call(methodCode, new PlArray(o), PlCx.SCALAR); | ||
| 5171 | } | ||
| 5172 | if (!bless.is_overload_fallback()) { | ||
| 5173 | break; | ||
| 5174 | } | ||
| 5175 | } | ||
| 5176 | |||
| 5177 | // nomethod | ||
| 5178 | PlObject methodCode = bless.overload_lookup(PlCx.OVERLOAD_NOMETHOD, 0); | ||
| 5179 | if (!methodCode.is_undef()) { | ||
| 5180 | return PerlOp.call(methodCode, new PlArray( o, PlCx.UNDEF, PlCx.UNDEF, new PlString(PlCx.OVERLOAD_STRING) ), PlCx.SCALAR); | ||
| 5181 | } | ||
| 5182 | } | ||
| 5183 | |||
| 5184 | return o.refstring(); | ||
| 5185 | } | ||
| 5186 | public static PlObject overload_to_number(PlObject o) { | ||
| 5187 | PlClass bless = o.blessed_class(); | ||
| 5188 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5189 | for (String ovl : new String[] { PlCx.OVERLOAD_NUM, PlCx.OVERLOAD_STRING, PlCx.OVERLOAD_BOOL }) { | ||
| 5190 | PlObject methodCode = bless.overload_lookup(ovl, 0); | ||
| 5191 | if (!methodCode.is_undef()) { | ||
| 5192 | return PerlOp.call(methodCode, new PlArray(o), PlCx.SCALAR); | ||
| 5193 | } | ||
| 5194 | if (!bless.is_overload_fallback()) { | ||
| 5195 | break; | ||
| 5196 | } | ||
| 5197 | } | ||
| 5198 | |||
| 5199 | // nomethod | ||
| 5200 | PlObject methodCode = bless.overload_lookup(PlCx.OVERLOAD_NOMETHOD, 0); | ||
| 5201 | if (!methodCode.is_undef()) { | ||
| 5202 | return PerlOp.call(methodCode, new PlArray( o, PlCx.UNDEF, PlCx.UNDEF, new PlString(PlCx.OVERLOAD_STRING) ), PlCx.SCALAR); | ||
| 5203 | } | ||
| 5204 | } | ||
| 5205 | |||
| 5206 | return o.refaddr(); | ||
| 5207 | } | ||
| 5208 | public static PlObject overload_to_boolean(PlObject o) { | ||
| 5209 | PlClass bless = o.blessed_class(); | ||
| 5210 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5211 | for (String ovl : new String[] { PlCx.OVERLOAD_BOOL, PlCx.OVERLOAD_NUM, PlCx.OVERLOAD_STRING }) { | ||
| 5212 | PlObject methodCode = bless.overload_lookup(ovl, 0); | ||
| 5213 | if (!methodCode.is_undef()) { | ||
| 5214 | return PerlOp.call(methodCode, new PlArray(o), PlCx.SCALAR); | ||
| 5215 | } | ||
| 5216 | if (!bless.is_overload_fallback()) { | ||
| 5217 | break; | ||
| 5218 | } | ||
| 5219 | } | ||
| 5220 | |||
| 5221 | // nomethod | ||
| 5222 | PlObject methodCode = bless.overload_lookup(PlCx.OVERLOAD_NOMETHOD, 0); | ||
| 5223 | if (!methodCode.is_undef()) { | ||
| 5224 | return PerlOp.call(methodCode, new PlArray( o, PlCx.UNDEF, PlCx.UNDEF, new PlString(PlCx.OVERLOAD_STRING) ), PlCx.SCALAR); | ||
| 5225 | } | ||
| 5226 | } | ||
| 5227 | |||
| 5228 | return PlCx.TRUE; | ||
| 5229 | } | ||
| 5230 | EOT | ||
| 5231 | , ((map { | ||
| 5232 | my $perl = $_; | ||
| 5233 | my $native; | ||
| 5234 | $native = $number_binop{$perl}{op} if exists $number_binop{$perl}; | ||
| 5235 | $native = "<=>" if $perl eq "num_cmp"; | ||
| 5236 | $native = "**" if $perl eq "pow"; | ||
| 5237 | $native = "atan2" if $perl eq "atan2"; | ||
| 5238 | $native = "%" if $perl eq "mod"; | ||
| 5239 | $native = ">>" if $perl eq "int_shr"; | ||
| 5240 | " public static PlObject overload_${perl}(PlObject o, PlObject other, PlObject swap) { | ||
| 5241 | PlClass bless = o.blessed_class(); | ||
| 5242 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5243 | PlObject methodCode = bless.overload_lookup(\"(${native}\", 0); | ||
| 5244 | if (!methodCode.is_undef()) { | ||
| 5245 | return PerlOp.call(methodCode, new PlArray(o, other, swap), PlCx.SCALAR); | ||
| 5246 | } | ||
| 5247 | if (bless.is_overload_fallback()) { | ||
| 5248 | o = PlClass.overload_to_number(o); | ||
| 5249 | } | ||
| 5250 | else { | ||
| 5251 | PlCORE.die(\"Operation ${native}: no method found\"); | ||
| 5252 | } | ||
| 5253 | } | ||
| 5254 | else { | ||
| 5255 | o = o.refaddr(); | ||
| 5256 | } | ||
| 5257 | if (swap.to_boolean()) { | ||
| 5258 | return other.${perl}(o); | ||
| 5259 | } | ||
| 5260 | return o.${perl}(other); | ||
| 5261 | } | ||
| 5262 | " | ||
| 5263 | } | ||
| 5264 | sort ( | ||
| 5265 | 'num_cmp', | ||
| 5266 | 'pow', | ||
| 5267 | 'atan2', | ||
| 5268 | 'mod', | ||
| 5269 | keys %number_binop, | ||
| 5270 | ) | ||
| 5271 | )) | ||
| 5272 | |||
| 5273 | , ((map { | ||
| 5274 | my $perl = $_; | ||
| 5275 | my $native = $perl; | ||
| 5276 | $native = "++" if $perl eq "_incr"; | ||
| 5277 | $native = "--" if $perl eq "_decr"; | ||
| 5278 | " public static PlObject overload_${perl}(PlObject o) { | ||
| 5279 | PlClass bless = o.blessed_class(); | ||
| 5280 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5281 | PlObject methodCode = bless.overload_lookup(\"(${native}\", 0); | ||
| 5282 | PlObject copyConstructorCode = bless.overload_lookup(\"(=\", 0); | ||
| 5283 | if (!methodCode.is_undef()) { | ||
| 5284 | |||
| 5285 | if (!copyConstructorCode.is_undef()) { | ||
| 5286 | o = PerlOp.call(copyConstructorCode, new PlArray(o), PlCx.SCALAR); | ||
| 5287 | } | ||
| 5288 | else if (o.is_scalarref()) { | ||
| 5289 | // mutator: copy the reference | ||
| 5290 | PlObject v = o.scalar_deref(\"main\"); | ||
| 5291 | if (v.is_lvalue()) { | ||
| 5292 | v = new PlLvalue(v.get()); | ||
| 5293 | } | ||
| 5294 | o = new PlLvalueRef(v); | ||
| 5295 | o.bless( bless.className() ); | ||
| 5296 | } | ||
| 5297 | |||
| 5298 | return PerlOp.call(methodCode, new PlArray(o), PlCx.SCALAR); | ||
| 5299 | } | ||
| 5300 | // if (bless.is_overload_fallback()) { | ||
| 5301 | PlObject v = PlClass.overload_to_number(o).${perl}(); | ||
| 5302 | if (o.is_scalarref()) { | ||
| 5303 | // auto generated mutator: copy the reference | ||
| 5304 | PlLvalueRef ret = new PlLvalueRef(v); | ||
| 5305 | ret.bless( bless.className() ); | ||
| 5306 | return ret; | ||
| 5307 | } | ||
| 5308 | // TODO - call the 'Copy constructor' | ||
| 5309 | return v; | ||
| 5310 | } | ||
| 5311 | return o.refaddr().${perl}(); | ||
| 5312 | } | ||
| 5313 | " | ||
| 5314 | } | ||
| 5315 | sort ( | ||
| 5316 | '_decr', | ||
| 5317 | '_incr', | ||
| 5318 | ) | ||
| 5319 | )) | ||
| 5320 | |||
| 5321 | , ((map { | ||
| 5322 | my $perl = $_; | ||
| 5323 | my $native = $perl; | ||
| 5324 | $native = "int" if $perl eq "op_int"; | ||
| 5325 | $native = "~" if $perl eq "complement"; | ||
| 5326 | " public static PlObject overload_${perl}(PlObject o) { | ||
| 5327 | PlClass bless = o.blessed_class(); | ||
| 5328 | // PlCORE.say(\"overload_${perl}\"); | ||
| 5329 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5330 | PlObject methodCode = bless.overload_lookup(\"(${native}\", 0); | ||
| 5331 | if (!methodCode.is_undef()) { | ||
| 5332 | // PlCORE.say(\"overload_${perl} hit \"); | ||
| 5333 | return PerlOp.call(methodCode, new PlArray(o), PlCx.SCALAR); | ||
| 5334 | } | ||
| 5335 | " | ||
| 5336 | . ( $perl eq "neg" ? | ||
| 5337 | " | ||
| 5338 | // neg falls back to (0-v) | ||
| 5339 | methodCode = bless.overload_lookup(\"(-\", 0); | ||
| 5340 | if (!methodCode.is_undef()) { | ||
| 5341 | // PlCORE.say(\"overload_${perl} fallback (- \"); | ||
| 5342 | return PerlOp.call(methodCode, new PlArray(o, PlCx.INT0, PlCx.TRUE), PlCx.SCALAR); | ||
| 5343 | } | ||
| 5344 | " | ||
| 5345 | : ()) | ||
| 5346 | . ( $perl eq "abs" ? | ||
| 5347 | " | ||
| 5348 | // abs falls back to (v < 0 ? 0-v : v) | ||
| 5349 | methodCode = bless.overload_lookup(\"(<\", 0); | ||
| 5350 | if (!methodCode.is_undef()) { | ||
| 5351 | // PlCORE.say(\"overload_${perl} fallback (< \"); | ||
| 5352 | int cmp = PerlOp.call(methodCode, new PlArray(o, PlCx.INT0, PlCx.FALSE), PlCx.SCALAR).to_int(); | ||
| 5353 | if (cmp > 0) { | ||
| 5354 | return o.neg(); | ||
| 5355 | } | ||
| 5356 | return o; | ||
| 5357 | } | ||
| 5358 | |||
| 5359 | methodCode = bless.overload_lookup(\"(<=>\", 0); | ||
| 5360 | if (!methodCode.is_undef()) { | ||
| 5361 | // PlCORE.say(\"overload_${perl} fallback (<=> \"); | ||
| 5362 | int cmp = PerlOp.call(methodCode, new PlArray(o, PlCx.INT0, PlCx.FALSE), PlCx.SCALAR).to_int(); | ||
| 5363 | if (cmp >= 0) { | ||
| 5364 | return o; | ||
| 5365 | } | ||
| 5366 | return o.neg(); | ||
| 5367 | } | ||
| 5368 | " | ||
| 5369 | : ()) | ||
| 5370 | . " | ||
| 5371 | if (bless.is_overload_fallback()) { | ||
| 5372 | // PlCORE.say(\"overload_${perl} plain fallback \"); | ||
| 5373 | return PlClass.overload_to_number(o).${perl}(); | ||
| 5374 | } | ||
| 5375 | // PlCORE.say(\"overload_${perl} fall through \"); | ||
| 5376 | PlCORE.die(\"Operation ${native}: no method found\"); | ||
| 5377 | } | ||
| 5378 | return o.refaddr().${perl}(); | ||
| 5379 | } | ||
| 5380 | " | ||
| 5381 | } | ||
| 5382 | sort ( | ||
| 5383 | @number_unary, | ||
| 5384 | ) | ||
| 5385 | )) | ||
| 5386 | |||
| 5387 | , ((map { | ||
| 5388 | my $perl = $_; | ||
| 5389 | my $native; | ||
| 5390 | $native = $string_binop{$perl}{str_op} if exists $string_binop{$perl}; | ||
| 5391 | $native = "cmp" if $perl eq "str_cmp"; | ||
| 5392 | $native = "x" if $perl eq "string_replicate"; | ||
| 5393 | $native = "~~" if $perl eq "smartmatch"; | ||
| 5394 | " public static PlObject overload_${perl}(PlObject o, PlObject other, PlObject swap) { | ||
| 5395 | PlClass bless = o.blessed_class(); | ||
| 5396 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5397 | PlObject methodCode = bless.overload_lookup(\"(${native}\", 0); | ||
| 5398 | if (!methodCode.is_undef()) { | ||
| 5399 | return PerlOp.call(methodCode, new PlArray(o, other, swap), PlCx.SCALAR); | ||
| 5400 | } | ||
| 5401 | if (bless.is_overload_fallback()) { | ||
| 5402 | o = PlClass.overload_to_string(o); | ||
| 5403 | } | ||
| 5404 | else { | ||
| 5405 | o = o.refstring(); | ||
| 5406 | } | ||
| 5407 | } | ||
| 5408 | else { | ||
| 5409 | o = o.refstring(); | ||
| 5410 | } | ||
| 5411 | if (swap.to_boolean()) { | ||
| 5412 | return other.${perl}(o); | ||
| 5413 | } | ||
| 5414 | return o.${perl}(other); | ||
| 5415 | } | ||
| 5416 | " | ||
| 5417 | } | ||
| 5418 | sort ( | ||
| 5419 | 'str_cmp', | ||
| 5420 | 'string_replicate', | ||
| 5421 | 'smartmatch', | ||
| 5422 | keys %string_binop, | ||
| 5423 | ) | ||
| 5424 | )) | ||
| 5425 | |||
| 5426 | , ((map { | ||
| 5427 | my $perl = $_; | ||
| 5428 | my $native; | ||
| 5429 | $native = $self_assign_number_binop{$perl}{op} if exists $self_assign_number_binop{$perl}; | ||
| 5430 | $native = "x=" if $perl eq "string_replicate"; | ||
| 5431 | " public static PlObject overload__self_${perl}(PlObject o, PlObject other, PlObject swap) { | ||
| 5432 | PlClass bless = o.blessed_class(); | ||
| 5433 | // PlCORE.say(\"in self_assign ${native} \"); | ||
| 5434 | if ( bless != null && bless.is_overloaded() ) { | ||
| 5435 | PlObject methodCode = bless.overload_lookup(\"(${native}\", 0); | ||
| 5436 | PlObject copyConstructorCode = bless.overload_lookup(\"(=\", 0); | ||
| 5437 | if (!methodCode.is_undef()) { | ||
| 5438 | // PlCORE.say(\"self_assign (${native} \" + other.toString()); | ||
| 5439 | |||
| 5440 | if (!copyConstructorCode.is_undef()) { | ||
| 5441 | o = PerlOp.call(copyConstructorCode, new PlArray(o), PlCx.SCALAR); | ||
| 5442 | } | ||
| 5443 | else if (o.is_scalarref()) { | ||
| 5444 | // mutator: copy the reference | ||
| 5445 | PlObject v = o.scalar_deref(\"main\"); | ||
| 5446 | if (v.is_lvalue()) { | ||
| 5447 | v = new PlLvalue(v.get()); | ||
| 5448 | } | ||
| 5449 | o = new PlLvalueRef(v); | ||
| 5450 | o.bless( bless.className() ); | ||
| 5451 | } | ||
| 5452 | |||
| 5453 | return PerlOp.call(methodCode, new PlArray(o, other, swap), PlCx.SCALAR); | ||
| 5454 | } | ||
| 5455 | // TODO - overload_self_assign_${perl} | ||
| 5456 | //if (bless.is_overload_fallback()) { | ||
| 5457 | // PlCORE.say(\"self_assign generated (${native} \" + other.toString()); | ||
| 5458 | |||
| 5459 | PlObject v; | ||
| 5460 | if (swap.to_boolean()) { | ||
| 5461 | v = other.${perl}(PlClass.overload_to_number(o)); | ||
| 5462 | } | ||
| 5463 | else { | ||
| 5464 | v = PlClass.overload_to_number(o).${perl}(other); | ||
| 5465 | } | ||
| 5466 | if (o.is_scalarref()) { | ||
| 5467 | // auto generated mutator: copy the reference | ||
| 5468 | PlLvalueRef ret = new PlLvalueRef(v); | ||
| 5469 | ret.bless( bless.className() ); | ||
| 5470 | return ret; | ||
| 5471 | } | ||
| 5472 | // TODO - call the 'Copy constructor' | ||
| 5473 | return v; | ||
| 5474 | |||
| 5475 | //} | ||
| 5476 | //else { | ||
| 5477 | // o = o.refstring(); | ||
| 5478 | //} | ||
| 5479 | } | ||
| 5480 | else { | ||
| 5481 | o = o.refstring(); | ||
| 5482 | } | ||
| 5483 | if (swap.to_boolean()) { | ||
| 5484 | return other.${perl}(o); | ||
| 5485 | } | ||
| 5486 | return o.${perl}(other); | ||
| 5487 | } | ||
| 5488 | " | ||
| 5489 | } | ||
| 5490 | sort ( | ||
| 5491 | 'string_replicate', | ||
| 5492 | keys %self_assign_number_binop, | ||
| 5493 | ) | ||
| 5494 | )) | ||
| 5495 | |||
| 5496 | , <<'EOT' | ||
| 5497 | } | ||
| 5498 | class PlLazyIndex extends PlLazyLvalue { | ||
| 5499 | private PlArray la; // @la | ||
| 5500 | private int i; // $la[$i] | ||
| 5501 | |||
| 5502 | public PlLazyIndex(PlArray la, int i) { | ||
| 5503 | this.la = la; | ||
| 5504 | this.i = i; | ||
| 5505 | } | ||
| 5506 | |||
| 5507 | // internal lazy api | ||
| 5508 | public PlLvalue create_scalar() { | ||
| 5509 | if (llv == null) { | ||
| 5510 | llv = la.create_scalar(i); | ||
| 5511 | } | ||
| 5512 | return llv; | ||
| 5513 | } | ||
| 5514 | |||
| 5515 | } | ||
| 5516 | |||
| 5517 | class PlLvalueSubstring extends PlLazyLvalue { | ||
| 5518 | private PlObject lv; | ||
| 5519 | private String start; | ||
| 5520 | private String end; | ||
| 5521 | private String replacement; | ||
| 5522 | |||
| 5523 | public PlLvalueSubstring(PlObject lv, int offset, int length) { | ||
| 5524 | this.lv = lv; | ||
| 5525 | this.replacement = lv.get().substr(offset, length).toString(); | ||
| 5526 | |||
| 5527 | String s = lv.toString(); | ||
| 5528 | int ofs = offset; | ||
| 5529 | int len = length; | ||
| 5530 | if (ofs < 0) { | ||
| 5531 | ofs = s.length() + ofs; | ||
| 5532 | } | ||
| 5533 | |||
| 5534 | if (len < 0) { | ||
| 5535 | len = s.length() + len; | ||
| 5536 | } | ||
| 5537 | else { | ||
| 5538 | len = ofs + len; | ||
| 5539 | } | ||
| 5540 | |||
| 5541 | if (len >= s.length()) { | ||
| 5542 | len = s.length(); | ||
| 5543 | } | ||
| 5544 | if (ofs < 0) { | ||
| 5545 | ofs = 0; | ||
| 5546 | } | ||
| 5547 | |||
| 5548 | PlObject ret = new PlString(s.substring(ofs, len)); | ||
| 5549 | this.start = ""; | ||
| 5550 | this.end = ""; | ||
| 5551 | |||
| 5552 | if (ofs > 0) { | ||
| 5553 | this.start = s.substring(0, ofs); | ||
| 5554 | } | ||
| 5555 | if (len < s.length()) { | ||
| 5556 | this.end = s.substring(len); | ||
| 5557 | } | ||
| 5558 | } | ||
| 5559 | |||
| 5560 | // internal lazy api | ||
| 5561 | public PlLvalue create_scalar() { | ||
| 5562 | return (PlLvalue)this.lv; | ||
| 5563 | } | ||
| 5564 | |||
| 5565 | public PlScalarImmutable get() { | ||
| 5566 | return new PlString( replacement ); | ||
| 5567 | } | ||
| 5568 | |||
| 5569 | public PlObject set(PlObject o) { | ||
| 5570 | this.replacement = o.toString(); | ||
| 5571 | lv.set( new PlString( start + replacement + end ) ); | ||
| 5572 | return this; | ||
| 5573 | } | ||
| 5574 | |||
| 5575 | public PlObject pre_decr() { | ||
| 5576 | // --$x | ||
| 5577 | PlObject o = this.get()._decr(); | ||
| 5578 | return this.set(o); | ||
| 5579 | } | ||
| 5580 | public PlObject post_decr() { | ||
| 5581 | // $x-- | ||
| 5582 | PlObject o = this.get(); | ||
| 5583 | this.set(o._decr()); | ||
| 5584 | return o; | ||
| 5585 | } | ||
| 5586 | public PlObject pre_incr() { | ||
| 5587 | // ++$x | ||
| 5588 | PlObject o = this.get()._incr(); | ||
| 5589 | return this.set(o); | ||
| 5590 | } | ||
| 5591 | public PlObject post_incr() { | ||
| 5592 | // $x++ | ||
| 5593 | PlObject o = this.get(); | ||
| 5594 | this.set(o._incr()); | ||
| 5595 | return o; | ||
| 5596 | } | ||
| 5597 | } | ||
| 5598 | class PlLazyTiedLookup extends PlLazyLvalue { | ||
| 5599 | private PlHash la; // %la | ||
| 5600 | private String i; // $la{$i} | ||
| 5601 | |||
| 5602 | public PlLazyTiedLookup(PlHash la, String i) { | ||
| 5603 | this.la = la; | ||
| 5604 | this.i = i; | ||
| 5605 | } | ||
| 5606 | |||
| 5607 | // internal lazy api | ||
| 5608 | public PlLvalue create_scalar() { | ||
| 5609 | if (llv == null) { | ||
| 5610 | llv = la.create_scalar(i); | ||
| 5611 | } | ||
| 5612 | return llv; | ||
| 5613 | } | ||
| 5614 | |||
| 5615 | public PlScalarImmutable get() { | ||
| 5616 | return (PlScalarImmutable)la.hget(i); | ||
| 5617 | } | ||
| 5618 | |||
| 5619 | public PlObject set(PlObject o) { | ||
| 5620 | la.hset(i, o); | ||
| 5621 | return this; | ||
| 5622 | } | ||
| 5623 | |||
| 5624 | public PlObject pre_decr() { | ||
| 5625 | // --$x | ||
| 5626 | PlObject o = this.get()._decr(); | ||
| 5627 | return this.set(o); | ||
| 5628 | } | ||
| 5629 | public PlObject post_decr() { | ||
| 5630 | // $x-- | ||
| 5631 | PlObject o = this.get(); | ||
| 5632 | this.set(o._decr()); | ||
| 5633 | return o; | ||
| 5634 | } | ||
| 5635 | public PlObject pre_incr() { | ||
| 5636 | // ++$x | ||
| 5637 | PlObject o = this.get()._incr(); | ||
| 5638 | return this.set(o); | ||
| 5639 | } | ||
| 5640 | public PlObject post_incr() { | ||
| 5641 | // $x++ | ||
| 5642 | PlObject o = this.get(); | ||
| 5643 | this.set(o._incr()); | ||
| 5644 | return o; | ||
| 5645 | } | ||
| 5646 | } | ||
| 5647 | class PlLazyLookup extends PlLazyLvalue { | ||
| 5648 | private PlHash la; // %la | ||
| 5649 | private String i; // $la{$i} | ||
| 5650 | |||
| 5651 | public PlLazyLookup(PlHash la, String i) { | ||
| 5652 | this.la = la; | ||
| 5653 | this.i = i; | ||
| 5654 | } | ||
| 5655 | |||
| 5656 | // internal lazy api | ||
| 5657 | public PlLvalue create_scalar() { | ||
| 5658 | if (llv == null) { | ||
| 5659 | llv = la.create_scalar(i); | ||
| 5660 | } | ||
| 5661 | return llv; | ||
| 5662 | } | ||
| 5663 | } | ||
| 5664 | class PlLazyScalarref extends PlLazyLvalue { | ||
| 5665 | private PlLvalue lv; // $lv | ||
| 5666 | |||
| 5667 | public PlLazyScalarref(PlLvalue lv) { | ||
| 5668 | this.lv = lv; | ||
| 5669 | } | ||
| 5670 | |||
| 5671 | // internal lazy api | ||
| 5672 | public PlLvalue create_scalar() { | ||
| 5673 | if (this.llv == null) { | ||
| 5674 | PlLvalue s = new PlLvalue(); | ||
| 5675 | lv.create_scalar().set(new PlLvalueRef(s)); | ||
| 5676 | this.llv = s; | ||
| 5677 | } | ||
| 5678 | return this.llv; | ||
| 5679 | } | ||
| 5680 | } | ||
| 5681 | |||
| 5682 | class PlLazyLvalue extends PlLvalue { | ||
| 5683 | public PlLvalue llv; // $$lv | ||
| 5684 | |||
| 5685 | public PlLazyLvalue() { | ||
| 5686 | } | ||
| 5687 | |||
| 5688 | public PlLvalue create_scalar() { | ||
| 5689 | return (PlLvalue)PlCORE.die("internal error: called PlLazyLvalue.create_scalar()"); | ||
| 5690 | } | ||
| 5691 | |||
| 5692 | public PlScalarImmutable get() { | ||
| 5693 | if (llv == null) { | ||
| 5694 | return PlCx.UNDEF; | ||
| 5695 | } | ||
| 5696 | return llv.get(); | ||
| 5697 | } | ||
| 5698 | |||
| 5699 | // Note: several versions of set() | ||
| 5700 | public PlObject set(PlObject o) { | ||
| 5701 | if (llv == null) { | ||
| 5702 | create_scalar(); | ||
| 5703 | } | ||
| 5704 | return llv.set(o); | ||
| 5705 | } | ||
| 5706 | public PlObject set(PlScalarImmutable o) { | ||
| 5707 | if (llv == null) { | ||
| 5708 | create_scalar(); | ||
| 5709 | } | ||
| 5710 | return llv.set(o); | ||
| 5711 | } | ||
| 5712 | public PlObject set(PlLvalue o) { | ||
| 5713 | if (llv == null) { | ||
| 5714 | create_scalar(); | ||
| 5715 | } | ||
| 5716 | return llv.set(o); | ||
| 5717 | } | ||
| 5718 | EOT | ||
| 5719 | , ((map { | ||
| 5720 | my $perl = $_; | ||
| 5721 | " public PlObject ${perl}(PlObject s) { | ||
| 5722 | return this.get().${perl}(s); | ||
| 5723 | } | ||
| 5724 | public PlObject ${perl}2(PlObject s) { | ||
| 5725 | return s.${perl}(this.get()); | ||
| 5726 | } | ||
| 5727 | " | ||
| 5728 | } | ||
| 5729 | sort ( | ||
| 5730 | 'num_cmp', | ||
| 5731 | 'mod', | ||
| 5732 | keys %number_binop, | ||
| 5733 | ) | ||
| 5734 | )) | ||
| 5735 | |||
| 5736 | # unary operators | ||
| 5737 | # | ||
| 5738 | , ((map { | ||
| 5739 | my ($op, $type) = @$_; | ||
| 5740 | " public $type $op() { | ||
| 5741 | return this.get().$op(); | ||
| 5742 | } | ||
| 5743 | " | ||
| 5744 | } | ||
| 5745 | map( [ $_ => 'PlObject' ], ( | ||
| 5746 | @number_unary, | ||
| 5747 | 'blessed', | ||
| 5748 | 'refaddr', # Scalar::Util::refaddr() | ||
| 5749 | 'reftype', # Scalar::Util::reftype() | ||
| 5750 | 'to_num', | ||
| 5751 | )), | ||
| 5752 | map( [ $_ => 'boolean' ], ( | ||
| 5753 | @boolean_unary, | ||
| 5754 | 'is_integer_range', | ||
| 5755 | )), | ||
| 5756 | [ 'toString' => 'String' ], | ||
| 5757 | [ 'to_int' => 'int' ], | ||
| 5758 | [ 'to_long' => 'long' ], | ||
| 5759 | [ 'to_double' => 'double' ], | ||
| 5760 | [ 'to_boolean' => 'boolean' ], | ||
| 5761 | [ 'blessed_class' => 'PlClass' ], | ||
| 5762 | [ 'ref' => 'PlString' ], | ||
| 5763 | [ 'ref_str' => 'String' ], | ||
| 5764 | [ 'ref_boolean' => 'boolean' ], | ||
| 5765 | |||
| 5766 | # add "unbox" accessors to Java classes that were declared with: 'package MyJavaClass { Java }' | ||
| 5767 | (map { my $class = $java_classes{$_}; | ||
| 5768 | $class->{import} || $class->{extends} || $class->{implements} | ||
| 5769 | ? [ $class->{perl_to_java}, $class->{java_type} ] | ||
| 5770 | : () | ||
| 5771 | } | ||
| 5772 | grep { $java_classes{$_}{perl_to_java} } | ||
| 5773 | sort keys %java_classes | ||
| 5774 | ), | ||
| 5775 | )) | ||
| 5776 | |||
| 5777 | , <<'EOT' | ||
| 5778 | |||
| 5779 | } | ||
| 5780 | class PlLvalueMagic { | ||
| 5781 | public PlLvalueMagic() { | ||
| 5782 | } | ||
| 5783 | |||
| 5784 | public Integer pos; | ||
| 5785 | public boolean regex_zero_length_flag; | ||
| 5786 | public PlObject tied; | ||
| 5787 | } | ||
| 5788 | class PlLvalue extends PlScalarObject { | ||
| 5789 | public PlScalarImmutable o; | ||
| 5790 | public PlLvalueMagic magic; | ||
| 5791 | |||
| 5792 | // Note: several versions of PlLvalue() | ||
| 5793 | public PlLvalue() { | ||
| 5794 | this.o = PlCx.UNDEF; | ||
| 5795 | } | ||
| 5796 | public PlLvalue(long o) { | ||
| 5797 | this.o = new PlInt(o); | ||
| 5798 | } | ||
| 5799 | public PlLvalue(PlObject o) { | ||
| 5800 | this.o = (PlScalarImmutable)o; | ||
| 5801 | } | ||
| 5802 | public PlLvalue(PlScalarImmutable o) { | ||
| 5803 | this.o = o; | ||
| 5804 | } | ||
| 5805 | public PlLvalue(PlLvalue o) { | ||
| 5806 | this.o = o.get(); | ||
| 5807 | } | ||
| 5808 | public PlLvalue(PlArray o) { | ||
| 5809 | // $a = @x | ||
| 5810 | this.o = o.scalar(); | ||
| 5811 | } | ||
| 5812 | public PlLvalue(PlHash o) { | ||
| 5813 | // $a = %x | ||
| 5814 | this.o = o.scalar(); | ||
| 5815 | } | ||
| 5816 | |||
| 5817 | public void addMagic() { | ||
| 5818 | if (this.magic == null) { | ||
| 5819 | this.magic = new PlLvalueMagic(); | ||
| 5820 | } | ||
| 5821 | } | ||
| 5822 | |||
| 5823 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 5824 | // want PlLvalue | ||
| 5825 | for (Class[] cl : params) { | ||
| 5826 | if (cl[pos].equals( this.getClass() )) { | ||
| 5827 | return new PerlArgumentLookupResult( this, cl[pos] ); | ||
| 5828 | } | ||
| 5829 | } | ||
| 5830 | // try other things | ||
| 5831 | return this.get().castToClass(params, pos); | ||
| 5832 | } | ||
| 5833 | |||
| 5834 | public Iterator<PlObject> iterator() { | ||
| 5835 | return new PlLvalueIterator(this); | ||
| 5836 | } | ||
| 5837 | |||
| 5838 | // tie scalar | ||
| 5839 | public PlObject tie(PlArray args) { | ||
| 5840 | if (this.o.is_filehandle()) { | ||
| 5841 | return this.o.tie(args); | ||
| 5842 | } | ||
| 5843 | this.addMagic(); | ||
| 5844 | if (this.magic.tied != null) { | ||
| 5845 | this.untie(); | ||
| 5846 | } | ||
| 5847 | PlObject self = PerlOp.call("TIESCALAR", args, PlCx.VOID); | ||
| 5848 | this.magic.tied = self; | ||
| 5849 | return self; | ||
| 5850 | } | ||
| 5851 | |||
| 5852 | public PlObject untie() { | ||
| 5853 | if (this.o.is_filehandle()) { | ||
| 5854 | return ((PlFileHandle)this.o).untie(); | ||
| 5855 | } | ||
| 5856 | |||
| 5857 | if (this.magic != null && this.magic.tied != null) { | ||
| 5858 | PlObject tied = this.magic.tied; | ||
| 5859 | PlObject untie = PerlOp.call("can", new PlArray(tied, new PlString("UNTIE")), PlCx.SCALAR); | ||
| 5860 | if (untie.to_boolean()) { | ||
| 5861 | untie.apply(PlCx.VOID, new PlArray(tied)); | ||
| 5862 | }; | ||
| 5863 | this.magic.tied = null; | ||
| 5864 | return tied; | ||
| 5865 | } | ||
| 5866 | return this; | ||
| 5867 | } | ||
| 5868 | public PlObject tied() { | ||
| 5869 | if (this.o.is_filehandle()) { | ||
| 5870 | return this.o.tied(); | ||
| 5871 | } | ||
| 5872 | |||
| 5873 | if (this.magic != null && this.magic.tied != null) { | ||
| 5874 | return this.magic.tied; | ||
| 5875 | } | ||
| 5876 | return PlCx.UNDEF; | ||
| 5877 | } | ||
| 5878 | |||
| 5879 | // internal lazy api | ||
| 5880 | public PlLvalue create_scalar() { | ||
| 5881 | if (this.o.is_undef()) { | ||
| 5882 | PlLvalue llv = new PlLvalue(); | ||
| 5883 | this.set(new PlLvalueRef(llv)); | ||
| 5884 | return llv; | ||
| 5885 | } | ||
| 5886 | else if (this.o.is_scalarref()) { | ||
| 5887 | return (PlLvalue)this.o.scalar_deref("main"); | ||
| 5888 | } | ||
| 5889 | return (PlLvalue)PlCORE.die("Not a SCALAR reference"); | ||
| 5890 | } | ||
| 5891 | |||
| 5892 | public PlObject pos() { | ||
| 5893 | if (this.magic != null && this.magic.pos != null) { | ||
| 5894 | return new PlInt(this.magic.pos); | ||
| 5895 | } | ||
| 5896 | return PlCx.UNDEF; | ||
| 5897 | } | ||
| 5898 | public PlObject set_pos(PlObject value) { | ||
| 5899 | addMagic(); | ||
| 5900 | this.magic.regex_zero_length_flag = false; | ||
| 5901 | if (value.is_undef()) { | ||
| 5902 | this.magic.pos = null; | ||
| 5903 | } | ||
| 5904 | else { | ||
| 5905 | this.magic.pos = value.to_int(); | ||
| 5906 | } | ||
| 5907 | return value; | ||
| 5908 | } | ||
| 5909 | |||
| 5910 | public PlScalarImmutable get() { | ||
| 5911 | if (this.magic != null && this.magic.tied != null) { | ||
| 5912 | PlScalarImmutable v = PerlOp.call("FETCH", new PlArray(this.magic.tied), PlCx.VOID).scalar(); | ||
| 5913 | this.o = v; | ||
| 5914 | return v; | ||
| 5915 | } | ||
| 5916 | return this.o; | ||
| 5917 | } | ||
| 5918 | public PlObject get_scalarref() { | ||
| 5919 | PlScalarImmutable o = this.get(); | ||
| 5920 | if (o.is_undef()) { | ||
| 5921 | PlLvalueRef ar = new PlLvalueRef(new PlLvalue()); | ||
| 5922 | this.set(ar); | ||
| 5923 | return ar; | ||
| 5924 | } | ||
| 5925 | else if (o.is_scalarref()) { | ||
| 5926 | return o; | ||
| 5927 | } | ||
| 5928 | // Modification of a read-only value attempted | ||
| 5929 | return o; | ||
| 5930 | } | ||
| 5931 | public PlObject get_arrayref() { | ||
| 5932 | PlScalarImmutable o = this.get(); | ||
| 5933 | if (o.is_undef()) { | ||
| 5934 | PlArrayRef ar = new PlArrayRef(); | ||
| 5935 | this.set(ar); | ||
| 5936 | return ar; | ||
| 5937 | } | ||
| 5938 | else if (o.is_arrayref()) { | ||
| 5939 | return o; | ||
| 5940 | } | ||
| 5941 | return PlCORE.die("Not an ARRAY reference"); | ||
| 5942 | } | ||
| 5943 | public PlObject get_hashref() { | ||
| 5944 | PlScalarImmutable o = this.get(); | ||
| 5945 | if (o.is_undef()) { | ||
| 5946 | PlHashRef hr = new PlHashRef(); | ||
| 5947 | this.set(hr); | ||
| 5948 | return hr; | ||
| 5949 | } | ||
| 5950 | else if (o.is_hashref()) { | ||
| 5951 | return o; | ||
| 5952 | } | ||
| 5953 | return PlCORE.die("Not a HASH reference"); | ||
| 5954 | } | ||
| 5955 | public PlObject aget(int i) { | ||
| 5956 | PlScalarImmutable o = this.get(); | ||
| 5957 | if (o.is_undef()) { | ||
| 5958 | o = new PlArrayRef(); | ||
| 5959 | this.set(o); | ||
| 5960 | } | ||
| 5961 | return o.aget(i); | ||
| 5962 | } | ||
| 5963 | |||
| 5964 | public PlObject aget_scalarref(int i) { | ||
| 5965 | PlScalarImmutable o = this.get(); | ||
| 5966 | if (o.is_undef()) { | ||
| 5967 | o = new PlArrayRef(); | ||
| 5968 | this.set(o); | ||
| 5969 | } | ||
| 5970 | return o.aget_scalarref(i); | ||
| 5971 | } | ||
| 5972 | public PlObject aget_arrayref(int i) { | ||
| 5973 | PlScalarImmutable o = this.get(); | ||
| 5974 | if (o.is_undef()) { | ||
| 5975 | o = new PlArrayRef(); | ||
| 5976 | this.set(o); | ||
| 5977 | } | ||
| 5978 | return o.aget_arrayref(i); | ||
| 5979 | } | ||
| 5980 | public PlObject aget_lvalue(int pos) { | ||
| 5981 | PlScalarImmutable o = this.get(); | ||
| 5982 | if (o.is_undef()) { | ||
| 5983 | o = new PlArrayRef(); | ||
| 5984 | this.set(o); | ||
| 5985 | } | ||
| 5986 | return o.aget_lvalue(pos); | ||
| 5987 | } | ||
| 5988 | public PlObject aget_hashref(int i) { | ||
| 5989 | PlScalarImmutable o = this.get(); | ||
| 5990 | if (o.is_undef()) { | ||
| 5991 | o = new PlArrayRef(); | ||
| 5992 | this.set(o); | ||
| 5993 | } | ||
| 5994 | return o.aget_hashref(i); | ||
| 5995 | } | ||
| 5996 | |||
| 5997 | public PlObject aset(int i, PlObject v) { | ||
| 5998 | PlScalarImmutable o = this.get(); | ||
| 5999 | if (o.is_undef()) { | ||
| 6000 | o = new PlArrayRef(); | ||
| 6001 | this.set(o); | ||
| 6002 | } | ||
| 6003 | return o.aset(i, v); | ||
| 6004 | } | ||
| 6005 | public PlObject hget(String i) { | ||
| 6006 | PlScalarImmutable o = this.get(); | ||
| 6007 | if (o.is_undef()) { | ||
| 6008 | o = new PlHashRef(); | ||
| 6009 | this.set(o); | ||
| 6010 | } | ||
| 6011 | return o.hget(i); | ||
| 6012 | } | ||
| 6013 | public PlObject hget_lvalue(String i) { | ||
| 6014 | PlScalarImmutable o = this.get(); | ||
| 6015 | if (o.is_undef()) { | ||
| 6016 | o = new PlHashRef(); | ||
| 6017 | this.set(o); | ||
| 6018 | } | ||
| 6019 | return o.hget_lvalue(i); | ||
| 6020 | } | ||
| 6021 | |||
| 6022 | public PlObject hget_scalarref(String i) { | ||
| 6023 | PlScalarImmutable o = this.get(); | ||
| 6024 | if (o.is_undef()) { | ||
| 6025 | o = new PlHashRef(); | ||
| 6026 | this.set(o); | ||
| 6027 | } | ||
| 6028 | return o.hget_scalarref(i); | ||
| 6029 | } | ||
| 6030 | public PlObject hget_arrayref(String i) { | ||
| 6031 | PlScalarImmutable o = this.get(); | ||
| 6032 | if (o.is_undef()) { | ||
| 6033 | o = new PlHashRef(); | ||
| 6034 | this.set(o); | ||
| 6035 | } | ||
| 6036 | return o.hget_arrayref(i); | ||
| 6037 | } | ||
| 6038 | public PlObject hget_hashref(String i) { | ||
| 6039 | PlScalarImmutable o = this.get(); | ||
| 6040 | if (o.is_undef()) { | ||
| 6041 | o = new PlHashRef(); | ||
| 6042 | this.set(o); | ||
| 6043 | } | ||
| 6044 | return o.hget_hashref(i); | ||
| 6045 | } | ||
| 6046 | |||
| 6047 | public PlObject hset(String s, PlObject v) { | ||
| 6048 | PlScalarImmutable o = this.get(); | ||
| 6049 | if (o.is_undef()) { | ||
| 6050 | o = new PlHashRef(); | ||
| 6051 | this.set(o); | ||
| 6052 | } | ||
| 6053 | return o.hset(s, v); | ||
| 6054 | } | ||
| 6055 | public PlObject hset(String s, PlLvalue v) { | ||
| 6056 | PlScalarImmutable o = this.get(); | ||
| 6057 | if (o.is_undef()) { | ||
| 6058 | o = new PlHashRef(); | ||
| 6059 | this.set(o); | ||
| 6060 | } | ||
| 6061 | return o.hset(s, v.get()); | ||
| 6062 | } | ||
| 6063 | |||
| 6064 | public PlObject scalar_deref(String namespace) { | ||
| 6065 | PlScalarImmutable o = this.get(); | ||
| 6066 | if (o.is_undef()) { | ||
| 6067 | return new PlLazyScalarref(this); | ||
| 6068 | } | ||
| 6069 | return o.scalar_deref(namespace); | ||
| 6070 | } | ||
| 6071 | public PlObject scalar_deref_strict() { | ||
| 6072 | PlScalarImmutable o = this.get(); | ||
| 6073 | return o.scalar_deref_strict(); | ||
| 6074 | } | ||
| 6075 | public PlObject scalar_deref_lvalue(String namespace) { | ||
| 6076 | PlScalarImmutable o = this.get(); | ||
| 6077 | if (o.is_undef()) { | ||
| 6078 | PlLvalue lv = new PlLvalue(); | ||
| 6079 | this.set(new PlLvalueRef(lv)); | ||
| 6080 | return lv; | ||
| 6081 | } | ||
| 6082 | return o.scalar_deref_lvalue(namespace); | ||
| 6083 | } | ||
| 6084 | public PlObject scalar_deref_set(String namespace, PlObject v) { | ||
| 6085 | PlScalarImmutable o = this.get(); | ||
| 6086 | if (o.is_undef()) { | ||
| 6087 | PlLvalueRef ar = new PlLvalueRef(new PlLvalue()); | ||
| 6088 | this.set(ar); | ||
| 6089 | } | ||
| 6090 | return o.scalar_deref_set(namespace, v); | ||
| 6091 | } | ||
| 6092 | |||
| 6093 | public PlArray array_deref(String namespace) { | ||
| 6094 | // @$x doesn't autovivify | ||
| 6095 | PlScalarImmutable o = this.get(); | ||
| 6096 | if (o.is_undef()) { | ||
| 6097 | return new PlArray(); | ||
| 6098 | } | ||
| 6099 | else if (o.is_arrayref()) { | ||
| 6100 | return (PlArray)(o.array_deref(namespace)); | ||
| 6101 | } | ||
| 6102 | return o.array_deref(namespace); | ||
| 6103 | } | ||
| 6104 | public PlArray array_deref_strict() { | ||
| 6105 | // @$x doesn't autovivify | ||
| 6106 | PlScalarImmutable o = this.get(); | ||
| 6107 | return o.array_deref_strict(); | ||
| 6108 | } | ||
| 6109 | public PlArray array_deref_lvalue() { | ||
| 6110 | PlScalarImmutable o = this.get(); | ||
| 6111 | if (o.is_undef()) { | ||
| 6112 | PlArrayRef ar = new PlArrayRef(); | ||
| 6113 | this.set(ar); | ||
| 6114 | return (PlArray)(ar.array_deref_lvalue()); | ||
| 6115 | } | ||
| 6116 | else if (o.is_arrayref()) { | ||
| 6117 | return (PlArray)(o.array_deref_lvalue()); | ||
| 6118 | } | ||
| 6119 | return o.array_deref_strict(); | ||
| 6120 | } | ||
| 6121 | public PlObject array_deref_set(PlObject v) { | ||
| 6122 | // @$x = ... | ||
| 6123 | PlScalarImmutable o = this.get(); | ||
| 6124 | if (o.is_undef()) { | ||
| 6125 | o = new PlArrayRef(); | ||
| 6126 | this.set(o); | ||
| 6127 | return o.array_deref_set(v); | ||
| 6128 | } | ||
| 6129 | else if (o.is_arrayref()) { | ||
| 6130 | return o.array_deref_set(v); | ||
| 6131 | } | ||
| 6132 | return o.array_deref_set(v); | ||
| 6133 | } | ||
| 6134 | |||
| 6135 | public PlObject hash_deref(String namespace) { | ||
| 6136 | // %$x doesn't autovivify | ||
| 6137 | PlScalarImmutable o = this.get(); | ||
| 6138 | if (o.is_undef()) { | ||
| 6139 | return new PlHash(); | ||
| 6140 | } | ||
| 6141 | else if (o.is_hashref()) { | ||
| 6142 | return o.hash_deref(namespace); | ||
| 6143 | } | ||
| 6144 | return o.hash_deref(namespace); | ||
| 6145 | } | ||
| 6146 | public PlHash hash_deref_strict() { | ||
| 6147 | // %$x doesn't autovivify | ||
| 6148 | PlScalarImmutable o = this.get(); | ||
| 6149 | return o.hash_deref_strict(); | ||
| 6150 | } | ||
| 6151 | public PlObject hash_deref_set(PlObject v) { | ||
| 6152 | // %$x = ... | ||
| 6153 | PlScalarImmutable o = this.get(); | ||
| 6154 | if (o.is_undef()) { | ||
| 6155 | o = new PlHashRef(); | ||
| 6156 | this.set(o); | ||
| 6157 | return o.hash_deref_set(v); | ||
| 6158 | } | ||
| 6159 | else if (o.is_hashref()) { | ||
| 6160 | return o.hash_deref_set(v); | ||
| 6161 | } | ||
| 6162 | return o.hash_deref_set(v); | ||
| 6163 | } | ||
| 6164 | |||
| 6165 | // Note: several versions of set() | ||
| 6166 | public PlObject set(PlObject o) { | ||
| 6167 | if (o.is_lvalue()) { | ||
| 6168 | o = o.get(); | ||
| 6169 | } | ||
| 6170 | if (this.magic != null && this.magic.tied != null) { | ||
| 6171 | PerlOp.call("STORE", new PlArray(this.magic.tied, o), PlCx.VOID); | ||
| 6172 | return this; | ||
| 6173 | } | ||
| 6174 | this.o = (PlScalarImmutable)o; | ||
| 6175 | return this; | ||
| 6176 | } | ||
| 6177 | public PlObject set(PlScalarImmutable o) { | ||
| 6178 | if (this.magic != null && this.magic.tied != null) { | ||
| 6179 | PerlOp.call("STORE", new PlArray(this.magic.tied, o), PlCx.VOID); | ||
| 6180 | return this; | ||
| 6181 | } | ||
| 6182 | this.o = o; | ||
| 6183 | return this; | ||
| 6184 | } | ||
| 6185 | public PlObject set(PlLvalue o) { | ||
| 6186 | if (this.magic != null && this.magic.tied != null) { | ||
| 6187 | PerlOp.call("STORE", new PlArray(this.magic.tied, o), PlCx.VOID); | ||
| 6188 | return this; | ||
| 6189 | } | ||
| 6190 | this.o = o.get(); | ||
| 6191 | return this; | ||
| 6192 | } | ||
| 6193 | public PlObject set(PlArray o) { | ||
| 6194 | // $a = @x | ||
| 6195 | return this.set(o.scalar()); | ||
| 6196 | } | ||
| 6197 | public PlObject set(PlHash o) { | ||
| 6198 | // $a = %x | ||
| 6199 | return this.set(o.scalar()); | ||
| 6200 | } | ||
| 6201 | public PlObject set(Object o) { | ||
| 6202 | // $a = new Object() | ||
| 6203 | // $a = null | ||
| 6204 | if (o == null) { | ||
| 6205 | return this.set(PlCx.UNDEF); | ||
| 6206 | } | ||
| 6207 | return this.set(PlJavaObject.fromObject(o)); | ||
| 6208 | } | ||
| 6209 | public PlObject set(String s) { | ||
| 6210 | return this.set(new PlString(s)); | ||
| 6211 | } | ||
| 6212 | public PlObject set(boolean s) { | ||
| 6213 | return this.set(new PlBool(s)); | ||
| 6214 | } | ||
| 6215 | public PlObject set(double s) { | ||
| 6216 | return this.set(new PlDouble(s)); | ||
| 6217 | } | ||
| 6218 | public PlObject set(long s) { | ||
| 6219 | return this.set(new PlInt(s)); | ||
| 6220 | } | ||
| 6221 | |||
| 6222 | public PlScalarImmutable length() { | ||
| 6223 | return this.get().length(); | ||
| 6224 | } | ||
| 6225 | public PlObject apply(int want, PlArray List__) { | ||
| 6226 | return this.get().apply(want, List__); | ||
| 6227 | } | ||
| 6228 | public PlObject hexists(String a) { | ||
| 6229 | // exists $v->{$a} | ||
| 6230 | return this.get().hexists(a); | ||
| 6231 | } | ||
| 6232 | public PlObject hexists(PlObject a) { | ||
| 6233 | // exists $v->{$a} | ||
| 6234 | return this.get().hexists(a); | ||
| 6235 | } | ||
| 6236 | public PlObject aexists(PlObject a) { | ||
| 6237 | // exists $v->[$a] | ||
| 6238 | return this.get().aexists(a); | ||
| 6239 | } | ||
| 6240 | public PlObject hdelete(int want, PlObject a) { | ||
| 6241 | // delete $v->{$a} | ||
| 6242 | return this.get().hdelete(want, a); | ||
| 6243 | } | ||
| 6244 | public PlObject adelete(int want, PlObject a) { | ||
| 6245 | // delete $v->[$a] | ||
| 6246 | return this.get().adelete(want, a); | ||
| 6247 | } | ||
| 6248 | |||
| 6249 | public PlObject vecSet(PlObject pOffset, PlObject pBits, PlObject pValue) { | ||
| 6250 | // vec($i, 0, 32) = 0x5065726C | ||
| 6251 | String vv = this.toString(); | ||
| 6252 | int offset = pOffset.to_int(); | ||
| 6253 | if (offset < 0) { | ||
| 6254 | return PlCORE.die("Negative offset to vec in lvalue context: " + offset); | ||
| 6255 | } | ||
| 6256 | int bits = pBits.to_int(); | ||
| 6257 | long value = pValue.to_long(); | ||
| 6258 | StringBuilder sb = new StringBuilder(vv); | ||
| 6259 | if (bits == 1) { | ||
| 6260 | int byteOfs = offset / 8; | ||
| 6261 | int bitOfs = offset - 8 * byteOfs; | ||
| 6262 | value = (value & 0b0001) << bitOfs; | ||
| 6263 | long mask = 0b0001 << bitOfs; | ||
| 6264 | if (byteOfs < sb.length()) { | ||
| 6265 | value = (sb.charAt(byteOfs) & ~mask) | value; | ||
| 6266 | } | ||
| 6267 | // fallback to 8bit | ||
| 6268 | offset = byteOfs; | ||
| 6269 | bits = 8; | ||
| 6270 | } | ||
| 6271 | if (bits == 2) { | ||
| 6272 | int byteOfs = offset / 4; | ||
| 6273 | int bitOfs = 2 * (offset - 4 * byteOfs); | ||
| 6274 | value = (value & 0b0011) << bitOfs; | ||
| 6275 | long mask = 0b0011 << bitOfs; | ||
| 6276 | if (byteOfs < sb.length()) { | ||
| 6277 | value = (sb.charAt(byteOfs) & ~mask) | value; | ||
| 6278 | } | ||
| 6279 | // fallback to 8bit | ||
| 6280 | offset = byteOfs; | ||
| 6281 | bits = 8; | ||
| 6282 | } | ||
| 6283 | if (bits == 4) { | ||
| 6284 | int byteOfs = offset / 2; | ||
| 6285 | int bitOfs = 4 * (offset - 2 * byteOfs); | ||
| 6286 | value = (value & 0b1111) << bitOfs; | ||
| 6287 | long mask = 0b1111 << bitOfs; | ||
| 6288 | if (byteOfs < sb.length()) { | ||
| 6289 | value = (sb.charAt(byteOfs) & ~mask) | value; | ||
| 6290 | } | ||
| 6291 | // fallback to 8bit | ||
| 6292 | offset = byteOfs; | ||
| 6293 | bits = 8; | ||
| 6294 | } | ||
| 6295 | if (bits == 8) { | ||
| 6296 | if (offset >= sb.length()) { | ||
| 6297 | sb.setLength(offset + 1); | ||
| 6298 | } | ||
| 6299 | sb.setCharAt(offset, (char)(value & 0xFF)); | ||
| 6300 | return this.set(new PlString(sb.toString())); | ||
| 6301 | } | ||
| 6302 | if (bits == 16) { | ||
| 6303 | if ((offset + 1) >= sb.length()) { | ||
| 6304 | sb.setLength(offset + 2); | ||
| 6305 | } | ||
| 6306 | sb.setCharAt(offset, (char)((value >> 8) & 0xFF)); | ||
| 6307 | sb.setCharAt(offset + 1, (char)(value & 0xFF)); | ||
| 6308 | return this.set(new PlString(sb.toString())); | ||
| 6309 | } | ||
| 6310 | if (bits == 32) { | ||
| 6311 | if ((offset + 3) >= sb.length()) { | ||
| 6312 | sb.setLength(offset + 4); | ||
| 6313 | } | ||
| 6314 | sb.setCharAt(offset, (char)((value >> 24) & 0xFF)); | ||
| 6315 | sb.setCharAt(offset + 1, (char)((value >> 16) & 0xFF)); | ||
| 6316 | sb.setCharAt(offset + 2, (char)((value >> 8) & 0xFF)); | ||
| 6317 | sb.setCharAt(offset + 3, (char)(value & 0xFF)); | ||
| 6318 | return this.set(new PlString(sb.toString())); | ||
| 6319 | } | ||
| 6320 | return PlCORE.die("Illegal number of bits in vec: " + bits); | ||
| 6321 | } | ||
| 6322 | |||
| 6323 | public boolean is_lvalue() { | ||
| 6324 | return true; | ||
| 6325 | } | ||
| 6326 | public Object toJava() { | ||
| 6327 | PlScalarImmutable res = this.get(); | ||
| 6328 | if ( res.is_JavaObject() ) { | ||
| 6329 | return res.toJava(); | ||
| 6330 | } | ||
| 6331 | return res; | ||
| 6332 | } | ||
| 6333 | |||
| 6334 | public PlObject pre_decr() { | ||
| 6335 | // --$x | ||
| 6336 | PlScalarImmutable res = this.get(); | ||
| 6337 | return this.set((PlScalarImmutable)res._decr()); | ||
| 6338 | } | ||
| 6339 | public PlObject post_decr() { | ||
| 6340 | // $x-- | ||
| 6341 | PlScalarImmutable res = this.get(); | ||
| 6342 | this.set((PlScalarImmutable)res._decr()); | ||
| 6343 | return res; | ||
| 6344 | } | ||
| 6345 | public PlObject pre_incr() { | ||
| 6346 | // ++$x | ||
| 6347 | PlScalarImmutable res = this.get(); | ||
| 6348 | return this.set((PlScalarImmutable)res._incr()); | ||
| 6349 | } | ||
| 6350 | public PlObject post_incr() { | ||
| 6351 | // $x++ | ||
| 6352 | PlScalarImmutable res = this.get(); | ||
| 6353 | this.set((PlScalarImmutable)res._incr()); | ||
| 6354 | return res; | ||
| 6355 | } | ||
| 6356 | public PlObject bless(String className) { | ||
| 6357 | return this.get().bless(className); | ||
| 6358 | } | ||
| 6359 | |||
| 6360 | public PlObject set_prototype(PlObject v) { | ||
| 6361 | return this.get().set_prototype(v); | ||
| 6362 | } | ||
| 6363 | |||
| 6364 | public PlObject pow(PlObject arg) { return this.get().pow(arg); } | ||
| 6365 | public PlObject atan2(PlObject arg) { return this.get().atan2(arg); } | ||
| 6366 | |||
| 6367 | public PlScalarImmutable scalar() { | ||
| 6368 | return this.get(); | ||
| 6369 | } | ||
| 6370 | public PlObject clone() throws CloneNotSupportedException { | ||
| 6371 | return this.get().clone(); | ||
| 6372 | } | ||
| 6373 | |||
| 6374 | public PlObject self_assign_or(PlObject s) { | ||
| 6375 | return (this.to_boolean() ? this : this.set(s)); | ||
| 6376 | } | ||
| 6377 | public PlObject self_assign_and(PlObject s) { | ||
| 6378 | return (this.to_boolean() ? this.set(s) : this); | ||
| 6379 | } | ||
| 6380 | EOT | ||
| 6381 | , ((map { | ||
| 6382 | my $perl = $_; | ||
| 6383 | " public PlObject ${perl}(PlObject s) { | ||
| 6384 | if (this.magic != null && this.magic.tied != null) { | ||
| 6385 | PlScalarImmutable v = PerlOp.call(\"FETCH\", new PlArray(this.magic.tied), PlCx.VOID).scalar(); | ||
| 6386 | this.o = v; | ||
| 6387 | } | ||
| 6388 | return this.o.${perl}(s); | ||
| 6389 | } | ||
| 6390 | public PlObject ${perl}2(PlObject s) { | ||
| 6391 | if (this.magic != null && this.magic.tied != null) { | ||
| 6392 | PlScalarImmutable v = PerlOp.call(\"FETCH\", new PlArray(this.magic.tied), PlCx.VOID).scalar(); | ||
| 6393 | this.o = v; | ||
| 6394 | } | ||
| 6395 | return s.${perl}(this.o); | ||
| 6396 | } | ||
| 6397 | " | ||
| 6398 | } | ||
| 6399 | sort ( | ||
| 6400 | 'num_cmp', | ||
| 6401 | 'mod', | ||
| 6402 | keys %number_binop, | ||
| 6403 | ) | ||
| 6404 | )) | ||
| 6405 | |||
| 6406 | , ((map { | ||
| 6407 | my $perl = $_; | ||
| 6408 | " public PlObject self_assign_${perl}(PlObject s) { | ||
| 6409 | return this.set(this.get()._self_${perl}(s)); | ||
| 6410 | } | ||
| 6411 | " | ||
| 6412 | } | ||
| 6413 | sort ( 'string_replicate', | ||
| 6414 | keys %self_assign_number_binop, | ||
| 6415 | ), | ||
| 6416 | )) | ||
| 6417 | |||
| 6418 | , ((map { | ||
| 6419 | my $perl = $_; | ||
| 6420 | " public PlObject self_assign_${perl}(PlObject s) { | ||
| 6421 | return PlCORE.die(\"not implemented\"); | ||
| 6422 | } | ||
| 6423 | " | ||
| 6424 | } | ||
| 6425 | sort ( 'str_and', | ||
| 6426 | 'str_or', | ||
| 6427 | 'str_xor', | ||
| 6428 | ), | ||
| 6429 | )) | ||
| 6430 | |||
| 6431 | # unary operators | ||
| 6432 | # | ||
| 6433 | , ((map { | ||
| 6434 | my ($op, $type) = @$_; | ||
| 6435 | " public $type $op() { | ||
| 6436 | if (this.magic != null && this.magic.tied != null) { | ||
| 6437 | PlScalarImmutable v = PerlOp.call(\"FETCH\", new PlArray(this.magic.tied), PlCx.VOID).scalar(); | ||
| 6438 | this.o = v; | ||
| 6439 | } | ||
| 6440 | return this.o.$op(); | ||
| 6441 | } | ||
| 6442 | " | ||
| 6443 | } | ||
| 6444 | map( [ $_ => 'PlObject' ], ( | ||
| 6445 | @number_unary, | ||
| 6446 | 'blessed', | ||
| 6447 | 'refaddr', # Scalar::Util::refaddr() | ||
| 6448 | 'reftype', # Scalar::Util::reftype() | ||
| 6449 | 'to_num', | ||
| 6450 | )), | ||
| 6451 | map( [ $_ => 'boolean' ], ( | ||
| 6452 | @boolean_unary, | ||
| 6453 | 'is_integer_range', | ||
| 6454 | )), | ||
| 6455 | [ 'toString' => 'String' ], | ||
| 6456 | [ 'to_int' => 'int' ], | ||
| 6457 | [ 'to_long' => 'long' ], | ||
| 6458 | [ 'to_double' => 'double' ], | ||
| 6459 | [ 'to_boolean' => 'boolean' ], | ||
| 6460 | [ 'blessed_class' => 'PlClass' ], | ||
| 6461 | [ 'ref' => 'PlString' ], | ||
| 6462 | [ 'ref_str' => 'String' ], | ||
| 6463 | [ 'ref_boolean' => 'boolean' ], | ||
| 6464 | |||
| 6465 | # add "unbox" accessors to Java classes that were declared with: 'package MyJavaClass { Java }' | ||
| 6466 | (map { my $class = $java_classes{$_}; | ||
| 6467 | $class->{import} || $class->{extends} || $class->{implements} | ||
| 6468 | ? [ $class->{perl_to_java}, $class->{java_type} ] | ||
| 6469 | : () | ||
| 6470 | } | ||
| 6471 | grep { $java_classes{$_}{perl_to_java} } | ||
| 6472 | sort keys %java_classes | ||
| 6473 | ), | ||
| 6474 | )) | ||
| 6475 | |||
| 6476 | , <<'EOT' | ||
| 6477 | |||
| 6478 | } | ||
| 6479 | |||
| 6480 | EOT | ||
| 6481 | , # list break | ||
| 6482 | <<'EOT' | ||
| 6483 | |||
| 6484 | class PlROvalue extends PlLvalue { | ||
| 6485 | |||
| 6486 | // Note: several versions of PlROvalue() | ||
| 6487 | public PlROvalue() { | ||
| 6488 | this.o = PlCx.UNDEF; | ||
| 6489 | } | ||
| 6490 | public PlROvalue(PlObject o) { | ||
| 6491 | this.o = (PlScalarImmutable)o; | ||
| 6492 | } | ||
| 6493 | public PlROvalue(PlScalarImmutable o) { | ||
| 6494 | this.o = o; | ||
| 6495 | } | ||
| 6496 | public PlROvalue(PlLvalue o) { | ||
| 6497 | this.o = o.get(); | ||
| 6498 | } | ||
| 6499 | public PlROvalue(PlArray o) { | ||
| 6500 | // $a = @x | ||
| 6501 | this.o = o.scalar(); | ||
| 6502 | } | ||
| 6503 | public PlROvalue(PlHash o) { | ||
| 6504 | // $a = %x | ||
| 6505 | this.o = o.scalar(); | ||
| 6506 | } | ||
| 6507 | |||
| 6508 | public PlObject set(Object o) { | ||
| 6509 | PlCORE.die("Modification of a read-only value attempted"); | ||
| 6510 | return this; | ||
| 6511 | } | ||
| 6512 | public PlObject set(PlScalarImmutable o) { | ||
| 6513 | return PlCORE.die("Modification of a read-only value attempted"); | ||
| 6514 | } | ||
| 6515 | public PlObject set(PlLvalue o) { | ||
| 6516 | return PlCORE.die("Modification of a read-only value attempted"); | ||
| 6517 | } | ||
| 6518 | |||
| 6519 | public PlObject pre_decr() { | ||
| 6520 | return PlCORE.die("Modification of a read-only value attempted"); | ||
| 6521 | } | ||
| 6522 | public PlObject post_decr() { | ||
| 6523 | return PlCORE.die("Modification of a read-only value attempted"); | ||
| 6524 | } | ||
| 6525 | public PlObject pre_incr() { | ||
| 6526 | return PlCORE.die("Modification of a read-only value attempted"); | ||
| 6527 | } | ||
| 6528 | public PlObject post_incr() { | ||
| 6529 | return PlCORE.die("Modification of a read-only value attempted"); | ||
| 6530 | } | ||
| 6531 | |||
| 6532 | } | ||
| 6533 | class PlSlice extends PlArray { | ||
| 6534 | public PlSlice( PlArrayList a ) { | ||
| 6535 | this.each_iterator = 0; | ||
| 6536 | this.a = a; | ||
| 6537 | } | ||
| 6538 | |||
| 6539 | public boolean is_slice() { | ||
| 6540 | return true; | ||
| 6541 | } | ||
| 6542 | } | ||
| 6543 | |||
| 6544 | class PlTieArrayIterator implements Iterator<PlObject> { | ||
| 6545 | public PlObject tied; | ||
| 6546 | private int key; | ||
| 6547 | |||
| 6548 | public PlTieArrayIterator(PlObject tied) { | ||
| 6549 | this.tied = tied; | ||
| 6550 | } | ||
| 6551 | public PlObject next() { | ||
| 6552 | return PerlOp.call("FETCH", new PlArray(this.tied, new PlInt(this.key)), PlCx.SCALAR); | ||
| 6553 | } | ||
| 6554 | public boolean hasNext() { | ||
| 6555 | return this.key < PerlOp.call("FETCHSIZE", new PlArray(this.tied), PlCx.SCALAR).to_int(); | ||
| 6556 | } | ||
| 6557 | } | ||
| 6558 | class PlTieArrayList extends PlArrayList { | ||
| 6559 | public PlObject tied; | ||
| 6560 | public PlArrayList old_var; | ||
| 6561 | |||
| 6562 | public PlTieArrayList() { | ||
| 6563 | } | ||
| 6564 | // add(PlObject) | ||
| 6565 | // add(pos, PlObject) | ||
| 6566 | // get(pos) | ||
| 6567 | // remove(pos) | ||
| 6568 | // set(pos, PlObject) | ||
| 6569 | // size() | ||
| 6570 | // clear() | ||
| 6571 | // iterator() | ||
| 6572 | |||
| 6573 | public boolean add(PlObject v) { | ||
| 6574 | PerlOp.call("PUSH", new PlArray(this.tied, v), PlCx.SCALAR); | ||
| 6575 | return true; | ||
| 6576 | } | ||
| 6577 | public void add(int i, PlObject v) { | ||
| 6578 | if (i == 0) { | ||
| 6579 | PerlOp.call("UNSHIFT", new PlArray(tied, v), PlCx.SCALAR); | ||
| 6580 | } | ||
| 6581 | else { | ||
| 6582 | PerlOp.call("PUSH", new PlArray(tied, v), PlCx.SCALAR); | ||
| 6583 | } | ||
| 6584 | } | ||
| 6585 | public PlScalarImmutable get(int i) { | ||
| 6586 | return PerlOp.call("FETCH", new PlArray(tied, new PlInt(i)), PlCx.SCALAR).scalar(); | ||
| 6587 | } | ||
| 6588 | public PlObject remove(int i) { | ||
| 6589 | if (i == 0) { | ||
| 6590 | return PerlOp.call("SHIFT", new PlArray(tied), PlCx.SCALAR); | ||
| 6591 | } | ||
| 6592 | return PerlOp.call("POP", new PlArray(tied), PlCx.SCALAR); | ||
| 6593 | } | ||
| 6594 | public PlObject set(int i, PlObject v) { | ||
| 6595 | return PerlOp.call("STORE", new PlArray(tied, new PlInt(i), v), PlCx.SCALAR); | ||
| 6596 | } | ||
| 6597 | public int size() { | ||
| 6598 | return PerlOp.call("FETCHSIZE", new PlArray(tied), PlCx.SCALAR).to_int(); | ||
| 6599 | } | ||
| 6600 | public void clear() { | ||
| 6601 | PerlOp.call("STORESIZE", new PlArray(tied, PlCx.INT0), PlCx.SCALAR); | ||
| 6602 | } | ||
| 6603 | public Iterator<PlObject> iterator() { | ||
| 6604 | return new PlTieArrayIterator(this.tied); | ||
| 6605 | } | ||
| 6606 | |||
| 6607 | // Perl API | ||
| 6608 | // add == PUSH | ||
| 6609 | // add(0, v) == UNSHIFT | ||
| 6610 | |||
| 6611 | public PlObject aexists(PlObject i) { | ||
| 6612 | return PerlOp.call("EXISTS", new PlArray(tied, i), PlCx.SCALAR); | ||
| 6613 | } | ||
| 6614 | public PlObject adelete(int want, PlObject i) { | ||
| 6615 | return PerlOp.call("DELETE", new PlArray(tied, i), want); | ||
| 6616 | } | ||
| 6617 | |||
| 6618 | public PlObject set_end_of_array_index(int i) { | ||
| 6619 | return PerlOp.call("STORESIZE", new PlArray(tied), PlCx.SCALAR); | ||
| 6620 | } | ||
| 6621 | public PlObject aset(int i, PlObject v) { | ||
| 6622 | this.set(i, v); | ||
| 6623 | return v; | ||
| 6624 | } | ||
| 6625 | public PlObject aget(int i) { | ||
| 6626 | return this.get(i); | ||
| 6627 | } | ||
| 6628 | public PlObject shift() { | ||
| 6629 | return PerlOp.call("SHIFT", new PlArray(tied), PlCx.SCALAR); | ||
| 6630 | } | ||
| 6631 | public PlObject pop() { | ||
| 6632 | return PerlOp.call("POP", new PlArray(tied), PlCx.SCALAR); | ||
| 6633 | } | ||
| 6634 | |||
| 6635 | public PlScalarImmutable scalar() { | ||
| 6636 | return PerlOp.call("SCALAR", new PlArray(this.tied), PlCx.SCALAR).scalar(); | ||
| 6637 | } | ||
| 6638 | public boolean is_tiedArray() { | ||
| 6639 | return true; | ||
| 6640 | } | ||
| 6641 | public PlObject tied() { | ||
| 6642 | return this.tied; | ||
| 6643 | } | ||
| 6644 | } // PlTieArrayList | ||
| 6645 | class PlArrayList extends ArrayList<PlObject> implements Iterable<PlObject> { | ||
| 6646 | public PlArrayList() { | ||
| 6647 | } | ||
| 6648 | // add(PlObject) | ||
| 6649 | // add(pos, PlObject) | ||
| 6650 | // get(pos) | ||
| 6651 | // remove(pos) | ||
| 6652 | // set(pos, PlObject) | ||
| 6653 | // size() | ||
| 6654 | // clear() | ||
| 6655 | // iterator() | ||
| 6656 | |||
| 6657 | // Perl API | ||
| 6658 | |||
| 6659 | public PlObject aexists(PlObject i) { | ||
| 6660 | int pos = i.to_int(); | ||
| 6661 | if (pos < 0) { | ||
| 6662 | pos = this.size() + pos; | ||
| 6663 | } | ||
| 6664 | if (pos < 0 || pos >= this.size()) { | ||
| 6665 | return PlCx.FALSE; | ||
| 6666 | } | ||
| 6667 | return PlCx.TRUE; | ||
| 6668 | } | ||
| 6669 | public PlObject adelete(int want, PlObject i) { | ||
| 6670 | int pos = i.to_int(); | ||
| 6671 | if (pos < 0) { | ||
| 6672 | pos = this.size() + pos; | ||
| 6673 | } | ||
| 6674 | if ((pos+1) == this.size()) { | ||
| 6675 | return this.pop(); | ||
| 6676 | } | ||
| 6677 | if (pos < 0 || pos >= this.size()) { | ||
| 6678 | return PlCx.FALSE; | ||
| 6679 | } | ||
| 6680 | PlObject res = this.aget(pos); | ||
| 6681 | this.aset(pos, PlCx.UNDEF); | ||
| 6682 | return res; | ||
| 6683 | } | ||
| 6684 | |||
| 6685 | public PlObject set_end_of_array_index(int i) { | ||
| 6686 | int size = i + 1; | ||
| 6687 | while (this.size() < size) { | ||
| 6688 | this.add(PlCx.UNDEF); | ||
| 6689 | } | ||
| 6690 | if (size < this.size() && this.size() > 0) { | ||
| 6691 | this.removeRange(size, this.size()); | ||
| 6692 | } | ||
| 6693 | return new PlInt(this.size()); | ||
| 6694 | } | ||
| 6695 | public PlObject aset(int i, PlObject v) { | ||
| 6696 | int size = this.size(); | ||
| 6697 | int pos = i; | ||
| 6698 | if (pos < 0) { | ||
| 6699 | pos = size + pos; | ||
| 6700 | } | ||
| 6701 | if (size <= pos) { | ||
| 6702 | while (size < pos) { | ||
| 6703 | this.add( PlCx.UNDEF ); | ||
| 6704 | size++; | ||
| 6705 | } | ||
| 6706 | this.add(v.scalar()); | ||
| 6707 | return v; | ||
| 6708 | } | ||
| 6709 | PlObject old = this.get(pos); | ||
| 6710 | if (old.is_lvalue()) { | ||
| 6711 | old.set(v.scalar()); | ||
| 6712 | } | ||
| 6713 | else { | ||
| 6714 | this.set(pos, v.scalar()); | ||
| 6715 | } | ||
| 6716 | return v; | ||
| 6717 | } | ||
| 6718 | public PlObject aget(int pos) { | ||
| 6719 | if (pos < 0) { | ||
| 6720 | pos = this.size() + pos; | ||
| 6721 | } | ||
| 6722 | if (pos < 0 || pos >= this.size()) { | ||
| 6723 | return PlCx.UNDEF; | ||
| 6724 | } | ||
| 6725 | return this.get(pos); | ||
| 6726 | } | ||
| 6727 | public PlObject shift() { | ||
| 6728 | int size = this.size(); | ||
| 6729 | if (size > 0) { | ||
| 6730 | return this.remove(0); | ||
| 6731 | } | ||
| 6732 | else { | ||
| 6733 | return PlCx.UNDEF; | ||
| 6734 | } | ||
| 6735 | } | ||
| 6736 | public PlObject pop() { | ||
| 6737 | int size = this.size() - 1; | ||
| 6738 | if (size >= 0) { | ||
| 6739 | return this.remove(size); | ||
| 6740 | } | ||
| 6741 | else { | ||
| 6742 | return PlCx.UNDEF; | ||
| 6743 | } | ||
| 6744 | } | ||
| 6745 | |||
| 6746 | public PlScalarImmutable scalar() { | ||
| 6747 | return new PlInt(this.hashCode()); | ||
| 6748 | } | ||
| 6749 | public boolean is_tiedArray() { | ||
| 6750 | return false; | ||
| 6751 | } | ||
| 6752 | public PlObject tied() { | ||
| 6753 | return PlCx.UNDEF; | ||
| 6754 | } | ||
| 6755 | } | ||
| 6756 | class PlArrayLvalueIterator implements Iterator<PlObject> { | ||
| 6757 | private final PlArray ar; | ||
| 6758 | private final PlArrayList a; | ||
| 6759 | private int pos; | ||
| 6760 | public PlArrayLvalueIterator(PlArray ar) { | ||
| 6761 | this.ar = ar; | ||
| 6762 | this.a = ar.a; | ||
| 6763 | this.pos = 0; | ||
| 6764 | } | ||
| 6765 | public PlObject next() { | ||
| 6766 | PlObject o = this.a.get(pos); | ||
| 6767 | if (o == null) { | ||
| 6768 | return new PlLazyIndex(this.ar, pos++); | ||
| 6769 | } | ||
| 6770 | if (o.is_lvalue()) { | ||
| 6771 | pos++; | ||
| 6772 | return o; | ||
| 6773 | } | ||
| 6774 | PlLvalue la = new PlLvalue(o); | ||
| 6775 | this.a.set(pos++, la); | ||
| 6776 | return la; | ||
| 6777 | } | ||
| 6778 | public boolean hasNext() { | ||
| 6779 | return pos < this.a.size(); | ||
| 6780 | } | ||
| 6781 | } | ||
| 6782 | class PlArray extends PlObject implements Iterable<PlObject> { | ||
| 6783 | public PlArrayList a; | ||
| 6784 | public int each_iterator; | ||
| 6785 | |||
| 6786 | public Iterator<PlObject> iterator() { | ||
| 6787 | return new PlArrayLvalueIterator(this); | ||
| 6788 | } | ||
| 6789 | |||
| 6790 | public PlArray( PlArrayList a ) { | ||
| 6791 | this.each_iterator = 0; | ||
| 6792 | this.a = a; | ||
| 6793 | } | ||
| 6794 | public PlArray() { | ||
| 6795 | this.each_iterator = 0; | ||
| 6796 | this.a = new PlArrayList(); | ||
| 6797 | } | ||
| 6798 | public PlArray(PlObject... args) { | ||
| 6799 | PlArrayList aa = new PlArrayList(); | ||
| 6800 | for (PlObject s : args) { | ||
| 6801 | if (s.is_hash()) { | ||
| 6802 | // @x = %x; | ||
| 6803 | s = s.to_array(); | ||
| 6804 | } | ||
| 6805 | if (s.is_array()) { | ||
| 6806 | // @x = ( @x, @y ); | ||
| 6807 | for (int i = 0; i < s.to_long(); i++) { | ||
| 6808 | PlObject v = s.aget(i); | ||
| 6809 | if (v.is_lvalue()) { | ||
| 6810 | v = v.get(); | ||
| 6811 | } | ||
| 6812 | aa.add(v); | ||
| 6813 | } | ||
| 6814 | } | ||
| 6815 | else if (s.is_lvalue()) { | ||
| 6816 | aa.add(s.get()); | ||
| 6817 | } | ||
| 6818 | else { | ||
| 6819 | aa.add(s); | ||
| 6820 | } | ||
| 6821 | } | ||
| 6822 | this.each_iterator = 0; | ||
| 6823 | this.a = aa; | ||
| 6824 | } | ||
| 6825 | |||
| 6826 | // TODO - this is part of a possible optimization to: split("", $str) | ||
| 6827 | // public PlArray(char... args) { | ||
| 6828 | // PlArrayList aa = new PlArrayList(); | ||
| 6829 | // for (char s : args) { | ||
| 6830 | // aa.add(new PlString(s)); | ||
| 6831 | // } | ||
| 6832 | // this.each_iterator = 0; | ||
| 6833 | // this.a = aa; | ||
| 6834 | // } | ||
| 6835 | |||
| 6836 | // tie hash | ||
| 6837 | public PlObject tie(PlArray args) { | ||
| 6838 | if (this.a.is_tiedArray()) { | ||
| 6839 | this.untie(); | ||
| 6840 | } | ||
| 6841 | PlTieArrayList v = new PlTieArrayList(); | ||
| 6842 | PlObject self = PerlOp.call("TIEARRAY", args, PlCx.VOID); | ||
| 6843 | v.tied = self; | ||
| 6844 | v.old_var = this.a; | ||
| 6845 | this.a = v; | ||
| 6846 | return self; | ||
| 6847 | } | ||
| 6848 | |||
| 6849 | public PlObject untie() { | ||
| 6850 | if (this.a.is_tiedArray()) { | ||
| 6851 | PlObject tied = this.a.tied(); | ||
| 6852 | PlObject untie = PerlOp.call("can", new PlArray(tied, new PlString("UNTIE")), PlCx.SCALAR); | ||
| 6853 | if (untie.to_boolean()) { | ||
| 6854 | untie.apply(PlCx.VOID, new PlArray(tied)); | ||
| 6855 | }; | ||
| 6856 | this.a = ((PlTieArrayList)a).old_var; | ||
| 6857 | return tied; | ||
| 6858 | } | ||
| 6859 | return this; | ||
| 6860 | } | ||
| 6861 | public PlObject tied() { | ||
| 6862 | return a.tied(); | ||
| 6863 | } | ||
| 6864 | |||
| 6865 | // internal lazy api | ||
| 6866 | public PlLvalue create_scalar(int i) { | ||
| 6867 | int size = this.a.size(); | ||
| 6868 | int pos = i; | ||
| 6869 | if (pos < 0) { | ||
| 6870 | return (PlLvalue)PlCORE.die("internal error: negative index on PlArray.create_scalar()"); | ||
| 6871 | } | ||
| 6872 | if (size <= pos) { | ||
| 6873 | while (size < pos) { | ||
| 6874 | this.a.add( PlCx.UNDEF ); | ||
| 6875 | size++; | ||
| 6876 | } | ||
| 6877 | PlLvalue v = new PlLvalue(); | ||
| 6878 | this.a.add(v); | ||
| 6879 | return v; | ||
| 6880 | } | ||
| 6881 | PlObject old = this.a.get(pos); | ||
| 6882 | if (old.is_lvalue()) { | ||
| 6883 | return (PlLvalue)old; | ||
| 6884 | } | ||
| 6885 | if (old.is_undef()) { | ||
| 6886 | PlLvalue v = new PlLvalue(); | ||
| 6887 | this.a.set(pos, v); | ||
| 6888 | return v; | ||
| 6889 | } | ||
| 6890 | return (PlLvalue)PlCORE.die("Not a SCALAR reference"); | ||
| 6891 | } | ||
| 6892 | |||
| 6893 | public static PlArray construct_list_of_aliases(PlScalarImmutable s) { | ||
| 6894 | PlArrayList aa = new PlArrayList(); | ||
| 6895 | aa.add(new PlROvalue(s)); // store lvalue as read-only | ||
| 6896 | return new PlArray(aa); | ||
| 6897 | } | ||
| 6898 | public static PlArray construct_list_of_aliases(PlScalarImmutable s1, PlScalarImmutable s2) { | ||
| 6899 | PlArrayList aa = new PlArrayList(); | ||
| 6900 | aa.add(new PlROvalue(s1)); // store lvalue as read-only | ||
| 6901 | aa.add(new PlROvalue(s2)); // store lvalue as read-only | ||
| 6902 | return new PlArray(aa); | ||
| 6903 | } | ||
| 6904 | public static PlArray construct_list_of_aliases(PlScalarImmutable... args) { | ||
| 6905 | PlArrayList aa = new PlArrayList(); | ||
| 6906 | for (PlObject s : args) { | ||
| 6907 | aa.add(new PlROvalue(s)); // store "read only" | ||
| 6908 | } | ||
| 6909 | return new PlArray(aa); | ||
| 6910 | } | ||
| 6911 | |||
| 6912 | public static PlArray construct_list_of_aliases(PlLvalue s) { | ||
| 6913 | PlArrayList aa = new PlArrayList(); | ||
| 6914 | aa.add(s); // store lvalue as-is | ||
| 6915 | return new PlArray(aa); | ||
| 6916 | } | ||
| 6917 | public static PlArray construct_list_of_aliases(PlLvalue s1, PlLvalue s2) { | ||
| 6918 | PlArrayList aa = new PlArrayList(); | ||
| 6919 | aa.add(s1); // store lvalue as-is | ||
| 6920 | aa.add(s2); // store lvalue as-is | ||
| 6921 | return new PlArray(aa); | ||
| 6922 | } | ||
| 6923 | |||
| 6924 | public static PlArray construct_list_of_aliases(PlArray s) { | ||
| 6925 | PlArrayList aa = new PlArrayList(); | ||
| 6926 | int ll = s.to_int(); | ||
| 6927 | for (int i = 0; i < ll; i++) { | ||
| 6928 | aa.add(s.aget_lvalue(i)); | ||
| 6929 | } | ||
| 6930 | return new PlArray(aa); | ||
| 6931 | } | ||
| 6932 | public static PlArray construct_list_of_aliases(PlObject s) { | ||
| 6933 | PlArrayList aa = new PlArrayList(); | ||
| 6934 | if (s.is_lvalue()) { | ||
| 6935 | aa.add(s); // store lvalue as-is | ||
| 6936 | } | ||
| 6937 | else if (s.is_hash()) { | ||
| 6938 | // ( %x ); | ||
| 6939 | s = ((PlHash)s).to_list_of_aliases(); | ||
| 6940 | for (int i = 0; i < s.to_long(); i++) { | ||
| 6941 | aa.add(s.aget_lvalue(i)); | ||
| 6942 | } | ||
| 6943 | } | ||
| 6944 | else if (s.is_array()) { | ||
| 6945 | // ( @x, @y ); | ||
| 6946 | int ll = s.to_int(); | ||
| 6947 | for (int i = 0; i < ll; i++) { | ||
| 6948 | aa.add(s.aget_lvalue(i)); | ||
| 6949 | } | ||
| 6950 | } | ||
| 6951 | else { | ||
| 6952 | aa.add(new PlROvalue(s)); // store "read only" | ||
| 6953 | } | ||
| 6954 | return new PlArray(aa); | ||
| 6955 | } | ||
| 6956 | public static PlArray construct_list_of_aliases(PlObject... args) { | ||
| 6957 | PlArrayList aa = new PlArrayList(); | ||
| 6958 | for (PlObject s : args) { | ||
| 6959 | if (s.is_lvalue()) { | ||
| 6960 | aa.add(s); // store lvalue as-is | ||
| 6961 | } | ||
| 6962 | else if (s.is_hash()) { | ||
| 6963 | // ( %x ); | ||
| 6964 | s = ((PlHash)s).to_list_of_aliases(); | ||
| 6965 | for (int i = 0; i < s.to_long(); i++) { | ||
| 6966 | aa.add(s.aget_lvalue(i)); | ||
| 6967 | } | ||
| 6968 | } | ||
| 6969 | else if (s.is_array()) { | ||
| 6970 | // ( @x, @y ); | ||
| 6971 | int ll = s.to_int(); | ||
| 6972 | for (int i = 0; i < ll; i++) { | ||
| 6973 | aa.add(s.aget_lvalue(i)); | ||
| 6974 | } | ||
| 6975 | } | ||
| 6976 | else { | ||
| 6977 | aa.add(new PlROvalue(s)); // store "read only" | ||
| 6978 | } | ||
| 6979 | } | ||
| 6980 | return new PlArray(aa); | ||
| 6981 | } | ||
| 6982 | public static PlArray construct_list_of_aliases(Object... args) { | ||
| 6983 | PlArrayList aa = new PlArrayList(); | ||
| 6984 | for (Object s1 : args) { | ||
| 6985 | PlObject s = PlJavaObject.fromObject(s1); | ||
| 6986 | if (s.is_lvalue()) { | ||
| 6987 | aa.add(s); // store lvalue as-is | ||
| 6988 | } | ||
| 6989 | else if (s.is_hash()) { | ||
| 6990 | // ( %x ); | ||
| 6991 | s = ((PlHash)s).to_list_of_aliases(); | ||
| 6992 | for (int i = 0; i < s.to_long(); i++) { | ||
| 6993 | aa.add(s.aget_lvalue(i)); | ||
| 6994 | } | ||
| 6995 | } | ||
| 6996 | else if (s.is_array()) { | ||
| 6997 | // ( @x, @y ); | ||
| 6998 | int ll = s.to_int(); | ||
| 6999 | for (int i = 0; i < ll; i++) { | ||
| 7000 | aa.add(s.aget_lvalue(i)); | ||
| 7001 | } | ||
| 7002 | } | ||
| 7003 | else { | ||
| 7004 | aa.add(new PlROvalue(s)); // store "read only" | ||
| 7005 | } | ||
| 7006 | } | ||
| 7007 | return new PlArray(aa); | ||
| 7008 | } | ||
| 7009 | |||
| 7010 | public static PlArray construct_list_of_references(PlObject... args) { | ||
| 7011 | PlArray aa = PlArray.construct_list_of_aliases(args); | ||
| 7012 | PlArray result = new PlArray(); | ||
| 7013 | for (PlObject s : aa) { | ||
| 7014 | result.push_void(new PlLvalueRef(s)); | ||
| 7015 | } | ||
| 7016 | return result; | ||
| 7017 | } | ||
| 7018 | |||
| 7019 | public static void static_list_set(PlObject src, PlLvalue... args) { | ||
| 7020 | int count = 0; | ||
| 7021 | for (PlLvalue s : args) { | ||
| 7022 | s.set(src.aget(count++)); | ||
| 7023 | } | ||
| 7024 | } | ||
| 7025 | public static void static_list_set(PlObject src_list, PlObject... args) { | ||
| 7026 | PlArray src = new PlArray(src_list); | ||
| 7027 | for (PlObject s : args) { | ||
| 7028 | if (s.is_hash()) { | ||
| 7029 | // ( %x ); | ||
| 7030 | ((PlHash)s).set(PlCx.VOID, src); | ||
| 7031 | src = new PlArray(); | ||
| 7032 | } | ||
| 7033 | else if (s.is_slice()) { | ||
| 7034 | // ( @x[3,4] ); - "slice" is not "slurpy" | ||
| 7035 | int s_size = s.to_int(); | ||
| 7036 | for (int i = 0; i < s_size; i++) { | ||
| 7037 | s.aset(i, src.shift()); | ||
| 7038 | } | ||
| 7039 | } | ||
| 7040 | else if (s.is_array()) { | ||
| 7041 | // ( @x ); - "array" is "slurpy" | ||
| 7042 | s.set(src); | ||
| 7043 | src = new PlArray(); | ||
| 7044 | } | ||
| 7045 | else if (s.is_lvalue()) { | ||
| 7046 | PlObject o = src.shift(); | ||
| 7047 | s.set(o); | ||
| 7048 | } | ||
| 7049 | else if (s.is_undef()) { | ||
| 7050 | src.shift(); // skip | ||
| 7051 | } | ||
| 7052 | else { | ||
| 7053 | PlCORE.die("Can't modify constant item in list assignment"); | ||
| 7054 | } | ||
| 7055 | } | ||
| 7056 | } | ||
| 7057 | |||
| 7058 | public static PlObject static_list_set(int want, PlObject src, PlLvalue... args) { | ||
| 7059 | int count = 0; | ||
| 7060 | for (PlLvalue s : args) { | ||
| 7061 | s.set(src.aget(count++)); | ||
| 7062 | } | ||
| 7063 | if (want == PlCx.VOID) { | ||
| 7064 | return PlCx.UNDEF; | ||
| 7065 | } | ||
| 7066 | if (want == PlCx.LIST) { | ||
| 7067 | return PlArray.construct_list_of_aliases(args); | ||
| 7068 | } | ||
| 7069 | return src.scalar(); | ||
| 7070 | } | ||
| 7071 | public static PlObject static_list_set(int want, PlObject src_list, PlObject... args) { | ||
| 7072 | PlArray src = new PlArray(src_list); | ||
| 7073 | for (PlObject s : args) { | ||
| 7074 | if (s.is_hash()) { | ||
| 7075 | // ( %x ); | ||
| 7076 | ((PlHash)s).set(PlCx.VOID, src); | ||
| 7077 | src = new PlArray(); | ||
| 7078 | } | ||
| 7079 | else if (s.is_slice()) { | ||
| 7080 | // ( @x[3,4] ); - "slice" is not "slurpy" | ||
| 7081 | int s_size = s.to_int(); | ||
| 7082 | for (int i = 0; i < s_size; i++) { | ||
| 7083 | s.aset(i, src.shift()); | ||
| 7084 | } | ||
| 7085 | } | ||
| 7086 | else if (s.is_array()) { | ||
| 7087 | // ( @x ); - "array" is "slurpy" | ||
| 7088 | s.set(src); | ||
| 7089 | src = new PlArray(); | ||
| 7090 | } | ||
| 7091 | else if (s.is_lvalue()) { | ||
| 7092 | PlObject o = src.shift(); | ||
| 7093 | s.set(o); | ||
| 7094 | } | ||
| 7095 | else if (s.is_undef()) { | ||
| 7096 | src.shift(); // skip | ||
| 7097 | } | ||
| 7098 | else { | ||
| 7099 | PlCORE.die("Can't modify constant item in list assignment"); | ||
| 7100 | } | ||
| 7101 | } | ||
| 7102 | if (want == PlCx.VOID) { | ||
| 7103 | return PlCx.UNDEF; | ||
| 7104 | } | ||
| 7105 | if (want == PlCx.LIST) { | ||
| 7106 | return PlArray.construct_list_of_aliases(args); | ||
| 7107 | } | ||
| 7108 | return src_list.scalar(); | ||
| 7109 | } | ||
| 7110 | public PlObject list_set(int want, PlArray s) { | ||
| 7111 | // @x[3,4] = ( @x, @y ); | ||
| 7112 | for (int i = 0; i < this.to_long(); i++) { | ||
| 7113 | this.aset(i, s.aget(i)); | ||
| 7114 | } | ||
| 7115 | this.each_iterator = 0; | ||
| 7116 | if (want == PlCx.LIST) { | ||
| 7117 | return this; | ||
| 7118 | } | ||
| 7119 | return this.pop(); | ||
| 7120 | } | ||
| 7121 | |||
| 7122 | public PlObject set(PlObject s) { | ||
| 7123 | this.a.clear(); | ||
| 7124 | PlObject tmp; | ||
| 7125 | if (s.is_hash()) { | ||
| 7126 | // @x = %x; | ||
| 7127 | s = s.to_array(); | ||
| 7128 | } | ||
| 7129 | if (s.is_array()) { | ||
| 7130 | // @x = ( @x, @y ); | ||
| 7131 | for (int i = 0; i < s.to_long(); i++) { | ||
| 7132 | tmp = s.aget(i); | ||
| 7133 | if (tmp.is_lvalue()) { | ||
| 7134 | this.a.add(tmp.get()); | ||
| 7135 | } | ||
| 7136 | else { | ||
| 7137 | this.a.add(tmp); | ||
| 7138 | } | ||
| 7139 | } | ||
| 7140 | } | ||
| 7141 | else { | ||
| 7142 | this.a.add(s); | ||
| 7143 | } | ||
| 7144 | this.each_iterator = 0; | ||
| 7145 | return this; | ||
| 7146 | } | ||
| 7147 | |||
| 7148 | public PlObject set(Map<String, String> env) { | ||
| 7149 | this.a.clear(); | ||
| 7150 | for (String envName : env.keySet()) { | ||
| 7151 | this.a.add(new PlString(envName)); | ||
| 7152 | this.a.add(new PlString(env.get(envName))); | ||
| 7153 | } | ||
| 7154 | this.each_iterator = 0; | ||
| 7155 | return this; | ||
| 7156 | } | ||
| 7157 | public PlArray(Map<String, String> strings) { | ||
| 7158 | PlArray arr = new PlArray(); | ||
| 7159 | arr.set(strings); | ||
| 7160 | this.each_iterator = arr.each_iterator; | ||
| 7161 | this.a = arr.a; | ||
| 7162 | } | ||
| 7163 | |||
| 7164 | EOT | ||
| 7165 | # add "box" array-of Java classes | ||
| 7166 | # that were declared with | ||
| 7167 | # | ||
| 7168 | # package MyJavaClass { Java } | ||
| 7169 | # | ||
| 7170 | , ((map { | ||
| 7171 | my $native = $_; | ||
| 7172 | my $perl = $native_to_perl{$native}; | ||
| 7173 | $native && $perl ? | ||
| 7174 | " public PlObject set(${native}[] stuffs) { | ||
| 7175 | this.a.clear(); | ||
| 7176 | // \@x = ${native}[] native; | ||
| 7177 | for(${native} i : stuffs){ | ||
| 7178 | this.a.add(new ${perl}(i)); | ||
| 7179 | } | ||
| 7180 | this.each_iterator = 0; | ||
| 7181 | return this; | ||
| 7182 | } | ||
| 7183 | public PlArray(${native}[] stuffs) { | ||
| 7184 | PlArray aa = new PlArray(); | ||
| 7185 | aa.set(stuffs); | ||
| 7186 | this.each_iterator = aa.each_iterator; | ||
| 7187 | this.a = aa.a; | ||
| 7188 | } | ||
| 7189 | " : () | ||
| 7190 | } | ||
| 7191 | sort keys %native_to_perl )) | ||
| 7192 | |||
| 7193 | , <<'EOT' | ||
| 7194 | public PlObject aget(int i) { | ||
| 7195 | return this.a.aget(i); | ||
| 7196 | } | ||
| 7197 | public PlObject aget_lvalue(int pos) { | ||
| 7198 | int size = this.a.size(); | ||
| 7199 | if (pos < 0) { | ||
| 7200 | pos = size + pos; | ||
| 7201 | } | ||
| 7202 | if (size <= pos) { | ||
| 7203 | return new PlLazyIndex(this, pos); | ||
| 7204 | } | ||
| 7205 | PlObject o = this.a.get(pos); | ||
| 7206 | if (o == null) { | ||
| 7207 | return new PlLazyIndex(this, pos); | ||
| 7208 | } | ||
| 7209 | if (o.is_lvalue()) { | ||
| 7210 | return o; | ||
| 7211 | } | ||
| 7212 | PlLvalue a = new PlLvalue(o); | ||
| 7213 | this.a.set(pos, a); | ||
| 7214 | return a; | ||
| 7215 | } | ||
| 7216 | public PlObject aget_lvalue_local(int i) { | ||
| 7217 | PlObject o = this.a.get(i); | ||
| 7218 | if (o == null) { | ||
| 7219 | this.a.set(i, PlCx.UNDEF); | ||
| 7220 | } | ||
| 7221 | return PerlOp.push_local(this, i); | ||
| 7222 | } | ||
| 7223 | |||
| 7224 | public PlObject aget_list_of_aliases(int want, PlArray a) { | ||
| 7225 | // @a[LIST] | ||
| 7226 | PlArrayList aa = new PlArrayList(); | ||
| 7227 | for (PlObject i : a) { | ||
| 7228 | aa.add( this.aget_lvalue(i.to_int()) ); | ||
| 7229 | } | ||
| 7230 | PlSlice result = new PlSlice(aa); | ||
| 7231 | if (want == PlCx.LIST) { | ||
| 7232 | return result; | ||
| 7233 | } | ||
| 7234 | return result.pop(); | ||
| 7235 | } | ||
| 7236 | public PlObject aget_hash_list_of_aliases(int want, PlArray a) { | ||
| 7237 | // %a[LIST] | ||
| 7238 | PlArrayList aa = new PlArrayList(); | ||
| 7239 | for (PlObject i : a) { | ||
| 7240 | aa.add( i ); | ||
| 7241 | aa.add( this.aget_lvalue(i.to_int()) ); | ||
| 7242 | } | ||
| 7243 | PlSlice result = new PlSlice(aa); | ||
| 7244 | if (want == PlCx.LIST) { | ||
| 7245 | return result; | ||
| 7246 | } | ||
| 7247 | return result.pop(); | ||
| 7248 | } | ||
| 7249 | |||
| 7250 | public PlObject aget_scalarref(int i) { | ||
| 7251 | PlObject o = this.a.aget(i); | ||
| 7252 | if (o.is_undef()) { | ||
| 7253 | return new PlLvalueRef(new PlLazyScalarref(new PlLazyIndex(this, i))); | ||
| 7254 | } | ||
| 7255 | return o; | ||
| 7256 | } | ||
| 7257 | |||
| 7258 | public PlObject aget_arrayref(int i) { | ||
| 7259 | PlObject o = this.a.aget(i); | ||
| 7260 | if (o.is_undef()) { | ||
| 7261 | PlArrayRef ar = new PlArrayRef(); | ||
| 7262 | this.a.aset(i, ar); | ||
| 7263 | return ar; | ||
| 7264 | } | ||
| 7265 | return o; | ||
| 7266 | } | ||
| 7267 | |||
| 7268 | public PlObject aget_hashref(int i) { | ||
| 7269 | PlObject o = this.a.aget(i); | ||
| 7270 | if (o.is_undef()) { | ||
| 7271 | PlHashRef hr = new PlHashRef(); | ||
| 7272 | this.a.aset(i, hr); | ||
| 7273 | return hr; | ||
| 7274 | } | ||
| 7275 | return o; | ||
| 7276 | } | ||
| 7277 | |||
| 7278 | public PlObject get_hash(int i) { | ||
| 7279 | PlObject o = this.a.aget(i); | ||
| 7280 | if (o.is_undef()) { | ||
| 7281 | PlHashRef hr = new PlHashRef(); | ||
| 7282 | this.a.aset(i, hr); | ||
| 7283 | return hr; | ||
| 7284 | } | ||
| 7285 | else if (o.is_hashref()) { | ||
| 7286 | return o; | ||
| 7287 | } | ||
| 7288 | return PlCORE.die("Not a HASH reference"); | ||
| 7289 | } | ||
| 7290 | |||
| 7291 | // Note: multiple versions of set() | ||
| 7292 | public PlObject aset(int i, PlObject v) { | ||
| 7293 | return this.a.aset(i, v); | ||
| 7294 | } | ||
| 7295 | public PlObject aset(int i, PlLvalue v) { | ||
| 7296 | return this.a.aset(i, v.get()); | ||
| 7297 | } | ||
| 7298 | |||
| 7299 | public PlObject push(PlObject... args) { | ||
| 7300 | for (int i = 0; i < args.length; i++) { | ||
| 7301 | this.push_void(args[i]); | ||
| 7302 | } | ||
| 7303 | return new PlInt(this.a.size()); | ||
| 7304 | } | ||
| 7305 | public void push_void(PlObject... args) { | ||
| 7306 | for (int i = 0; i < args.length; i++) { | ||
| 7307 | this.push_void(args[i]); | ||
| 7308 | } | ||
| 7309 | } | ||
| 7310 | |||
| 7311 | public PlObject unshift(PlObject... args) { | ||
| 7312 | for (int i = args.length-1; i >= 0; i--) { | ||
| 7313 | this.unshift(args[i]); | ||
| 7314 | } | ||
| 7315 | return new PlInt(this.a.size()); | ||
| 7316 | } | ||
| 7317 | public PlObject aset(int s, Object o) { | ||
| 7318 | return this.aset(s, PlJavaObject.fromObject(o)); | ||
| 7319 | } | ||
| 7320 | public PlObject push(Object o) { | ||
| 7321 | return this.push(PlJavaObject.fromObject(o)); | ||
| 7322 | } | ||
| 7323 | public void push_void(Object o) { | ||
| 7324 | this.push_void(PlJavaObject.fromObject(o)); | ||
| 7325 | } | ||
| 7326 | public PlObject unshift(Object o) { | ||
| 7327 | return this.unshift(PlJavaObject.fromObject(o)); | ||
| 7328 | } | ||
| 7329 | |||
| 7330 | EOT | ||
| 7331 | , ((map { | ||
| 7332 | my $native = $_; | ||
| 7333 | my $perl = $native_to_perl{$native}; | ||
| 7334 | $native && $perl ? | ||
| 7335 | " public PlObject aset(int i, $native s) { | ||
| 7336 | return this.aset(i, new $perl(s)); | ||
| 7337 | } | ||
| 7338 | public PlObject push($native s) { | ||
| 7339 | return this.push(new $perl(s)); | ||
| 7340 | } | ||
| 7341 | public void push_void($native s) { | ||
| 7342 | this.push_void(new $perl(s)); | ||
| 7343 | } | ||
| 7344 | public PlObject unshift($native s) { | ||
| 7345 | return this.unshift(new $perl(s)); | ||
| 7346 | } | ||
| 7347 | " : () | ||
| 7348 | } | ||
| 7349 | sort keys %native_to_perl )) | ||
| 7350 | |||
| 7351 | , <<'EOT' | ||
| 7352 | public PlObject aset_alias(int i, PlLvalue lvalue) { | ||
| 7353 | return this.a.set(i, lvalue); | ||
| 7354 | } | ||
| 7355 | |||
| 7356 | // Note: multiple versions of push() | ||
| 7357 | public PlObject push(PlObject v) { | ||
| 7358 | if (v.is_array()) { | ||
| 7359 | return this.push( (PlArray)v ); | ||
| 7360 | } | ||
| 7361 | else if (v.is_hash()) { | ||
| 7362 | return this.push( (PlHash)v ); | ||
| 7363 | } | ||
| 7364 | this.a.add(v.scalar()); | ||
| 7365 | return new PlInt(this.a.size()); | ||
| 7366 | } | ||
| 7367 | public void push_void(PlObject v) { | ||
| 7368 | if (v.is_array()) { | ||
| 7369 | this.push_void( (PlArray)v ); | ||
| 7370 | return; | ||
| 7371 | } | ||
| 7372 | else if (v.is_hash()) { | ||
| 7373 | this.push_void( (PlHash)v ); | ||
| 7374 | return; | ||
| 7375 | } | ||
| 7376 | this.a.add(v.scalar()); | ||
| 7377 | } | ||
| 7378 | |||
| 7379 | public PlObject push(PlLvalue v) { | ||
| 7380 | this.a.add(v.get()); | ||
| 7381 | return new PlInt(this.a.size()); | ||
| 7382 | } | ||
| 7383 | public void push_void(PlLvalue v) { | ||
| 7384 | this.a.add(v.get()); | ||
| 7385 | } | ||
| 7386 | public PlObject push(PlHash args) { | ||
| 7387 | return push(args.to_array()); | ||
| 7388 | } | ||
| 7389 | public void push_void(PlHash args) { | ||
| 7390 | push_void(args.to_array()); | ||
| 7391 | } | ||
| 7392 | public PlObject push(PlArray args) { | ||
| 7393 | int size = args.a.size(); | ||
| 7394 | for (int i = 0; i < size; i++) { | ||
| 7395 | PlObject s = args.aget(i); | ||
| 7396 | if (s.is_hash()) { | ||
| 7397 | // @x = %x; | ||
| 7398 | s = s.to_array(); | ||
| 7399 | } | ||
| 7400 | if (s.is_array()) { | ||
| 7401 | this.push_void(s); | ||
| 7402 | } | ||
| 7403 | else { | ||
| 7404 | this.a.add(s); | ||
| 7405 | } | ||
| 7406 | } | ||
| 7407 | return new PlInt(this.a.size()); | ||
| 7408 | } | ||
| 7409 | public void push_void(PlArray args) { | ||
| 7410 | int size = args.a.size(); | ||
| 7411 | for (int i = 0; i < size; i++) { | ||
| 7412 | PlObject s = args.aget(i); | ||
| 7413 | if (s.is_hash()) { | ||
| 7414 | // @x = %x; | ||
| 7415 | s = s.to_array(); | ||
| 7416 | } | ||
| 7417 | if (s.is_array()) { | ||
| 7418 | this.push_void(s); | ||
| 7419 | } | ||
| 7420 | else { | ||
| 7421 | this.a.add(s); | ||
| 7422 | } | ||
| 7423 | } | ||
| 7424 | } | ||
| 7425 | |||
| 7426 | // Note: multiple versions of unshift() | ||
| 7427 | public PlObject unshift(PlObject v) { | ||
| 7428 | if (v.is_array()) { | ||
| 7429 | return this.unshift( (PlArray)v ); | ||
| 7430 | } | ||
| 7431 | else if (v.is_hash()) { | ||
| 7432 | return this.unshift( (PlHash)v ); | ||
| 7433 | } | ||
| 7434 | this.a.add(0, v.scalar()); | ||
| 7435 | return new PlInt(this.a.size()); | ||
| 7436 | } | ||
| 7437 | public PlObject unshift(PlLvalue v) { | ||
| 7438 | this.a.add(0, v.get()); | ||
| 7439 | return new PlInt(this.a.size()); | ||
| 7440 | } | ||
| 7441 | public PlObject unshift(PlHash args) { | ||
| 7442 | return unshift(args.to_array()); | ||
| 7443 | } | ||
| 7444 | public PlObject unshift(PlArray args) { | ||
| 7445 | args = new PlArray(args); // allow "unshift @x, @x" - TODO: optimize | ||
| 7446 | int size = args.a.size(); | ||
| 7447 | for (int i = size - 1; i >= 0; i--) { | ||
| 7448 | PlObject s = args.aget(i); | ||
| 7449 | if (s.is_hash()) { | ||
| 7450 | // @x = %x; | ||
| 7451 | s = s.to_array(); | ||
| 7452 | } | ||
| 7453 | if (s.is_array()) { | ||
| 7454 | this.unshift(s); | ||
| 7455 | } | ||
| 7456 | else { | ||
| 7457 | this.a.add(0, s); | ||
| 7458 | } | ||
| 7459 | } | ||
| 7460 | return new PlInt(this.a.size()); | ||
| 7461 | } | ||
| 7462 | |||
| 7463 | public PlObject pop() { | ||
| 7464 | return this.a.pop(); | ||
| 7465 | } | ||
| 7466 | public PlObject shift() { | ||
| 7467 | return this.a.shift(); | ||
| 7468 | } | ||
| 7469 | public PlObject aexists(PlObject i) { | ||
| 7470 | return this.a.aexists(i); | ||
| 7471 | } | ||
| 7472 | public PlObject adelete(int want, PlObject i) { | ||
| 7473 | return this.a.adelete(want, i); | ||
| 7474 | } | ||
| 7475 | public PlObject values() { | ||
| 7476 | // return a copy | ||
| 7477 | return new PlArray(this); | ||
| 7478 | } | ||
| 7479 | public PlObject keys() { | ||
| 7480 | PlArray aa = new PlArray(); | ||
| 7481 | int size = this.to_int(); | ||
| 7482 | for (int i = 0; i < size; i++) { | ||
| 7483 | aa.push_void(new PlInt(i)); | ||
| 7484 | } | ||
| 7485 | return aa; | ||
| 7486 | } | ||
| 7487 | public PlObject each() { | ||
| 7488 | PlArray aa = new PlArray(); | ||
| 7489 | int size = this.to_int(); | ||
| 7490 | if (this.each_iterator < size) { | ||
| 7491 | aa.push_void(new PlInt(this.each_iterator)); | ||
| 7492 | aa.push_void(this.a.aget(this.each_iterator)); | ||
| 7493 | this.each_iterator++; | ||
| 7494 | } | ||
| 7495 | else { | ||
| 7496 | // return empty list | ||
| 7497 | this.each_iterator = 0; | ||
| 7498 | } | ||
| 7499 | return aa; | ||
| 7500 | } | ||
| 7501 | public String toString() { | ||
| 7502 | StringBuilder sb = new StringBuilder(); | ||
| 7503 | int size = this.to_int(); | ||
| 7504 | for (int i = 0; i < size; i++) { | ||
| 7505 | String item = this.a.aget(i).toString(); | ||
| 7506 | sb.append(item); | ||
| 7507 | } | ||
| 7508 | return sb.toString(); | ||
| 7509 | } | ||
| 7510 | public long to_long() { | ||
| 7511 | return this.a.size(); | ||
| 7512 | } | ||
| 7513 | public int to_int() { | ||
| 7514 | return this.a.size(); | ||
| 7515 | } | ||
| 7516 | public PlScalarImmutable length_of_array() { | ||
| 7517 | return new PlInt(this.a.size()); | ||
| 7518 | } | ||
| 7519 | public boolean length_of_array_boolean() { | ||
| 7520 | return ( this.a.size() > 0 ); | ||
| 7521 | } | ||
| 7522 | public int length_of_array_int() { | ||
| 7523 | return this.a.size(); | ||
| 7524 | } | ||
| 7525 | public PlObject end_of_array_index() { | ||
| 7526 | return new PlInt(this.a.size() - 1); | ||
| 7527 | } | ||
| 7528 | public PlObject set_end_of_array_index(PlObject o) { | ||
| 7529 | return this.a.set_end_of_array_index(o.to_int()); | ||
| 7530 | } | ||
| 7531 | public double to_double() { | ||
| 7532 | return 0.0 + this.to_long(); | ||
| 7533 | } | ||
| 7534 | public boolean to_boolean() { | ||
| 7535 | return (this.a.size() > 0); | ||
| 7536 | } | ||
| 7537 | public PlObject to_num() { | ||
| 7538 | return new PlInt(this.a.size()); | ||
| 7539 | } | ||
| 7540 | public boolean is_array() { | ||
| 7541 | return true; | ||
| 7542 | } | ||
| 7543 | public PlScalarImmutable scalar() { | ||
| 7544 | return new PlInt(this.a.size()); | ||
| 7545 | } | ||
| 7546 | } | ||
| 7547 | |||
| 7548 | |||
| 7549 | class PlTieHashIterator implements Iterator<Map.Entry<String, PlObject>> { | ||
| 7550 | public PlObject tied; | ||
| 7551 | private PlObject key; | ||
| 7552 | |||
| 7553 | public PlTieHashIterator(PlObject tied) { | ||
| 7554 | this.tied = tied; | ||
| 7555 | } | ||
| 7556 | public Map.Entry<String, PlObject> next() { | ||
| 7557 | return new AbstractMap.SimpleEntry<String, PlObject>( | ||
| 7558 | this.key.toString(), | ||
| 7559 | PerlOp.call("FETCH", new PlArray(this.tied, this.key), PlCx.SCALAR) | ||
| 7560 | ); | ||
| 7561 | } | ||
| 7562 | public boolean hasNext() { | ||
| 7563 | if (this.key == null) { | ||
| 7564 | this.key = PerlOp.call("FIRSTKEY", new PlArray(this.tied), PlCx.SCALAR); | ||
| 7565 | } | ||
| 7566 | else { | ||
| 7567 | this.key = PerlOp.call("NEXTKEY", new PlArray(this.tied), PlCx.SCALAR); | ||
| 7568 | } | ||
| 7569 | return !this.key.is_undef(); | ||
| 7570 | } | ||
| 7571 | } | ||
| 7572 | class PlTieHashMap extends PlHashMap { | ||
| 7573 | public PlObject tied; | ||
| 7574 | public PlHashMap old_var; | ||
| 7575 | |||
| 7576 | public PlTieHashMap() { | ||
| 7577 | } | ||
| 7578 | // get(String) | ||
| 7579 | // put(String, PlObject) | ||
| 7580 | // containsKey(String) | ||
| 7581 | // remove(String) | ||
| 7582 | // clear() | ||
| 7583 | // entrySet().iterator() == iterator() | ||
| 7584 | |||
| 7585 | public PlScalarImmutable get(Object i) { | ||
| 7586 | return PerlOp.call("FETCH", new PlArray(this.tied, new PlString((String)i)), PlCx.SCALAR).scalar(); | ||
| 7587 | } | ||
| 7588 | public PlObject put(String i, PlObject v) { | ||
| 7589 | return PerlOp.call("STORE", new PlArray(this.tied, new PlString(i), v), PlCx.SCALAR); | ||
| 7590 | } | ||
| 7591 | public boolean containsKey(Object i) { | ||
| 7592 | return PerlOp.call("EXISTS", new PlArray(this.tied, new PlString((String)i)), PlCx.SCALAR).to_boolean(); | ||
| 7593 | } | ||
| 7594 | public PlObject remove(Object i) { | ||
| 7595 | return PerlOp.call("DELETE", new PlArray(this.tied, new PlString((String)i)), PlCx.SCALAR); | ||
| 7596 | } | ||
| 7597 | public void clear() { | ||
| 7598 | PerlOp.call("CLEAR", new PlArray(this.tied), PlCx.SCALAR); | ||
| 7599 | } | ||
| 7600 | public Iterator<Map.Entry<String, PlObject>> iterator() { | ||
| 7601 | return new PlTieHashIterator(this.tied); | ||
| 7602 | } | ||
| 7603 | public PlScalarImmutable scalar() { | ||
| 7604 | return PerlOp.call("SCALAR", new PlArray(this.tied), PlCx.SCALAR).scalar(); | ||
| 7605 | } | ||
| 7606 | public boolean is_tiedHash() { | ||
| 7607 | return true; | ||
| 7608 | } | ||
| 7609 | public PlObject tied() { | ||
| 7610 | return this.tied; | ||
| 7611 | } | ||
| 7612 | |||
| 7613 | } // PlTieHashMap | ||
| 7614 | |||
| 7615 | class PlHashMap extends HashMap<String, PlObject> implements Iterable<Map.Entry<String, PlObject>> { | ||
| 7616 | public PlHashMap() { | ||
| 7617 | } | ||
| 7618 | // get(String) | ||
| 7619 | // put(String, PlObject) | ||
| 7620 | // containsKey(String) | ||
| 7621 | // remove(String) | ||
| 7622 | // clear() | ||
| 7623 | // entrySet().iterator() == iterator() | ||
| 7624 | |||
| 7625 | public Iterator<Map.Entry<String, PlObject>> iterator() { | ||
| 7626 | return this.entrySet().iterator(); | ||
| 7627 | } | ||
| 7628 | public PlScalarImmutable scalar() { | ||
| 7629 | return new PlInt(this.hashCode()); | ||
| 7630 | } | ||
| 7631 | public boolean is_tiedHash() { | ||
| 7632 | return false; | ||
| 7633 | } | ||
| 7634 | public PlObject tied() { | ||
| 7635 | return PlCx.UNDEF; | ||
| 7636 | } | ||
| 7637 | } | ||
| 7638 | class PlHashLvalueIterator implements Iterator<PlObject> { | ||
| 7639 | private final PlHash hr; | ||
| 7640 | private final PlHashMap h; | ||
| 7641 | private Iterator<Map.Entry<String, PlObject>> each_iterator; | ||
| 7642 | private boolean is_key; | ||
| 7643 | private Map.Entry<String, PlObject> entry; | ||
| 7644 | private String key; | ||
| 7645 | |||
| 7646 | public PlHashLvalueIterator(PlHash hr) { | ||
| 7647 | this.hr = hr; | ||
| 7648 | this.h = hr.h; | ||
| 7649 | this.each_iterator = this.h.iterator(); | ||
| 7650 | this.is_key = true; | ||
| 7651 | } | ||
| 7652 | public PlObject next() { | ||
| 7653 | if (is_key) { | ||
| 7654 | is_key = false; | ||
| 7655 | this.entry = this.each_iterator.next(); | ||
| 7656 | this.key = entry.getKey(); | ||
| 7657 | return new PlLvalue(new PlString(this.key)); | ||
| 7658 | } | ||
| 7659 | is_key = true; | ||
| 7660 | PlObject o = this.entry.getValue(); | ||
| 7661 | if (o == null) { | ||
| 7662 | return new PlLazyLookup(this.hr, this.key); | ||
| 7663 | } | ||
| 7664 | if (o.is_lvalue()) { | ||
| 7665 | return o; | ||
| 7666 | } | ||
| 7667 | PlLvalue lh = new PlLvalue(o); | ||
| 7668 | this.h.put(this.key, lh); | ||
| 7669 | return lh; | ||
| 7670 | } | ||
| 7671 | public boolean hasNext() { | ||
| 7672 | if (is_key) { | ||
| 7673 | return this.each_iterator.hasNext(); | ||
| 7674 | } | ||
| 7675 | return true; | ||
| 7676 | } | ||
| 7677 | } | ||
| 7678 | class PlHash extends PlObject implements Iterable<PlObject> { | ||
| 7679 | public PlHashMap h; | ||
| 7680 | public Iterator<Map.Entry<String, PlObject>> each_iterator; | ||
| 7681 | |||
| 7682 | public Iterator<PlObject> iterator() { | ||
| 7683 | return new PlHashLvalueIterator(this); | ||
| 7684 | } | ||
| 7685 | |||
| 7686 | public PlHash() { | ||
| 7687 | this.h = new PlHashMap(); | ||
| 7688 | } | ||
| 7689 | public PlHash(PlObject... args) { | ||
| 7690 | this.h = new PlHashMap(); | ||
| 7691 | int args_size = args.length; | ||
| 7692 | for (int i = 0; i < args_size; i++) { | ||
| 7693 | PlObject s = args[i]; | ||
| 7694 | if (s.is_hash()) { | ||
| 7695 | // @x = %x; | ||
| 7696 | s = s.to_array(); | ||
| 7697 | } | ||
| 7698 | if (s.is_array()) { | ||
| 7699 | // %x = ( @x, @y ); | ||
| 7700 | int array_size = s.to_int(); | ||
| 7701 | for (int j = 0; j < array_size; j++) { | ||
| 7702 | String key = s.aget(j).toString(); | ||
| 7703 | j++; | ||
| 7704 | PlObject value; | ||
| 7705 | if ( j >= array_size ) { | ||
| 7706 | // TODO - emit warning about odd number of arguments | ||
| 7707 | value = PlCx.UNDEF; | ||
| 7708 | } | ||
| 7709 | else { | ||
| 7710 | value = s.aget(j); | ||
| 7711 | } | ||
| 7712 | this.hset(key, value); | ||
| 7713 | } | ||
| 7714 | } | ||
| 7715 | else { | ||
| 7716 | i++; | ||
| 7717 | PlObject value; | ||
| 7718 | if ( i >= args_size ) { | ||
| 7719 | // TODO - emit warning about odd number of arguments | ||
| 7720 | value = PlCx.UNDEF; | ||
| 7721 | } | ||
| 7722 | else { | ||
| 7723 | value = args[i]; | ||
| 7724 | } | ||
| 7725 | this.hset(s.toString(), value); | ||
| 7726 | } | ||
| 7727 | } | ||
| 7728 | } | ||
| 7729 | |||
| 7730 | |||
| 7731 | // tie hash | ||
| 7732 | public PlObject tie(PlArray args) { | ||
| 7733 | if (this.h.is_tiedHash()) { | ||
| 7734 | this.untie(); | ||
| 7735 | } | ||
| 7736 | PlTieHashMap v = new PlTieHashMap(); | ||
| 7737 | PlObject self = PerlOp.call("TIEHASH", args, PlCx.VOID); | ||
| 7738 | v.tied = self; | ||
| 7739 | v.old_var = this.h; | ||
| 7740 | this.h = v; | ||
| 7741 | return self; | ||
| 7742 | } | ||
| 7743 | |||
| 7744 | public PlObject untie() { | ||
| 7745 | if (this.h.is_tiedHash()) { | ||
| 7746 | PlObject tied = this.h.tied(); | ||
| 7747 | PlObject untie = PerlOp.call("can", new PlArray(tied, new PlString("UNTIE")), PlCx.SCALAR); | ||
| 7748 | if (untie.to_boolean()) { | ||
| 7749 | untie.apply(PlCx.VOID, new PlArray(tied)); | ||
| 7750 | }; | ||
| 7751 | this.h = ((PlTieHashMap)h).old_var; | ||
| 7752 | return tied; | ||
| 7753 | } | ||
| 7754 | return this; | ||
| 7755 | } | ||
| 7756 | public PlObject tied() { | ||
| 7757 | return h.tied(); | ||
| 7758 | } | ||
| 7759 | |||
| 7760 | |||
| 7761 | // internal lazy api | ||
| 7762 | public PlLvalue create_scalar(String i) { | ||
| 7763 | PlObject o = this.h.get(i); | ||
| 7764 | if (o == null) { | ||
| 7765 | PlLvalue a = new PlLvalue(); | ||
| 7766 | this.h.put(i, a); | ||
| 7767 | return a; | ||
| 7768 | } | ||
| 7769 | if (o.is_lvalue()) { | ||
| 7770 | return (PlLvalue)o; | ||
| 7771 | } | ||
| 7772 | if (o.is_undef()) { | ||
| 7773 | PlLvalue a = new PlLvalue(); | ||
| 7774 | this.h.put(i, a); | ||
| 7775 | return a; | ||
| 7776 | } | ||
| 7777 | return (PlLvalue)PlCORE.die("Not a SCALAR reference"); | ||
| 7778 | } | ||
| 7779 | |||
| 7780 | public PlObject set(int want, PlObject s) { | ||
| 7781 | this.h.clear(); | ||
| 7782 | if (s.is_hash()) { | ||
| 7783 | // @x = %x; | ||
| 7784 | s = s.to_array(); | ||
| 7785 | } | ||
| 7786 | if (s.is_array()) { | ||
| 7787 | // %x = ( @x, @y ); | ||
| 7788 | int array_size = s.to_int(); | ||
| 7789 | for (int j = 0; j < array_size; j++) { | ||
| 7790 | String key = s.aget(j).toString(); | ||
| 7791 | j++; | ||
| 7792 | PlObject value; | ||
| 7793 | if ( j >= array_size ) { | ||
| 7794 | // odd number of arguments | ||
| 7795 | if (array_size == 1 && s.aget(0).is_ref()) { | ||
| 7796 | PlCORE.warn(PlCx.VOID, new PlArray(new PlString("Reference found where even-sized list expected"))); | ||
| 7797 | } | ||
| 7798 | else { | ||
| 7799 | PlCORE.warn(PlCx.VOID, new PlArray(new PlString("Odd number of elements in hash assignment"))); | ||
| 7800 | } | ||
| 7801 | value = PlCx.UNDEF; | ||
| 7802 | } | ||
| 7803 | else { | ||
| 7804 | value = s.aget(j); | ||
| 7805 | } | ||
| 7806 | this.hset(key, value); | ||
| 7807 | } | ||
| 7808 | } | ||
| 7809 | else { | ||
| 7810 | // TODO - emit warning about odd number of arguments | ||
| 7811 | PlCORE.warn(PlCx.VOID, new PlArray(new PlString("Odd number of elements in hash assignment"))); | ||
| 7812 | this.hset(s.toString(), PlCx.UNDEF); | ||
| 7813 | } | ||
| 7814 | this.each_iterator = null; | ||
| 7815 | if (want == PlCx.LIST) { | ||
| 7816 | return this.to_list_of_aliases(); | ||
| 7817 | } | ||
| 7818 | return s.scalar(); | ||
| 7819 | } | ||
| 7820 | |||
| 7821 | public PlObject to_array() { | ||
| 7822 | PlArray aa = new PlArray(); | ||
| 7823 | for (Map.Entry<String, PlObject> entry : this.h) { | ||
| 7824 | String key = entry.getKey(); | ||
| 7825 | PlObject value = entry.getValue(); | ||
| 7826 | aa.push_void(new PlString(key)); | ||
| 7827 | aa.push_void(value); | ||
| 7828 | } | ||
| 7829 | return aa; | ||
| 7830 | } | ||
| 7831 | |||
| 7832 | public PlArray to_list_of_aliases() { | ||
| 7833 | PlArrayList aa = new PlArrayList(); | ||
| 7834 | for (Map.Entry<String, PlObject> entry : this.h) { | ||
| 7835 | String key = entry.getKey(); | ||
| 7836 | aa.add(new PlString(key)); | ||
| 7837 | PlObject value = this.hget_lvalue(key); | ||
| 7838 | aa.add(value); | ||
| 7839 | } | ||
| 7840 | PlSlice result = new PlSlice(aa); | ||
| 7841 | return result; | ||
| 7842 | } | ||
| 7843 | |||
| 7844 | public PlObject hget(String i) { | ||
| 7845 | PlObject o = this.h.get(i); | ||
| 7846 | if (o == null) { | ||
| 7847 | return PlCx.UNDEF; | ||
| 7848 | } | ||
| 7849 | return o; | ||
| 7850 | } | ||
| 7851 | public PlObject hget_list_of_aliases(int want, PlArray a) { | ||
| 7852 | // @a{LIST} | ||
| 7853 | PlArrayList aa = new PlArrayList(); | ||
| 7854 | for (int i = 0; i < a.to_int(); i++) { | ||
| 7855 | String key = a.aget(i).toString(); | ||
| 7856 | PlObject value = this.hget_lvalue(key); | ||
| 7857 | aa.add(value); | ||
| 7858 | } | ||
| 7859 | PlSlice result = new PlSlice(aa); | ||
| 7860 | if (want == PlCx.LIST) { | ||
| 7861 | return result; | ||
| 7862 | } | ||
| 7863 | return result.pop(); | ||
| 7864 | } | ||
| 7865 | public PlObject hget_hash_list_of_aliases(int want, PlArray a) { | ||
| 7866 | // %a{LIST} | ||
| 7867 | PlArrayList aa = new PlArrayList(); | ||
| 7868 | for (int i = 0; i < a.to_int(); i++) { | ||
| 7869 | String key = a.aget(i).toString(); | ||
| 7870 | aa.add(new PlString(key)); | ||
| 7871 | PlObject value = this.hget_lvalue(key); | ||
| 7872 | aa.add(value); | ||
| 7873 | } | ||
| 7874 | PlArray result = new PlArray(aa); | ||
| 7875 | if (want == PlCx.LIST) { | ||
| 7876 | return result; | ||
| 7877 | } | ||
| 7878 | return result.pop(); | ||
| 7879 | } | ||
| 7880 | |||
| 7881 | public PlObject hget_lvalue(String i) { | ||
| 7882 | PlObject o = this.h.get(i); | ||
| 7883 | if (o == null) { | ||
| 7884 | return new PlLazyLookup(this, i); | ||
| 7885 | } | ||
| 7886 | else if (o.is_lvalue()) { | ||
| 7887 | return o; | ||
| 7888 | } | ||
| 7889 | |||
| 7890 | if (this.h.is_tiedHash()) { | ||
| 7891 | return new PlLazyTiedLookup(this, i); | ||
| 7892 | } | ||
| 7893 | |||
| 7894 | PlLvalue a = new PlLvalue(o); | ||
| 7895 | this.h.put(i, a); | ||
| 7896 | return a; | ||
| 7897 | } | ||
| 7898 | public PlObject hget_lvalue_local(String i) { | ||
| 7899 | PlObject o = this.h.get(i); | ||
| 7900 | if (o == null) { | ||
| 7901 | this.h.put(i, PlCx.UNDEF); | ||
| 7902 | } | ||
| 7903 | |||
| 7904 | if (this.h.is_tiedHash()) { | ||
| 7905 | PerlOp.push_local(this, i); | ||
| 7906 | return new PlLazyTiedLookup(this, i); | ||
| 7907 | } | ||
| 7908 | |||
| 7909 | return PerlOp.push_local(this, i); | ||
| 7910 | } | ||
| 7911 | |||
| 7912 | public PlObject hget_scalarref(String i) { | ||
| 7913 | PlObject o = this.h.get(i); | ||
| 7914 | if (o == null || o.is_undef()) { | ||
| 7915 | return new PlLvalueRef(new PlLazyScalarref(new PlLazyLookup(this, i))); | ||
| 7916 | } | ||
| 7917 | else if (o.is_scalarref()) { | ||
| 7918 | return o; | ||
| 7919 | } | ||
| 7920 | // Modification of a read-only value attempted | ||
| 7921 | return o; | ||
| 7922 | } | ||
| 7923 | |||
| 7924 | public PlObject hget_arrayref(String i) { | ||
| 7925 | PlObject o = this.h.get(i); | ||
| 7926 | if (o == null || o.is_undef()) { | ||
| 7927 | PlArrayRef ar = new PlArrayRef(); | ||
| 7928 | this.hset(i, ar); | ||
| 7929 | return ar; | ||
| 7930 | } | ||
| 7931 | else if (o.is_arrayref()) { | ||
| 7932 | return o; | ||
| 7933 | } | ||
| 7934 | return PlCORE.die("Not an ARRAY reference"); | ||
| 7935 | } | ||
| 7936 | |||
| 7937 | public PlObject hget_hashref(String i) { | ||
| 7938 | PlObject o = this.h.get(i); | ||
| 7939 | if (o == null || o.is_undef()) { | ||
| 7940 | PlHashRef hr = new PlHashRef(); | ||
| 7941 | this.hset(i, hr); | ||
| 7942 | return hr; | ||
| 7943 | } | ||
| 7944 | else if (o.is_hashref()) { | ||
| 7945 | return o; | ||
| 7946 | } | ||
| 7947 | return PlCORE.die("Not a HASH reference"); | ||
| 7948 | } | ||
| 7949 | |||
| 7950 | // Note: multiple versions of set() | ||
| 7951 | public PlObject hset(String key, PlObject v) { | ||
| 7952 | PlObject value = v.scalar(); | ||
| 7953 | PlObject o = this.h.get(key); | ||
| 7954 | if (o != null && o.is_lvalue()) { | ||
| 7955 | o.set(value); | ||
| 7956 | } | ||
| 7957 | else { | ||
| 7958 | this.h.put(key, value); | ||
| 7959 | } | ||
| 7960 | return v; | ||
| 7961 | } | ||
| 7962 | public PlObject hset(String key, PlLvalue v) { | ||
| 7963 | PlObject value = v.get(); | ||
| 7964 | PlObject o = this.h.get(key); | ||
| 7965 | if (o != null && o.is_lvalue()) { | ||
| 7966 | o.set(value); | ||
| 7967 | } | ||
| 7968 | else { | ||
| 7969 | this.h.put(key, value); | ||
| 7970 | } | ||
| 7971 | return v; | ||
| 7972 | } | ||
| 7973 | public PlObject hset(int want, PlArray s, PlArray v) { | ||
| 7974 | PlArray aa = new PlArray(); | ||
| 7975 | |||
| 7976 | for (int i = 0; i < v.to_int(); i++){ | ||
| 7977 | aa.push_void(this.hset(v.aget(i).toString(), s.aget(i))); | ||
| 7978 | }; | ||
| 7979 | if (want == PlCx.LIST) { | ||
| 7980 | return aa; | ||
| 7981 | } | ||
| 7982 | return aa.pop(); | ||
| 7983 | } | ||
| 7984 | public PlObject hset_alias(String s, PlObject lvalue) { | ||
| 7985 | return this.h.put(s, lvalue); | ||
| 7986 | } | ||
| 7987 | public PlObject hexists(String i) { | ||
| 7988 | return this.h.containsKey(i) ? PlCx.TRUE : PlCx.FALSE; | ||
| 7989 | } | ||
| 7990 | public PlObject hexists(PlObject i) { | ||
| 7991 | return this.h.containsKey(i.toString()) ? PlCx.TRUE : PlCx.FALSE; | ||
| 7992 | } | ||
| 7993 | public PlObject hdelete(int want, PlObject i) { | ||
| 7994 | PlObject r = this.h.remove(i.toString()); | ||
| 7995 | if (r == null) { | ||
| 7996 | return PlCx.UNDEF; | ||
| 7997 | } | ||
| 7998 | return r; | ||
| 7999 | } | ||
| 8000 | public PlObject hdelete(int want, PlArray a) { | ||
| 8001 | PlArray aa = new PlArray(); | ||
| 8002 | |||
| 8003 | for (int i = 0; i < a.to_int(); i++) { | ||
| 8004 | PlObject r = this.hdelete(want, a.aget(i)); | ||
| 8005 | aa.push_void(r); | ||
| 8006 | } | ||
| 8007 | if (want == PlCx.LIST) { | ||
| 8008 | return aa; | ||
| 8009 | } | ||
| 8010 | return aa.pop(); | ||
| 8011 | } | ||
| 8012 | public PlObject hdelete(int want, PlString a) { | ||
| 8013 | PlArray aa = new PlArray(); | ||
| 8014 | aa.push_void(a); | ||
| 8015 | return this.hdelete(want, aa); | ||
| 8016 | } | ||
| 8017 | public PlObject hdelete(int want, PlLvalue a) { | ||
| 8018 | PlArray aa = new PlArray(); | ||
| 8019 | aa.push_void(a); | ||
| 8020 | return this.hdelete(want, aa); | ||
| 8021 | } | ||
| 8022 | public PlObject values() { | ||
| 8023 | PlArray aa = new PlArray(); | ||
| 8024 | for (Map.Entry<String, PlObject> entry : this.h) { | ||
| 8025 | PlObject value = entry.getValue(); | ||
| 8026 | aa.push_void(value); | ||
| 8027 | } | ||
| 8028 | return aa; | ||
| 8029 | } | ||
| 8030 | public PlObject keys() { | ||
| 8031 | PlArray aa = new PlArray(); | ||
| 8032 | for (Map.Entry<String, PlObject> entry : this.h) { | ||
| 8033 | String key = entry.getKey(); | ||
| 8034 | aa.push_void(new PlString(key)); | ||
| 8035 | } | ||
| 8036 | return aa; | ||
| 8037 | } | ||
| 8038 | public PlObject each() { | ||
| 8039 | PlArray aa = new PlArray(); | ||
| 8040 | if (this.each_iterator == null) { | ||
| 8041 | this.each_iterator = this.h.iterator(); | ||
| 8042 | } | ||
| 8043 | if (this.each_iterator.hasNext()) { | ||
| 8044 | Map.Entry<String, PlObject> entry = this.each_iterator.next(); | ||
| 8045 | String key = entry.getKey(); | ||
| 8046 | aa.push_void(new PlString(key)); | ||
| 8047 | PlObject value = entry.getValue(); | ||
| 8048 | aa.push_void(value); | ||
| 8049 | } | ||
| 8050 | else { | ||
| 8051 | // return empty list | ||
| 8052 | this.each_iterator = null; | ||
| 8053 | } | ||
| 8054 | return aa; | ||
| 8055 | } | ||
| 8056 | public PlObject hset(String s, Object o) { | ||
| 8057 | return this.hset(s, PlJavaObject.fromObject(o)); | ||
| 8058 | } | ||
| 8059 | EOT | ||
| 8060 | , ((map { | ||
| 8061 | my $native = $_; | ||
| 8062 | my $perl = $native_to_perl{$native}; | ||
| 8063 | $native && $perl ? | ||
| 8064 | " public PlObject hset(String s, $native v) { | ||
| 8065 | return this.hset(s, new $perl(v)); | ||
| 8066 | } | ||
| 8067 | " : () | ||
| 8068 | } | ||
| 8069 | sort keys %native_to_perl )) | ||
| 8070 | |||
| 8071 | , <<'EOT' | ||
| 8072 | |||
| 8073 | public String toString() { | ||
| 8074 | // TODO | ||
| 8075 | return "" + this.hashCode(); | ||
| 8076 | } | ||
| 8077 | public int to_int() { | ||
| 8078 | // TODO | ||
| 8079 | return this.hashCode(); | ||
| 8080 | } | ||
| 8081 | public long to_long() { | ||
| 8082 | // TODO | ||
| 8083 | return this.hashCode(); | ||
| 8084 | } | ||
| 8085 | public double to_double() { | ||
| 8086 | return 0.0 + this.to_long(); | ||
| 8087 | } | ||
| 8088 | public boolean to_boolean() { | ||
| 8089 | for (Map.Entry<String, PlObject> entry : this.h) { | ||
| 8090 | return true; | ||
| 8091 | } | ||
| 8092 | return false; | ||
| 8093 | } | ||
| 8094 | public PlObject to_num() { | ||
| 8095 | return this.scalar(); | ||
| 8096 | } | ||
| 8097 | public boolean is_hash() { | ||
| 8098 | return true; | ||
| 8099 | } | ||
| 8100 | public PlScalarImmutable scalar() { | ||
| 8101 | return this.h.scalar(); | ||
| 8102 | } | ||
| 8103 | } | ||
| 8104 | class PlUndef extends PlScalarImmutable { | ||
| 8105 | public PlUndef() { | ||
| 8106 | } | ||
| 8107 | public PlObject apply(int want, PlArray List__) { | ||
| 8108 | // $a->() | ||
| 8109 | PlCORE.die("Can't use an undefined value as a subroutine reference"); | ||
| 8110 | return this; | ||
| 8111 | } | ||
| 8112 | public PlScalarImmutable length() { | ||
| 8113 | return PlCx.UNDEF; | ||
| 8114 | } | ||
| 8115 | public int to_int() { | ||
| 8116 | return 0; | ||
| 8117 | } | ||
| 8118 | public long to_long() { | ||
| 8119 | return 0; | ||
| 8120 | } | ||
| 8121 | public double to_double() { | ||
| 8122 | return 0.0; | ||
| 8123 | } | ||
| 8124 | public String toString() { | ||
| 8125 | return ""; | ||
| 8126 | } | ||
| 8127 | public boolean to_boolean() { | ||
| 8128 | return false; | ||
| 8129 | } | ||
| 8130 | public PlObject to_num() { | ||
| 8131 | return PlCx.INT0; | ||
| 8132 | } | ||
| 8133 | public boolean is_undef() { | ||
| 8134 | return true; | ||
| 8135 | } | ||
| 8136 | |||
| 8137 | public PlObject scalar_deref_strict() { | ||
| 8138 | return PlCORE.die("Can't use an undefined value as a SCALAR reference"); | ||
| 8139 | } | ||
| 8140 | public PlArray array_deref_strict() { | ||
| 8141 | return (PlArray)PlCORE.die("Can't use an undefined value as an ARRAY reference"); | ||
| 8142 | } | ||
| 8143 | public PlHash hash_deref_strict() { | ||
| 8144 | return (PlHash)PlCORE.die("Can't use an undefined value as a HASH reference"); | ||
| 8145 | } | ||
| 8146 | |||
| 8147 | } | ||
| 8148 | class PlBool extends PlScalarImmutable { | ||
| 8149 | private final boolean i; | ||
| 8150 | public PlBool(boolean i) { | ||
| 8151 | this.i = i; | ||
| 8152 | } | ||
| 8153 | |||
| 8154 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 8155 | // want boolean | ||
| 8156 | for (Class[] cl : params) { | ||
| 8157 | if (cl[pos].equals( java.lang.Boolean.TYPE )) { | ||
| 8158 | return new PerlArgumentLookupResult( this.to_boolean(), cl[pos] ); | ||
| 8159 | } | ||
| 8160 | } | ||
| 8161 | // try other things | ||
| 8162 | return super.castToClass(params, pos); | ||
| 8163 | } | ||
| 8164 | |||
| 8165 | public int to_int() { | ||
| 8166 | if (this.i) { | ||
| 8167 | return 1; | ||
| 8168 | } | ||
| 8169 | else { | ||
| 8170 | return 0; | ||
| 8171 | } | ||
| 8172 | } | ||
| 8173 | public long to_long() { | ||
| 8174 | if (this.i) { | ||
| 8175 | return 1; | ||
| 8176 | } | ||
| 8177 | else { | ||
| 8178 | return 0; | ||
| 8179 | } | ||
| 8180 | } | ||
| 8181 | public double to_double() { | ||
| 8182 | if (this.i) { | ||
| 8183 | return 1.0; | ||
| 8184 | } | ||
| 8185 | else { | ||
| 8186 | return 0.0; | ||
| 8187 | } | ||
| 8188 | } | ||
| 8189 | public String toString() { | ||
| 8190 | if (this.i) { | ||
| 8191 | return "1"; | ||
| 8192 | } | ||
| 8193 | else { | ||
| 8194 | return ""; | ||
| 8195 | } | ||
| 8196 | } | ||
| 8197 | public boolean to_boolean() { | ||
| 8198 | return this.i; | ||
| 8199 | } | ||
| 8200 | public PlObject to_num() { | ||
| 8201 | if (i) { | ||
| 8202 | return PlCx.INT1; | ||
| 8203 | } | ||
| 8204 | else { | ||
| 8205 | return PlCx.INT0; | ||
| 8206 | } | ||
| 8207 | } | ||
| 8208 | public boolean is_bool() { | ||
| 8209 | return true; | ||
| 8210 | } | ||
| 8211 | public PlObject _decr() { | ||
| 8212 | // --$x | ||
| 8213 | if (i) { | ||
| 8214 | return PlCx.INT0; | ||
| 8215 | } | ||
| 8216 | else { | ||
| 8217 | return PlCx.MIN1; | ||
| 8218 | } | ||
| 8219 | } | ||
| 8220 | public PlObject _incr() { | ||
| 8221 | // ++$x | ||
| 8222 | if (i) { | ||
| 8223 | return PlCx.INT2; | ||
| 8224 | } | ||
| 8225 | else { | ||
| 8226 | return PlCx.INT1; | ||
| 8227 | } | ||
| 8228 | } | ||
| 8229 | public PlObject neg() { | ||
| 8230 | if (i) { | ||
| 8231 | return PlCx.MIN1; | ||
| 8232 | } | ||
| 8233 | else { | ||
| 8234 | return PlCx.INT0; | ||
| 8235 | } | ||
| 8236 | } | ||
| 8237 | public PlObject apply(int want, PlArray List__) { | ||
| 8238 | if (i) { | ||
| 8239 | // RT #63790: calling PL_sv_yes as a sub is special-cased to silently | ||
| 8240 | // return (so Foo->import() silently fails if import() doesn't exist), | ||
| 8241 | return PerlOp.context(want); | ||
| 8242 | } | ||
| 8243 | else { | ||
| 8244 | return PlCORE.die("Undefined subroutine"); | ||
| 8245 | } | ||
| 8246 | } | ||
| 8247 | } | ||
| 8248 | class PlInt extends PlScalarImmutable { | ||
| 8249 | private final long i; | ||
| 8250 | public PlInt(long i) { | ||
| 8251 | this.i = i; | ||
| 8252 | } | ||
| 8253 | public PlInt(int i) { | ||
| 8254 | this.i = (long)i; | ||
| 8255 | } | ||
| 8256 | |||
| 8257 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 8258 | // TODO - byte - short - int - long - float - double | ||
| 8259 | |||
| 8260 | // want long | ||
| 8261 | for (Class[] cl : params) { | ||
| 8262 | if (cl[pos].equals( java.lang.Long.TYPE )) { | ||
| 8263 | return new PerlArgumentLookupResult( this.to_long(), cl[pos] ); | ||
| 8264 | } | ||
| 8265 | } | ||
| 8266 | // want int | ||
| 8267 | for (Class[] cl : params) { | ||
| 8268 | if (cl[pos].equals( java.lang.Integer.TYPE )) { | ||
| 8269 | return new PerlArgumentLookupResult( this.to_int(), cl[pos] ); | ||
| 8270 | } | ||
| 8271 | } | ||
| 8272 | // want float | ||
| 8273 | for (Class[] cl : params) { | ||
| 8274 | if (cl[pos].equals( java.lang.Float.TYPE )) { | ||
| 8275 | return new PerlArgumentLookupResult( this.to_float(), cl[pos] ); | ||
| 8276 | } | ||
| 8277 | } | ||
| 8278 | // want double | ||
| 8279 | for (Class[] cl : params) { | ||
| 8280 | if (cl[pos].equals( java.lang.Double.TYPE )) { | ||
| 8281 | return new PerlArgumentLookupResult( this.to_double(), cl[pos] ); | ||
| 8282 | } | ||
| 8283 | } | ||
| 8284 | // try other things | ||
| 8285 | return super.castToClass(params, pos); | ||
| 8286 | } | ||
| 8287 | |||
| 8288 | public int to_int() { | ||
| 8289 | return (int)this.i; | ||
| 8290 | } | ||
| 8291 | public long to_long() { | ||
| 8292 | return this.i; | ||
| 8293 | } | ||
| 8294 | public double to_double() { | ||
| 8295 | return (double)(this.i); | ||
| 8296 | } | ||
| 8297 | public String toString() { | ||
| 8298 | return "" + this.i; | ||
| 8299 | } | ||
| 8300 | public boolean to_boolean() { | ||
| 8301 | return this.i != 0; | ||
| 8302 | } | ||
| 8303 | public PlObject to_num() { | ||
| 8304 | return this; | ||
| 8305 | } | ||
| 8306 | public PlObject mod(PlObject o) { | ||
| 8307 | return PerlOp.mod(this, o); | ||
| 8308 | } | ||
| 8309 | public boolean is_int() { | ||
| 8310 | return true; | ||
| 8311 | } | ||
| 8312 | public boolean is_integer_range() { | ||
| 8313 | return true; | ||
| 8314 | } | ||
| 8315 | public PlObject _decr() { | ||
| 8316 | // --$x | ||
| 8317 | return new PlInt(i-1); | ||
| 8318 | } | ||
| 8319 | public PlObject _incr() { | ||
| 8320 | // ++$x | ||
| 8321 | return new PlInt(i+1); | ||
| 8322 | } | ||
| 8323 | public PlObject neg() { | ||
| 8324 | return new PlInt(-i); | ||
| 8325 | } | ||
| 8326 | public PlObject mul2(PlObject s) { | ||
| 8327 | long v = s.to_long(); | ||
| 8328 | // 3037000000 is sqrt(Long.MAX_VALUE) | ||
| 8329 | if (i > 3037000000L || i < -3037000000L || v > 3037000000L || v < -3037000000L) { | ||
| 8330 | return new PlDouble(this.to_double()).mul2(s); | ||
| 8331 | } | ||
| 8332 | return new PlInt(v * i); | ||
| 8333 | } | ||
| 8334 | } | ||
| 8335 | class PlDouble extends PlScalarImmutable { | ||
| 8336 | private final double i; | ||
| 8337 | public PlDouble(double i) { | ||
| 8338 | this.i = i; | ||
| 8339 | } | ||
| 8340 | |||
| 8341 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 8342 | // TODO - byte - short - int - long - float - double | ||
| 8343 | |||
| 8344 | // want double | ||
| 8345 | for (Class[] cl : params) { | ||
| 8346 | if (cl[pos].equals( java.lang.Double.TYPE )) { | ||
| 8347 | return new PerlArgumentLookupResult( this.to_double(), cl[pos] ); | ||
| 8348 | } | ||
| 8349 | } | ||
| 8350 | // want float | ||
| 8351 | for (Class[] cl : params) { | ||
| 8352 | if (cl[pos].equals( java.lang.Float.TYPE )) { | ||
| 8353 | return new PerlArgumentLookupResult( this.to_float(), cl[pos] ); | ||
| 8354 | } | ||
| 8355 | } | ||
| 8356 | // want long | ||
| 8357 | for (Class[] cl : params) { | ||
| 8358 | if (cl[pos].equals( java.lang.Long.TYPE )) { | ||
| 8359 | return new PerlArgumentLookupResult( this.to_long(), cl[pos] ); | ||
| 8360 | } | ||
| 8361 | } | ||
| 8362 | // want int | ||
| 8363 | for (Class[] cl : params) { | ||
| 8364 | if (cl[pos].equals( java.lang.Integer.TYPE )) { | ||
| 8365 | return new PerlArgumentLookupResult( this.to_int(), cl[pos] ); | ||
| 8366 | } | ||
| 8367 | } | ||
| 8368 | // try other things | ||
| 8369 | return super.castToClass(params, pos); | ||
| 8370 | } | ||
| 8371 | |||
| 8372 | public int to_int() { | ||
| 8373 | return (int)(this.i); | ||
| 8374 | } | ||
| 8375 | public long to_long() { | ||
| 8376 | return (long)(this.i); | ||
| 8377 | } | ||
| 8378 | public double to_double() { | ||
| 8379 | return this.i; | ||
| 8380 | } | ||
| 8381 | public String toString() { | ||
| 8382 | if (Double.isNaN(this.i)) { | ||
| 8383 | return "NaN"; | ||
| 8384 | } | ||
| 8385 | if (Double.isInfinite(this.i)) { | ||
| 8386 | return this.i > 0 ? "Inf" : "-Inf"; | ||
| 8387 | } | ||
| 8388 | double v = this.i; | ||
| 8389 | String s; | ||
| 8390 | if ( v < 0.0001 && v > -0.0001 | ||
| 8391 | || v < -1E14 | ||
| 8392 | || v > 1E14 ) | ||
| 8393 | { | ||
| 8394 | // use scientific notation | ||
| 8395 | s = String.format("%20.20e", v); | ||
| 8396 | s = s.replaceAll("\\.?0*e", "e"); | ||
| 8397 | if (s.equals("0e+00") || s.equals("-0e+00")) { | ||
| 8398 | s = "0"; | ||
| 8399 | } | ||
| 8400 | } | ||
| 8401 | else { | ||
| 8402 | s = String.format("%20.20f", v); | ||
| 8403 | s = s.replaceAll("\\.?0*$", ""); | ||
| 8404 | } | ||
| 8405 | return s; | ||
| 8406 | } | ||
| 8407 | public boolean to_boolean() { | ||
| 8408 | return this.i != 0.0; | ||
| 8409 | } | ||
| 8410 | public PlObject to_num() { | ||
| 8411 | return this; | ||
| 8412 | } | ||
| 8413 | public PlObject _decr() { | ||
| 8414 | // --$x | ||
| 8415 | return new PlDouble(i-1); | ||
| 8416 | } | ||
| 8417 | public PlObject _incr() { | ||
| 8418 | // ++$x | ||
| 8419 | return new PlDouble(i+1); | ||
| 8420 | } | ||
| 8421 | public PlObject neg() { | ||
| 8422 | return new PlDouble(-i); | ||
| 8423 | } | ||
| 8424 | public PlObject abs() { | ||
| 8425 | return new PlDouble(i < 0.0 ? -i : i); | ||
| 8426 | } | ||
| 8427 | public PlObject num_cmp(PlObject b) { | ||
| 8428 | if (Double.isNaN(this.i)) { | ||
| 8429 | return PlCx.UNDEF; | ||
| 8430 | } | ||
| 8431 | double bb = b.to_double(); | ||
| 8432 | if (Double.isNaN(bb)) { | ||
| 8433 | return PlCx.UNDEF; | ||
| 8434 | } | ||
| 8435 | int c = ((Double)this.i).compareTo(bb); | ||
| 8436 | return (c == 0 ? PlCx.INT0 : c < 0 ? PlCx.MIN1 : PlCx.INT1); | ||
| 8437 | } | ||
| 8438 | public PlObject num_cmp2(PlObject b) { | ||
| 8439 | if (Double.isNaN(this.i)) { | ||
| 8440 | return PlCx.UNDEF; | ||
| 8441 | } | ||
| 8442 | double bb = b.to_double(); | ||
| 8443 | if (Double.isNaN(bb)) { | ||
| 8444 | return PlCx.UNDEF; | ||
| 8445 | } | ||
| 8446 | int c = ((Double)bb).compareTo(this.i); | ||
| 8447 | return (c == 0 ? PlCx.INT0 : c < 0 ? PlCx.MIN1 : PlCx.INT1); | ||
| 8448 | } | ||
| 8449 | EOT | ||
| 8450 | , ((map { | ||
| 8451 | my $perl = $_; | ||
| 8452 | my $native = $number_binop{$perl}{op}; | ||
| 8453 | my $returns = $number_binop{$perl}{num_returns}; | ||
| 8454 | if ($returns eq 'PlInt') { | ||
| 8455 | " public PlObject ${perl}(PlObject s) { | ||
| 8456 | // num - int, num - num | ||
| 8457 | return new ${returns}( this.to_long() ${native} s.to_long() ); | ||
| 8458 | } | ||
| 8459 | public PlObject ${perl}2(PlObject s) { | ||
| 8460 | // int - num | ||
| 8461 | return new ${returns}( s.to_long() ${native} this.to_long() ); | ||
| 8462 | } | ||
| 8463 | " | ||
| 8464 | } | ||
| 8465 | else { | ||
| 8466 | " public PlObject ${perl}(PlObject s) { | ||
| 8467 | // num - int, num - num | ||
| 8468 | return new ${returns}( this.i ${native} s.to_double() ); | ||
| 8469 | } | ||
| 8470 | public PlObject ${perl}2(PlObject s) { | ||
| 8471 | // int - num | ||
| 8472 | return new ${returns}( s.to_double() ${native} this.i ); | ||
| 8473 | } | ||
| 8474 | " | ||
| 8475 | } | ||
| 8476 | } | ||
| 8477 | sort keys %number_binop )) | ||
| 8478 | |||
| 8479 | , <<'EOT' | ||
| 8480 | public PlObject mod(PlObject o) { | ||
| 8481 | return PerlOp.mod(this, o); | ||
| 8482 | } | ||
| 8483 | public boolean is_num() { | ||
| 8484 | return true; | ||
| 8485 | } | ||
| 8486 | public boolean is_integer_range() { | ||
| 8487 | return !Double.isNaN(i) && i <= Long.MAX_VALUE && i >= Long.MIN_VALUE; | ||
| 8488 | } | ||
| 8489 | } | ||
| 8490 | class PlStringLazyError extends PlString { | ||
| 8491 | private Exception e; | ||
| 8492 | |||
| 8493 | public PlStringLazyError(Exception e) { | ||
| 8494 | this.e = e; | ||
| 8495 | } | ||
| 8496 | public String toString() { | ||
| 8497 | if (this.s == null) { | ||
| 8498 | this.s = e.getClass().getCanonicalName() + ": " + e.getMessage() + "\n" + java.util.Arrays.toString(e.getStackTrace()); | ||
| 8499 | } | ||
| 8500 | return this.s; | ||
| 8501 | } | ||
| 8502 | public boolean to_boolean() { | ||
| 8503 | return true; | ||
| 8504 | } | ||
| 8505 | |||
| 8506 | public PlObject abs() { | ||
| 8507 | this.toString(); | ||
| 8508 | return this.parse().abs(); | ||
| 8509 | } | ||
| 8510 | public PlObject num_cmp(PlObject b) { | ||
| 8511 | this.toString(); | ||
| 8512 | return this.parse().num_cmp(b); | ||
| 8513 | } | ||
| 8514 | public PlObject num_cmp2(PlObject b) { | ||
| 8515 | this.toString(); | ||
| 8516 | return b.num_cmp(this.parse()); | ||
| 8517 | } | ||
| 8518 | public boolean is_integer_range() { | ||
| 8519 | this.toString(); | ||
| 8520 | return this.parse().is_integer_range(); | ||
| 8521 | } | ||
| 8522 | EOT | ||
| 8523 | , ((map { | ||
| 8524 | my $perl = $_; | ||
| 8525 | my $native = $number_binop{$perl}{op}; | ||
| 8526 | my $returns = $number_binop{$perl}{returns}; | ||
| 8527 | my $num_returns = $number_binop{$perl}{num_returns}; | ||
| 8528 | if ($returns eq 'PlDouble') { | ||
| 8529 | " public PlObject ${perl}(PlObject b) { | ||
| 8530 | // 'num' - int, 'num' - num | ||
| 8531 | this.toString(); | ||
| 8532 | return this.parse().${perl}(b); | ||
| 8533 | } | ||
| 8534 | public PlObject ${perl}2(PlObject b) { | ||
| 8535 | // int - 'num' | ||
| 8536 | this.toString(); | ||
| 8537 | return b.${perl}(this.parse()); | ||
| 8538 | } | ||
| 8539 | " | ||
| 8540 | } | ||
| 8541 | else { | ||
| 8542 | " public PlObject ${perl}(PlObject b) { | ||
| 8543 | // 'num' - int, 'num' - num | ||
| 8544 | this.toString(); | ||
| 8545 | return this.parse().${perl}(b); | ||
| 8546 | } | ||
| 8547 | public PlObject ${perl}2(PlObject b) { | ||
| 8548 | // int - 'num' | ||
| 8549 | this.toString(); | ||
| 8550 | return b.${perl}(this.parse()); | ||
| 8551 | } | ||
| 8552 | " | ||
| 8553 | } | ||
| 8554 | } | ||
| 8555 | sort keys %number_binop )) | ||
| 8556 | |||
| 8557 | , <<'EOT' | ||
| 8558 | } | ||
| 8559 | class PlStringConstant extends PlString { | ||
| 8560 | public static HashMap<String, PlStringConstant> constants = new HashMap<String, PlStringConstant>(); | ||
| 8561 | |||
| 8562 | // inherited: String s | ||
| 8563 | private PlClass cls; | ||
| 8564 | public PlLvalue codeRef; // CODE | ||
| 8565 | public PlLvalue scalarRef; // SCALAR | ||
| 8566 | public PlLvalue arrayRef; // ARRAY | ||
| 8567 | public PlLvalue hashRef; // HASH | ||
| 8568 | public PlLvalue fileRef; // IO | ||
| 8569 | // TODO - "FORMAT" | ||
| 8570 | |||
| 8571 | public String namespace; // PACKAGE - "main" in "main::x"; maybe null | ||
| 8572 | public String name; // NAME - "x" in "main::x"; maybe null | ||
| 8573 | |||
| 8574 | public PlStringConstant(String s) { | ||
| 8575 | super(s); | ||
| 8576 | |||
| 8577 | int pos = s.lastIndexOf("::"); | ||
| 8578 | if (pos != -1) { | ||
| 8579 | this.namespace = s.substring(0, pos); | ||
| 8580 | this.name = s.substring(pos+2); | ||
| 8581 | } | ||
| 8582 | |||
| 8583 | this.codeRef = new PlLvalue(); | ||
| 8584 | this.scalarRef = new PlLvalue(); | ||
| 8585 | this.arrayRef = new PlLvalue(new PlArrayRef()); | ||
| 8586 | this.hashRef = new PlLvalue(new PlHashRef()); | ||
| 8587 | this.fileRef = new PlLvalue(new PlFileHandle(s)); | ||
| 8588 | } | ||
| 8589 | |||
| 8590 | public static PlStringConstant getConstant(String s) { | ||
| 8591 | PlStringConstant v = constants.get(s); | ||
| 8592 | if (v == null) { | ||
| 8593 | v = new PlStringConstant(s); | ||
| 8594 | constants.put(s, v); | ||
| 8595 | } | ||
| 8596 | return v; | ||
| 8597 | } | ||
| 8598 | |||
| 8599 | public PlClass blessed_class() { | ||
| 8600 | if (cls == null) { | ||
| 8601 | cls = PlClass.getInstance(s); | ||
| 8602 | } | ||
| 8603 | return cls; | ||
| 8604 | } | ||
| 8605 | public PlObject cset(PlObject v) { | ||
| 8606 | PlClass.getInstance(this.namespace).invalidate_method_cache(this.name, 0); | ||
| 8607 | return this.codeRef.set(v); | ||
| 8608 | } | ||
| 8609 | public PlObject cset_alias(PlLvalue v) { | ||
| 8610 | PlClass.getInstance(this.namespace).invalidate_method_cache(this.name, 0); | ||
| 8611 | return this.codeRef = v; | ||
| 8612 | } | ||
| 8613 | public PlObject apply(int want, PlArray List__) { | ||
| 8614 | if (this.codeRef.is_undef()) { | ||
| 8615 | if (this.namespace != null) { | ||
| 8616 | PlLvalue autoload = PlV.cget_no_autoload(this.namespace + "::AUTOLOAD"); | ||
| 8617 | if ( autoload.is_coderef() ) { | ||
| 8618 | PlV.sset(this.namespace + "::AUTOLOAD", new PlString(this.s)); | ||
| 8619 | return autoload.apply(want, List__); | ||
| 8620 | } | ||
| 8621 | } | ||
| 8622 | return PlCORE.die("Undefined subroutine &" + name + " called"); | ||
| 8623 | } | ||
| 8624 | return this.codeRef.apply(want, List__); | ||
| 8625 | } | ||
| 8626 | } | ||
| 8627 | class PlString extends PlScalarImmutable { | ||
| 8628 | public java.lang.String s; | ||
| 8629 | private PlObject numericValue; | ||
| 8630 | |||
| 8631 | public PlString() { | ||
| 8632 | } | ||
| 8633 | public PlString(String s) { | ||
| 8634 | // if (s == null) { | ||
| 8635 | // s = ""; | ||
| 8636 | // } | ||
| 8637 | this.s = s; | ||
| 8638 | } | ||
| 8639 | public PlString(char s) { | ||
| 8640 | this.s = "" + s; | ||
| 8641 | } | ||
| 8642 | public PlObject scalar_deref_lvalue(String namespace) { | ||
| 8643 | return this.scalar_deref(namespace); | ||
| 8644 | } | ||
| 8645 | public PlObject scalar_deref(String namespace) { | ||
| 8646 | if (s.length() == 1) { | ||
| 8647 | if (this._looks_like_non_negative_integer()) { | ||
| 8648 | return PerlOp.regex_var(this.to_int()); | ||
| 8649 | } | ||
| 8650 | if (s.equals("&") || s.equals("`") || s.equals("'")) { | ||
| 8651 | return PerlOp.regex_var(s); | ||
| 8652 | } | ||
| 8653 | if (s.equals("$")) { | ||
| 8654 | return PerlOp.getPID(); | ||
| 8655 | } | ||
| 8656 | |||
| 8657 | // special variables like $_ $\ | ||
| 8658 | EOT | ||
| 8659 | , (( map { | ||
| 8660 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 8661 | my $perl_name = $_; | ||
| 8662 | $perl_name = "\\\\" if $perl_name eq "\\"; | ||
| 8663 | " | ||
| 8664 | if (s.equals(\"${perl_name}\")) { | ||
| 8665 | return PlV.${java_name}; | ||
| 8666 | } | ||
| 8667 | " | ||
| 8668 | } | ||
| 8669 | sort keys %Perlito5::Java::special_scalar | ||
| 8670 | )) | ||
| 8671 | , <<'EOT' | ||
| 8672 | |||
| 8673 | } | ||
| 8674 | if (s.indexOf("::") == -1) { | ||
| 8675 | return PlV.sget( namespace + "::" + s ); | ||
| 8676 | } | ||
| 8677 | |||
| 8678 | // special variables like $_ $\ | ||
| 8679 | EOT | ||
| 8680 | , (( map { | ||
| 8681 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 8682 | my $perl_name = $_; | ||
| 8683 | $perl_name = "\\\\" if $perl_name eq "\\"; | ||
| 8684 | " | ||
| 8685 | if (s.equals(\"main::${perl_name}\")) { | ||
| 8686 | return PlV.${java_name}; | ||
| 8687 | } | ||
| 8688 | " | ||
| 8689 | } | ||
| 8690 | sort keys %Perlito5::Java::special_scalar | ||
| 8691 | )) | ||
| 8692 | , <<'EOT' | ||
| 8693 | |||
| 8694 | return PlV.sget(s); | ||
| 8695 | } | ||
| 8696 | public PlObject scalar_deref_strict() { | ||
| 8697 | return PlCORE.die("Can't use string (\"" + this.s + "\") as a SCALAR ref while \"strict refs\" in use"); | ||
| 8698 | } | ||
| 8699 | public PlObject scalar_deref_set(String namespace, PlObject v) { | ||
| 8700 | if (s.length() == 1) { | ||
| 8701 | |||
| 8702 | // special variables like $_ $\ | ||
| 8703 | EOT | ||
| 8704 | , (( map { | ||
| 8705 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 8706 | my $perl_name = $_; | ||
| 8707 | $perl_name = "\\\\" if $perl_name eq "\\"; | ||
| 8708 | " | ||
| 8709 | if (s.equals(\"${perl_name}\")) { | ||
| 8710 | return PlV.${java_name}.set(v); | ||
| 8711 | } | ||
| 8712 | " | ||
| 8713 | } | ||
| 8714 | sort keys %Perlito5::Java::special_scalar | ||
| 8715 | )) | ||
| 8716 | , <<'EOT' | ||
| 8717 | |||
| 8718 | } | ||
| 8719 | if (s.indexOf("::") == -1) { | ||
| 8720 | return PlV.sset( namespace + "::" + s, v ); | ||
| 8721 | } | ||
| 8722 | |||
| 8723 | // special variables like $_ $\ | ||
| 8724 | EOT | ||
| 8725 | , (( map { | ||
| 8726 | my $java_name = $Perlito5::Java::special_scalar{$_}; | ||
| 8727 | my $perl_name = $_; | ||
| 8728 | $perl_name = "\\\\" if $perl_name eq "\\"; | ||
| 8729 | " | ||
| 8730 | if (s.equals(\"main::${perl_name}\")) { | ||
| 8731 | return PlV.${java_name}.set(v); | ||
| 8732 | } | ||
| 8733 | " | ||
| 8734 | } | ||
| 8735 | sort keys %Perlito5::Java::special_scalar | ||
| 8736 | )) | ||
| 8737 | , <<'EOT' | ||
| 8738 | |||
| 8739 | return PlV.sset(s, v); | ||
| 8740 | } | ||
| 8741 | public PlArray array_deref_lvalue() { | ||
| 8742 | // TODO - concatenate current namespace if needed | ||
| 8743 | return PlV.array_get(s); | ||
| 8744 | } | ||
| 8745 | public PlArray array_deref_strict() { | ||
| 8746 | return (PlArray)PlCORE.die("Can't use string (\"" + this.s + "\") as an ARRAY ref while \"strict refs\" in use"); | ||
| 8747 | } | ||
| 8748 | public PlArray array_deref(String namespace) { | ||
| 8749 | if (s.indexOf("::") == -1) { | ||
| 8750 | return PlV.array_get( namespace + "::" + s ); | ||
| 8751 | } | ||
| 8752 | return PlV.array_get(s); | ||
| 8753 | } | ||
| 8754 | public PlObject array_deref_set(PlObject v) { | ||
| 8755 | // TODO - concatenate current namespace if needed | ||
| 8756 | return PlV.aset(s, v); | ||
| 8757 | } | ||
| 8758 | public PlObject hget(String i) { | ||
| 8759 | return this.hash_deref("").hget(i); | ||
| 8760 | } | ||
| 8761 | public PlObject hash_deref(String namespace) { | ||
| 8762 | int pos = s.lastIndexOf("::"); | ||
| 8763 | if (pos == -1) { | ||
| 8764 | return PlV.hash_get( namespace + "::" + s ); | ||
| 8765 | } | ||
| 8766 | else if (pos == s.length() - 2) { | ||
| 8767 | // %{"Module::"} | ||
| 8768 | return PerlOp.getSymbolTable(this.s); | ||
| 8769 | } | ||
| 8770 | return PlV.hash_get(s); | ||
| 8771 | } | ||
| 8772 | public PlHash hash_deref_strict() { | ||
| 8773 | return (PlHash)PlCORE.die("Can't use string (\"" + this.s + "\") as a HASH ref while \"strict refs\" in use"); | ||
| 8774 | } | ||
| 8775 | public PlObject hash_deref_set(PlObject v) { | ||
| 8776 | // TODO - concatenate current namespace if needed | ||
| 8777 | return PlV.hash_set(s, v); | ||
| 8778 | } | ||
| 8779 | public PlObject hdelete(int want, PlObject a) { | ||
| 8780 | // TODO - concatenate current namespace if needed | ||
| 8781 | int pos = s.lastIndexOf("::"); | ||
| 8782 | // if (pos == -1) { | ||
| 8783 | // s = namespace + "::" + s; | ||
| 8784 | // } | ||
| 8785 | if (pos == s.length() - 2) { | ||
| 8786 | // %{"Module::"} | ||
| 8787 | return PerlOp.deleteSymbolTable(this.s, a); | ||
| 8788 | } | ||
| 8789 | return PlV.hash_get(s).hdelete(want, a); | ||
| 8790 | } | ||
| 8791 | public PlClass blessed_class() { | ||
| 8792 | return PlClass.getInstance(s); | ||
| 8793 | } | ||
| 8794 | |||
| 8795 | public PlObject parse() { | ||
| 8796 | if (numericValue == null) { | ||
| 8797 | numericValue = this._parse(); | ||
| 8798 | } | ||
| 8799 | return numericValue; | ||
| 8800 | } | ||
| 8801 | private boolean _looks_like_non_negative_integer() { | ||
| 8802 | final int length = s.length(); | ||
| 8803 | for (int offset = 0; offset < length; ) { | ||
| 8804 | final int c = s.codePointAt(offset); | ||
| 8805 | switch (c) { | ||
| 8806 | case '0': case '1': case '2': case '3': case '4': | ||
| 8807 | case '5': case '6': case '7': case '8': case '9': | ||
| 8808 | break; | ||
| 8809 | default: | ||
| 8810 | return false; | ||
| 8811 | } | ||
| 8812 | } | ||
| 8813 | return true; | ||
| 8814 | } | ||
| 8815 | private PlObject _parse_exp(int length, int signal, int offset, int next) { | ||
| 8816 | // 123.45E^^^ | ||
| 8817 | int offset_orig = next; | ||
| 8818 | int offset3 = next; | ||
| 8819 | if (offset3 >= length) { | ||
| 8820 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset_orig - 1))); | ||
| 8821 | } | ||
| 8822 | final int sig = s.codePointAt(offset3); | ||
| 8823 | if (sig == '+' || sig == '-') { | ||
| 8824 | offset3++; | ||
| 8825 | if (offset3 >= length) { | ||
| 8826 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset_orig - 1))); | ||
| 8827 | } | ||
| 8828 | } | ||
| 8829 | final int num = s.codePointAt(offset3); | ||
| 8830 | if (num < '0' || num > '9') { | ||
| 8831 | // illegal exp | ||
| 8832 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset_orig - 1))); | ||
| 8833 | } | ||
| 8834 | for ( ; offset3 < length; ) { | ||
| 8835 | final int c3 = s.codePointAt(offset3); | ||
| 8836 | switch (c3) { | ||
| 8837 | case '0': case '1': case '2': case '3': case '4': | ||
| 8838 | case '5': case '6': case '7': case '8': case '9': | ||
| 8839 | break; | ||
| 8840 | default: // invalid | ||
| 8841 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset3))); | ||
| 8842 | } | ||
| 8843 | offset3++; | ||
| 8844 | } | ||
| 8845 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset3))); | ||
| 8846 | } | ||
| 8847 | private PlObject _parse_dot(int length, int signal, int offset, int next) { | ||
| 8848 | // 123.^^^ | ||
| 8849 | int offset3 = next; | ||
| 8850 | for ( ; offset3 < length; ) { | ||
| 8851 | final int c3 = s.codePointAt(offset3); | ||
| 8852 | switch (c3) { | ||
| 8853 | case '0': case '1': case '2': case '3': case '4': | ||
| 8854 | case '5': case '6': case '7': case '8': case '9': | ||
| 8855 | break; | ||
| 8856 | case 'E': case 'e': | ||
| 8857 | // start exponential part | ||
| 8858 | return _parse_exp(length, signal, offset, offset3+1); | ||
| 8859 | default: // invalid | ||
| 8860 | try { | ||
| 8861 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset3))); | ||
| 8862 | } | ||
| 8863 | catch (NumberFormatException e) { | ||
| 8864 | // string is "." | ||
| 8865 | return PlCx.INT0; // string is "." | ||
| 8866 | } | ||
| 8867 | } | ||
| 8868 | offset3++; | ||
| 8869 | } | ||
| 8870 | if (offset3 == 1) { | ||
| 8871 | return PlCx.INT0; // string is "." | ||
| 8872 | } | ||
| 8873 | return new PlDouble(Double.parseDouble(this.s.substring(0, offset3))); | ||
| 8874 | } | ||
| 8875 | private PlObject _parse() { | ||
| 8876 | final int length = s.length(); | ||
| 8877 | int signal = 0; | ||
| 8878 | for (int offset = 0; offset < length; ) { | ||
| 8879 | final int c = s.codePointAt(offset); | ||
| 8880 | switch (c) { | ||
| 8881 | case 'i': case 'I': | ||
| 8882 | if (length > 2 && this.s.substring(offset, offset+3).equalsIgnoreCase("inf")) { | ||
| 8883 | if (signal < 0) { | ||
| 8884 | return new PlDouble(Double.NEGATIVE_INFINITY); | ||
| 8885 | } | ||
| 8886 | else { | ||
| 8887 | return new PlDouble(Double.POSITIVE_INFINITY); | ||
| 8888 | } | ||
| 8889 | } | ||
| 8890 | return PlCx.INT0; | ||
| 8891 | case 'n': case 'N': | ||
| 8892 | if (length > 2 && this.s.substring(offset, offset+3).equalsIgnoreCase("nan")) { | ||
| 8893 | return new PlDouble(Double.NaN); | ||
| 8894 | } | ||
| 8895 | return PlCx.INT0; | ||
| 8896 | case '.': // starts with dot | ||
| 8897 | if (signal != 0) { | ||
| 8898 | signal = 1; | ||
| 8899 | } | ||
| 8900 | return _parse_dot(length, signal, offset, offset+1); | ||
| 8901 | case '0': case '1': case '2': case '3': case '4': | ||
| 8902 | case '5': case '6': case '7': case '8': case '9': | ||
| 8903 | // starts with number | ||
| 8904 | if (signal == 0) { | ||
| 8905 | signal = 1; | ||
| 8906 | } | ||
| 8907 | int offset2 = offset+1; | ||
| 8908 | for ( ; offset2 < length; ) { | ||
| 8909 | final int c2 = s.codePointAt(offset2); | ||
| 8910 | switch (c2) { | ||
| 8911 | case '0': case '1': case '2': case '3': case '4': | ||
| 8912 | case '5': case '6': case '7': case '8': case '9': | ||
| 8913 | // more numbers | ||
| 8914 | break; | ||
| 8915 | case '.': | ||
| 8916 | // start decimal part | ||
| 8917 | return _parse_dot(length, signal, offset, offset2+1); | ||
| 8918 | case 'E': case 'e': | ||
| 8919 | // start exponential part | ||
| 8920 | return _parse_exp(length, signal, offset, offset2+1); | ||
| 8921 | default: | ||
| 8922 | // return integer | ||
| 8923 | try { | ||
| 8924 | if (signal < 0) { | ||
| 8925 | return new PlInt(-Long.parseLong(this.s.substring(offset, offset2))); | ||
| 8926 | } | ||
| 8927 | else { | ||
| 8928 | return new PlInt(Long.parseLong(this.s.substring(offset, offset2))); | ||
| 8929 | } | ||
| 8930 | } | ||
| 8931 | catch (NumberFormatException e) { | ||
| 8932 | return new PlDouble(Double.parseDouble(this.s.substring(offset, offset2))); | ||
| 8933 | } | ||
| 8934 | } | ||
| 8935 | offset2++; | ||
| 8936 | } | ||
| 8937 | // integer | ||
| 8938 | try { | ||
| 8939 | if (signal < 0) { | ||
| 8940 | return new PlInt(-Long.parseLong(this.s.substring(offset, offset2))); | ||
| 8941 | } | ||
| 8942 | else { | ||
| 8943 | return new PlInt(Long.parseLong(this.s.substring(offset, offset2))); | ||
| 8944 | } | ||
| 8945 | } | ||
| 8946 | catch (NumberFormatException e) { | ||
| 8947 | return new PlDouble(Double.parseDouble(this.s.substring(offset, offset2))); | ||
| 8948 | } | ||
| 8949 | case '+': // starts with + | ||
| 8950 | if (signal != 0) { | ||
| 8951 | // invalid | ||
| 8952 | return PlCx.INT0; | ||
| 8953 | } | ||
| 8954 | signal = 1; | ||
| 8955 | break; | ||
| 8956 | case '-': // starts with - | ||
| 8957 | if (signal != 0) { | ||
| 8958 | // invalid | ||
| 8959 | return PlCx.INT0; | ||
| 8960 | } | ||
| 8961 | signal = -1; | ||
| 8962 | break; | ||
| 8963 | case ' ': case '\t': case '\n': case '\r': | ||
| 8964 | // starts with space | ||
| 8965 | if (signal != 0) { | ||
| 8966 | // invalid | ||
| 8967 | return PlCx.INT0; | ||
| 8968 | } | ||
| 8969 | break; | ||
| 8970 | default: // invalid | ||
| 8971 | return PlCx.INT0; | ||
| 8972 | } | ||
| 8973 | offset++; | ||
| 8974 | } | ||
| 8975 | return PlCx.INT0; | ||
| 8976 | } | ||
| 8977 | public int to_int() { | ||
| 8978 | return this.parse().to_int(); | ||
| 8979 | } | ||
| 8980 | public long to_long() { | ||
| 8981 | return this.parse().to_long(); | ||
| 8982 | } | ||
| 8983 | public double to_double() { | ||
| 8984 | return this.parse().to_double(); | ||
| 8985 | } | ||
| 8986 | public String toString() { | ||
| 8987 | return this.s; | ||
| 8988 | } | ||
| 8989 | public boolean to_boolean() { | ||
| 8990 | return !( this.s.equals("") || this.s.equals("0") ); | ||
| 8991 | } | ||
| 8992 | public PlObject to_num() { | ||
| 8993 | return this.parse(); | ||
| 8994 | } | ||
| 8995 | public char to_char() { | ||
| 8996 | if (this.s.length() == 0) { | ||
| 8997 | return '\u0000'; | ||
| 8998 | } | ||
| 8999 | return this.s.charAt(0); | ||
| 9000 | } | ||
| 9001 | public boolean is_string() { | ||
| 9002 | return true; | ||
| 9003 | } | ||
| 9004 | public boolean boolean_str_le(String b) { | ||
| 9005 | return this.s.compareTo(b) <= 0; | ||
| 9006 | } | ||
| 9007 | public int int_length() { | ||
| 9008 | return this.s.length(); | ||
| 9009 | } | ||
| 9010 | public PlScalarImmutable length() { | ||
| 9011 | return new PlInt(this.s.length()); | ||
| 9012 | } | ||
| 9013 | public PlObject _decr() { | ||
| 9014 | // --$x | ||
| 9015 | return this.add(PlCx.MIN1); | ||
| 9016 | } | ||
| 9017 | |||
| 9018 | // $x++ when $x is PlString | ||
| 9019 | private static final String _string_increment(String s) { | ||
| 9020 | if (s.length() < 2) { | ||
| 9021 | final int c = s.codePointAt(0); | ||
| 9022 | if ((c >= '0' && c <= '8') || (c >= 'A' && c <= 'Y') || (c >= 'a' && c <= 'y')) { | ||
| 9023 | return "" + (char)(c + 1); | ||
| 9024 | } | ||
| 9025 | if (c == '9') { | ||
| 9026 | return "10"; | ||
| 9027 | } | ||
| 9028 | if (c == 'Z') { | ||
| 9029 | return "AA"; | ||
| 9030 | } | ||
| 9031 | if (c == 'z') { | ||
| 9032 | return "aa"; | ||
| 9033 | } | ||
| 9034 | return "1"; | ||
| 9035 | } | ||
| 9036 | String c = _string_increment(s.substring(s.length()-1, s.length())); | ||
| 9037 | if (c.length() == 1) { | ||
| 9038 | // AAAC => AAAD | ||
| 9039 | return s.substring(0, s.length()-1) + c; | ||
| 9040 | } | ||
| 9041 | // AAAZ => AABA | ||
| 9042 | return _string_increment(s.substring(0, s.length()-1)) + c.substring(c.length()-1, c.length()); | ||
| 9043 | } | ||
| 9044 | public PlObject _incr() { | ||
| 9045 | // ++$x | ||
| 9046 | final int length = s.length(); | ||
| 9047 | if (length == 0) { | ||
| 9048 | return PlCx.INT1; | ||
| 9049 | } | ||
| 9050 | int c = this.s.codePointAt(0); | ||
| 9051 | switch (c) { | ||
| 9052 | case ' ': case '\t': case '\n': case '\r': | ||
| 9053 | case '+': case '-': case '.': | ||
| 9054 | case '0': case '1': case '2': case '3': case '4': | ||
| 9055 | case '5': case '6': case '7': case '8': case '9': | ||
| 9056 | return this.add(PlCx.INT1); | ||
| 9057 | } | ||
| 9058 | c = s.codePointAt(length - 1); | ||
| 9059 | if ((c >= '0' && c <= '8') || (c >= 'A' && c <= 'Y') || (c >= 'a' && c <= 'y')) { | ||
| 9060 | return new PlString(s.substring(0, length-1) + (char)(c + 1)); | ||
| 9061 | } | ||
| 9062 | return new PlString(_string_increment(this.s)); | ||
| 9063 | } | ||
| 9064 | public PlObject neg() { | ||
| 9065 | final int length = s.length(); | ||
| 9066 | if (length == 0) { | ||
| 9067 | return PlCx.INT0; | ||
| 9068 | } | ||
| 9069 | final int c = this.s.codePointAt(0); | ||
| 9070 | switch (c) { | ||
| 9071 | case '+': case '-': | ||
| 9072 | if (c == '+') { | ||
| 9073 | return new PlString( '-' + s.substring(1) ); | ||
| 9074 | } | ||
| 9075 | if (c == '-') { | ||
| 9076 | return new PlString( '+' + s.substring(1) ); | ||
| 9077 | } | ||
| 9078 | case '.': | ||
| 9079 | case ' ': case '\t': case '\n': case '\r': | ||
| 9080 | case '0': case '1': case '2': case '3': case '4': | ||
| 9081 | case '5': case '6': case '7': case '8': case '9': | ||
| 9082 | return this.parse().neg(); | ||
| 9083 | } | ||
| 9084 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { | ||
| 9085 | return new PlString( '-' + s ); | ||
| 9086 | } | ||
| 9087 | return PlCx.INT0; | ||
| 9088 | } | ||
| 9089 | public PlObject abs() { | ||
| 9090 | return this.parse().abs(); | ||
| 9091 | } | ||
| 9092 | public PlObject num_cmp(PlObject b) { | ||
| 9093 | return this.parse().num_cmp(b); | ||
| 9094 | } | ||
| 9095 | public PlObject num_cmp2(PlObject b) { | ||
| 9096 | return b.num_cmp(this.parse()); | ||
| 9097 | } | ||
| 9098 | public boolean is_integer_range() { | ||
| 9099 | return this.parse().is_integer_range(); | ||
| 9100 | } | ||
| 9101 | EOT | ||
| 9102 | , ((map { | ||
| 9103 | my $perl = $_; | ||
| 9104 | my $native = $number_binop{$perl}{op}; | ||
| 9105 | my $returns = $number_binop{$perl}{returns}; | ||
| 9106 | my $num_returns = $number_binop{$perl}{num_returns}; | ||
| 9107 | if ($returns eq 'PlDouble') { | ||
| 9108 | " public PlObject ${perl}(PlObject b) { | ||
| 9109 | // 'num' - int, 'num' - num | ||
| 9110 | return this.parse().${perl}(b); | ||
| 9111 | } | ||
| 9112 | public PlObject ${perl}2(PlObject b) { | ||
| 9113 | // int - 'num' | ||
| 9114 | return b.${perl}(this.parse()); | ||
| 9115 | } | ||
| 9116 | " | ||
| 9117 | } | ||
| 9118 | else { | ||
| 9119 | " public PlObject ${perl}(PlObject b) { | ||
| 9120 | // 'num' - int, 'num' - num | ||
| 9121 | return this.parse().${perl}(b); | ||
| 9122 | } | ||
| 9123 | public PlObject ${perl}2(PlObject b) { | ||
| 9124 | // int - 'num' | ||
| 9125 | return b.${perl}(this.parse()); | ||
| 9126 | } | ||
| 9127 | " | ||
| 9128 | } | ||
| 9129 | } | ||
| 9130 | sort keys %number_binop )) | ||
| 9131 | |||
| 9132 | , <<'EOT' | ||
| 9133 | } | ||
| 9134 | EOT | ||
| 9135 | |||
| 9136 | # Java API | ||
| 9137 | # work in progress | ||
| 9138 | # See "Java" and "Packages" object in Nashorn | ||
| 9139 | # https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html | ||
| 9140 | |||
| 9141 | , <<'EOT' | ||
| 9142 | class PlJavaObject extends PlReference { | ||
| 9143 | private Object stuff; | ||
| 9144 | |||
| 9145 | static PlObject fromObject(Object o) { | ||
| 9146 | // Runtime casting | ||
| 9147 | if (o == null) { | ||
| 9148 | return PlCx.UNDEF; | ||
| 9149 | } | ||
| 9150 | if (o instanceof PlObject) { | ||
| 9151 | // this is already a Perl object | ||
| 9152 | return (PlObject)o; | ||
| 9153 | } | ||
| 9154 | if (o instanceof Number) { | ||
| 9155 | if (o instanceof Byte) { | ||
| 9156 | return new PlInt((Byte)o); | ||
| 9157 | } | ||
| 9158 | if (o instanceof Short) { | ||
| 9159 | return new PlInt((Short)o); | ||
| 9160 | } | ||
| 9161 | if (o instanceof Integer) { | ||
| 9162 | return new PlInt((Integer)o); | ||
| 9163 | } | ||
| 9164 | if (o instanceof Long) { | ||
| 9165 | return new PlInt((Long)o); | ||
| 9166 | } | ||
| 9167 | if (o instanceof Float) { | ||
| 9168 | return new PlDouble((Float)o); | ||
| 9169 | } | ||
| 9170 | if (o instanceof Double) { | ||
| 9171 | return new PlDouble((Double)o); | ||
| 9172 | } | ||
| 9173 | // TODO - there are more Number types | ||
| 9174 | } | ||
| 9175 | if (o instanceof Character) { | ||
| 9176 | return new PlString((Character)o); | ||
| 9177 | } | ||
| 9178 | if (o instanceof String) { | ||
| 9179 | return new PlString((String)o); | ||
| 9180 | } | ||
| 9181 | if (o instanceof Boolean) { | ||
| 9182 | return new PlBool((Boolean)o); | ||
| 9183 | } | ||
| 9184 | // TODO - more castings from %java_classes | ||
| 9185 | return new PlJavaObject(o); | ||
| 9186 | } | ||
| 9187 | |||
| 9188 | public PerlArgumentLookupResult castToClass(ArrayList<Class[]> params, int pos) { | ||
| 9189 | // want same Java class | ||
| 9190 | // System.out.println("PerlArgumentLookupResult castToClass from " + this.ref_str()); | ||
| 9191 | for (Class[] cl : params) { | ||
| 9192 | // System.out.println(" compare to " + cl[pos]); | ||
| 9193 | if (cl[pos].equals( this.stuff.getClass() )) { | ||
| 9194 | // System.out.println(" match"); | ||
| 9195 | return new PerlArgumentLookupResult( this.stuff, cl[pos] ); | ||
| 9196 | } | ||
| 9197 | // try again with Class.cast() | ||
| 9198 | try { | ||
| 9199 | cl[pos].cast( this.stuff ); | ||
| 9200 | // cast returns ok | ||
| 9201 | // System.out.println(" can cast()"); | ||
| 9202 | return new PerlArgumentLookupResult( this.stuff, cl[pos] ); | ||
| 9203 | } | ||
| 9204 | catch (ClassCastException e) { | ||
| 9205 | // System.out.println(" cannot cast()"); | ||
| 9206 | } | ||
| 9207 | } | ||
| 9208 | // try other things | ||
| 9209 | return super.castToClass(params, pos); | ||
| 9210 | } | ||
| 9211 | |||
| 9212 | public PlJavaObject(Object stuff) { | ||
| 9213 | this.stuff = stuff; | ||
| 9214 | } | ||
| 9215 | public Object toJava() { | ||
| 9216 | return this.stuff; | ||
| 9217 | } | ||
| 9218 | public PlString ref() { | ||
| 9219 | return new PlString(stuff.getClass().getCanonicalName()); | ||
| 9220 | } | ||
| 9221 | public String ref_str() { | ||
| 9222 | return stuff.getClass().getCanonicalName(); | ||
| 9223 | } | ||
| 9224 | public boolean ref_boolean() { | ||
| 9225 | return true; | ||
| 9226 | } | ||
| 9227 | |||
| 9228 | public PlObject reftype() { | ||
| 9229 | // Scalar::Util::reftype() | ||
| 9230 | return ref(); | ||
| 9231 | } | ||
| 9232 | public String toString() { | ||
| 9233 | if (stuff == null) { | ||
| 9234 | return ""; | ||
| 9235 | } | ||
| 9236 | return this.stuff.toString(); | ||
| 9237 | } | ||
| 9238 | public boolean to_boolean() { | ||
| 9239 | if (stuff == null) { | ||
| 9240 | return false; | ||
| 9241 | } | ||
| 9242 | return true; | ||
| 9243 | } | ||
| 9244 | public boolean is_JavaObject() { | ||
| 9245 | return true; | ||
| 9246 | } | ||
| 9247 | public boolean is_undef() { | ||
| 9248 | return stuff == null; | ||
| 9249 | } | ||
| 9250 | public PlObject clone() throws CloneNotSupportedException { | ||
| 9251 | // TODO - test if implements 'Cloneable' and call stuff.clone() if possible | ||
| 9252 | return this; | ||
| 9253 | } | ||
| 9254 | } | ||
| 9255 | EOT | ||
| 9256 | |||
| 9257 | , <<'EOT' | ||
| 9258 | // end Perl-Java runtime | ||
| 9259 | EOT | ||
| 9260 | ); | ||
| 9261 | } # end of emit_java() | ||
| 9262 | |||
| 9263 | 1; | ||
| 9264 | |||
| 9265 | __END__ |