cobble-lang 0.6.3

A modern, Python-like language for creating Minecraft Data Packs
Documentation
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
use crate::ast::*;
use crate::transpiler::Transpiler;

impl Transpiler {
    pub(in crate::transpiler) fn process_execute_block(
        &mut self,
        exec_block: &ExecuteBlock,
    ) -> Result<(), String> {
        // Build execute command from modifiers
        let mut execute_parts = vec!["execute".to_string()];

        let mut has_macro_params = false;
        for modifier in &exec_block.modifiers {
            match modifier {
                ExecuteModifier::As(selector) => {
                    // Check if selector contains a variable interpolation pattern {param}
                    let mut processed_selector =
                        if selector.starts_with('{') && selector.ends_with('}') {
                            let param_name = &selector[1..selector.len() - 1];
                            if self.current_context.is_param(param_name) {
                                has_macro_params = true;
                                format!("$({})", param_name)
                            } else {
                                selector.clone()
                            }
                        } else {
                            selector.clone()
                        };

                    // Replace selector aliases (@Name -> @a[...])
                    if processed_selector.starts_with('@') {
                        let selector_name = processed_selector.strip_prefix('@').unwrap_or("");
                        if let Some(actual_selector) = self.selector_aliases.get(selector_name) {
                            processed_selector = actual_selector.clone();
                        }
                    }

                    execute_parts.push(format!("as {}", processed_selector));
                }
                ExecuteModifier::At(selector) => {
                    // Check if selector contains a variable interpolation pattern {param}
                    let mut processed_selector =
                        if selector.starts_with('{') && selector.ends_with('}') {
                            let param_name = &selector[1..selector.len() - 1];
                            if self.current_context.is_param(param_name) {
                                has_macro_params = true;
                                format!("$({})", param_name)
                            } else {
                                selector.clone()
                            }
                        } else {
                            selector.clone()
                        };

                    // Replace selector aliases (@Name -> @a[...])
                    if processed_selector.starts_with('@') {
                        let selector_name = processed_selector.strip_prefix('@').unwrap_or("");
                        if let Some(actual_selector) = self.selector_aliases.get(selector_name) {
                            processed_selector = actual_selector.clone();
                        }
                    }

                    execute_parts.push(format!("at {}", processed_selector));
                }
                ExecuteModifier::If(expr) => {
                    // Python-style expression - translate to Minecraft condition
                    let condition = self.translate_condition(expr)?;

                    // Special handling for OR conditions
                    if condition.starts_with("OR(") {
                        // Mark that this execute block has OR condition
                        // We'll handle this specially when generating commands
                        execute_parts.push(format!("OR_CONDITION:{}", condition));
                        // Don't return error, continue processing
                        continue;
                    }

                    // Special handling for AND conditions which return multiple "if" parts
                    if condition.contains(" if ") || condition.contains(" unless ") {
                        // This is an AND condition that already includes multiple conditions
                        // Don't add another "if" prefix
                        execute_parts.push(condition);
                    } else if condition.starts_with("if ") || condition.starts_with("unless ") {
                        // Single condition with prefix
                        execute_parts.push(condition);
                    } else {
                        // Single condition without prefix
                        execute_parts.push(format!("if {}", condition));
                    }
                }
                ExecuteModifier::IfRaw(condition) => {
                    // Check for macro parameters in the condition
                    if condition.contains("{") && condition.contains("}") {
                        // Check if any of the {var} patterns are macro parameters
                        for param in &self.current_context.params {
                            if condition.contains(&format!("{{{}}}", param)) {
                                has_macro_params = true;
                                break;
                            }
                        }
                    }

                    // Check if this is actually a Python expression that needs translation
                    if self.looks_like_python_expression(condition) {
                        // Try to parse and translate as Python expression
                        if let Ok(translated) =
                            self.try_translate_python_expression(condition, false)
                        {
                            // Check if it's an OR condition marker
                            if translated.starts_with("OR(") {
                                // Mark as OR condition for special handling
                                execute_parts.push(format!("OR_CONDITION:{}", translated));
                                continue;
                            }
                            // Check if the translated condition already has a prefix
                            // This happens with AND conditions ("if ... if ...") or != conditions ("unless ...")
                            if translated.starts_with("if ") || translated.starts_with("unless ") {
                                execute_parts.push(translated);
                            } else {
                                execute_parts.push(format!("if {}", translated));
                            }
                        } else {
                            // Translation failed - this is a Python expression we can't handle
                            return Err(format!(
                                "Failed to translate Python expression '{}' to Minecraft condition.\n\
                                 This may be an unsupported construct like OR conditions.\n\
                                 Consider rewriting the condition or using raw Minecraft syntax.",
                                condition
                            ));
                        }
                    } else {
                        // Raw Minecraft syntax - check for AND/OR conditions
                        if condition.contains(" or ") {
                            // OR conditions need special handling with temp variables
                            // Mark as OR condition for later processing
                            execute_parts.push(format!(
                                "OR_CONDITION:OR({})",
                                condition.replace(" or ", ";")
                            ));
                        } else if condition.contains(" and ") {
                            // Split on " and " and create multiple if conditions
                            let parts: Vec<&str> = condition.split(" and ").collect();
                            for part in parts {
                                let part = part.trim();
                                // Fix spacing for range operators
                                let fixed_part = if part.contains("matches..") {
                                    part.replace("matches..", "matches ..")
                                } else if part.contains("matches-") && !part.contains("matches -") {
                                    part.replace("matches-", "matches -")
                                } else {
                                    part.to_string()
                                };
                                execute_parts.push(format!("if {}", fixed_part));
                            }
                        } else {
                            // Fix spacing for range operators in single conditions too
                            let fixed_condition = if condition.contains("matches..") {
                                condition.replace("matches..", "matches ..")
                            } else if condition.contains("matches-")
                                && !condition.contains("matches -")
                            {
                                condition.replace("matches-", "matches -")
                            } else {
                                condition.to_string()
                            };
                            execute_parts.push(format!("if {}", fixed_condition));
                        }
                    }
                }
                ExecuteModifier::Unless(expr) => {
                    // Python-style expression - translate to Minecraft condition
                    let condition = self.translate_condition(expr)?;

                    // Special handling for OR conditions
                    if condition.starts_with("OR(") {
                        // unless (A or B) = unless A and unless B (De Morgan's law)
                        // We can chain unless conditions in Minecraft
                        let or_conditions = Transpiler::flatten_or_conditions(&condition)?;
                        for cond in or_conditions {
                            // Add each condition as "unless"
                            if cond.starts_with("unless ") {
                                execute_parts.push(cond);
                            } else {
                                execute_parts.push(format!("unless {}", cond));
                            }
                        }
                        continue;
                    }

                    // Special handling for AND conditions
                    if condition.contains(" if ") || condition.contains(" unless ") {
                        // This is an AND condition that has "if" parts
                        // For unless (A and B), we apply De Morgan's law: unless (A and B) = unless A or unless B
                        // Since Minecraft doesn't support OR directly, we need to use a flag variable
                        execute_parts.push(format!("UNLESS_AND:{}", condition));
                        continue;
                    }

                    // translate_condition may return "unless ..." for != operator
                    if condition.starts_with("unless ") {
                        execute_parts.push(condition);
                    } else {
                        execute_parts.push(format!("unless {}", condition));
                    }
                }
                ExecuteModifier::UnlessRaw(condition) => {
                    // Check if this is actually a Python expression that needs translation
                    if self.looks_like_python_expression(condition) {
                        // Try to parse and translate as Python expression
                        if let Ok(translated) =
                            self.try_translate_python_expression(condition, true)
                        {
                            // Check if it's an OR condition marker
                            if translated.starts_with("OR(") {
                                // unless (A or B) = unless A and unless B (De Morgan's law)
                                let or_conditions = Transpiler::flatten_or_conditions(&translated)?;
                                for cond in or_conditions {
                                    if cond.starts_with("unless ") {
                                        execute_parts.push(cond);
                                    } else {
                                        execute_parts.push(format!("unless {}", cond));
                                    }
                                }
                                continue;
                            }
                            // Check if the translated condition already has "unless" prefix(es)
                            if translated.starts_with("unless ") {
                                execute_parts.push(translated);
                            } else {
                                execute_parts.push(format!("unless {}", translated));
                            }
                        } else {
                            // Translation failed - this is a Python expression we can't handle
                            return Err(format!(
                                "Failed to translate Python expression '{}' to Minecraft condition.\n\
                                 This may be an unsupported construct.\n\
                                 Consider rewriting the condition or using raw Minecraft syntax.",
                                condition
                            ));
                        }
                    } else {
                        // Raw Minecraft syntax - check for AND conditions
                        if condition.contains(" and ") {
                            // Split on " and " and create multiple unless conditions
                            // unless (A and B) requires special handling with temp variable
                            execute_parts.push(format!("UNLESS_AND:{}", condition));
                        } else {
                            // Fix spacing for range operators
                            let fixed_condition = if condition.contains("matches..") {
                                condition.replace("matches..", "matches ..")
                            } else if condition.contains("matches-")
                                && !condition.contains("matches -")
                            {
                                condition.replace("matches-", "matches -")
                            } else {
                                condition.to_string()
                            };
                            execute_parts.push(format!("unless {}", fixed_condition));
                        }
                    }
                }
                ExecuteModifier::Positioned(pos) => {
                    // Workaround for parser bug: positioned might incorrectly contain "if" conditions
                    if pos.contains(" if score ") {
                        // Split at " if " to separate positioned from condition
                        if let Some(if_pos) = pos.find(" if score ") {
                            let actual_pos = &pos[..if_pos];
                            let condition = &pos[if_pos + 4..]; // Skip " if "

                            execute_parts.push(format!("positioned {}", actual_pos));

                            // Process the condition part
                            if condition.contains(" and ") {
                                // Split AND conditions
                                let parts: Vec<&str> = condition.split(" and ").collect();
                                for part in parts {
                                    let part = part.trim();
                                    let fixed_part = if part.contains("matches..") {
                                        part.replace("matches..", "matches ..")
                                    } else {
                                        part.to_string()
                                    };
                                    execute_parts.push(format!("if {}", fixed_part));
                                }
                            } else {
                                let fixed_condition = if condition.contains("matches..") {
                                    condition.replace("matches..", "matches ..")
                                } else {
                                    condition.to_string()
                                };
                                execute_parts.push(format!("if {}", fixed_condition));
                            }
                        } else {
                            execute_parts.push(format!("positioned {}", pos));
                        }
                    } else {
                        execute_parts.push(format!("positioned {}", pos));
                    }
                }
                ExecuteModifier::Rotated(rot) => {
                    execute_parts.push(format!("rotated {}", rot));
                }
                ExecuteModifier::In(dimension) => {
                    execute_parts.push(format!("in {}", dimension));
                }
                ExecuteModifier::Anchored(anchor) => {
                    execute_parts.push(format!("anchored {}", anchor));
                }
                ExecuteModifier::Align(axes) => {
                    execute_parts.push(format!("align {}", axes));
                }
                ExecuteModifier::Store(store_cmd) => {
                    execute_parts.push(format!("store {}", store_cmd));
                }
            }
        }

        // If we have macro params, replace {param} with $(param) in execute parts
        if has_macro_params {
            execute_parts = execute_parts
                .into_iter()
                .map(|part| {
                    let mut result = part.clone();
                    for param in &self.current_context.params {
                        let from = format!("{{{}}}", param);
                        let to = format!("$({})", param);
                        result = result.replace(&from, &to);
                    }
                    result
                })
                .collect();
        }

        // Check if we have special conditions
        let has_or_condition = execute_parts.iter().any(|p| p.starts_with("OR_CONDITION:"));
        let has_unless_and = execute_parts.iter().any(|p| p.starts_with("UNLESS_AND:"));

        if has_unless_and {
            // Handle unless (A and B) - use temp variable
            // unless (A and B) = check if both are true, then unless that result
            let mut unless_and_str = None;
            let mut other_modifiers = Vec::new();

            for part in &execute_parts {
                if let Some(stripped) = part.strip_prefix("UNLESS_AND:") {
                    unless_and_str = Some(stripped);
                } else {
                    other_modifiers.push(part.clone());
                }
            }

            if let Some(and_str) = unless_and_str {
                // Generate a unique temp variable for unless AND result
                self.data_pack.track_objective("temp");
                let unless_var = format!("unless_temp_{}", self.get_unique_id());
                let modifier_args = Self::modifier_args(&other_modifiers);
                let score_holder = if Self::has_as_modifier(&other_modifiers) {
                    "@s".to_string()
                } else {
                    unless_var.clone()
                };

                // Initialize result to 0 (false)
                if let Some(ref mut commands) = self.current_function {
                    if modifier_args.is_empty() && score_holder != "@s" {
                        commands.push(format!("scoreboard players set {} temp 0", score_holder));
                    } else {
                        commands.push(Self::execute_with_modifiers(
                            &modifier_args,
                            &format!("run scoreboard players set {} temp 0", score_holder),
                        ));
                    }

                    // Set to 1 if ALL conditions are true (the AND check)
                    // Split the AND conditions and add "if" prefix to each
                    let and_conditions: Vec<String> = and_str
                        .split(" and ")
                        .map(|cond| {
                            let cond = cond.trim();
                            // Fix spacing for range operators
                            let fixed_cond = if cond.contains("matches..") {
                                cond.replace("matches..", "matches ..")
                            } else {
                                cond.to_string()
                            };
                            format!("if {}", fixed_cond)
                        })
                        .collect();

                    let and_check = and_conditions.join(" ");

                    let check_cmd = Self::execute_with_modifiers(
                        &modifier_args,
                        &format!(
                            "{} run scoreboard players set {} temp 1",
                            and_check, score_holder
                        ),
                    );

                    if has_macro_params {
                        commands.push(format!("${}", check_cmd));
                    } else {
                        commands.push(check_cmd);
                    }
                }

                // Now process body with the unless result check (unless the AND was true)
                let execute_prefix = Self::execute_with_modifiers(
                    &modifier_args,
                    &format!("unless score {} temp matches 1", score_holder),
                );

                // Process body statements
                for stmt in &exec_block.body {
                    let capture = self.capture_statement(stmt)?;
                    self.append_transformed_capture(capture, |cmd| {
                        let inner_cmd = if let Some(stripped) = cmd.strip_prefix('$') {
                            has_macro_params = true;
                            stripped
                        } else {
                            cmd
                        };

                        let final_cmd =
                            if let Some(inner_parts) = inner_cmd.strip_prefix("execute ") {
                                format!("{} {}", execute_prefix, inner_parts)
                            } else {
                                format!("{} run {}", execute_prefix, inner_cmd)
                            };

                        if has_macro_params {
                            format!("${}", final_cmd)
                        } else {
                            final_cmd
                        }
                    })?;
                }
            }
        } else if has_or_condition {
            // Handle OR condition specially
            // Extract the OR condition and other modifiers
            let mut or_condition_str = None;
            let mut other_modifiers = Vec::new();

            for part in &execute_parts {
                if let Some(stripped) = part.strip_prefix("OR_CONDITION:") {
                    or_condition_str = Some(stripped);
                } else {
                    other_modifiers.push(part.clone());
                }
            }

            if let Some(or_str) = or_condition_str {
                // Process OR condition
                let or_conditions = Transpiler::flatten_or_conditions(or_str)?;
                let modifier_args = Self::modifier_args(&other_modifiers);

                // Generate a unique temp variable for this OR result
                self.data_pack.track_objective("temp");
                let or_var = format!("or_temp_{}", self.get_unique_id());
                let score_holder = if Self::has_as_modifier(&other_modifiers) {
                    "@s".to_string()
                } else {
                    or_var.clone()
                };

                // Initialize OR result to 0
                if let Some(ref mut commands) = self.current_function {
                    if modifier_args.is_empty() && score_holder != "@s" {
                        commands.push(format!("scoreboard players set {} temp 0", score_holder));
                    } else {
                        commands.push(Self::execute_with_modifiers(
                            &modifier_args,
                            &format!("run scoreboard players set {} temp 0", score_holder),
                        ));
                    }

                    // Check each OR condition
                    for cond in or_conditions {
                        let cond_prefix = if cond.starts_with("if ") || cond.starts_with("unless ")
                        {
                            cond.clone()
                        } else {
                            format!("if {}", cond)
                        };

                        // If any condition is true, set or_result to 1
                        let check_cmd = Self::execute_with_modifiers(
                            &modifier_args,
                            &format!(
                                "{} run scoreboard players set {} temp 1",
                                cond_prefix, score_holder
                            ),
                        );

                        if has_macro_params {
                            commands.push(format!("${}", check_cmd));
                        } else {
                            commands.push(check_cmd);
                        }
                    }
                }

                // Now process body with the OR result check
                let execute_prefix = Self::execute_with_modifiers(
                    &modifier_args,
                    &format!("if score {} temp matches 1", score_holder),
                );

                // Process body statements
                for stmt in &exec_block.body {
                    let capture = self.capture_statement(stmt)?;
                    self.append_transformed_capture(capture, |cmd| {
                        let inner_cmd = if let Some(stripped) = cmd.strip_prefix('$') {
                            has_macro_params = true;
                            stripped
                        } else {
                            cmd
                        };

                        let final_cmd =
                            if let Some(inner_parts) = inner_cmd.strip_prefix("execute ") {
                                format!("{} {}", execute_prefix, inner_parts)
                            } else {
                                format!("{} run {}", execute_prefix, inner_cmd)
                            };

                        if has_macro_params {
                            format!("${}", final_cmd)
                        } else {
                            final_cmd
                        }
                    })?;
                }
            }
        } else {
            // Normal execute without OR condition
            let execute_prefix = execute_parts.join(" ");

            // Process body statements
            for stmt in &exec_block.body {
                let capture = self.capture_statement(stmt)?;
                self.append_transformed_capture(capture, |cmd| {
                    let inner_cmd = if let Some(stripped) = cmd.strip_prefix('$') {
                        has_macro_params = true;
                        stripped
                    } else {
                        cmd
                    };

                    let final_cmd = if let Some(inner_parts) = inner_cmd.strip_prefix("execute ") {
                        format!("{} {}", execute_prefix, inner_parts)
                    } else {
                        format!("{} run {}", execute_prefix, inner_cmd)
                    };

                    if has_macro_params {
                        format!("${}", final_cmd)
                    } else {
                        final_cmd
                    }
                })?;
            }
        }

        Ok(())
    }

    fn modifier_args(parts: &[String]) -> String {
        parts
            .iter()
            .filter(|part| part.as_str() != "execute")
            .cloned()
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn has_as_modifier(parts: &[String]) -> bool {
        parts.iter().any(|part| part.starts_with("as "))
    }

    fn execute_with_modifiers(modifier_args: &str, tail: &str) -> String {
        if modifier_args.is_empty() {
            format!("execute {}", tail)
        } else {
            format!("execute {} {}", modifier_args, tail)
        }
    }
}