-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssignmentHandler.java
More file actions
615 lines (543 loc) · 24.8 KB
/
Copy pathAssignmentHandler.java
File metadata and controls
615 lines (543 loc) · 24.8 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
package cod.interpreter.handler;
import cod.ast.node.*;
import cod.error.InternalError;
import cod.error.ProgramError;
import cod.range.NaturalArray;
import cod.range.RangeObjects;
import cod.interpreter.context.ExecutionContext;
import cod.interpreter.*;
import cod.semantic.ConstructorResolver;
import cod.semantic.NamingValidator;
import java.util.*;
public class AssignmentHandler {
private static final String BORROW_MUTATION_VIOLATION =
"Borrow checker violation: cannot mutate index %d while it is currently borrowed by an active pointer";
private final TypeHandler typeSystem;
private final Interpreter interpreter;
private final ExpressionHandler expressionHandler;
private final InterpreterVisitor dispatcher;
public AssignmentHandler(TypeHandler typeSystem, Interpreter interpreter,
ExpressionHandler expressionHandler, InterpreterVisitor dispatcher) {
if (typeSystem == null) {
throw new InternalError("AssignmentHandler constructed with null typeSystem");
}
if (interpreter == null) {
throw new InternalError("AssignmentHandler constructed with null interpreter");
}
if (expressionHandler == null) {
throw new InternalError("AssignmentHandler constructed with null expressionHandler");
}
if (dispatcher == null) {
throw new InternalError("AssignmentHandler constructed with null dispatcher");
}
this.typeSystem = typeSystem;
this.interpreter = interpreter;
this.expressionHandler = expressionHandler;
this.dispatcher = dispatcher;
}
// === Core Assignment Logic ===
public Object handleAssignment(Assignment node, ExecutionContext ctx) {
if (node == null) {
throw new InternalError("handleAssignment called with null node");
}
if (ctx == null) {
throw new InternalError("handleAssignment called with null context");
}
try {
Object newValue = dispatcher.dispatch(node.right);
if (node.left instanceof IndexAccess) {
return handleIndexAssignment((IndexAccess) node.left, newValue, ctx);
} else if (node.left instanceof Expr) {
return handleVariableAssignment((Expr) node.left, newValue, ctx);
}
throw new ProgramError("Invalid assignment target");
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Assignment failed", e);
}
}
// In AssignmentHandler.java - optimized slot handling
public Object handleSlotAssignment(SlotAssignment node, ExecutionContext ctx) {
if (node == null) {
throw new InternalError("handleSlotAssignment called with null node");
}
if (ctx == null) {
throw new InternalError("handleSlotAssignment called with null context");
}
try {
Object value = dispatcher.dispatch(node.value);
String varName = node.slotName;
if (varName != null && !varName.isEmpty() && !"_".equals(varName)) {
return assignToSlot(varName, value, ctx);
}
// Implicit slot - use first available slot
if (ctx.getSlotCount() > 0) {
String slotTarget = ctx.getSlotName(0); // O(1) with new optimization
return assignToSlot(slotTarget, value, ctx);
}
return handleRegularAssignment(varName, value, ctx);
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Slot assignment failed", e);
}
}
public Object handleMultipleSlotAssignment(MultipleSlotAssignment node, ExecutionContext ctx) {
if (node == null) {
throw new InternalError("handleMultipleSlotAssignment called with null node");
}
if (ctx == null) {
throw new InternalError("handleMultipleSlotAssignment called with null context");
}
try {
Object lastValue = null;
int slotIndex = 0;
for (SlotAssignment assign : node.assignments) {
Object value = dispatcher.dispatch(assign.value);
String target = determineTargetOptimized(assign, ctx, slotIndex);
assignToSlot(target, value, ctx);
lastValue = value;
slotIndex++;
}
return lastValue;
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Multiple slot assignment failed", e);
}
}
// NEW: O(1) target determination
private String determineTargetOptimized(SlotAssignment assign, ExecutionContext ctx, int slotIndex) {
String varName = assign.slotName;
if (varName != null && !varName.isEmpty() && !"_".equals(varName)) {
return varName;
}
if (slotIndex < ctx.getSlotCount()) {
return ctx.getSlotName(slotIndex); // O(1)
}
throw new ProgramError("Too many positional slot assignments. Expected at most " +
ctx.getSlotCount() + ", got " + (slotIndex + 1));
}
// NEW: O(1) slot assignment
private Object assignToSlot(String slotTarget, Object value, ExecutionContext ctx) {
try {
if (ctx.hasSlot(slotTarget)) { // O(1)
String declaredType = ctx.getSlotType(slotTarget); // O(1)
validateAssignmentType(declaredType, value, slotTarget);
value = typeSystem.normalizeForDeclaredType(declaredType, value);
value = typeSystem.wrapUnionType(value, declaredType);
ctx.setSlotValue(slotTarget, value); // O(1)
ctx.markSlotAssigned(slotTarget); // O(1)
return value;
}
throw new ProgramError("Assignment to slot '" + slotTarget + "' failed: slot not declared");
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Slot assignment failed for: " + slotTarget, e);
}
}
// === Assignment Implementation Methods ===
@SuppressWarnings("unchecked")
private Object handleIndexAssignment(IndexAccess indexAccess, Object newValue, ExecutionContext ctx) {
try {
Object arrayObj = dispatcher.dispatch(indexAccess.array);
arrayObj = typeSystem.unwrap(arrayObj);
Object indexObj = dispatcher.dispatch(indexAccess.index);
indexObj = typeSystem.unwrap(indexObj);
if (indexObj instanceof List) {
return assignTupleIndex(arrayObj, (List<?>) indexObj, newValue);
}
if (RangeObjects.isRangeSpec(indexObj)) {
return assignRange(arrayObj, indexObj, newValue);
}
if (RangeObjects.isMultiRangeSpec(indexObj)) {
return assignMultiRange(arrayObj, indexObj, newValue);
}
if (arrayObj instanceof NaturalArray) {
NaturalArray natural = (NaturalArray) arrayObj;
long index = expressionHandler.toLongIndex(indexObj);
ensureNoActiveBorrow(arrayObj, index, ctx);
Object previous = natural.peekMaterialized(index);
natural.set(index, newValue);
ctx.trackValueReplacement(previous, newValue);
return newValue;
}
if (arrayObj instanceof List) {
int intIndex = expressionHandler.toIntIndex(indexObj);
ensureNoActiveBorrow(arrayObj, intIndex, ctx);
List<Object> list = (List<Object>) arrayObj;
Object previous = list.set(intIndex, newValue);
ctx.trackValueReplacement(previous, newValue);
return newValue;
}
throw new ProgramError("Invalid array assignment target: " +
(arrayObj != null ? arrayObj.getClass().getSimpleName() : "null"));
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Index assignment failed", e);
}
}
@SuppressWarnings("unchecked")
private Object assignTupleIndex(Object arrayObj, List<?> tupleIndices, Object newValue) {
if (tupleIndices == null || tupleIndices.isEmpty()) {
throw new ProgramError("Invalid multidimensional assignment: empty index tuple");
}
Object current = arrayObj;
for (int i = 0; i < tupleIndices.size() - 1; i++) {
Object idxObj = typeSystem.unwrap(tupleIndices.get(i));
if (RangeObjects.isRangeSpec(idxObj)) {
current = extractRange(current, idxObj);
continue;
}
if (RangeObjects.isMultiRangeSpec(idxObj)) {
current = extractMultiRange(current, idxObj);
continue;
}
if (current instanceof NaturalArray) {
NaturalArray natural = (NaturalArray) current;
long idx = expressionHandler.toLongIndex(idxObj);
current = natural.get(idx);
continue;
}
if (current instanceof List) {
List<Object> list = (List<Object>) current;
int idx = expressionHandler.toIntIndex(idxObj);
if (idx < 0 || idx >= list.size()) {
throw new ProgramError("Index out of bounds: " + idx + " for array of size " + list.size());
}
current = list.get(idx);
continue;
}
throw new ProgramError("Invalid multidimensional assignment path: expected NaturalArray or List, got "
+ (current != null ? current.getClass().getSimpleName() : "null"));
}
Object lastIdxObj = typeSystem.unwrap(tupleIndices.get(tupleIndices.size() - 1));
if (RangeObjects.isRangeSpec(lastIdxObj)) {
return assignRange(current, lastIdxObj, newValue);
}
if (RangeObjects.isMultiRangeSpec(lastIdxObj)) {
return assignMultiRange(current, lastIdxObj, newValue);
}
if (current instanceof NaturalArray) {
NaturalArray natural = (NaturalArray) current;
long idx = expressionHandler.toLongIndex(lastIdxObj);
natural.set(idx, newValue);
return newValue;
}
if (current instanceof List) {
List<Object> list = (List<Object>) current;
int idx = expressionHandler.toIntIndex(lastIdxObj);
list.set(idx, newValue);
return newValue;
}
throw new ProgramError("Invalid array assignment target in multidimensional assignment: " +
(current != null ? current.getClass().getSimpleName() : "null"));
}
@SuppressWarnings("unchecked")
private Object extractRange(Object array, Object range) {
if (array instanceof NaturalArray) {
return ((NaturalArray) array).getRange(range);
}
if (array instanceof List) {
List<Object> list = (List<Object>) array;
List<Object> result = new ArrayList<Object>();
long start = expressionHandler.toLongIndex(RangeObjects.getStart(range));
long end = expressionHandler.toLongIndex(RangeObjects.getEnd(range));
long step = expressionHandler.calculateStep(range);
start = normalizeListIndex(start, list.size());
end = normalizeListIndex(end, list.size());
if (start < 0 || start >= list.size()) {
throw new ProgramError("Range start index out of bounds: " + start + " for array of size " + list.size());
}
if (end < 0 || end >= list.size()) {
throw new ProgramError("Range end index out of bounds: " + end + " for array of size " + list.size());
}
if (step > 0) {
for (long i = start; i <= end && i < list.size(); i += step) {
result.add(list.get((int) i));
}
} else if (step < 0) {
for (long i = start; i >= end && i >= 0; i += step) {
result.add(list.get((int) i));
}
} else {
throw new ProgramError("Range step cannot be zero");
}
return result;
}
throw new ProgramError("Cannot apply range index during multidimensional assignment to " +
(array != null ? array.getClass().getSimpleName() : "null"));
}
@SuppressWarnings("unchecked")
private Object extractMultiRange(Object array, Object multiRange) {
if (array instanceof NaturalArray) {
return ((NaturalArray) array).getMultiRange(multiRange);
}
if (array instanceof List) {
List<Object> list = (List<Object>) array;
List<Object> result = new ArrayList<Object>();
for (Object range : RangeObjects.getRanges(multiRange)) {
Object sub = extractRange(list, range);
if (sub instanceof List) {
result.addAll((List<Object>) sub);
}
}
return result;
}
throw new ProgramError("Cannot apply multi-range index during multidimensional assignment to " +
(array != null ? array.getClass().getSimpleName() : "null"));
}
private long normalizeListIndex(long index, int size) {
if (index < 0) {
return size + index;
}
return index;
}
private void ensureNoActiveBorrow(Object container, long index, ExecutionContext ctx) {
if (!isBorrowCheckerActive(ctx)) {
return;
}
if (ctx.hasActiveBorrow(container, index)) {
throwBorrowMutationViolation(index);
}
}
private void throwBorrowMutationViolation(long index) {
throw new ProgramError(String.format(BORROW_MUTATION_VIOLATION, index));
}
private boolean isBorrowCheckerActive(ExecutionContext ctx) {
return ctx != null && (ctx.isUnsafeExecutionContext() || (ctx.currentClass != null && ctx.currentClass.isUnsafe));
}
private Object handleVariableAssignment(Expr target, Object newValue, ExecutionContext ctx) {
try {
if (target instanceof PropertyAccess) {
PropertyAccess prop = (PropertyAccess) target;
if (prop.left instanceof This) {
if (prop.right instanceof Identifier) {
Identifier right = (Identifier) prop.right;
return assignToField(right.name, newValue, ctx);
}
} else if (prop.left instanceof Super) {
if (prop.right instanceof Identifier) {
Identifier right = (Identifier) prop.right;
return assignToSuperField(right.name, newValue, ctx);
}
}
throw new ProgramError("Invalid property assignment target");
}
if (target instanceof Identifier) {
Identifier id = (Identifier) target;
return assignToVariableScoped(id.name, newValue, ctx);
}
throw new ProgramError("Invalid assignment target: " +
(target != null ? target.getClass().getSimpleName() : "null"));
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Variable assignment failed", e);
}
}
private Object handleRegularAssignment(String varName, Object value, ExecutionContext ctx) {
if (varName == null || "_".equals(varName)) {
throw new ProgramError("Invalid assignment: cannot assign to '_'");
}
return assignToVariableScoped(varName, value, ctx);
}
public Object assignToVariableScoped(String varName, Object newValue, ExecutionContext ctx) {
// First check all scopes for existing variable
for (int i = ctx.getScopeDepth() - 1; i >= 0; i--) {
Map<String, Object> scope = ctx.getLocalsStack().get(i);
if (scope.containsKey(varName)) {
if (NamingValidator.isAllCaps(varName)) {
throw new ProgramError("Cannot reassign constant '" + varName + "'");
}
return updateVariableInScope(varName, newValue, scope, i, ctx);
}
}
// Then check object fields
if (ctx.objectInstance != null && ctx.objectInstance.type != null) {
Object fieldValue = interpreter.getConstructorResolver()
.getFieldFromHierarchy(ctx.objectInstance.type, varName, ctx);
if (fieldValue != null) {
if (NamingValidator.isAllCaps(varName)) {
throw new ProgramError("Cannot reassign constant field '" + varName + "'");
}
ctx.setObjectField(varName, newValue);
return newValue;
}
}
// If not found anywhere, this is an error - variable must be declared with := first
throw new ProgramError("Variable '" + varName + "' not declared. Use ':=' for declaration, or declare it first.");
}
private Object updateVariableInScope(String varName, Object newValue,
Map<String, Object> scope, int scopeIndex,
ExecutionContext ctx) {
try {
String declaredType = null;
if (scopeIndex < ctx.getLocalTypesStack().size()) {
Map<String, String> typeScope = ctx.getLocalTypesStack().get(scopeIndex);
declaredType = typeScope.get(varName);
}
if (declaredType != null) {
validateAssignmentType(declaredType, newValue, varName);
newValue = typeSystem.normalizeForDeclaredType(declaredType, newValue);
newValue = typeSystem.wrapUnionType(newValue, declaredType);
}
ctx.setVariable(varName, newValue);
return newValue;
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Variable update failed for: " + varName, e);
}
}
private Object assignToField(String fieldName, Object newValue, ExecutionContext ctx) {
try {
Object existingField = interpreter.getConstructorResolver()
.getFieldFromHierarchy(ctx.objectInstance.type, fieldName, ctx);
if (existingField != null) {
if (NamingValidator.isAllCaps(fieldName)) {
throw new ProgramError("Cannot reassign constant field '" + fieldName + "'");
}
ctx.setObjectField(fieldName, newValue);
return newValue;
} else {
throw new ProgramError("Cannot assign to undefined field via this: " + fieldName);
}
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Field assignment failed for: " + fieldName, e);
}
}
private Object assignToSuperField(String fieldName, Object newValue, ExecutionContext ctx) {
try {
if (ctx.objectInstance == null || ctx.objectInstance.type == null) {
throw new ProgramError("Cannot assign to 'super." + fieldName + "' outside of object context");
}
if (ctx.objectInstance.type.extendName == null) {
throw new ProgramError("Cannot assign to 'super." + fieldName + "' - no parent class");
}
ConstructorResolver resolver = interpreter.getConstructorResolver();
Type parentType = resolver.findParentType(ctx.objectInstance.type, ctx);
if (parentType == null) {
throw new ProgramError("Parent class not found for 'super." + fieldName + "'");
}
boolean fieldDeclared = isFieldDeclaredInTypeHierarchy(parentType, fieldName, ctx);
if (fieldDeclared) {
if (NamingValidator.isAllCaps(fieldName)) {
throw new ProgramError("Cannot reassign constant field '" + fieldName + "'");
}
ctx.setObjectField(fieldName, newValue);
return newValue;
}
throw new ProgramError("Cannot assign to undefined field via super: " + fieldName);
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Super field assignment failed for: " + fieldName, e);
}
}
// === Helper Methods ===
private void validateAssignmentType(String declaredType, Object value, String name) {
if (!typeSystem.validateTypeWithNullable(declaredType, value)) {
throw new ProgramError(
"Type mismatch in assignment for " + name +
". Expected " + declaredType +
", got " + typeSystem.getConcreteType(value));
}
}
private boolean isFieldDeclaredInTypeHierarchy(Type type, String fieldName, ExecutionContext ctx) {
Type current = type;
ConstructorResolver resolver = interpreter.getConstructorResolver();
while (current != null) {
if (current.fields != null) {
for (Field field : current.fields) {
if (field != null && field.name != null && field.name.equals(fieldName)) {
return true;
}
}
}
current = resolver.findParentType(current, ctx);
}
return false;
}
// === Range/Multi-Range Assignment Methods ===
@SuppressWarnings("unchecked")
private Object assignRange(Object array, Object range, Object value) {
try {
if (array instanceof NaturalArray) {
NaturalArray natural = (NaturalArray) array;
natural.setRange(range, value);
return value;
} else if (array instanceof List) {
List<Object> list = (List<Object>) array;
setListRange(list, range, value);
return value;
}
throw new ProgramError("Cannot assign range to " +
(array != null ? array.getClass().getSimpleName() : "null"));
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Range assignment failed", e);
}
}
@SuppressWarnings("unchecked")
private Object assignMultiRange(Object array, Object multiRange, Object value) {
try {
if (array instanceof NaturalArray) {
NaturalArray natural = (NaturalArray) array;
natural.setMultiRange(multiRange, value);
return value;
} else if (array instanceof List) {
List<Object> list = (List<Object>) array;
setListMultiRange(list, multiRange, value);
return value;
}
throw new ProgramError("Cannot assign multi-range to " +
(array != null ? array.getClass().getSimpleName() : "null"));
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("Multi-range assignment failed", e);
}
}
private void setListRange(List<Object> list, Object range, Object value) {
try {
long start = expressionHandler.toLongIndex(RangeObjects.getStart(range));
long end = expressionHandler.toLongIndex(RangeObjects.getEnd(range));
long step = expressionHandler.calculateStep(range);
if (start < 0) start = list.size() + start;
if (end < 0) end = list.size() + end;
if (step > 0) {
for (long i = start; i <= end && i < list.size(); i += step) {
list.set((int) i, value);
}
} else if (step < 0) {
for (long i = start; i >= end && i >= 0; i += step) {
list.set((int) i, value);
}
} else {
throw new InternalError("Step cannot be zero - should have been caught earlier");
}
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("List range assignment failed", e);
}
}
private void setListMultiRange(List<Object> list, Object multiRange, Object value) {
try {
for (Object range : RangeObjects.getRanges(multiRange)) {
setListRange(list, range, value);
}
} catch (ProgramError e) {
throw e;
} catch (Exception e) {
throw new InternalError("List multi-range assignment failed", e);
}
}
}