-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCompileExistsDelete.java
More file actions
687 lines (658 loc) · 29.2 KB
/
CompileExistsDelete.java
File metadata and controls
687 lines (658 loc) · 29.2 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package org.perlonjava.backend.bytecode;
import org.perlonjava.frontend.astnode.*;
import org.perlonjava.runtime.runtimetypes.NameNormalizer;
import org.perlonjava.runtime.runtimetypes.RuntimeContextType;
import java.util.ArrayList;
import java.util.List;
public class CompileExistsDelete {
public static void visitExists(BytecodeCompiler bc, OperatorNode node) {
if (node.operand == null || !(node.operand instanceof ListNode list) || list.elements.isEmpty()) {
bc.throwCompilerException("exists requires an argument");
return;
}
Node arg = list.elements.get(0);
if (arg instanceof BinaryOperatorNode binOp) {
switch (binOp.operator) {
case "{" -> visitExistsHash(bc, node, binOp);
case "[" -> visitExistsArray(bc, node, binOp);
case "->" -> visitExistsArrow(bc, node, binOp);
default -> visitExistsGeneric(bc, arg);
}
} else {
visitExistsGeneric(bc, arg);
}
}
private static void visitExistsHash(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess) {
int hashReg = resolveHashFromBinaryOp(bc, hashAccess, node.getIndex());
int keyReg = compileHashKey(bc, hashAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_EXISTS);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
}
private static void visitExistsArray(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess) {
int arrayReg = compileArrayForExistsDelete(bc, arrayAccess, node.getIndex());
int indexReg = compileArrayIndex(bc, arrayAccess);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_EXISTS);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
}
private static void visitExistsArrow(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrowAccess) {
if (arrowAccess.right instanceof HashLiteralNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int hashReg = derefHash(bc, refReg, node.getIndex());
int keyReg = compileHashKey(bc, arrowAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_EXISTS);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
} else if (arrowAccess.right instanceof ArrayLiteralNode indexNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int arrayReg = derefArray(bc, refReg, node.getIndex());
if (indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array index required for exists");
return;
}
// Compile index in SCALAR context
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
int indexReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_EXISTS);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
} else {
visitExistsGeneric(bc, arrowAccess);
}
}
private static void visitExistsGeneric(BytecodeCompiler bc, Node arg) {
arg.accept(bc);
int argReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.EXISTS);
bc.emitReg(rd);
bc.emitReg(argReg);
bc.lastResultReg = rd;
}
public static void visitDelete(BytecodeCompiler bc, OperatorNode node) {
if (node.operand == null || !(node.operand instanceof ListNode list) || list.elements.isEmpty()) {
bc.throwCompilerException("delete requires an argument");
return;
}
Node arg = list.elements.get(0);
if (arg instanceof BinaryOperatorNode binOp) {
switch (binOp.operator) {
case "{" -> visitDeleteHash(bc, node, binOp);
case "[" -> visitDeleteArray(bc, node, binOp);
case "->" -> visitDeleteArrow(bc, node, binOp);
default -> visitDeleteGeneric(bc, arg);
}
} else {
visitDeleteGeneric(bc, arg);
}
}
private static void visitDeleteHash(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess) {
if (hashAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("@")) {
visitDeleteHashSlice(bc, node, hashAccess, leftOp);
return;
}
if (hashAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("%")) {
visitDeleteHashKVSlice(bc, node, hashAccess, leftOp);
return;
}
int hashReg = resolveHashFromBinaryOp(bc, hashAccess, node.getIndex());
int keyReg = compileHashKey(bc, hashAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
}
private static void visitDeleteHashSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess, OperatorNode leftOp) {
int hashReg;
if (leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
hashReg = bc.getVariableRegister(hashVarName);
} else {
hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Hash slice delete requires identifier");
return;
}
if (!(hashAccess.right instanceof HashLiteralNode keysNode)) {
bc.throwCompilerException("Hash slice delete requires HashLiteralNode");
return;
}
List<Integer> keyRegs = new ArrayList<>();
for (Node keyElement : keysNode.elements) {
if (keyElement instanceof IdentifierNode keyId) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(keyId.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
keyRegs.add(keyReg);
} else {
// Compile key in SCALAR context
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
keyRegs.add(bc.lastResultReg);
}
}
int keysListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(keysListReg);
bc.emit(keyRegs.size());
for (int keyReg : keyRegs) {
bc.emitReg(keyReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keysListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteHashKVSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess, OperatorNode leftOp) {
int hashReg;
if (leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
hashReg = bc.getVariableRegister(hashVarName);
} else {
hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Hash kv-slice delete requires identifier");
return;
}
if (!(hashAccess.right instanceof HashLiteralNode keysNode)) {
bc.throwCompilerException("Hash kv-slice delete requires HashLiteralNode");
return;
}
List<Integer> keyRegs = new ArrayList<>();
for (Node keyElement : keysNode.elements) {
if (keyElement instanceof IdentifierNode keyId) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(keyId.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
keyRegs.add(keyReg);
} else {
// Compile key in SCALAR context
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
keyRegs.add(bc.lastResultReg);
}
}
int keysListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(keysListReg);
bc.emit(keyRegs.size());
for (int keyReg : keyRegs) {
bc.emitReg(keyReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_KV_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keysListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArray(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess) {
if (arrayAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("@")) {
visitDeleteArraySlice(bc, node, arrayAccess, leftOp);
return;
}
if (arrayAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("%")) {
visitDeleteArrayKVSlice(bc, node, arrayAccess, leftOp);
return;
}
int arrayReg = compileArrayForExistsDelete(bc, arrayAccess, node.getIndex());
int indexReg = compileArrayIndex(bc, arrayAccess);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArraySlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess, OperatorNode leftOp) {
int arrayReg;
if (leftOp.operand instanceof IdentifierNode id) {
String arrayVarName = "@" + id.name;
if (bc.hasVariable(arrayVarName)) {
arrayReg = bc.getVariableRegister(arrayVarName);
} else {
arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Array slice delete requires identifier");
return;
}
if (!(arrayAccess.right instanceof ArrayLiteralNode indicesNode)) {
bc.throwCompilerException("Array slice delete requires ArrayLiteralNode");
return;
}
List<Integer> indexRegs = new ArrayList<>();
for (Node indexElement : indicesNode.elements) {
// Compile index in SCALAR context
bc.compileNode(indexElement, -1, RuntimeContextType.SCALAR);
indexRegs.add(bc.lastResultReg);
}
int indicesListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(indicesListReg);
bc.emit(indexRegs.size());
for (int indexReg : indexRegs) {
bc.emitReg(indexReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indicesListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArrayKVSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess, OperatorNode leftOp) {
int arrayReg;
if (leftOp.operand instanceof IdentifierNode id) {
String arrayVarName = "@" + id.name;
if (bc.hasVariable(arrayVarName)) {
arrayReg = bc.getVariableRegister(arrayVarName);
} else {
arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Array kv-slice delete requires identifier");
return;
}
if (!(arrayAccess.right instanceof ArrayLiteralNode indicesNode)) {
bc.throwCompilerException("Array kv-slice delete requires ArrayLiteralNode");
return;
}
List<Integer> indexRegs = new ArrayList<>();
for (Node indexElement : indicesNode.elements) {
// Compile index in SCALAR context
bc.compileNode(indexElement, -1, RuntimeContextType.SCALAR);
indexRegs.add(bc.lastResultReg);
}
int indicesListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(indicesListReg);
bc.emit(indexRegs.size());
for (int indexReg : indexRegs) {
bc.emitReg(indexReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_KV_SLICE_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indicesListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteArrow(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrowAccess) {
if (arrowAccess.right instanceof HashLiteralNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int hashReg = derefHash(bc, refReg, node.getIndex());
int keyReg = compileHashKey(bc, arrowAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_DELETE);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
} else if (arrowAccess.right instanceof ArrayLiteralNode indexNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int arrayReg = derefArray(bc, refReg, node.getIndex());
if (indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array index required for delete");
return;
}
// Compile index in SCALAR context
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
int indexReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_DELETE);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
} else {
visitDeleteGeneric(bc, arrowAccess);
}
}
private static void visitDeleteGeneric(BytecodeCompiler bc, Node arg) {
arg.accept(bc);
int argReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.DELETE);
bc.emitReg(rd);
bc.emitReg(argReg);
bc.lastResultReg = rd;
}
private static int resolveHashFromBinaryOp(BytecodeCompiler bc, BinaryOperatorNode hashAccess, int tokenIndex) {
if (hashAccess.left instanceof OperatorNode leftOp) {
if (leftOp.operator.equals("$") && leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
return bc.getVariableRegister(hashVarName);
}
int hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
return hashReg;
} else {
// Compile operand in SCALAR context to ensure we get a result register
bc.compileNode(leftOp.operand, -1, RuntimeContextType.SCALAR);
int scalarReg = bc.lastResultReg;
return derefHash(bc, scalarReg, tokenIndex);
}
} else if (hashAccess.left instanceof BinaryOperatorNode) {
// Compile in SCALAR context to ensure we get a result register
bc.compileNode(hashAccess.left, -1, RuntimeContextType.SCALAR);
int scalarReg = bc.lastResultReg;
return derefHash(bc, scalarReg, tokenIndex);
}
bc.throwCompilerException("Hash access requires variable or expression on left side");
return -1;
}
private static int derefHash(BytecodeCompiler bc, int scalarReg, int tokenIndex) {
int hashReg = bc.allocateRegister();
if (bc.isStrictRefsEnabled()) {
bc.emitWithToken(Opcodes.DEREF_HASH, tokenIndex);
bc.emitReg(hashReg);
bc.emitReg(scalarReg);
} else {
int pkgIdx = bc.addToStringPool(bc.getCurrentPackage());
bc.emitWithToken(Opcodes.DEREF_HASH_NONSTRICT, tokenIndex);
bc.emitReg(hashReg);
bc.emitReg(scalarReg);
bc.emit(pkgIdx);
}
return hashReg;
}
private static int derefArray(BytecodeCompiler bc, int scalarReg, int tokenIndex) {
int arrayReg = bc.allocateRegister();
if (bc.isStrictRefsEnabled()) {
bc.emitWithToken(Opcodes.DEREF_ARRAY, tokenIndex);
bc.emitReg(arrayReg);
bc.emitReg(scalarReg);
} else {
int pkgIdx = bc.addToStringPool(bc.getCurrentPackage());
bc.emitWithToken(Opcodes.DEREF_ARRAY_NONSTRICT, tokenIndex);
bc.emitReg(arrayReg);
bc.emitReg(scalarReg);
bc.emit(pkgIdx);
}
return arrayReg;
}
private static int compileHashKey(BytecodeCompiler bc, Node keySpec) {
if (keySpec instanceof HashLiteralNode keyNode && !keyNode.elements.isEmpty()) {
Node keyElement = keyNode.elements.get(0);
if (keyElement instanceof IdentifierNode id) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(id.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
return keyReg;
} else {
// Compile in scalar context to ensure we get a RuntimeScalar
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
return bc.lastResultReg;
}
} else {
// Compile in scalar context to ensure we get a RuntimeScalar
bc.compileNode(keySpec, -1, RuntimeContextType.SCALAR);
return bc.lastResultReg;
}
}
private static int compileArrayForExistsDelete(BytecodeCompiler bc, BinaryOperatorNode arrayAccess, int tokenIndex) {
if (!(arrayAccess.left instanceof OperatorNode leftOp) || !leftOp.operator.equals("$")
|| !(leftOp.operand instanceof IdentifierNode)) {
bc.throwCompilerException("Array exists/delete requires simple array variable");
return -1;
}
String varName = ((IdentifierNode) leftOp.operand).name;
String arrayVarName = "@" + varName;
if (bc.currentSubroutineBeginId != 0 && bc.currentSubroutineClosureVars != null
&& bc.currentSubroutineClosureVars.contains(arrayVarName)) {
int arrayReg = bc.allocateRegister();
int nameIdx = bc.addToStringPool(arrayVarName);
bc.emitWithToken(Opcodes.RETRIEVE_BEGIN_ARRAY, tokenIndex);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
bc.emit(bc.currentSubroutineBeginId);
return arrayReg;
} else if (bc.hasVariable(arrayVarName)) {
return bc.getVariableRegister(arrayVarName);
} else {
int arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(varName, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
return arrayReg;
}
}
private static int compileArrayIndex(BytecodeCompiler bc, BinaryOperatorNode arrayAccess) {
if (!(arrayAccess.right instanceof ArrayLiteralNode indexNode) || indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array exists/delete requires index");
return -1;
}
// Compile in scalar context to ensure we get a RuntimeScalar
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
return bc.lastResultReg;
}
/**
* Handles `delete local` in the bytecode interpreter.
* Mirrors visitDelete but uses HASH_DELETE_LOCAL / ARRAY_DELETE_LOCAL opcodes.
*/
public static void visitDeleteLocal(BytecodeCompiler bc, OperatorNode node) {
if (node.operand == null || !(node.operand instanceof ListNode list) || list.elements.isEmpty()) {
bc.throwCompilerException("delete local requires an argument");
return;
}
Node arg = list.elements.get(0);
if (arg instanceof BinaryOperatorNode binOp) {
switch (binOp.operator) {
case "{" -> visitDeleteLocalHash(bc, node, binOp);
case "[" -> visitDeleteLocalArray(bc, node, binOp);
case "->" -> visitDeleteLocalArrow(bc, node, binOp);
default -> bc.throwCompilerException("delete local requires hash or array element");
}
} else {
bc.throwCompilerException("delete local requires hash or array element");
}
}
private static void visitDeleteLocalHash(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess) {
if (hashAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("@")) {
visitDeleteLocalHashSlice(bc, node, hashAccess, leftOp);
return;
}
int hashReg = resolveHashFromBinaryOp(bc, hashAccess, node.getIndex());
int keyReg = compileHashKey(bc, hashAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_DELETE_LOCAL);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
}
private static void visitDeleteLocalHashSlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode hashAccess, OperatorNode leftOp) {
int hashReg;
if (leftOp.operand instanceof IdentifierNode id) {
String hashVarName = "%" + id.name;
if (bc.hasVariable(hashVarName)) {
hashReg = bc.getVariableRegister(hashVarName);
} else {
hashReg = bc.allocateRegister();
String globalHashName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalHashName);
bc.emit(Opcodes.LOAD_GLOBAL_HASH);
bc.emitReg(hashReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Hash slice delete local requires identifier");
return;
}
if (!(hashAccess.right instanceof HashLiteralNode keysNode)) {
bc.throwCompilerException("Hash slice delete local requires HashLiteralNode");
return;
}
List<Integer> keyRegs = new ArrayList<>();
for (Node keyElement : keysNode.elements) {
if (keyElement instanceof IdentifierNode keyId) {
int keyReg = bc.allocateRegister();
int keyIdx = bc.addToStringPool(keyId.name);
bc.emit(Opcodes.LOAD_STRING);
bc.emitReg(keyReg);
bc.emit(keyIdx);
keyRegs.add(keyReg);
} else {
bc.compileNode(keyElement, -1, RuntimeContextType.SCALAR);
keyRegs.add(bc.lastResultReg);
}
}
int keysListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(keysListReg);
bc.emit(keyRegs.size());
for (int keyReg : keyRegs) {
bc.emitReg(keyReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_SLICE_DELETE_LOCAL);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keysListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteLocalArray(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess) {
if (arrayAccess.left instanceof OperatorNode leftOp && leftOp.operator.equals("@")) {
visitDeleteLocalArraySlice(bc, node, arrayAccess, leftOp);
return;
}
int arrayReg = compileArrayForExistsDelete(bc, arrayAccess, node.getIndex());
int indexReg = compileArrayIndex(bc, arrayAccess);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_DELETE_LOCAL);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
}
private static void visitDeleteLocalArraySlice(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrayAccess, OperatorNode leftOp) {
int arrayReg;
if (leftOp.operand instanceof IdentifierNode id) {
String arrayVarName = "@" + id.name;
if (bc.hasVariable(arrayVarName)) {
arrayReg = bc.getVariableRegister(arrayVarName);
} else {
arrayReg = bc.allocateRegister();
String globalArrayName = NameNormalizer.normalizeVariableName(id.name, bc.getCurrentPackage());
int nameIdx = bc.addToStringPool(globalArrayName);
bc.emit(Opcodes.LOAD_GLOBAL_ARRAY);
bc.emitReg(arrayReg);
bc.emit(nameIdx);
}
} else {
bc.throwCompilerException("Array slice delete local requires identifier");
return;
}
if (!(arrayAccess.right instanceof ArrayLiteralNode indicesNode)) {
bc.throwCompilerException("Array slice delete local requires ArrayLiteralNode");
return;
}
List<Integer> indexRegs = new ArrayList<>();
for (Node indexElement : indicesNode.elements) {
bc.compileNode(indexElement, -1, RuntimeContextType.SCALAR);
indexRegs.add(bc.lastResultReg);
}
int indicesListReg = bc.allocateRegister();
bc.emit(Opcodes.CREATE_LIST);
bc.emitReg(indicesListReg);
bc.emit(indexRegs.size());
for (int indexReg : indexRegs) {
bc.emitReg(indexReg);
}
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_SLICE_DELETE_LOCAL);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indicesListReg);
bc.lastResultReg = rd;
}
private static void visitDeleteLocalArrow(BytecodeCompiler bc, OperatorNode node, BinaryOperatorNode arrowAccess) {
if (arrowAccess.right instanceof HashLiteralNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int hashReg = derefHash(bc, refReg, node.getIndex());
int keyReg = compileHashKey(bc, arrowAccess.right);
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.HASH_DELETE_LOCAL);
bc.emitReg(rd);
bc.emitReg(hashReg);
bc.emitReg(keyReg);
bc.lastResultReg = rd;
} else if (arrowAccess.right instanceof ArrayLiteralNode indexNode) {
bc.compileNode(arrowAccess.left, -1, RuntimeContextType.SCALAR);
int refReg = bc.lastResultReg;
int arrayReg = derefArray(bc, refReg, node.getIndex());
if (indexNode.elements.isEmpty()) {
bc.throwCompilerException("Array index required for delete local");
return;
}
bc.compileNode(indexNode.elements.get(0), -1, RuntimeContextType.SCALAR);
int indexReg = bc.lastResultReg;
int rd = bc.allocateOutputRegister();
bc.emit(Opcodes.ARRAY_DELETE_LOCAL);
bc.emitReg(rd);
bc.emitReg(arrayReg);
bc.emitReg(indexReg);
bc.lastResultReg = rd;
} else {
bc.throwCompilerException("delete local requires hash or array element");
}
}
}