onion-frontend 0.3.4

Compilation frontend for the Onion programming language - lexer, parser, and IR generator
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
590
591
592
593
594
595
596
597
//! 编译时模块:提供 AST 与 Onion VM 对象间的双向转换和集成。
//!
//! 本模块实现了编译时 AST 节点的 VM 对象包装、绑定、以及相互转换机制。
//! 主要类型 `OnionASTObject` 实现了 `OnionObjectExt` trait,使 AST 能在 VM 中以对象形式操作。
//! 支持 AST <-> VM 对象的双向转换、序列化、运算符重载等。
//!
//! # 主要内容
//! - `OnionASTObject`:AST 的 VM 对象包装器
//! - AST 与 VM 基础类型(String/Boolean/Number/Tuple/Pair 等)的互转
//! - 运算符重载(如 `<<` 用于替换子节点)
//! - 子模块:`ast_bindings`(AST 构造绑定)、`native`(原生函数)、`solver`(求解器)
//!
//! # 用法示例
//! ```ignore
//! let ast_obj = OnionASTObject::new(ast_node);
//! let vm_obj = OnionObject::Custom(Arc::new(ast_obj));
//! let converted_back = OnionASTObject::from_onion(&vm_obj)?;
//! ```

pub mod ast_bindings;
pub mod native;
pub mod solver;
use std::sync::Arc;

use onion_vm::{
    GCTraceable,
    lambda::runnable::RuntimeError,
    types::{
        object::{OnionObject, OnionObjectCell, OnionObjectExt, OnionStaticObject},
        tuple::OnionTuple,
    },
};

use crate::parser::ast::{ASTNode, ASTNodeType};
use base64::engine::Engine;

/// AST 节点的 VM 对象包装器。
///
/// 将 AST 节点包装为 Onion VM 可操作的自定义对象,实现 VM 对象接口,
/// 支持相等性比较、序列化、运算符重载、类型查询等。
#[derive(Debug, Clone)]
pub struct OnionASTObject {
    ast: ASTNode,
}

impl GCTraceable<OnionObjectCell> for OnionASTObject {
    fn collect(&self, _: &mut std::collections::VecDeque<onion_vm::GCArcWeak<OnionObjectCell>>) {}
}

impl OnionObjectExt for OnionASTObject {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn upgrade(&self, _: &mut Vec<onion_vm::GCArc<OnionObjectCell>>) {}

    fn equals(&self, other: &OnionObject) -> Result<bool, RuntimeError> {
        other.with_data(|data| match data {
            OnionObject::Custom(data) => {
                if let Some(ast_object) = data.as_any().downcast_ref::<OnionASTObject>() {
                    Ok(self.ast == ast_object.ast)
                } else {
                    Ok(false)
                }
            }
            _ => Ok(false),
        })
    }

    fn key_of(&self) -> Result<OnionStaticObject, RuntimeError> {
        // 获取一个移除了子节点的 AST 节点
        let key_ast = ASTNode {
            node_type: self.ast.node_type.clone(),
            children: vec![],
            source_location: self.ast.source_location.clone(),
        };
        Ok(OnionObject::Custom(Arc::new(OnionASTObject { ast: key_ast })).stabilize())
    }

    fn value_of(&self) -> Result<OnionStaticObject, RuntimeError> {
        // 获取 AST 的子节点列表
        let children = self
            .ast
            .children
            .iter()
            .map(|child| OnionObject::Custom(Arc::new(OnionASTObject { ast: child.clone() })))
            .collect::<Vec<_>>();
        Ok(OnionObject::Tuple(OnionTuple::new(children).into()).stabilize())
    }

    fn len(&self) -> Result<OnionStaticObject, RuntimeError> {
        Ok(OnionObject::Integer(self.ast.children.len() as i64).stabilize())
    }

    fn apply(&self, value: &OnionObject) -> Result<OnionStaticObject, RuntimeError> {
        value.with_data(|data| match data {
            OnionObject::Integer(i) => {
                // 通过索引访问子节点
                let index = if *i < 0 {
                    return Err(RuntimeError::InvalidOperation(
                        "Negative index is not allowed".into(),
                    ));
                } else {
                    *i as usize
                };

                if index >= self.ast.children.len() {
                    return Err(RuntimeError::InvalidOperation(
                        format!(
                            "Index {} out of bounds for AST children of length {}",
                            index,
                            self.ast.children.len()
                        )
                        .into(),
                    ));
                }

                let child_ast = self.ast.children[index].clone();
                Ok(OnionObject::Custom(Arc::new(OnionASTObject { ast: child_ast })).stabilize())
            }
            OnionObject::Pair(pair) => {
                // 通过 Pair 替换指定索引的子节点
                let index = pair.get_key().with_data(|key_data| match key_data {
                    OnionObject::Integer(i) => {
                        if *i < 0 {
                            Err(RuntimeError::InvalidOperation(
                                "Negative index is not allowed".into(),
                            ))
                        } else {
                            Ok(*i as usize)
                        }
                    }
                    _ => Err(RuntimeError::InvalidType(
                        "Pair key must be an integer index".into()
                    )),
                })?;

                if index >= self.ast.children.len() {
                    return Err(RuntimeError::InvalidOperation(
                        format!(
                            "Index {} out of bounds for AST children of length {}",
                            index,
                            self.ast.children.len()
                        )
                        .into(),
                    ));
                }

                // 将 Pair 的值转换为 AST 节点
                let new_child_ast = OnionASTObject::from_onion(pair.get_value())?;

                // 创建新的子节点列表,替换指定索引的节点
                let mut new_children = self.ast.children.clone();
                new_children[index] = new_child_ast;

                // 创建新的 AST 对象
                let new_ast = ASTNode {
                    node_type: self.ast.node_type.clone(),
                    source_location: self.ast.source_location.clone(),
                    children: new_children,
                };

                Ok(OnionObject::Custom(Arc::new(OnionASTObject { ast: new_ast })).stabilize())
            }
            _ => Err(RuntimeError::InvalidType(
                "Apply argument must be an integer (for access) or a pair (for replacement)".into()
            )),
        })
    }

    fn with_attribute(
            &self,
            key: &OnionObject,
            f: &mut dyn FnMut(&OnionObject) -> Result<(), RuntimeError>,
        ) -> Result<(), RuntimeError> {
        key.with_data(|key_data| match key_data {
            OnionObject::String(attr_name) => {
                match attr_name.as_ref() {
                    "node_type" => {
                        // 返回节点类型的字符串表示
                        let type_name = match &self.ast.node_type {
                            ASTNodeType::Null => "Null",
                            ASTNodeType::Undefined => "Undefined",
                            ASTNodeType::String(_) => "String",
                            ASTNodeType::Boolean(_) => "Boolean",
                            ASTNodeType::Number(_) => "Number",
                            ASTNodeType::Base64(_) => "Base64",
                            ASTNodeType::Variable(_) => "Variable",
                            ASTNodeType::Required(_) => "Required",
                            ASTNodeType::Let(_) => "Let",
                            ASTNodeType::Frame => "Frame",
                            ASTNodeType::Assign => "Assign",
                            ASTNodeType::LambdaDef(_, _) => "LambdaDef",
                            ASTNodeType::Expressions => "Expressions",
                            ASTNodeType::Apply => "Apply",
                            ASTNodeType::Operation(_) => "Operation",
                            ASTNodeType::Tuple => "Tuple",
                            ASTNodeType::AssumeTuple => "AssumeTuple",
                            ASTNodeType::Pair => "Pair",
                            ASTNodeType::GetAttr => "GetAttr",
                            ASTNodeType::Return => "Return",
                            ASTNodeType::If => "If",
                            ASTNodeType::While => "While",
                            ASTNodeType::Modifier(_) => "Modifier",
                            ASTNodeType::Break => "Break",
                            ASTNodeType::Continue => "Continue",
                            ASTNodeType::Range => "Range",
                            ASTNodeType::In => "In",
                            ASTNodeType::Namespace(_) => "Namespace",
                            ASTNodeType::LazySet => "Set",
                            ASTNodeType::Map => "Map",
                            ASTNodeType::Is => "Is",
                            ASTNodeType::Raise => "Raise",
                            ASTNodeType::Dynamic => "Dynamic",
                            ASTNodeType::Static => "Static",
                            ASTNodeType::Comptime => "Comptime",
                        };
                        let type_obj = OnionObject::String(type_name.into());
                        f(&type_obj)
                    },
                    "has_data" => {
                        // 返回是否携带数据
                        let has_data = matches!(
                            &self.ast.node_type,
                            ASTNodeType::String(_) | ASTNodeType::Boolean(_) | ASTNodeType::Number(_) |
                            ASTNodeType::Base64(_) | ASTNodeType::Variable(_) | ASTNodeType::Required(_) |
                            ASTNodeType::Let(_) | ASTNodeType::LambdaDef(_, _) | ASTNodeType::Operation(_) |
                            ASTNodeType::Modifier(_) | ASTNodeType::Namespace(_)
                        );
                        let has_data_obj = OnionObject::Boolean(has_data);
                        f(&has_data_obj)
                    },
                    "data" => {
                        // 返回节点类型携带的原始数据
                        match &self.ast.node_type {
                            ASTNodeType::String(s) => {
                                let data_obj = OnionObject::String(s.clone().into());
                                f(&data_obj)
                            },
                            ASTNodeType::Boolean(b) => {
                                let data_obj = OnionObject::Boolean(*b);
                                f(&data_obj)
                            },
                            ASTNodeType::Number(n) => {
                                let data_obj = OnionObject::String(n.clone().into());
                                f(&data_obj)
                            },
                            ASTNodeType::Base64(b64) => {
                                let data_obj = OnionObject::String(b64.clone().into());
                                f(&data_obj)
                            },
                            ASTNodeType::Variable(name) | ASTNodeType::Required(name) | 
                            ASTNodeType::Let(name) | ASTNodeType::Namespace(name) => {
                                let data_obj = OnionObject::String(name.clone().into());
                                f(&data_obj)
                            },
                            ASTNodeType::LambdaDef(is_dyn, captures) => {
                                // 返回一个包含 is_dyn 和 captures 的元组
                                let captures_vec: Vec<OnionObject> = captures.iter()
                                    .map(|s| OnionObject::String(s.clone().into()))
                                    .collect();
                                let captures_tuple = OnionObject::Tuple(OnionTuple::new(captures_vec).into());
                                let data_tuple = OnionObject::Tuple(OnionTuple::new(vec![
                                    OnionObject::Boolean(*is_dyn),
                                    captures_tuple
                                ]).into());
                                f(&data_tuple)
                            },
                            ASTNodeType::Operation(op) => {
                                let op_str = match op {
                                    crate::parser::ast::ASTNodeOperation::Add => "+",
                                    crate::parser::ast::ASTNodeOperation::Abs => "abs",
                                    crate::parser::ast::ASTNodeOperation::Subtract => "-",
                                    crate::parser::ast::ASTNodeOperation::Minus => "minus",
                                    crate::parser::ast::ASTNodeOperation::Multiply => "*",
                                    crate::parser::ast::ASTNodeOperation::Divide => "/",
                                    crate::parser::ast::ASTNodeOperation::Modulus => "%",
                                    crate::parser::ast::ASTNodeOperation::Power => "**",
                                    crate::parser::ast::ASTNodeOperation::And => "and",
                                    crate::parser::ast::ASTNodeOperation::Xor => "xor",
                                    crate::parser::ast::ASTNodeOperation::Or => "or",
                                    crate::parser::ast::ASTNodeOperation::Not => "not",
                                    crate::parser::ast::ASTNodeOperation::Equal => "==",
                                    crate::parser::ast::ASTNodeOperation::NotEqual => "!=",
                                    crate::parser::ast::ASTNodeOperation::Greater => ">",
                                    crate::parser::ast::ASTNodeOperation::Less => "<",
                                    crate::parser::ast::ASTNodeOperation::GreaterEqual => ">=",
                                    crate::parser::ast::ASTNodeOperation::LessEqual => "<=",
                                    crate::parser::ast::ASTNodeOperation::LeftShift => "<<",
                                    crate::parser::ast::ASTNodeOperation::RightShift => ">>",
                                };
                                let data_obj = OnionObject::String(op_str.into());
                                f(&data_obj)
                            },
                            ASTNodeType::Modifier(mod_type) => {
                                let mod_str = match mod_type {
                                    crate::parser::ast::ASTNodeModifier::Mut => "mut",
                                    crate::parser::ast::ASTNodeModifier::Const => "const",
                                    crate::parser::ast::ASTNodeModifier::KeyOf => "keyof",
                                    crate::parser::ast::ASTNodeModifier::ValueOf => "valueof",
                                    crate::parser::ast::ASTNodeModifier::Assert => "assert",
                                    crate::parser::ast::ASTNodeModifier::Import => "import",
                                    crate::parser::ast::ASTNodeModifier::TypeOf => "typeof",
                                    crate::parser::ast::ASTNodeModifier::LengthOf => "lengthof",
                                    crate::parser::ast::ASTNodeModifier::Launch => "launch",
                                    crate::parser::ast::ASTNodeModifier::Spawn => "spawn",
                                    crate::parser::ast::ASTNodeModifier::Async => "async",
                                    crate::parser::ast::ASTNodeModifier::Sync => "sync",
                                    crate::parser::ast::ASTNodeModifier::Atomic => "atomic",
                                };
                                let data_obj = OnionObject::String(mod_str.into());
                                f(&data_obj)
                            },
                            _ => {
                                // 对于没有数据的节点类型,返回 null
                                let null_obj = OnionObject::Null;
                                f(&null_obj)
                            }
                        }
                    },
                    // 直接通过数据字段名访问
                    "value" => {
                        match &self.ast.node_type {
                            ASTNodeType::String(s) | ASTNodeType::Number(s) | ASTNodeType::Base64(s) => {
                                let value_obj = OnionObject::String(s.clone().into());
                                f(&value_obj)
                            },
                            ASTNodeType::Boolean(b) => {
                                let value_obj = OnionObject::Boolean(*b);
                                f(&value_obj)
                            },
                            _ => Err(RuntimeError::InvalidOperation(
                                "Attribute 'value' is only supported for String, Number, Base64, and Boolean node types".into()
                            ))
                        }
                    },
                    "name" => {
                        match &self.ast.node_type {
                            ASTNodeType::Variable(name) | ASTNodeType::Required(name) | 
                            ASTNodeType::Let(name) | ASTNodeType::Namespace(name) => {
                                let name_obj = OnionObject::String(name.clone().into());
                                f(&name_obj)
                            },
                            _ => Err(RuntimeError::InvalidOperation(
                                "Attribute 'name' is only supported for Variable, Required, Let, and Namespace node types".into()
                            ))
                        }
                    },
                    "op" => {
                        match &self.ast.node_type {
                            ASTNodeType::Operation(op) => {
                                let op_str = match op {
                                    crate::parser::ast::ASTNodeOperation::Add => "+",
                                    crate::parser::ast::ASTNodeOperation::Abs => "abs",
                                    crate::parser::ast::ASTNodeOperation::Subtract => "-",
                                    crate::parser::ast::ASTNodeOperation::Minus => "minus",
                                    crate::parser::ast::ASTNodeOperation::Multiply => "*",
                                    crate::parser::ast::ASTNodeOperation::Divide => "/",
                                    crate::parser::ast::ASTNodeOperation::Modulus => "%",
                                    crate::parser::ast::ASTNodeOperation::Power => "**",
                                    crate::parser::ast::ASTNodeOperation::And => "and",
                                    crate::parser::ast::ASTNodeOperation::Xor => "xor",
                                    crate::parser::ast::ASTNodeOperation::Or => "or",
                                    crate::parser::ast::ASTNodeOperation::Not => "not",
                                    crate::parser::ast::ASTNodeOperation::Equal => "==",
                                    crate::parser::ast::ASTNodeOperation::NotEqual => "!=",
                                    crate::parser::ast::ASTNodeOperation::Greater => ">",
                                    crate::parser::ast::ASTNodeOperation::Less => "<",
                                    crate::parser::ast::ASTNodeOperation::GreaterEqual => ">=",
                                    crate::parser::ast::ASTNodeOperation::LessEqual => "<=",
                                    crate::parser::ast::ASTNodeOperation::LeftShift => "<<",
                                    crate::parser::ast::ASTNodeOperation::RightShift => ">>",
                                };
                                let op_obj = OnionObject::String(op_str.into());
                                f(&op_obj)
                            },
                            _ => Err(RuntimeError::InvalidOperation(
                                "Attribute 'op' is only supported for Operation node type".into()
                            ))
                        }
                    },
                    "modifier" => {
                        match &self.ast.node_type {
                            ASTNodeType::Modifier(mod_type) => {
                                let mod_str = match mod_type {
                                    crate::parser::ast::ASTNodeModifier::Mut => "mut",
                                    crate::parser::ast::ASTNodeModifier::Const => "const",
                                    crate::parser::ast::ASTNodeModifier::KeyOf => "keyof",
                                    crate::parser::ast::ASTNodeModifier::ValueOf => "valueof",
                                    crate::parser::ast::ASTNodeModifier::Assert => "assert",
                                    crate::parser::ast::ASTNodeModifier::Import => "import",
                                    crate::parser::ast::ASTNodeModifier::TypeOf => "typeof",
                                    crate::parser::ast::ASTNodeModifier::LengthOf => "lengthof",
                                    crate::parser::ast::ASTNodeModifier::Launch => "launch",
                                    crate::parser::ast::ASTNodeModifier::Spawn => "spawn",
                                    crate::parser::ast::ASTNodeModifier::Async => "async",
                                    crate::parser::ast::ASTNodeModifier::Sync => "sync",
                                    crate::parser::ast::ASTNodeModifier::Atomic => "atomic",
                                };
                                let mod_obj = OnionObject::String(mod_str.into());
                                f(&mod_obj)
                            },
                            _ => Err(RuntimeError::InvalidOperation(
                                "Attribute 'modifier' is only supported for Modifier node type".into()
                            ))
                        }
                    },
                    "is_dyn" | "dyn" => {
                        match &self.ast.node_type {
                            ASTNodeType::LambdaDef(is_dyn, _) => {
                                let dyn_obj = OnionObject::Boolean(*is_dyn);
                                f(&dyn_obj)
                            },
                            _ => Err(RuntimeError::InvalidOperation(
                                "Attribute 'is_dyn'/'dyn' is only supported for LambdaDef node type".into()
                            ))
                        }
                    },
                    "captures" => {
                        match &self.ast.node_type {
                            ASTNodeType::LambdaDef(_, captures) => {
                                let captures_vec: Vec<OnionObject> = captures.iter()
                                    .map(|s| OnionObject::String(s.clone().into()))
                                    .collect();
                                let captures_obj = OnionObject::Tuple(OnionTuple::new(captures_vec).into());
                                f(&captures_obj)
                            },
                            _ => Err(RuntimeError::InvalidOperation(
                                "Attribute 'captures' is only supported for LambdaDef node type".into()
                            ))
                        }
                    },
                    _ => Err(RuntimeError::InvalidOperation(
                        format!("Unknown attribute '{}'. Available attributes: node_type, has_data, data, value (for String/Number/Base64/Boolean), name (for Variable/Required/Let/Namespace), op (for Operation), modifier (for Modifier), is_dyn/dyn (for LambdaDef), captures (for LambdaDef)", attr_name).into()
                    ))
                }
            },
            _ => Err(RuntimeError::InvalidType(
                "Attribute key must be a string".into()
            ))
        })
    }

    fn type_of(&self) -> Result<String, RuntimeError> {
        Ok("OnionASTObject".to_string())
    }

    fn to_string(&self, _ptrs: &Vec<*const OnionObject>) -> Result<String, RuntimeError> {
        Ok(format!("OnionASTObject: {:?}", self.ast))
    }
    fn repr(&self, _ptrs: &Vec<*const OnionObject>) -> Result<String, RuntimeError> {
        Ok(format!("OnionASTObject: {:?}", self.ast))
    }

    fn binary_shl(&self, other: &OnionObject) -> Result<OnionStaticObject, RuntimeError> {
        // self << tuple 用来将自身的元组替换成另一个
        other.with_data(|data| match data {
            OnionObject::Tuple(tuple) => {
                let mut children = vec![];
                for elem in tuple.get_elements() {
                    children.push(OnionASTObject::from_onion(elem)?);
                }
                let new_ast = ASTNode {
                    node_type: self.ast.node_type.clone(),
                    source_location: self.ast.source_location.clone(),
                    children,
                };
                Ok(OnionObject::Custom(Arc::new(OnionASTObject { ast: new_ast })).stabilize())
            }
            _ => Err(RuntimeError::InvalidType(
                "Expected a tuple for binary shift left".into(),
            )),
        })
    }
}

impl OnionASTObject {
    /// 创建新的 AST 对象包装器。
    pub fn new(ast: ASTNode) -> Self {
        Self { ast }
    }

    /// 将 Onion VM 对象转换为 AST 节点。
    ///
    /// # 参数
    /// - `object`:VM 对象引用。
    ///
    /// # 返回
    /// 转换后的 AST 节点,或运行时错误。
    ///
    /// # 支持的类型
    /// - Custom(OnionASTObject):直接提取 AST
    /// - Boolean/String/Bytes/Float/Integer/Null/Undefined:转为对应字面量节点
    /// - Tuple:转为 Tuple 节点(递归转换子元素)
    /// - Range:转为 Range 节点
    /// - Pair:转为 Pair 节点
    pub fn from_onion(object: &OnionObject) -> Result<ASTNode, RuntimeError> {
        match object {
            OnionObject::Custom(ast_object) => {
                if let Some(ast_object) = ast_object.as_any().downcast_ref::<OnionASTObject>() {
                    Ok(ast_object.ast.clone())
                } else {
                    Err(RuntimeError::InvalidType(
                        format!(
                            "Unsupported OnionObject type for AST conversion: {:?}",
                            object
                        )
                        .into(),
                    ))
                }
            }
            OnionObject::Mut(_) => Err(RuntimeError::InvalidOperation(
                ("Mutable objects may introduce cyclic references, ".to_owned()
                    + "which cannot be safely or deterministically converted to AST objects")
                    .into(),
            )),
            OnionObject::Boolean(v) => Ok(ASTNode {
                node_type: ASTNodeType::Boolean(*v),
                source_location: None,
                children: vec![],
            }),
            OnionObject::String(s) => Ok(ASTNode {
                node_type: ASTNodeType::String(s.as_ref().into()),
                source_location: None,
                children: vec![],
            }),
            OnionObject::Bytes(b) => {
                let b64 = base64::engine::general_purpose::STANDARD.encode(b);
                Ok(ASTNode {
                    node_type: ASTNodeType::Base64(b64),
                    source_location: None,
                    children: vec![],
                })
            }
            OnionObject::Float(f) => Ok(ASTNode {
                node_type: ASTNodeType::Number(f.to_string()),
                source_location: None,
                children: vec![],
            }),
            OnionObject::Integer(i) => Ok(ASTNode {
                node_type: ASTNodeType::Number(i.to_string()),
                source_location: None,
                children: vec![],
            }),
            OnionObject::Null => Ok(ASTNode {
                node_type: ASTNodeType::Null,
                source_location: None,
                children: vec![],
            }),
            OnionObject::Undefined(_) => Ok(ASTNode {
                node_type: ASTNodeType::Undefined,
                source_location: None,
                children: vec![],
            }),
            OnionObject::Tuple(tuple) => {
                let children = tuple
                    .get_elements()
                    .iter()
                    .map(|elem| OnionASTObject::from_onion(elem))
                    .collect::<Result<Vec<_>, _>>()?;
                Ok(ASTNode {
                    node_type: ASTNodeType::Tuple,
                    source_location: None,
                    children,
                })
            }
            OnionObject::Range(start, end) => Ok(ASTNode {
                node_type: ASTNodeType::Range,
                source_location: None,
                children: vec![
                    ASTNode {
                        node_type: ASTNodeType::Number(start.to_string()),
                        source_location: None,
                        children: vec![],
                    },
                    ASTNode {
                        node_type: ASTNodeType::Number(end.to_string()),
                        source_location: None,
                        children: vec![],
                    },
                ],
            }),
            OnionObject::Pair(pair) => {
                let left = OnionASTObject::from_onion(pair.get_key())?;
                let right = OnionASTObject::from_onion(pair.get_value())?;
                Ok(ASTNode {
                    node_type: ASTNodeType::Pair,
                    source_location: None,
                    children: vec![left, right],
                })
            }
            v => Err(RuntimeError::InvalidType(
                format!("Unsupported OnionObject type for AST conversion: {:?}", v).into(),
            )),
        }
    }
}