-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRuntimeList.java
More file actions
627 lines (562 loc) · 20.7 KB
/
RuntimeList.java
File metadata and controls
627 lines (562 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package org.perlonjava.runtime;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import static org.perlonjava.runtime.RuntimeScalarCache.scalarTrue;
import static org.perlonjava.runtime.RuntimeScalarCache.scalarUndef;
/**
* The RuntimeList class simulates a Perl list.
* It provides methods to manipulate and access a dynamic list of Perl values.
*/
public class RuntimeList extends RuntimeBase {
// List to hold the elements of the list.
public List<RuntimeBase> elements;
// Constructor
public RuntimeList() {
this.elements = new ArrayList<>();
}
public RuntimeList(List<RuntimeScalar> list) {
this.elements = new ArrayList<>(list);
}
public RuntimeList(RuntimeBase... values) {
this.elements = new ArrayList<>();
for (RuntimeBase value : values) {
Iterator<RuntimeScalar> iterator = value.iterator();
while (iterator.hasNext()) {
this.elements.add(iterator.next());
}
}
}
/**
* Constructs a RuntimeList with a single scalar value.
*
* @param value The initial scalar value for the list.
*/
public RuntimeList(RuntimeScalar value) {
this.elements = new ArrayList<>();
this.elements.add(value);
}
/**
* Constructs a RuntimeList from another RuntimeList.
*
* @param value The RuntimeList to initialize this list with.
*/
public RuntimeList(RuntimeList value) {
this.elements = value.elements;
}
/**
* Constructs a RuntimeList from a RuntimeArray.
*
* @param value The RuntimeArray to initialize this list with.
*/
public RuntimeList(RuntimeArray value) {
this.elements = new ArrayList<>();
this.elements.add(value);
}
/**
* Constructs a RuntimeList from a RuntimeHash.
*
* @param value The RuntimeHash to initialize this list with.
*/
public RuntimeList(RuntimeHash value) {
this.elements = new ArrayList<>();
this.elements.add(value);
}
/**
* Adds the elements of this list to another RuntimeList.
*
* @param list The RuntimeList to which elements will be added.
*/
public void addToList(RuntimeList list) {
for (RuntimeBase elem : elements) {
list.add(elem);
}
this.elements.clear(); // Consume the list
}
/**
* Adds the elements of this list to a RuntimeArray.
*
* @param array The RuntimeArray to which elements will be added.
*/
public void addToArray(RuntimeArray array) {
int size = this.size();
for (int i = 0; i < size; i++) {
this.elements.get(i).addToArray(array);
}
this.elements.clear(); // Consume the list
}
/**
* Adds the scalar value of this list to a RuntimeScalar.
*
* @param scalar The RuntimeScalar object.
* @return The scalar with the list's scalar value set.
*/
public RuntimeScalar addToScalar(RuntimeScalar scalar) {
return scalar.set(this.scalar());
}
/**
* Adds an element to the list.
*
* @param value The value to add.
*/
public void add(RuntimeBase value) {
// CRITICAL: RuntimeControlFlowList must NOT be flattened!
// It's a control flow marker that should propagate, not a normal list
if (value instanceof RuntimeControlFlowList) {
// This is a control flow marker - should not happen in normal code
// If we reach here, it means a control flow marker wasn't caught properly
// For now, just add it as-is to avoid corrupting the marker
this.elements.add(value);
} else if (value instanceof RuntimeList list) {
this.elements.addAll(list.elements);
} else {
this.elements.add(value);
}
}
public void add(RuntimeScalar value) {
this.elements.add(value);
}
public void add(RuntimeArray value) {
this.elements.add(value);
}
public void add(RuntimeHash value) {
this.elements.add(value);
}
/**
* Adds an integer value to the list.
*
* @param value The integer value to add.
*/
public void add(int value) {
this.elements.add(new RuntimeScalar(value));
}
/**
* Adds a string value to the list.
*
* @param value The string value to add.
*/
public void add(String value) {
this.elements.add(new RuntimeScalar(value));
}
/**
* Adds a double value to the list.
*
* @param value The double value to add.
*/
public void add(Double value) {
this.elements.add(new RuntimeScalar(value));
}
/**
* Merges another RuntimeList into this list.
*
* @param value The RuntimeList to merge.
*/
public void add(RuntimeList value) {
int size = value.size();
for (int i = 0; i < size; i++) {
this.elements.add(value.elements.get(i));
}
}
/**
* Gets the size of the list.
*
* @return The number of elements in the list.
*/
public int size() {
return elements.size();
}
public boolean isEmpty() {
return elements.isEmpty();
}
/**
* Counts the total number of elements in the list, including nested elements.
*
* @return The total number of elements.
*/
public int countElements() {
int count = 0;
for (RuntimeBase elem : elements) {
count = count + elem.countElements();
}
return count;
}
public RuntimeList clone() {
RuntimeList newList = new RuntimeList();
for (RuntimeBase elem : elements) {
for (RuntimeScalar scalar : elem) {
newList.add(scalar.clone());
}
}
return newList;
}
/**
* Gets the array value of the list as aliases into an array.
*
* @param arr The array to set aliases into.
* @return The updated array with aliases.
*/
public RuntimeArray setArrayOfAlias(RuntimeArray arr) {
for (RuntimeBase elem : elements) {
elem.setArrayOfAlias(arr);
}
return arr;
}
/**
* Gets the list value of the list.
*
* @return This RuntimeList.
*/
public RuntimeList getList() {
return this;
}
/**
* Evaluates the boolean representation of the list.
*
* @return True if the scalar value of the list is true.
*/
public boolean getBoolean() {
return scalar().getBoolean();
}
/**
* Checks if the list is defined.
*
* @return True if the scalar value of the list is defined.
*/
public boolean getDefinedBoolean() {
return scalar().getDefinedBoolean();
}
/**
* Throws an exception as the 'keys' operation is not implemented for lists.
*
* @throws PerlCompilerException Always thrown as 'keys' is not implemented.
*/
public RuntimeArray keys() {
throw new PerlCompilerException("Type of arg 1 to keys must be hash or array");
}
/**
* Throws an exception as the 'values' operation is not implemented for lists.
*
* @throws PerlCompilerException Always thrown as 'values' is not implemented.
*/
public RuntimeArray values() {
throw new PerlCompilerException("Type of arg 1 to values must be hash or array");
}
/**
* Throws an exception as the 'each' operation is not implemented for lists.
*
* @throws PerlCompilerException Always thrown as 'each' is not implemented.
*/
public RuntimeList each(int ctx) {
throw new PerlCompilerException("Type of arg 1 to each must be hash or array");
}
/**
* Removes the last character from each element in the list.
*
* @return A scalar representing the result of the chop operation.
*/
public RuntimeScalar chop() {
if (this.isEmpty()) {
this.elements.add(GlobalVariable.getGlobalVariable("main::_"));
}
RuntimeScalar result = new RuntimeScalar("");
Iterator<RuntimeScalar> iterator = this.iterator();
while (iterator.hasNext()) {
result = iterator.next().chop();
}
return result;
}
/**
* Removes the trailing newline from each element in the list.
*
* @return A scalar representing the result of the chomp operation.
*/
public RuntimeScalar chomp() {
if (this.isEmpty()) {
this.elements.add(GlobalVariable.getGlobalVariable("main::_"));
}
int count = 0;
Iterator<RuntimeScalar> iterator = this.iterator();
while (iterator.hasNext()) {
count = count + iterator.next().chomp().getInt();
}
return new RuntimeScalar(count);
}
/**
* Gets the scalar value of the list.
*
* @return The scalar value of the last element in the list.
*/
public RuntimeScalar scalar() {
if (isEmpty()) {
return scalarUndef; // Return undefined if empty
}
// XXX expand the last element
return elements.getLast().scalar();
}
/**
* Special scalar conversion for stat/lstat operators.
* In Perl, stat in scalar context returns:
* - "" (empty string) if stat fails (empty list)
* - 1 (true) if stat succeeds (non-empty list)
*/
public RuntimeScalar statScalar() {
if (isEmpty()) {
return new RuntimeScalar(""); // Empty string, not undef
}
return scalarTrue; // Return 1 for success
}
/**
* Creates a reference from a list.
* For single-element lists (e.g., from constant subs), creates a reference to that element.
* For empty or multi-element lists, this is an error in scalar context.
*
* @return A RuntimeScalar reference to the list element
* @throws PerlCompilerException if the list doesn't contain exactly one element
*/
public RuntimeScalar createReference() {
if (elements.size() == 1) {
// Single element list - create reference to that element
return elements.get(0).scalar().createReference();
}
// Empty or multi-element list in reference context is an error
throw new PerlCompilerException("Can't create reference to list with " + elements.size() + " elements");
}
/**
* Creates a list reference.
*
* @return A new RuntimeList with references to the elements of this list.
*/
public RuntimeList createListReference() {
RuntimeList result = new RuntimeList();
List<RuntimeBase> resultList = result.elements;
Iterator<RuntimeScalar> iterator = this.iterator();
while (iterator.hasNext()) {
resultList.add(iterator.next().createReference());
}
return result;
}
/**
* Sets the items in the list to the values in another list.
* In list context, returns the argument list.
* In scalar context, returns the number of elements in the argument list.
*
* @param value The list to set from.
* @return The original list.
*/
public RuntimeArray setFromList(RuntimeList value) {
boolean hasUndefPlaceholderLhs = false;
for (RuntimeBase elem : elements) {
if (elem instanceof RuntimeScalarReadOnly runtimeScalarReadOnly
&& !runtimeScalarReadOnly.getDefinedBoolean()) {
hasUndefPlaceholderLhs = true;
break;
}
}
// Preserve RHS aliases before consuming `value`.
// Only needed when the LHS contains `undef` placeholders.
List<RuntimeScalar> rhsAliasElements = null;
IdentityHashMap<RuntimeScalar, Boolean> lhsScalars = null;
if (hasUndefPlaceholderLhs) {
RuntimeArray rhsAliases = new RuntimeArray();
value.setArrayOfAlias(rhsAliases);
rhsAliasElements = rhsAliases.elements;
lhsScalars = new IdentityHashMap<>();
for (RuntimeBase elem : elements) {
if (elem instanceof RuntimeScalar s && !(s instanceof RuntimeScalarReadOnly)) {
lhsScalars.put(s, Boolean.TRUE);
}
}
}
// Materialize the RHS once into a flat list.
// Avoids O(n^2) from repeated RuntimeArray.shift() which does removeFirst() on ArrayList.
RuntimeArray rhs = new RuntimeArray();
value.addToArray(rhs);
List<RuntimeScalar> rhsElements = rhs.elements;
int rhsSize = rhsElements.size();
int rhsIndex = 0;
// Build result with assigned values (including undef for unassigned scalars)
RuntimeArray result = new RuntimeArray();
// Store original size for scalar context
result.scalarContextSize = rhsSize;
for (RuntimeBase elem : elements) {
if (elem instanceof RuntimeScalarReadOnly runtimeScalarReadOnly && !runtimeScalarReadOnly.getDefinedBoolean()) {
// `undef` placeholder on LHS: consume one RHS value, but return something for this position.
// When safe, return the RHS element as an lvalue (alias). Otherwise return the pre-assignment value.
RuntimeScalar rhsValue = (rhsIndex < rhsSize) ? rhsElements.get(rhsIndex) : new RuntimeScalar();
RuntimeScalar rhsAlias = (rhsAliasElements != null && rhsIndex < rhsAliasElements.size())
? rhsAliasElements.get(rhsIndex)
: null;
// If the RHS element is also assigned on the LHS, it must not be returned as an alias
// (it would reflect the post-assignment value rather than the pre-assignment value).
boolean useAlias = rhsAlias != null && lhsScalars != null && !lhsScalars.containsKey(rhsAlias);
result.elements.add(useAlias ? rhsAlias : rhsValue);
if (rhsIndex < rhsSize) {
rhsIndex++;
}
} else if (elem instanceof RuntimeScalar runtimeScalar) {
RuntimeScalar assigned = (rhsIndex < rhsSize) ? rhsElements.get(rhsIndex++) : new RuntimeScalar();
runtimeScalar.set(assigned);
result.elements.add(runtimeScalar); // Add reference to the variable itself
} else if (elem instanceof RuntimeArray runtimeArray) {
List<RuntimeScalar> remaining = (rhsIndex < rhsSize)
? new ArrayList<>(rhsElements.subList(rhsIndex, rhsSize))
: new ArrayList<>();
// For tied arrays, we must assign through the tied interface.
// Overwriting `elements` would discard the TieArray wrapper while leaving
// `type == TIED_ARRAY`, leading to ClassCastException when tie operations
// cast `array.elements` back to TieArray.
if (runtimeArray.type == RuntimeArray.TIED_ARRAY) {
RuntimeList remainingList = new RuntimeList();
remainingList.elements.addAll(remaining);
runtimeArray.setFromList(remainingList);
} else {
runtimeArray.elements = remaining;
}
result.elements.addAll(remaining); // Use original references
rhsIndex = rhsSize; // Consume the rest
} else if (elem instanceof RuntimeHash runtimeHash) {
RuntimeArray remainingArr = new RuntimeArray();
remainingArr.elements = (rhsIndex < rhsSize)
? new ArrayList<>(rhsElements.subList(rhsIndex, rhsSize))
: new ArrayList<>();
RuntimeHash hash = RuntimeHash.createHashForAssignment(remainingArr);
runtimeHash.elements = hash.elements;
// Add lvalue references to hash elements for list assignment
for (Map.Entry<String, RuntimeScalar> entry : hash.elements.entrySet()) {
result.elements.add(new RuntimeScalar(entry.getKey()));
result.elements.add(entry.getValue()); // Add reference to hash value
}
rhsIndex = rhsSize; // Consume the rest
}
}
return result;
}
/**
* Converts the list to a string, concatenating all elements without separators.
*
* @return A string representation of the list.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (RuntimeBase element : elements) {
sb.append(element.toString());
}
return sb.toString();
}
/**
* Undefines the elements of the list.
*
* @return The updated RuntimeList after undefining.
*/
public RuntimeList undefine() {
for (RuntimeBase elem : elements) {
elem.undefine();
}
return this;
}
/**
* Validates that all elements in this list are not AutovivificationArrays or AutovivificationHashes.
* This should be called by operations that should NOT autovivify (like sort, reverse).
*
* @throws PerlCompilerException if any element contains an autovivification placeholder
*/
public void validateNoAutovivification() {
for (RuntimeBase element : elements) {
switch (element) {
case RuntimeArray array when array.elements instanceof AutovivificationArray ->
throw new PerlCompilerException("Can't use an undefined value as an ARRAY reference");
case RuntimeHash hash when hash.elements instanceof AutovivificationHash ->
throw new PerlCompilerException("Can't use an undefined value as a HASH reference");
default -> {
// Element is valid, continue checking
}
}
}
}
/**
* Saves the current state of the instance.
*
* <p>This method creates a snapshot of the current elements,
* and pushes it onto a static stack for later restoration. After saving, it clears
* the current elements and resets the values.
*/
@Override
public void dynamicSaveState() {
for (RuntimeBase elem : elements) {
elem.dynamicSaveState();
}
}
/**
* Restores the most recently saved state of the instance.
*
* <p>This method pops the most recent state from the static stack and restores
* the elements. If no state is saved, it does nothing.
*/
@Override
public void dynamicRestoreState() {
// Note: this method is probably not needed,
// because the elements are handled by their respective classes.
for (RuntimeBase elem : elements) {
elem.dynamicRestoreState();
}
}
/**
* Returns an iterator for the list.
*
* @return An iterator over the elements of the list.
*/
public Iterator<RuntimeScalar> iterator() {
return new RuntimeListIterator(elements);
}
/**
* Inner class implementing the Iterator interface for RuntimeList.
*/
private static class RuntimeListIterator implements Iterator<RuntimeScalar> {
private final List<RuntimeBase> elements;
private int currentIndex = 0;
private Iterator<RuntimeScalar> currentIterator;
public RuntimeListIterator(List<RuntimeBase> elements) {
this.elements = elements;
if (!elements.isEmpty()) {
currentIterator = elements.getFirst().iterator();
}
}
@Override
public boolean hasNext() {
while (currentIterator != null) {
if (currentIterator.hasNext()) {
return true;
}
// Move to the next element's iterator
currentIndex++;
if (currentIndex < elements.size()) {
currentIterator = elements.get(currentIndex).iterator();
} else {
currentIterator = null; // No more elements
}
}
return false;
}
@Override
public RuntimeScalar next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return currentIterator.next();
}
}
// ========== Control Flow Support ==========
/**
* Check if this RuntimeList represents non-local control flow.
* Uses instanceof check which is optimized by JIT compiler.
*
* @return true if this is a RuntimeControlFlowList, false otherwise
*/
public boolean isNonLocalGoto() {
return this instanceof RuntimeControlFlowList;
}
}