Skip to content

Commit d13f4c5

Browse files
committed
refactor
1 parent f948e86 commit d13f4c5

File tree

1 file changed

+85
-130
lines changed

1 file changed

+85
-130
lines changed

src/main/java/org/perlonjava/runtime/RuntimeScalar.java

Lines changed: 85 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static RuntimeList caller(RuntimeList args, int ctx) {
144144
RuntimeList res = new RuntimeList();
145145
int frame = 0;
146146
if (!args.elements.isEmpty()) {
147-
frame = ((RuntimeScalar) args.elements.get(0)).getInt();
147+
frame = ((RuntimeScalar) args.elements.getFirst()).getInt();
148148
}
149149
CallerStack.CallerInfo info = CallerStack.peek();
150150
if (info == null) {
@@ -154,7 +154,7 @@ public static RuntimeList caller(RuntimeList args, int ctx) {
154154
frame++; // frame 0 is the current method, so we need to increment it
155155
if (frame >= 0 && frame < stackTrace.size()) {
156156
if (ctx == RuntimeContextType.SCALAR) {
157-
res.add(new RuntimeScalar(stackTrace.get(frame).get(0)));
157+
res.add(new RuntimeScalar(stackTrace.get(frame).getFirst()));
158158
} else {
159159
res.add(new RuntimeScalar(stackTrace.get(frame).get(0)));
160160
res.add(new RuntimeScalar(stackTrace.get(frame).get(1)));
@@ -191,7 +191,7 @@ public static RuntimeList localtime(RuntimeList args, int ctx) {
191191
if (args.elements.isEmpty()) {
192192
date = ZonedDateTime.now();
193193
} else {
194-
long arg = ((RuntimeScalar) args.elements.get(0)).getInt();
194+
long arg = ((RuntimeScalar) args.elements.getFirst()).getInt();
195195
date = Instant.ofEpochSecond(arg).atZone(ZoneId.systemDefault());
196196
}
197197
if (ctx == RuntimeContextType.SCALAR) {
@@ -218,7 +218,7 @@ public static RuntimeList gmtime(RuntimeList args, int ctx) {
218218
if (args.elements.isEmpty()) {
219219
date = ZonedDateTime.now(ZoneOffset.UTC);
220220
} else {
221-
long arg = ((RuntimeScalar) args.elements.get(0)).getInt();
221+
long arg = ((RuntimeScalar) args.elements.getFirst()).getInt();
222222
date = Instant.ofEpochSecond(arg).atZone(ZoneId.of("UTC"));
223223
}
224224
if (ctx == RuntimeContextType.SCALAR) {
@@ -354,64 +354,46 @@ public RuntimeScalar isa(RuntimeScalar className) {
354354

355355
// Getters
356356
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+
};
369364
}
370365

371366
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+
};
384374
}
385375

386376
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+
};
399384
}
400385

401386
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 -> {
408391
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+
};
415397
}
416398

417399
// Get the Scalar alias into an Array
@@ -492,38 +474,29 @@ public RuntimeArray setFromList(RuntimeList value) {
492474

493475
@Override
494476
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+
};
511486
}
512487

513488
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 -> {
520493
if (value == null) {
521-
return "CODE(0x14500834042)";
494+
yield "CODE(0x14500834042)";
522495
}
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+
};
527500
}
528501

529502
public int getIntRef() {
@@ -554,26 +527,20 @@ public RuntimeScalar hashDerefGet(RuntimeScalar index) {
554527

555528
// Method to implement `delete $v->{key}`
556529
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+
};
565535
}
566536

567537
// Method to implement `exists $v->{key}`
568538
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+
};
577544
}
578545

579546
// Method to implement `$v->[10]`
@@ -592,51 +559,38 @@ public RuntimeScalar arrayDerefGet(RuntimeScalar index) {
592559

593560
// Method to implement `@$v`
594561
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+
};
603567
}
604568

605569
// Method to implement `%$v`
606570
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+
};
615576
}
616577

617578
// Method to implement `$$v`
618579
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+
};
627585
}
628586

629587
// Method to implement `*$v`
630588
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+
};
640594
}
641595

642596
// Method to apply (execute) a subroutine reference
@@ -671,6 +625,7 @@ public RuntimeScalar bless(RuntimeScalar className) {
671625

672626
public RuntimeScalar ref() {
673627
String str;
628+
int blessId;
674629
switch (type) {
675630
case CODE:
676631
str = "CODE";
@@ -679,7 +634,7 @@ public RuntimeScalar ref() {
679634
str = "GLOB";
680635
break;
681636
case REFERENCE:
682-
int blessId = ((RuntimeBaseEntity) value).blessId;
637+
blessId = ((RuntimeBaseEntity) value).blessId;
683638
str = blessId == 0 ? "REF" : NameNormalizer.getBlessStr(blessId);
684639
break;
685640
case ARRAYREFERENCE:
@@ -706,7 +661,7 @@ public RuntimeScalar ref() {
706661
*/
707662
public RuntimeList call(RuntimeScalar method, RuntimeArray args, int callContext) throws Exception {
708663
// insert `this` into the parameter list
709-
args.elements.add(0, this);
664+
args.elements.addFirst(this);
710665

711666
if (method.type == RuntimeScalarType.CODE) {
712667
// If method is a subroutine reference, just call it

0 commit comments

Comments
 (0)