@@ -144,7 +144,7 @@ public static RuntimeList caller(RuntimeList args, int ctx) {
144
144
RuntimeList res = new RuntimeList ();
145
145
int frame = 0 ;
146
146
if (!args .elements .isEmpty ()) {
147
- frame = ((RuntimeScalar ) args .elements .get ( 0 )).getInt ();
147
+ frame = ((RuntimeScalar ) args .elements .getFirst ( )).getInt ();
148
148
}
149
149
CallerStack .CallerInfo info = CallerStack .peek ();
150
150
if (info == null ) {
@@ -154,7 +154,7 @@ public static RuntimeList caller(RuntimeList args, int ctx) {
154
154
frame ++; // frame 0 is the current method, so we need to increment it
155
155
if (frame >= 0 && frame < stackTrace .size ()) {
156
156
if (ctx == RuntimeContextType .SCALAR ) {
157
- res .add (new RuntimeScalar (stackTrace .get (frame ).get ( 0 )));
157
+ res .add (new RuntimeScalar (stackTrace .get (frame ).getFirst ( )));
158
158
} else {
159
159
res .add (new RuntimeScalar (stackTrace .get (frame ).get (0 )));
160
160
res .add (new RuntimeScalar (stackTrace .get (frame ).get (1 )));
@@ -191,7 +191,7 @@ public static RuntimeList localtime(RuntimeList args, int ctx) {
191
191
if (args .elements .isEmpty ()) {
192
192
date = ZonedDateTime .now ();
193
193
} else {
194
- long arg = ((RuntimeScalar ) args .elements .get ( 0 )).getInt ();
194
+ long arg = ((RuntimeScalar ) args .elements .getFirst ( )).getInt ();
195
195
date = Instant .ofEpochSecond (arg ).atZone (ZoneId .systemDefault ());
196
196
}
197
197
if (ctx == RuntimeContextType .SCALAR ) {
@@ -218,7 +218,7 @@ public static RuntimeList gmtime(RuntimeList args, int ctx) {
218
218
if (args .elements .isEmpty ()) {
219
219
date = ZonedDateTime .now (ZoneOffset .UTC );
220
220
} else {
221
- long arg = ((RuntimeScalar ) args .elements .get ( 0 )).getInt ();
221
+ long arg = ((RuntimeScalar ) args .elements .getFirst ( )).getInt ();
222
222
date = Instant .ofEpochSecond (arg ).atZone (ZoneId .of ("UTC" ));
223
223
}
224
224
if (ctx == RuntimeContextType .SCALAR ) {
@@ -354,64 +354,46 @@ public RuntimeScalar isa(RuntimeScalar className) {
354
354
355
355
// Getters
356
356
public int getInt () {
357
- switch (type ) {
358
- case INTEGER :
359
- return (int ) value ;
360
- case DOUBLE :
361
- return (int ) ((double ) value );
362
- case STRING :
363
- return this .parseNumber ().getInt ();
364
- case UNDEF :
365
- return 0 ;
366
- default :
367
- return ((RuntimeScalarReference ) value ).getIntRef ();
368
- }
357
+ return switch (type ) {
358
+ case INTEGER -> (int ) value ;
359
+ case DOUBLE -> (int ) ((double ) value );
360
+ case STRING -> this .parseNumber ().getInt ();
361
+ case UNDEF -> 0 ;
362
+ default -> ((RuntimeScalarReference ) value ).getIntRef ();
363
+ };
369
364
}
370
365
371
366
public long getLong () {
372
- switch (type ) {
373
- case INTEGER :
374
- return (int ) value ;
375
- case DOUBLE :
376
- return (long ) ((double ) value );
377
- case STRING :
378
- return this .parseNumber ().getLong ();
379
- case UNDEF :
380
- return 0L ;
381
- default :
382
- return ((RuntimeScalarReference ) value ).getIntRef ();
383
- }
367
+ return switch (type ) {
368
+ case INTEGER -> (int ) value ;
369
+ case DOUBLE -> (long ) ((double ) value );
370
+ case STRING -> this .parseNumber ().getLong ();
371
+ case UNDEF -> 0L ;
372
+ default -> ((RuntimeScalarReference ) value ).getIntRef ();
373
+ };
384
374
}
385
375
386
376
public double getDouble () {
387
- switch (this .type ) {
388
- case INTEGER :
389
- return (int ) this .value ;
390
- case DOUBLE :
391
- return (double ) this .value ;
392
- case STRING :
393
- return this .parseNumber ().getDouble ();
394
- case UNDEF :
395
- return 0.0 ;
396
- default :
397
- return ((RuntimeScalarReference ) value ).getDoubleRef ();
398
- }
377
+ return switch (this .type ) {
378
+ case INTEGER -> (int ) this .value ;
379
+ case DOUBLE -> (double ) this .value ;
380
+ case STRING -> this .parseNumber ().getDouble ();
381
+ case UNDEF -> 0.0 ;
382
+ default -> ((RuntimeScalarReference ) value ).getDoubleRef ();
383
+ };
399
384
}
400
385
401
386
public boolean getBoolean () {
402
- switch (type ) {
403
- case INTEGER :
404
- return (int ) value != 0 ;
405
- case DOUBLE :
406
- return (double ) value != 0.0 ;
407
- case STRING :
387
+ return switch (type ) {
388
+ case INTEGER -> (int ) value != 0 ;
389
+ case DOUBLE -> (double ) value != 0.0 ;
390
+ case STRING -> {
408
391
String s = (String ) value ;
409
- return !s .isEmpty () && !s .equals ("0" );
410
- case UNDEF :
411
- return false ;
412
- default :
413
- return ((RuntimeScalarReference ) value ).getBooleanRef ();
414
- }
392
+ yield !s .isEmpty () && !s .equals ("0" );
393
+ }
394
+ case UNDEF -> false ;
395
+ default -> ((RuntimeScalarReference ) value ).getBooleanRef ();
396
+ };
415
397
}
416
398
417
399
// Get the Scalar alias into an Array
@@ -492,38 +474,29 @@ public RuntimeArray setFromList(RuntimeList value) {
492
474
493
475
@ Override
494
476
public String toString () {
495
- switch (type ) {
496
- case INTEGER :
497
- return Integer .toString ((int ) value );
498
- case DOUBLE :
499
- return Double .toString ((double ) value );
500
- case STRING :
501
- return (String ) value ;
502
- case UNDEF :
503
- return "" ;
504
- case GLOB :
505
- return value .toString ();
506
- case REGEX :
507
- return value .toString ();
508
- default :
509
- return ((RuntimeScalarReference ) value ).toStringRef ();
510
- }
477
+ return switch (type ) {
478
+ case INTEGER -> Integer .toString ((int ) value );
479
+ case DOUBLE -> Double .toString ((double ) value );
480
+ case STRING -> (String ) value ;
481
+ case UNDEF -> "" ;
482
+ case GLOB -> value .toString ();
483
+ case REGEX -> value .toString ();
484
+ default -> ((RuntimeScalarReference ) value ).toStringRef ();
485
+ };
511
486
}
512
487
513
488
public String toStringRef () {
514
- switch (type ) {
515
- case UNDEF :
516
- return "REF(0x14500834042)" ;
517
- case CODE :
518
- return ((RuntimeCode ) value ).toStringRef ();
519
- case GLOB :
489
+ return switch (type ) {
490
+ case UNDEF -> "REF(0x14500834042)" ;
491
+ case CODE -> ((RuntimeCode ) value ).toStringRef ();
492
+ case GLOB -> {
520
493
if (value == null ) {
521
- return "CODE(0x14500834042)" ;
494
+ yield "CODE(0x14500834042)" ;
522
495
}
523
- return ((RuntimeCode ) value ).toStringRef ();
524
- default :
525
- return "REF(" + value .hashCode () + ")" ;
526
- }
496
+ yield ((RuntimeCode ) value ).toStringRef ();
497
+ }
498
+ default -> "REF(" + value .hashCode () + ")" ;
499
+ };
527
500
}
528
501
529
502
public int getIntRef () {
@@ -554,26 +527,20 @@ public RuntimeScalar hashDerefGet(RuntimeScalar index) {
554
527
555
528
// Method to implement `delete $v->{key}`
556
529
public RuntimeScalar hashDerefDelete (RuntimeScalar index ) {
557
- switch (type ) {
558
- case UNDEF :
559
- return new RuntimeScalar ();
560
- case HASHREFERENCE :
561
- return ((RuntimeHash ) value ).delete (index );
562
- default :
563
- throw new PerlCompilerException ("Variable does not contain a hash reference" );
564
- }
530
+ return switch (type ) {
531
+ case UNDEF -> new RuntimeScalar ();
532
+ case HASHREFERENCE -> ((RuntimeHash ) value ).delete (index );
533
+ default -> throw new PerlCompilerException ("Variable does not contain a hash reference" );
534
+ };
565
535
}
566
536
567
537
// Method to implement `exists $v->{key}`
568
538
public RuntimeScalar hashDerefExists (RuntimeScalar index ) {
569
- switch (type ) {
570
- case UNDEF :
571
- return new RuntimeScalar ();
572
- case HASHREFERENCE :
573
- return ((RuntimeHash ) value ).exists (index );
574
- default :
575
- throw new PerlCompilerException ("Variable does not contain a hash reference" );
576
- }
539
+ return switch (type ) {
540
+ case UNDEF -> new RuntimeScalar ();
541
+ case HASHREFERENCE -> ((RuntimeHash ) value ).exists (index );
542
+ default -> throw new PerlCompilerException ("Variable does not contain a hash reference" );
543
+ };
577
544
}
578
545
579
546
// Method to implement `$v->[10]`
@@ -592,51 +559,38 @@ public RuntimeScalar arrayDerefGet(RuntimeScalar index) {
592
559
593
560
// Method to implement `@$v`
594
561
public RuntimeArray arrayDeref () {
595
- switch (type ) {
596
- case UNDEF :
597
- throw new PerlCompilerException ("Can't use an undefined value as an ARRAY reference" );
598
- case ARRAYREFERENCE :
599
- return (RuntimeArray ) value ;
600
- default :
601
- throw new PerlCompilerException ("Variable does not contain an array reference" );
602
- }
562
+ return switch (type ) {
563
+ case UNDEF -> throw new PerlCompilerException ("Can't use an undefined value as an ARRAY reference" );
564
+ case ARRAYREFERENCE -> (RuntimeArray ) value ;
565
+ default -> throw new PerlCompilerException ("Variable does not contain an array reference" );
566
+ };
603
567
}
604
568
605
569
// Method to implement `%$v`
606
570
public RuntimeHash hashDeref () {
607
- switch (type ) {
608
- case UNDEF :
609
- throw new PerlCompilerException ("Can't use an undefined value as an HASH reference" );
610
- case HASHREFERENCE :
611
- return (RuntimeHash ) value ;
612
- default :
613
- throw new PerlCompilerException ("Variable does not contain an hash reference" );
614
- }
571
+ return switch (type ) {
572
+ case UNDEF -> throw new PerlCompilerException ("Can't use an undefined value as an HASH reference" );
573
+ case HASHREFERENCE -> (RuntimeHash ) value ;
574
+ default -> throw new PerlCompilerException ("Variable does not contain an hash reference" );
575
+ };
615
576
}
616
577
617
578
// Method to implement `$$v`
618
579
public RuntimeScalar scalarDeref () {
619
- switch (type ) {
620
- case UNDEF :
621
- throw new PerlCompilerException ("Can't use an undefined value as a SCALAR reference" );
622
- case REFERENCE :
623
- return (RuntimeScalar ) value ;
624
- default :
625
- throw new PerlCompilerException ("Variable does not contain a scalar reference" );
626
- }
580
+ return switch (type ) {
581
+ case UNDEF -> throw new PerlCompilerException ("Can't use an undefined value as a SCALAR reference" );
582
+ case REFERENCE -> (RuntimeScalar ) value ;
583
+ default -> throw new PerlCompilerException ("Variable does not contain a scalar reference" );
584
+ };
627
585
}
628
586
629
587
// Method to implement `*$v`
630
588
public RuntimeGlob globDeref () {
631
- switch (type ) {
632
- case UNDEF :
633
- throw new PerlCompilerException ("Can't use an undefined value as a GLOB reference" );
634
- case GLOB :
635
- case GLOBREFERENCE :
636
- return (RuntimeGlob ) value ;
637
- default :
638
- throw new PerlCompilerException ("Variable does not contain a glob reference" );
639
- }
589
+ return switch (type ) {
590
+ case UNDEF -> throw new PerlCompilerException ("Can't use an undefined value as a GLOB reference" );
591
+ case GLOB , GLOBREFERENCE -> (RuntimeGlob ) value ;
592
+ default -> throw new PerlCompilerException ("Variable does not contain a glob reference" );
593
+ };
640
594
}
641
595
642
596
// Method to apply (execute) a subroutine reference
@@ -671,6 +625,7 @@ public RuntimeScalar bless(RuntimeScalar className) {
671
625
672
626
public RuntimeScalar ref () {
673
627
String str ;
628
+ int blessId ;
674
629
switch (type ) {
675
630
case CODE :
676
631
str = "CODE" ;
@@ -679,7 +634,7 @@ public RuntimeScalar ref() {
679
634
str = "GLOB" ;
680
635
break ;
681
636
case REFERENCE :
682
- int blessId = ((RuntimeBaseEntity ) value ).blessId ;
637
+ blessId = ((RuntimeBaseEntity ) value ).blessId ;
683
638
str = blessId == 0 ? "REF" : NameNormalizer .getBlessStr (blessId );
684
639
break ;
685
640
case ARRAYREFERENCE :
@@ -706,7 +661,7 @@ public RuntimeScalar ref() {
706
661
*/
707
662
public RuntimeList call (RuntimeScalar method , RuntimeArray args , int callContext ) throws Exception {
708
663
// insert `this` into the parameter list
709
- args .elements .add ( 0 , this );
664
+ args .elements .addFirst ( this );
710
665
711
666
if (method .type == RuntimeScalarType .CODE ) {
712
667
// If method is a subroutine reference, just call it
0 commit comments