forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathother.rs
More file actions
142 lines (135 loc) · 4.63 KB
/
other.rs
File metadata and controls
142 lines (135 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use super::*;
use num_traits::ToPrimitive;
use rustpython_compiler_core::bytecode;
impl Node for ruff::ConversionFlag {
fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
vm.ctx.new_int(self as u8).into()
}
fn ast_from_object(
vm: &VirtualMachine,
_source_code: &SourceCodeOwned,
object: PyObjectRef,
) -> PyResult<Self> {
i32::try_from_object(vm, object)?
.to_u32()
.and_then(bytecode::ConversionFlag::from_op_arg)
.map(|flag| match flag {
bytecode::ConversionFlag::None => Self::None,
bytecode::ConversionFlag::Str => Self::Str,
bytecode::ConversionFlag::Ascii => Self::Ascii,
bytecode::ConversionFlag::Repr => Self::Repr,
})
.ok_or_else(|| vm.new_value_error("invalid conversion flag".to_owned()))
}
}
// /// This is just a string, not strictly an AST node. But it makes AST conversions easier.
impl Node for ruff::name::Name {
fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
vm.ctx.new_str(self.as_str()).to_pyobject(vm)
}
fn ast_from_object(
vm: &VirtualMachine,
_source_code: &SourceCodeOwned,
object: PyObjectRef,
) -> PyResult<Self> {
match object.downcast::<PyStr>() {
Ok(name) => Ok(Self::new(name)),
Err(_) => Err(vm.new_value_error("expected str for name".to_owned())),
}
}
}
impl Node for ruff::Decorator {
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
ruff::Expr::ast_to_object(self.expression, vm, source_code)
}
fn ast_from_object(
vm: &VirtualMachine,
source_code: &SourceCodeOwned,
object: PyObjectRef,
) -> PyResult<Self> {
let expression = ruff::Expr::ast_from_object(vm, source_code, object)?;
let range = expression.range();
Ok(ruff::Decorator { expression, range })
}
}
// product
impl Node for ruff::Alias {
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
let Self {
name,
asname,
range: _range,
} = self;
let node = NodeAst
.into_ref_with_type(vm, pyast::NodeAlias::static_type().to_owned())
.unwrap();
let dict = node.as_object().dict().unwrap();
dict.set_item("name", name.ast_to_object(vm, source_code), vm)
.unwrap();
dict.set_item("asname", asname.ast_to_object(vm, source_code), vm)
.unwrap();
node_add_location(&dict, _range, vm, source_code);
node.into()
}
fn ast_from_object(
vm: &VirtualMachine,
source_code: &SourceCodeOwned,
object: PyObjectRef,
) -> PyResult<Self> {
Ok(Self {
name: Node::ast_from_object(
vm,
source_code,
get_node_field(vm, &object, "name", "alias")?,
)?,
asname: get_node_field_opt(vm, &object, "asname")?
.map(|obj| Node::ast_from_object(vm, source_code, obj))
.transpose()?,
range: range_from_object(vm, source_code, object, "alias")?,
})
}
}
// product
impl Node for ruff::WithItem {
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
let Self {
context_expr,
optional_vars,
range: _range,
} = self;
let node = NodeAst
.into_ref_with_type(vm, pyast::NodeWithItem::static_type().to_owned())
.unwrap();
let dict = node.as_object().dict().unwrap();
dict.set_item(
"context_expr",
context_expr.ast_to_object(vm, source_code),
vm,
)
.unwrap();
dict.set_item(
"optional_vars",
optional_vars.ast_to_object(vm, source_code),
vm,
)
.unwrap();
node.into()
}
fn ast_from_object(
vm: &VirtualMachine,
source_code: &SourceCodeOwned,
object: PyObjectRef,
) -> PyResult<Self> {
Ok(Self {
context_expr: Node::ast_from_object(
vm,
source_code,
get_node_field(vm, &object, "context_expr", "withitem")?,
)?,
optional_vars: get_node_field_opt(vm, &object, "optional_vars")?
.map(|obj| Node::ast_from_object(vm, source_code, obj))
.transpose()?,
range: Default::default(),
})
}
}