opy-rs 0.3.8

Standalone OverPy-compatible .opy implementation with parsing, tooling, and bounded Workshop compilation.
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
use super::*;

/// One value-position call `lower_call` resolves by name rather than
/// through the manifest.
#[derive(Clone, Copy)]
pub(crate) enum SpecialValueCall {
    /// `sorted(array, key)` — the key argument lowers as a lambda and only
    /// the `key=` keyword spelling is accepted.
    Sorted,
    /// `createWorkshopSetting(type, category, name, default, sortOrder?)` —
    /// its own arity and setting-type handling.
    CreateWorkshopSetting,
    /// `createWorkshopSetting{Bool,Enum,Int,Float,Hero}` — passed through
    /// verbatim as a call.
    SettingFactory,
}

/// The special-call spellings `lower_call` dispatches on. `crate::lookup`'s
/// reported special-call names and the `crate::matcher` candidate pool must
/// be exactly the spellings here — the lookup tests assert the tables
/// agree (issue #469).
pub(crate) const SPECIAL_VALUE_CALLS: &[(&str, SpecialValueCall)] = &[
    ("sorted", SpecialValueCall::Sorted),
    (
        "createWorkshopSetting",
        SpecialValueCall::CreateWorkshopSetting,
    ),
    (
        "createWorkshopSettingBool",
        SpecialValueCall::SettingFactory,
    ),
    (
        "createWorkshopSettingEnum",
        SpecialValueCall::SettingFactory,
    ),
    ("createWorkshopSettingInt", SpecialValueCall::SettingFactory),
    (
        "createWorkshopSettingFloat",
        SpecialValueCall::SettingFactory,
    ),
    (
        "createWorkshopSettingHero",
        SpecialValueCall::SettingFactory,
    ),
];

impl SpecialValueCall {
    /// The special call a name resolves to, when it is one.
    pub(crate) fn from_name(name: &str) -> Option<SpecialValueCall> {
        SPECIAL_VALUE_CALLS
            .iter()
            .find(|(spelling, _)| *spelling == name)
            .map(|(_, special)| *special)
    }
}

fn literal_number(expr: &Expr) -> Option<f64> {
    match expr {
        Expr::Number { value, .. } => Some(*value),
        Expr::Unary { op, operand, .. } if matches!(op.as_str(), "+" | "-") => {
            literal_number(operand).map(|value| if op == "-" { -value } else { value })
        }
        _ => None,
    }
}

fn compressed_literal_values(expr: &Expr) -> Option<Vec<f64>> {
    match expr {
        Expr::Null { .. } => Some(vec![0.0]),
        Expr::Number { .. } | Expr::Unary { .. } => literal_number(expr).map(|value| vec![value]),
        Expr::Call { name, args, .. } if name == "vect" && args.len() == 3 => Some(
            args.iter()
                .map(|arg| literal_number(&arg.value))
                .collect::<Option<Vec<_>>>()?,
        ),
        _ => None,
    }
}

fn is_compressible_column(values: &[&Expr]) -> bool {
    let Some(numbers) = values
        .iter()
        .map(|value| compressed_literal_values(value))
        .collect::<Option<Vec<_>>>()
    else {
        return false;
    };
    is_compressible_components(&numbers)
}

fn is_compressible_components(values: &[Vec<f64>]) -> bool {
    let Some((_, limit)) = crate::lower::compressed_component_mode(values) else {
        return false;
    };
    values.iter().flatten().all(|value| value.abs() < limit)
}

impl Lowerer {
    pub(super) fn lower_tabular(
        &mut self,
        args: &[cst::CallArg],
        span: Span,
        macro_params: &[String],
    ) -> Vec<HirStmt> {
        let Some(cst::Expr::Array {
            elements: targets, ..
        }) = args.first().map(|arg| &arg.value)
        else {
            self.error_at(
                "tabular-arguments",
                "tabular first argument must be an array of variables".to_string(),
                span,
            );
            return Vec::new();
        };
        let Some(cst::Expr::Array {
            elements: values, ..
        }) = args.get(1).map(|arg| &arg.value)
        else {
            self.error_at(
                "tabular-arguments",
                "tabular second argument must be an array".to_string(),
                span,
            );
            return Vec::new();
        };
        if targets.is_empty() {
            self.error_at(
                "tabular-arguments",
                "tabular requires at least one target variable".to_string(),
                span,
            );
            return Vec::new();
        }
        if values.len() % targets.len() != 0 {
            self.error_at(
                "tabular-arguments",
                format!(
                    "tabular second argument must have a length that is a multiple of {} (length is {})",
                    targets.len(),
                    values.len()
                ),
                args.get(1).map_or(span, |arg| arg.value.span()),
            );
            return Vec::new();
        }
        let compress = matches!(
            args.get(2).map(|arg| &arg.value),
            Some(cst::Expr::Bool { value: true, .. })
        );
        let mut result = Vec::with_capacity(targets.len());
        for (column, target) in targets.iter().enumerate() {
            let target = self.lower_expr(target, macro_params, CallPosition::Value);
            let column_values_cst = values
                .iter()
                .skip(column)
                .step_by(targets.len())
                .collect::<Vec<_>>();
            let column_values = column_values_cst
                .iter()
                .map(|value| self.lower_expr(value, macro_params, CallPosition::Value))
                .collect();
            let value = HirExpr::Array {
                elements: column_values,
                span: Some(span.into()),
            };
            let value = if compress && is_compressible_column(&column_values_cst) {
                HirExpr::Call {
                    name: "compressed".to_string(),
                    args: vec![value],
                    debug_source: None,
                    span: Some(span.into()),
                }
            } else {
                value
            };
            result.push(HirStmt::Assign {
                target: Box::new(target),
                value: Box::new(value),
                span: Some(span.into()),
            });
        }
        result
    }

    pub(super) fn lower_split_dict_array(
        &mut self,
        args: &[cst::CallArg],
        span: Span,
        macro_params: &[String],
    ) -> Vec<HirStmt> {
        let Some(fields) = args.first().map(|arg| &arg.value) else {
            self.error_at(
                "split-dict-array-arity",
                "splitDictArray requires a field mapping and data array".to_string(),
                span,
            );
            return Vec::new();
        };
        let Some(rows) = args.get(1).map(|arg| &arg.value) else {
            self.error_at(
                "split-dict-array-arity",
                "splitDictArray requires a field mapping and data array".to_string(),
                span,
            );
            return Vec::new();
        };
        let Expr::Dict {
            entries: field_entries,
            ..
        } = fields
        else {
            self.error_at(
                "split-dict-array-arguments",
                "splitDictArray first argument must be a dictionary".to_string(),
                fields.span(),
            );
            return Vec::new();
        };
        let Expr::Array { elements, .. } = rows else {
            self.error_at(
                "split-dict-array-arguments",
                "splitDictArray second argument must be an array".to_string(),
                rows.span(),
            );
            return Vec::new();
        };

        let compress = matches!(
            args.get(2).map(|arg| &arg.value),
            Some(Expr::Bool { value: true, .. })
        );
        let mut result = Vec::with_capacity(field_entries.len());
        for field in field_entries {
            let Some(field_name) = expr_identifier(&field.key) else {
                self.error_at(
                    "split-dict-array-key",
                    "splitDictArray field keys must be identifiers".to_string(),
                    field.key.span(),
                );
                continue;
            };
            let target = self.lower_expr(&field.value, macro_params, CallPosition::Value);
            let previous = self.allow_dict_literal;
            self.allow_dict_literal = true;
            let values: Vec<HirExpr> = elements
                .iter()
                .map(|element| match element {
                    Expr::Dict { entries, .. } => entries
                        .iter()
                        .find(|entry| expr_identifier(&entry.key) == Some(field_name))
                        .map(|entry| {
                            self.lower_expr(&entry.value, macro_params, CallPosition::Value)
                        })
                        .unwrap_or(HirExpr::Null { span: None }),
                    _ => HirExpr::Null { span: None },
                })
                .collect();
            self.allow_dict_literal = previous;
            let compressible = compress && is_compressible(&values);
            let array = HirExpr::Array {
                elements: values,
                span: Some(span.into()),
            };
            let value = if compressible {
                HirExpr::Call {
                    name: "compressed".to_string(),
                    args: vec![array],
                    debug_source: None,
                    span: Some(span.into()),
                }
            } else {
                array
            };
            result.push(HirStmt::Assign {
                target: Box::new(target),
                value: Box::new(value),
                span: Some(span.into()),
            });
        }
        result
    }

    pub(super) fn lower_macro_body(&mut self, body: &[Stmt], params: &[String]) -> Vec<HirStmt> {
        body.iter()
            .map(|stmt| match stmt {
                Stmt::Expr { expr, span } => HirStmt::Expr {
                    expr: Box::new(self.lower_expr(expr, params, CallPosition::MacroBody)),
                    span: Some(span.into()),
                },
                _ => self.lower_stmt(stmt, params, false, false),
            })
            .collect()
    }

    /// Dispatch a by-name special call to its lowering behavior. The match
    /// is exhaustive over [`SPECIAL_VALUE_CALLS`] kinds, so every spelling
    /// the table (and the lookup) reports is bound to a real handler.
    pub(super) fn lower_special_call(
        &mut self,
        special: SpecialValueCall,
        name: &str,
        args: &[cst::CallArg],
        span: Span,
        macro_params: &[String],
    ) -> HirExpr {
        match special {
            SpecialValueCall::CreateWorkshopSetting => {
                self.lower_workshop_setting(args, span, macro_params)
            }
            SpecialValueCall::Sorted => {
                // The reference's special `sorted` parse path only strips a
                // `key=` prefix inside the second argument's lambda; every
                // other keyword spelling or position fails upstream parsing
                // (issue #437).
                for (index, arg) in args.iter().enumerate() {
                    if let Some((keyword, keyword_span)) = &arg.keyword {
                        if index != 1 || keyword != "key" {
                            self.error_at(
                                "unknown-keyword",
                                format!(
                                    "unknown keyword argument '{keyword}' for function 'sorted'"
                                ),
                                *keyword_span,
                            );
                        }
                    }
                }
                HirExpr::Call {
                    name: name.to_string(),
                    args: self.lower_arg_values_with_lambda(args, macro_params, |index, arg| {
                        index == 1 || arg.keyword.as_ref().is_some_and(|(name, _)| name == "key")
                    }),
                    debug_source: None,
                    span: Some(span.into()),
                }
            }
            SpecialValueCall::SettingFactory => HirExpr::Call {
                name: name.to_string(),
                args: self.lower_arg_values(args, macro_params),
                debug_source: None,
                span: Some(span.into()),
            },
        }
    }

    pub(super) fn lower_workshop_setting(
        &mut self,
        args: &[cst::CallArg],
        span: Span,
        macro_params: &[String],
    ) -> HirExpr {
        if !(4..=5).contains(&args.len()) {
            self.error_at(
                "invalid-arity",
                format!(
                    "function 'createWorkshopSetting' takes 4 or 5 arguments, received {}",
                    args.len()
                ),
                span,
            );
            return HirExpr::Null { span: None };
        }
        for arg in args {
            if let Some((keyword, keyword_span)) = &arg.keyword {
                self.error_at(
                    "keyword-unsupported",
                    format!(
                        "function 'createWorkshopSetting' does not accept keyword arguments ('{keyword}')"
                    ),
                    *keyword_span,
                );
            }
        }

        let setting_type = match &args[0].value {
            Expr::Type {
                name,
                args: type_args,
                span: type_span,
            } => HirExpr::Type {
                name: name.clone(),
                args: type_args
                    .iter()
                    .map(|arg| self.lower_expr(arg, macro_params, CallPosition::Value))
                    .collect(),
                span: Some((*type_span).into()),
            },
            Expr::Name {
                name,
                span: type_span,
            } if matches!(name.as_str(), "bool" | "int" | "float") => HirExpr::Type {
                name: name.clone(),
                args: Vec::new(),
                span: Some((*type_span).into()),
            },
            other => {
                self.error_at(
                    "invalid-argument",
                    "argument 1 of 'createWorkshopSetting' must be a setting type".to_string(),
                    other.span(),
                );
                HirExpr::Null { span: None }
            }
        };
        let mut lowered = Vec::with_capacity(5);
        lowered.push(setting_type);
        lowered.extend(
            args[1..]
                .iter()
                .map(|arg| self.lower_expr(&arg.value, macro_params, CallPosition::Value)),
        );
        if args.len() == 4 {
            lowered.push(HirExpr::Number {
                value: 0.0,
                text: "0".to_string(),
                span: None,
            });
        }
        HirExpr::Call {
            name: "createWorkshopSetting".to_string(),
            args: lowered,
            debug_source: None,
            span: Some(span.into()),
        }
    }

    pub(super) fn qualified_macro_for_member(&self, member: &str) -> Option<String> {
        let suffix = format!(".{member}");
        self.macro_declarations
            .iter()
            .filter(|(name, order)| name.ends_with(&suffix) && **order <= self.current_order)
            .map(|(name, _)| name.clone())
            .next()
    }
}

/// Whether `compressed()` accepts these elements: only numbers, or only
/// vectors of numbers.
fn is_compressible(values: &[HirExpr]) -> bool {
    let values = values
        .iter()
        .map(|value| match value {
            HirExpr::Vector { x, y, z, .. } => Some(vec![
                crate::hir::visit::literal_number(x)?,
                crate::hir::visit::literal_number(y)?,
                crate::hir::visit::literal_number(z)?,
            ]),
            _ => crate::hir::visit::literal_number(value).map(|number| vec![number]),
        })
        .collect::<Option<Vec<_>>>();
    values.as_deref().is_some_and(is_compressible_components)
}