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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
use crate::ast::*;
use crate::transpiler::Transpiler;
impl Transpiler {
pub(in crate::transpiler) fn process_assignment(
&mut self,
assign: &Assignment,
) -> Result<(), String> {
// Infer the type of the value being assigned
let value_type = self.infer_type(&assign.value);
// Check if this assignment is type-safe
self.check_type_assignment(&assign.target, &value_type)?;
// Record the variable's type
self.variable_types
.insert(assign.target.clone(), value_type);
// Store the variable value for later use
self.variables
.insert(assign.target.clone(), assign.value.clone());
// If we're not in a function, store as module-level variable
// These will be automatically initialized in the _cobble_init function
if self.current_function.is_none() {
// Storage types (Array, Map, String) use NBT storage, not scoreboard
match &assign.value {
Expression::Array(_) | Expression::Map(_) | Expression::String(_) => {
let storage_path = format!("vars.{}", assign.target);
self.variable_storage_paths
.insert(assign.target.clone(), storage_path);
}
_ => {
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
}
}
self.module_level_vars.push((
assign.target.clone(),
assign.value.clone(),
self.current_statement_source.clone(),
));
return Ok(());
}
if !matches!(
&assign.value,
Expression::Array(_) | Expression::Map(_) | Expression::String(_)
) {
self.variable_storage_paths.remove(&assign.target);
}
// Try to evaluate constant expressions first
if let Some(const_value) = self.try_eval_const(&assign.value) {
// The expression is a constant - fold it at compile time
if let Some(ref mut commands) = self.current_function {
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
// Warn if number exceeds scoreboard range
if const_value > i32::MAX as f64 || const_value < i32::MIN as f64 {
eprintln!(
"⚠️ Warning: Constant expression result {} for variable '{}' exceeds Minecraft scoreboard range.\n\
Scoreboard range: {} to {}\n\
Value will be clamped to: {}",
const_value,
assign.target,
i32::MIN,
i32::MAX,
if const_value > i32::MAX as f64 { i32::MAX } else { i32::MIN }
);
}
// Warn if float has fractional part
if const_value.fract() != 0.0 {
eprintln!(
"⚠️ Warning: Constant expression result {} for variable '{}' will lose precision.\n\
Scoreboard only supports integers. Fractional part will be truncated to: {}",
const_value,
assign.target,
const_value as i32
);
}
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, const_value as i32
));
}
return Ok(());
}
if Self::is_runtime_condition_expression(&assign.value) {
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
let processed_condition = self.preprocess_condition(&assign.value)?;
let condition_cmd =
self.normalize_if_condition(self.translate_condition(&processed_condition)?)?;
let condition_execute_args = Self::condition_execute_args(&condition_cmd);
if let Some(ref mut commands) = self.current_function {
commands.push(format!("scoreboard players set {} temp 0", assign.target));
commands.push(format!(
"execute {} run scoreboard players set {} temp 1",
condition_execute_args, assign.target
));
}
return Ok(());
}
// Check if we need to use the complex expression evaluator
// Do this before borrowing to avoid borrow checker issues
// 4. Handle Complex Expressions (Math, Binary, etc.)
let needs_complex_eval = match &assign.value {
Expression::Binary(left, _, right) => {
// Simple binary (atom op atom) handled by optimized code in assignment.rs
// Complex binary (nested expressions) need the expression evaluator
!matches!(
(&**left, &**right),
(
Expression::Identifier(_) | Expression::Number(_),
Expression::Identifier(_) | Expression::Number(_)
)
)
}
Expression::Unary(_, _) | Expression::Call(_, _) => true,
// Attribute/Subscript falling through here means they failed storage resolution
// which likely means invalid base. `evaluate_expression_to_target` will handle error reporting.
Expression::Attribute(_, _) | Expression::Subscript(_, _) => true,
_ => false,
};
if needs_complex_eval {
// Handle complex nested expressions
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
let expr_commands =
self.evaluate_expression_to_target(&assign.value, &assign.target)?;
if let Some(ref mut commands) = self.current_function {
commands.extend(expr_commands);
}
return Ok(());
}
// Check for Storage assignments (Array, Map, String)
let storage_assignment = matches!(
&assign.value,
Expression::Array(_) | Expression::Map(_) | Expression::String(_)
);
if storage_assignment {
let storage_path = format!("vars.{}", assign.target);
self.variable_storage_paths
.insert(assign.target.clone(), storage_path.clone());
let snbt = self.serialize_to_snbt(&assign.value)?;
let cmd = format!(
"data modify storage {}:global {} set value {}",
self.data_pack.namespace, storage_path, snbt
);
if let Some(ref mut commands) = self.current_function {
commands.push(cmd);
}
return Ok(());
}
// If it's a score assignment, generate scoreboard command
if let Some(ref mut commands) = self.current_function {
match &assign.value {
Expression::Array(_) | Expression::Map(_) | Expression::String(_) => {
// Handled above
return Ok(());
}
Expression::Boolean(b) => {
// Boolean assignment
// Store as scoreboard (0/1) AND storage (byte) if needed?
// For now, keep existing scoreboard behavior for boolean
// But if we want to put it in storage:
// Let's stick to scoreboard for consistency with existing code
// unless explicitly requested?
// Existing code does NOTHING for Boolean assignment!
// See original code: "String and Boolean values ... don't generate scoreboard commands"
// This means they were effectively compile-time only or broken.
// Let's fix it by using scoreboard for boolean.
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
let val = if *b { 1 } else { 0 };
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, val
));
}
Expression::Number(n) => {
// Direct number assignment
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
// Warn if number exceeds scoreboard range
if *n > i32::MAX as f64 || *n < i32::MIN as f64 {
eprintln!(
"⚠️ Warning: Value {} for variable '{}' exceeds Minecraft scoreboard range.\n\
Scoreboard range: {} to {}\n\
Value will be clamped to: {}",
n,
assign.target,
i32::MIN,
i32::MAX,
if *n > i32::MAX as f64 { i32::MAX } else { i32::MIN }
);
}
// Warn if float has fractional part
if n.fract() != 0.0 {
eprintln!(
"⚠️ Warning: Float value {} for variable '{}' will lose precision.\n\
Scoreboard only supports integers. Fractional part will be truncated to: {}",
n,
assign.target,
*n as i32
);
}
let score = *n as i32;
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, score
));
}
Expression::Identifier(var) => {
// Check if source is a storage variable (Array/Map)
let src_path_opt = self.variable_storage_paths.get(var).cloned();
if let Some(src_path) = src_path_opt {
// Storage copy: target = src
// Mark target as storage variable
let target_path = format!("vars.{}", assign.target);
self.variable_storage_paths
.insert(assign.target.clone(), target_path.clone());
let namespace = self.data_pack.namespace.clone();
commands.push(format!(
"data modify storage {}:global {} set from storage {}:global {}",
namespace, target_path, namespace, src_path
));
} else {
// Variable-to-variable assignment (Scoreboard)
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
let var_obj = self
.variable_objectives
.get(var)
.unwrap_or(&"temp".to_string())
.clone();
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
}
Expression::Binary(left, op, right) => {
// Binary operation (already handled complex case above, so this is simple)
self.data_pack.track_objective("temp");
self.variable_objectives
.insert(assign.target.clone(), "temp".to_string());
self.scoreboard_variables.insert(assign.target.clone());
match (&**left, &**right) {
(Expression::Identifier(var), Expression::Number(n)) => {
// Handle variable op number (e.g., score = x + 5)
if n.fract() != 0.0 {
eprintln!(
"⚠️ Warning: Float value {} in binary operation will lose precision.\n\
Scoreboard only supports integers. Fractional part will be truncated to: {}",
n, *n as i32
);
}
let value = *n as i32;
let var_obj = self
.variable_objectives
.get(var)
.unwrap_or(&"temp".to_string())
.clone();
match op {
BinaryOp::Add => {
// Optimization: Skip self-assignment if target == var
if assign.target != *var || var_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
if value < 0 {
commands.push(format!(
"scoreboard players remove {} temp {}",
assign.target, -value
));
} else {
commands.push(format!(
"scoreboard players add {} temp {}",
assign.target, value
));
}
}
BinaryOp::Sub => {
// Optimization: Skip self-assignment if target == var
if assign.target != *var || var_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
if value < 0 {
commands.push(format!(
"scoreboard players add {} temp {}",
assign.target, -value
));
} else {
commands.push(format!(
"scoreboard players remove {} temp {}",
assign.target, value
));
}
}
BinaryOp::Mul => {
// Optimization: Skip self-assignment if target == var
if assign.target != *var || var_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
commands.push(format!(
"scoreboard players set #multiplier temp {}",
value
));
commands.push(format!(
"scoreboard players operation {} temp *= #multiplier temp",
assign.target
));
}
BinaryOp::Div => {
// Check for division by zero at compile time
if value == 0 {
return Err(format!(
"Division by zero in assignment: {} = {} / {}",
assign.target, var, value
));
}
// Optimization: Skip self-assignment if target == var
if assign.target != *var || var_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
commands.push(format!(
"scoreboard players set #divisor temp {}",
value
));
commands.push(format!(
"scoreboard players operation {} temp /= #divisor temp",
assign.target
));
}
BinaryOp::Mod => {
// Check for modulo by zero at compile time
if value == 0 {
return Err(format!(
"Modulo by zero in assignment: {} = {} % {}",
assign.target, var, value
));
}
// Optimization: Skip self-assignment if target == var
if assign.target != *var || var_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
commands.push(format!(
"scoreboard players set #modulus temp {}",
value
));
commands.push(format!(
"scoreboard players operation {} temp %= #modulus temp",
assign.target
));
}
BinaryOp::Pow => {
// Power operation: compile-time expansion
if value < 0 {
return Err(
"Power exponent must be non-negative".to_string()
);
}
// Limit maximum exponent to prevent excessive command generation
const MAX_POWER_EXPONENT: i32 = 100;
if value > MAX_POWER_EXPONENT {
return Err(format!(
"Power exponent too large: {} > {}.\n\
\n\
Large exponents generate {} multiplication commands, which is excessive.\n\
Solution: Use a smaller exponent or implement iterative multiplication:\n\
\n\
result = 1\n\
for i in range({}):\n\
result = result * base",
value, MAX_POWER_EXPONENT, value - 1, value
));
}
if value == 0 {
// x^0 = 1
commands.push(format!(
"scoreboard players set {} temp 1",
assign.target
));
} else {
// Optimization: Skip self-assignment if target == var
if assign.target != *var || var_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
}
if value > 1 {
commands.push(format!(
"scoreboard players operation #power_base temp = {} temp",
assign.target
));
for _ in 0..(value - 1) {
commands.push(format!(
"scoreboard players operation {} temp *= #power_base temp",
assign.target
));
}
}
}
}
_ => {}
}
}
(Expression::Number(n1), Expression::Number(n2)) => {
// Constant expression evaluation with error checking
let result_f64 = match op {
BinaryOp::Add => *n1 + *n2,
BinaryOp::Sub => *n1 - *n2,
BinaryOp::Mul => *n1 * *n2,
BinaryOp::Div => {
if *n2 == 0.0 {
return Err(format!(
"Division by zero in constant expression: {} / {}",
n1, n2
));
}
*n1 / *n2
}
BinaryOp::Mod => {
if *n2 == 0.0 {
return Err(format!(
"Modulo by zero in constant expression: {} % {}",
n1, n2
));
}
(*n1 as i32 % *n2 as i32) as f64
}
BinaryOp::Pow => {
let base = *n1 as i32;
let exp = *n2 as i32;
if exp < 0 {
return Err(
"Power exponent must be non-negative".to_string()
);
}
match base.checked_pow(exp as u32) {
Some(result) => result as f64,
None => {
eprintln!(
"⚠️ Warning: Power operation {}^{} overflows i32, clamping to i32::MAX",
base, exp
);
i32::MAX as f64
}
}
}
_ => 0.0,
};
let result = result_f64 as i32;
if result_f64.fract() != 0.0 {
eprintln!(
"⚠️ Warning: Constant expression result {} will lose precision.\n\
Scoreboard only supports integers. Fractional part will be truncated to: {}",
result_f64, result
);
}
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, result
));
}
(Expression::Number(n), Expression::Identifier(var)) => {
// Handle Number + Variable (e.g., score = 5 + x)
self.data_pack.track_objective("temp");
// Mark target as scoreboard-backed
self.scoreboard_variables.insert(assign.target.clone());
if n.fract() != 0.0 {
eprintln!(
"⚠️ Warning: Float value {} in binary operation will lose precision.\n\
Scoreboard only supports integers. Fractional part will be truncated to: {}",
n, *n as i32
);
}
let value = *n as i32;
let var_obj = self
.variable_objectives
.get(var)
.unwrap_or(&"temp".to_string())
.clone();
match op {
BinaryOp::Add => {
// score = value + var → score = var, score += value
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
if value < 0 {
commands.push(format!(
"scoreboard players remove {} temp {}",
assign.target, -value
));
} else {
commands.push(format!(
"scoreboard players add {} temp {}",
assign.target, value
));
}
}
BinaryOp::Sub => {
// score = value - var → score = value, score -= var
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, value
));
commands.push(format!(
"scoreboard players operation {} temp -= {} {}",
assign.target, var, var_obj
));
}
BinaryOp::Mul => {
// score = value * var → score = var, score *= value
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var, var_obj
));
commands.push(format!(
"scoreboard players set #multiplier temp {}",
value
));
commands.push(format!(
"scoreboard players operation {} temp *= #multiplier temp",
assign.target
));
}
BinaryOp::Div => {
// score = value / var (not commonly used, but implemented)
// Check if var is a compile-time constant with value 0
if let Some(const_val) = self.compile_time_constants.get(var) {
if *const_val == 0.0 {
return Err(format!(
"Division by zero: Variable '{}' has constant value 0.\n\
\n\
Division by zero causes undefined behavior in Minecraft.\n\
Solution: Check the divisor before division:\n\
\n\
if {} != 0:\n\
{} = {} / {}",
var, var, assign.target, value, var
));
}
}
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, value
));
commands.push(format!(
"scoreboard players operation {} temp /= {} {}",
assign.target, var, var_obj
));
}
BinaryOp::Mod => {
// score = value % var
// Check if var is a compile-time constant with value 0
if let Some(const_val) = self.compile_time_constants.get(var) {
if *const_val == 0.0 {
return Err(format!(
"Modulo by zero: Variable '{}' has constant value 0.\n\
\n\
Modulo by zero causes undefined behavior in Minecraft.\n\
Solution: Check the divisor before modulo:\n\
\n\
if {} != 0:\n\
{} = {} % {}",
var, var, assign.target, value, var
));
}
}
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, value
));
commands.push(format!(
"scoreboard players operation {} temp %= {} {}",
assign.target, var, var_obj
));
}
BinaryOp::Pow => {
// Power with variable exponent is not supported at compile time
return Err("Power with variable exponent is not supported. Use constant exponents only.".to_string());
}
_ => {}
}
}
(Expression::Identifier(var1), Expression::Identifier(var2)) => {
let left_const = self.compile_time_constants.get(var1).copied();
let right_const = self.compile_time_constants.get(var2).copied();
if let Some(value) = left_const {
let num = value as i32;
let var_obj = self
.variable_objectives
.get(var2)
.unwrap_or(&"temp".to_string())
.clone();
match op {
BinaryOp::Add => {
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, num
));
commands.push(format!(
"scoreboard players operation {} temp += {} {}",
assign.target, var2, var_obj
));
}
BinaryOp::Sub => {
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, num
));
commands.push(format!(
"scoreboard players operation {} temp -= {} {}",
assign.target, var2, var_obj
));
}
BinaryOp::Mul => {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var2, var_obj
));
commands.push(format!(
"scoreboard players set #multiplier temp {}",
num
));
commands.push(format!(
"scoreboard players operation {} temp *= #multiplier temp",
assign.target
));
}
BinaryOp::Div => {
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, num
));
commands.push(format!(
"scoreboard players operation {} temp /= {} {}",
assign.target, var2, var_obj
));
}
BinaryOp::Mod => {
commands.push(format!(
"scoreboard players set {} temp {}",
assign.target, num
));
commands.push(format!(
"scoreboard players operation {} temp %= {} {}",
assign.target, var2, var_obj
));
}
BinaryOp::Pow => {
return Err("Power with variable exponent is not supported. Use constant exponents only.".to_string());
}
_ => {}
}
} else if let Some(value) = right_const {
let num = value as i32;
let var1_obj = self
.variable_objectives
.get(var1)
.unwrap_or(&"temp".to_string())
.clone();
if assign.target != *var1 || var1_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var1, var1_obj
));
}
match op {
BinaryOp::Add => {
if num < 0 {
commands.push(format!(
"scoreboard players remove {} temp {}",
assign.target, -num
));
} else {
commands.push(format!(
"scoreboard players add {} temp {}",
assign.target, num
));
}
}
BinaryOp::Sub => {
if num < 0 {
commands.push(format!(
"scoreboard players add {} temp {}",
assign.target, -num
));
} else {
commands.push(format!(
"scoreboard players remove {} temp {}",
assign.target, num
));
}
}
BinaryOp::Mul => {
commands.push(format!(
"scoreboard players set #multiplier temp {}",
num
));
commands.push(format!(
"scoreboard players operation {} temp *= #multiplier temp",
assign.target
));
}
BinaryOp::Div => {
if num == 0 {
return Err(
"Division by zero in assignment".to_string()
);
}
commands.push(format!(
"scoreboard players set #divisor temp {}",
num
));
commands.push(format!(
"scoreboard players operation {} temp /= #divisor temp",
assign.target
));
}
BinaryOp::Mod => {
if num == 0 {
return Err("Modulo by zero in assignment".to_string());
}
commands.push(format!(
"scoreboard players set #modulus temp {}",
num
));
commands.push(format!(
"scoreboard players operation {} temp %= #modulus temp",
assign.target
));
}
BinaryOp::Pow => {
if num < 0 {
return Err(
"Power exponent must be non-negative".to_string()
);
}
if num == 0 {
commands.push(format!(
"scoreboard players set {} temp 1",
assign.target
));
} else if num == 1 {
// nothing to do
} else {
commands.push(format!(
"scoreboard players operation #power_base temp = {} temp",
assign.target
));
for _ in 0..(num - 1) {
commands.push(format!(
"scoreboard players operation {} temp *= #power_base temp",
assign.target
));
}
}
}
_ => {}
}
} else {
// Handle variable op variable (e.g., z = x * y)
let var1_obj = self
.variable_objectives
.get(var1)
.unwrap_or(&"temp".to_string())
.clone();
let var2_obj = self
.variable_objectives
.get(var2)
.unwrap_or(&"temp".to_string())
.clone();
if assign.target != *var1 || var1_obj != "temp" {
commands.push(format!(
"scoreboard players operation {} temp = {} {}",
assign.target, var1, var1_obj
));
}
match op {
BinaryOp::Add => {
commands.push(format!(
"scoreboard players operation {} temp += {} {}",
assign.target, var2, var2_obj
));
}
BinaryOp::Sub => {
commands.push(format!(
"scoreboard players operation {} temp -= {} {}",
assign.target, var2, var2_obj
));
}
BinaryOp::Mul => {
commands.push(format!(
"scoreboard players operation {} temp *= {} {}",
assign.target, var2, var2_obj
));
}
BinaryOp::Div => {
// Check if var2 is a compile-time constant with value 0
if let Some(const_val) =
self.compile_time_constants.get(var2)
{
if *const_val == 0.0 {
return Err(format!(
"Division by zero: Variable '{}' has constant value 0.\n\
\n\
Division by zero causes undefined behavior in Minecraft.\n\
Solution: Check the divisor before division:\n\
\n\
if {} != 0:\n\
{} = {} / {}",
var2, var2, assign.target, assign.target, var2
));
}
}
commands.push(format!(
"scoreboard players operation {} temp /= {} {}",
assign.target, var2, var2_obj
));
}
BinaryOp::Mod => {
// Check if var2 is a compile-time constant with value 0
if let Some(const_val) =
self.compile_time_constants.get(var2)
{
if *const_val == 0.0 {
return Err(format!(
"Modulo by zero: Variable '{}' has constant value 0.\n\
\n\
Modulo by zero causes undefined behavior in Minecraft.\n\
Solution: Check the divisor before modulo:\n\
\n\
if {} != 0:\n\
{} = {} % {}",
var2, var2, assign.target, assign.target, var2
));
}
}
commands.push(format!(
"scoreboard players operation {} temp %= {} {}",
assign.target, var2, var2_obj
));
}
BinaryOp::Pow => {
return Err("Power with variable exponent is not supported. Use constant exponents only.".to_string());
}
_ => {}
}
}
}
_ => {
// Unsupported combination
return Err(format!(
"Unsupported binary operation: {:?} between {:?} and {:?}",
op, left, right
));
}
}
}
Expression::Call(func, _args) => {
// Function calls in assignments are not supported
let func_name = match &**func {
Expression::Identifier(name) => name.clone(),
Expression::Attribute(obj, method) => {
if let Expression::Identifier(module) = &**obj {
format!("{}.{}", module, method)
} else {
"unknown".to_string()
}
}
_ => "unknown".to_string(),
};
return Err(format!(
"Cannot assign function call result to variable.\n\n\
Attempted: {} = {}()\n\n\
Minecraft functions don't return values that can be assigned to variables.\n\n\
Solutions:\n\
1. Call the function separately (not in an assignment):\n\
{}()\n\
# Then use scoreboard operations to track state\n\n\
2. If you need to track state, use scoreboard variables:\n\
# In the called function:\n\
def {}():\n\
result = 42 # This sets a scoreboard value\n\
\n\
# In the caller:\n\
{}()\n\
# Now 'result' variable can be used\n\n\
3. Pass the target variable name as a parameter:\n\
def set_value(target_var):\n\
/scoreboard players set {{target_var}} temp 42\n\
\n\
set_value(\"my_var\")",
assign.target, func_name, func_name, func_name, func_name
));
}
Expression::None => {
// None value
return Err(format!(
"Cannot assign None/null to variable '{}'.\n\n\
Minecraft scoreboards require numeric values.\n\n\
Solutions:\n\
1. Use 0 to represent 'no value':\n\
{} = 0\n\n\
2. Use -1 to represent 'unset':\n\
{} = -1\n\n\
3. Check for a specific value before using:\n\
if {} != -1:\n\
# value is set",
assign.target, assign.target, assign.target, assign.target
));
}
_ => {
// Catch-all for any other unsupported expression types
return Err(format!(
"Unsupported expression type in assignment to variable '{}'.\n\n\
Expression type: {:?}\n\n\
Supported assignment types:\n\
- Numbers: x = 10\n\
- Variables: x = y\n\
- Arithmetic: x = y + z * 2\n\
- Unary operations: x = -y\n\
- Strings (in commands only): message = \"text\" (use in tellraw)\n\
- Booleans (in commands only): flag = True (use in tellraw)\n\n\
Not supported in assignments:\n\
- Function calls: x = func() (call separately)\n\
- Attribute access: x = obj.field\n\
- Subscripts: x = arr[0]\n\
- None/null values",
assign.target, assign.value
));
}
}
}
Ok(())
}
fn is_runtime_condition_expression(expr: &Expression) -> bool {
match expr {
Expression::Binary(_, op, _) => matches!(
op,
BinaryOp::Eq
| BinaryOp::NotEq
| BinaryOp::Lt
| BinaryOp::LtEq
| BinaryOp::Gt
| BinaryOp::GtEq
| BinaryOp::And
| BinaryOp::Or
),
Expression::Unary(UnaryOp::Not, _) => true,
_ => false,
}
}
pub(in crate::transpiler) fn serialize_to_snbt(
&self,
expr: &Expression,
) -> Result<String, String> {
match expr {
Expression::Number(n) => {
if n.fract() == 0.0 {
Ok(format!("{}", *n as i32))
} else {
Ok(format!("{}f", n))
}
}
Expression::String(s) => {
let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
Ok(format!("\"{}\"", escaped))
}
Expression::Boolean(b) => Ok(if *b {
"1b".to_string()
} else {
"0b".to_string()
}),
Expression::Array(items) => {
let mut result = String::from("[");
for (i, item) in items.iter().enumerate() {
if i > 0 {
result.push(',');
}
result.push_str(&self.serialize_to_snbt(item)?);
}
result.push(']');
Ok(result)
}
Expression::Map(entries) => {
let mut result = String::from("{");
for (i, (key, value)) in entries.iter().enumerate() {
if i > 0 {
result.push(',');
}
result.push_str(&Self::serialize_snbt_key(key));
result.push(':');
result.push_str(&self.serialize_to_snbt(value)?);
}
result.push('}');
Ok(result)
}
Expression::Identifier(name) => {
if let Some(val) = self.compile_time_constants.get(name) {
Ok(format!("{}", val))
} else {
Err(format!("Variables inside array/map literals are not yet supported in this context ('{}'). Use constants or literal values.", name))
}
}
_ => Err(format!(
"Unsupported expression in SNBT serialization: {:?}",
expr
)),
}
}
fn serialize_snbt_key(key: &str) -> String {
if !key.is_empty()
&& key
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.' || c == '+')
{
return key.to_string();
}
let escaped = key.replace('\\', "\\\\").replace('"', "\\\"");
format!("\"{}\"", escaped)
}
}