Skip to content

Commit f45b660

Browse files
committed
Fix todos by implementing missing ast conversions and deleting unused code
1 parent 86668ee commit f45b660

File tree

9 files changed

+166
-203
lines changed

9 files changed

+166
-203
lines changed

vm/src/stdlib/ast/argument.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
use super::*;
2-
impl Node for ruff::Arguments {
3-
fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
4-
todo!()
5-
}
6-
fn ast_from_object(
7-
_vm: &VirtualMachine,
8-
_source_code: &SourceCodeOwned,
9-
_object: PyObjectRef,
10-
) -> PyResult<Self> {
11-
todo!()
12-
}
13-
}
142

153
pub(super) struct PositionalArguments {
164
pub range: TextRange,

vm/src/stdlib/ast/constant.rs

Lines changed: 102 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
22
use crate::builtins::{PyComplex, PyTuple};
3-
use ruff_python_ast::ExprContext;
43

54
#[derive(Debug)]
65
pub(super) struct Constant {
@@ -83,59 +82,64 @@ pub(crate) enum ConstantLiteral {
8382
Str(String),
8483
Bytes(Vec<u8>),
8584
Int(ruff::Int),
86-
Tuple(Vec<Constant>),
85+
Tuple {
86+
value: Vec<Constant>,
87+
ctx: ruff::ExprContext,
88+
},
8789
Float(f64),
88-
Complex { real: f64, imag: f64 },
90+
Complex {
91+
real: f64,
92+
imag: f64,
93+
},
8994
Ellipsis,
9095
}
9196

9297
// constructor
9398
impl Node for Constant {
9499
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
95100
let Self { range, value } = self;
96-
let is_str = matches!(&value, ConstantLiteral::Str(_));
97-
let mut is_unicode = false;
101+
let mut string_kind = None;
102+
let mut tuple_ctx = None;
98103
let value = match value {
99104
ConstantLiteral::None => vm.ctx.none(),
100105
ConstantLiteral::Bool(value) => vm.ctx.new_bool(value).to_pyobject(vm),
101106
ConstantLiteral::Str(value) => {
102-
if !value.is_ascii() {
103-
is_unicode = true;
104-
}
107+
string_kind = Some(!value.is_ascii());
105108
vm.ctx.new_str(value).to_pyobject(vm)
106109
}
107110
ConstantLiteral::Bytes(value) => vm.ctx.new_bytes(value).to_pyobject(vm),
108111
ConstantLiteral::Int(value) => value.ast_to_object(vm, source_code),
109-
ConstantLiteral::Tuple(value) => vm
110-
.ctx
111-
.new_tuple(
112-
value
113-
.into_iter()
114-
.map(|c| c.ast_to_object(vm, source_code))
115-
.collect(),
116-
)
117-
.to_pyobject(vm),
112+
ConstantLiteral::Tuple { value, ctx } => {
113+
tuple_ctx = Some(ctx.ast_to_object(vm, source_code));
114+
let value = value
115+
.into_iter()
116+
.map(|c| c.ast_to_object(vm, source_code))
117+
.collect();
118+
vm.ctx.new_tuple(value).to_pyobject(vm)
119+
}
118120
ConstantLiteral::Float(value) => vm.ctx.new_float(value).into_pyobject(vm),
119121
ConstantLiteral::Complex { real, imag } => vm
120122
.ctx
121123
.new_complex(num_complex::Complex::new(real, imag))
122124
.into_pyobject(vm),
123125
ConstantLiteral::Ellipsis => vm.ctx.ellipsis(),
124126
};
125-
// TODO: Figure out how this works
126-
let kind = vm.ctx.new_str("u").to_pyobject(vm);
127127
let node = NodeAst
128128
.into_ref_with_type(vm, pyast::NodeExprConstant::static_type().to_owned())
129129
.unwrap();
130130
let dict = node.as_object().dict().unwrap();
131131
dict.set_item("value", value, vm).unwrap();
132-
if is_str {
133-
if is_unicode {
134-
dict.set_item("kind", kind, vm).unwrap();
132+
if let Some(is_unicode_str) = string_kind {
133+
// TODO: Figure out how this works
134+
let kind = if is_unicode_str {
135+
vm.ctx.new_str("u").to_pyobject(vm)
135136
} else {
136-
dict.set_item("kind", vm.ctx.empty_str.to_pyobject(vm), vm)
137-
.unwrap();
138-
}
137+
vm.ctx.empty_str.to_pyobject(vm)
138+
};
139+
dict.set_item("kind", kind, vm).unwrap();
140+
}
141+
if let Some(tuple_ctx) = tuple_ctx {
142+
dict.set_item("ctx", tuple_ctx, vm).unwrap();
139143
}
140144
node_add_location(&dict, range, vm, source_code);
141145
node.into()
@@ -177,7 +181,12 @@ impl Node for Constant {
177181
.cloned()
178182
.map(|object| Node::ast_from_object(vm, source_code, object))
179183
.collect::<PyResult<_>>()?;
180-
ConstantLiteral::Tuple(tuple)
184+
let ctx_object = get_node_field(vm, &object, "ctx", "Constant")?;
185+
let ctx_object = Node::ast_from_object(vm, source_code, ctx_object)?;
186+
ConstantLiteral::Tuple {
187+
value: tuple,
188+
ctx: ctx_object,
189+
}
181190
} else if cls.is(vm.ctx.types.float_type) {
182191
let float = value_object.try_into_value(vm)?;
183192
ConstantLiteral::Float(float)
@@ -247,11 +256,10 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
247256
range,
248257
value: ruff::Number::Int(value),
249258
}),
250-
ConstantLiteral::Tuple(value) => ruff::Expr::Tuple(ruff::ExprTuple {
259+
ConstantLiteral::Tuple { value, ctx } => ruff::Expr::Tuple(ruff::ExprTuple {
251260
range,
252261
elts: value.into_iter().map(constant_to_ruff_expr).collect(),
253-
// TODO
254-
ctx: ExprContext::Invalid,
262+
ctx,
255263
// TODO: Does this matter?
256264
parenthesized: true,
257265
}),
@@ -270,3 +278,68 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr {
270278
}
271279
}
272280
}
281+
282+
pub(super) fn number_literal_to_object(
283+
vm: &VirtualMachine,
284+
source_code: &SourceCodeOwned,
285+
constant: ruff::ExprNumberLiteral,
286+
) -> PyObjectRef {
287+
let ruff::ExprNumberLiteral { range, value } = constant;
288+
let c = match value {
289+
ruff::Number::Int(n) => Constant::new_int(n, range),
290+
ruff::Number::Float(n) => Constant::new_float(n, range),
291+
ruff::Number::Complex { real, imag } => Constant::new_complex(real, imag, range),
292+
};
293+
c.ast_to_object(vm, source_code)
294+
}
295+
296+
pub(super) fn string_literal_to_object(
297+
vm: &VirtualMachine,
298+
source_code: &SourceCodeOwned,
299+
constant: ruff::ExprStringLiteral,
300+
) -> PyObjectRef {
301+
let ruff::ExprStringLiteral { range, value } = constant;
302+
let c = Constant::new_str(value.to_str(), range);
303+
c.ast_to_object(vm, source_code)
304+
}
305+
306+
pub(super) fn bytes_literal_to_object(
307+
vm: &VirtualMachine,
308+
source_code: &SourceCodeOwned,
309+
constant: ruff::ExprBytesLiteral,
310+
) -> PyObjectRef {
311+
let ruff::ExprBytesLiteral { range, value } = constant;
312+
let bytes = value.as_slice().iter().flat_map(|b| b.value.iter());
313+
let c = Constant::new_bytes(bytes.copied(), range);
314+
c.ast_to_object(vm, source_code)
315+
}
316+
317+
pub(super) fn boolean_literal_to_object(
318+
vm: &VirtualMachine,
319+
source_code: &SourceCodeOwned,
320+
constant: ruff::ExprBooleanLiteral,
321+
) -> PyObjectRef {
322+
let ruff::ExprBooleanLiteral { range, value } = constant;
323+
let c = Constant::new_bool(value, range);
324+
c.ast_to_object(vm, source_code)
325+
}
326+
327+
pub(super) fn none_literal_to_object(
328+
vm: &VirtualMachine,
329+
source_code: &SourceCodeOwned,
330+
constant: ruff::ExprNoneLiteral,
331+
) -> PyObjectRef {
332+
let ruff::ExprNoneLiteral { range } = constant;
333+
let c = Constant::new_none(range);
334+
c.ast_to_object(vm, source_code)
335+
}
336+
337+
pub(super) fn ellipsis_literal_to_object(
338+
vm: &VirtualMachine,
339+
source_code: &SourceCodeOwned,
340+
constant: ruff::ExprEllipsisLiteral,
341+
) -> PyObjectRef {
342+
let ruff::ExprEllipsisLiteral { range } = constant;
343+
let c = Constant::new_ellipsis(range);
344+
c.ast_to_object(vm, source_code)
345+
}

vm/src/stdlib/ast/elif_else_clause.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
use super::*;
22

3-
impl Node for Vec<ruff::ElifElseClause> {
4-
fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
5-
todo!()
6-
}
7-
8-
fn ast_from_object(
9-
_vm: &VirtualMachine,
10-
_source_code: &SourceCodeOwned,
11-
_object: PyObjectRef,
12-
) -> PyResult<Self> {
13-
todo!()
14-
}
15-
}
16-
173
pub(super) fn ast_to_object(
184
clause: ruff::ElifElseClause,
195
mut rest: std::vec::IntoIter<ruff::ElifElseClause>,

vm/src/stdlib/ast/expression.rs

Lines changed: 22 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,25 @@ impl Node for ruff::Expr {
3030
ruff::Expr::List(cons) => cons.ast_to_object(vm, source_code),
3131
ruff::Expr::Tuple(cons) => cons.ast_to_object(vm, source_code),
3232
ruff::Expr::Slice(cons) => cons.ast_to_object(vm, source_code),
33-
ruff::Expr::NumberLiteral(cons) => cons.ast_to_object(vm, source_code),
34-
ruff::Expr::StringLiteral(cons) => cons.ast_to_object(vm, source_code),
35-
ruff::Expr::FString(cons) => cons.ast_to_object(vm, source_code),
36-
ruff::Expr::BytesLiteral(cons) => cons.ast_to_object(vm, source_code),
37-
ruff::Expr::BooleanLiteral(cons) => cons.ast_to_object(vm, source_code),
38-
ruff::Expr::NoneLiteral(cons) => cons.ast_to_object(vm, source_code),
39-
ruff::Expr::EllipsisLiteral(cons) => cons.ast_to_object(vm, source_code),
33+
ruff::Expr::NumberLiteral(cons) => {
34+
constant::number_literal_to_object(vm, source_code, cons)
35+
}
36+
ruff::Expr::StringLiteral(cons) => {
37+
constant::string_literal_to_object(vm, source_code, cons)
38+
}
39+
ruff::Expr::FString(cons) => string::fstring_to_object(vm, source_code, cons),
40+
ruff::Expr::BytesLiteral(cons) => {
41+
constant::bytes_literal_to_object(vm, source_code, cons)
42+
}
43+
ruff::Expr::BooleanLiteral(cons) => {
44+
constant::boolean_literal_to_object(vm, source_code, cons)
45+
}
46+
ruff::Expr::NoneLiteral(cons) => {
47+
constant::none_literal_to_object(vm, source_code, cons)
48+
}
49+
ruff::Expr::EllipsisLiteral(cons) => {
50+
constant::ellipsis_literal_to_object(vm, source_code, cons)
51+
}
4052
ruff::Expr::Named(cons) => cons.ast_to_object(vm, source_code),
4153
ruff::Expr::IpyEscapeCommand(_) => {
4254
unimplemented!("IPython escape command is not allowed in Python AST")
@@ -1099,7 +1111,9 @@ impl Node for ruff::ExprContext {
10991111
ruff::ExprContext::Load => pyast::NodeExprContextLoad::static_type(),
11001112
ruff::ExprContext::Store => pyast::NodeExprContextStore::static_type(),
11011113
ruff::ExprContext::Del => pyast::NodeExprContextDel::static_type(),
1102-
ruff::ExprContext::Invalid => todo!(),
1114+
ruff::ExprContext::Invalid => {
1115+
unimplemented!("Invalid expression context is not allowed in Python AST")
1116+
}
11031117
};
11041118
NodeAst
11051119
.into_ref_with_type(vm, node_type.to_owned())
@@ -1181,88 +1195,3 @@ impl Node for ruff::Comprehension {
11811195
})
11821196
}
11831197
}
1184-
1185-
impl Node for ruff::ExprBytesLiteral {
1186-
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
1187-
let Self { range, value } = self;
1188-
let bytes = value.as_slice().iter().flat_map(|b| b.value.iter());
1189-
let c = Constant::new_bytes(bytes.copied(), range);
1190-
c.ast_to_object(vm, source_code)
1191-
}
1192-
1193-
fn ast_from_object(
1194-
_vm: &VirtualMachine,
1195-
_source_code: &SourceCodeOwned,
1196-
_object: PyObjectRef,
1197-
) -> PyResult<Self> {
1198-
todo!()
1199-
}
1200-
}
1201-
1202-
impl Node for ruff::ExprBooleanLiteral {
1203-
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
1204-
let Self { range, value } = self;
1205-
let c = Constant::new_bool(value, range);
1206-
c.ast_to_object(vm, source_code)
1207-
}
1208-
1209-
fn ast_from_object(
1210-
_vm: &VirtualMachine,
1211-
_source_code: &SourceCodeOwned,
1212-
_object: PyObjectRef,
1213-
) -> PyResult<Self> {
1214-
todo!()
1215-
}
1216-
}
1217-
1218-
impl Node for ruff::ExprNoneLiteral {
1219-
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
1220-
let Self { range } = self;
1221-
let c = Constant::new_none(range);
1222-
c.ast_to_object(vm, source_code)
1223-
}
1224-
1225-
fn ast_from_object(
1226-
_vm: &VirtualMachine,
1227-
_source_code: &SourceCodeOwned,
1228-
_object: PyObjectRef,
1229-
) -> PyResult<Self> {
1230-
todo!()
1231-
}
1232-
}
1233-
1234-
impl Node for ruff::ExprEllipsisLiteral {
1235-
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
1236-
let Self { range } = self;
1237-
let c = Constant::new_ellipsis(range);
1238-
c.ast_to_object(vm, source_code)
1239-
}
1240-
1241-
fn ast_from_object(
1242-
_vm: &VirtualMachine,
1243-
_source_code: &SourceCodeOwned,
1244-
_object: PyObjectRef,
1245-
) -> PyResult<Self> {
1246-
todo!()
1247-
}
1248-
}
1249-
1250-
impl Node for ruff::ExprNumberLiteral {
1251-
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
1252-
let Self { range, value } = self;
1253-
let c = match value {
1254-
ruff::Number::Int(n) => Constant::new_int(n, range),
1255-
ruff::Number::Float(n) => Constant::new_float(n, range),
1256-
ruff::Number::Complex { real, imag } => Constant::new_complex(real, imag, range),
1257-
};
1258-
c.ast_to_object(vm, source_code)
1259-
}
1260-
1261-
fn ast_from_object(
1262-
_vm: &VirtualMachine,
1263-
_source_code: &SourceCodeOwned,
1264-
_object: PyObjectRef,
1265-
) -> PyResult<Self> {
1266-
todo!()
1267-
}
1268-
}

vm/src/stdlib/ast/other.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ impl Node for ruff::ConversionFlag {
3333
// }
3434

3535
impl Node for ruff::Decorator {
36-
fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
37-
todo!()
36+
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
37+
ruff::Expr::ast_to_object(self.expression, vm, source_code)
3838
}
3939

4040
fn ast_from_object(
41-
_vm: &VirtualMachine,
42-
_source_code: &SourceCodeOwned,
43-
_object: PyObjectRef,
41+
vm: &VirtualMachine,
42+
source_code: &SourceCodeOwned,
43+
object: PyObjectRef,
4444
) -> PyResult<Self> {
45-
todo!()
45+
let expression = ruff::Expr::ast_from_object(vm, source_code, object)?;
46+
let range = expression.range();
47+
Ok(ruff::Decorator { expression, range })
4648
}
4749
}
4850

0 commit comments

Comments
 (0)