Skip to content

Commit 3f06a4b

Browse files
committed
impl more ast
1 parent 35f8860 commit 3f06a4b

File tree

1 file changed

+65
-38
lines changed

1 file changed

+65
-38
lines changed

crates/vm/src/stdlib/ast/pattern.rs

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,31 @@ impl Node for ruff::PatternMatchSingleton {
199199
}
200200

201201
impl Node for ruff::Singleton {
202-
fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
203-
todo!()
202+
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
203+
match self {
204+
ruff::Singleton::None => vm.ctx.none(),
205+
ruff::Singleton::True => vm.ctx.new_bool(true).into(),
206+
ruff::Singleton::False => vm.ctx.new_bool(false).into(),
207+
}
204208
}
205209

206210
fn ast_from_object(
207-
_vm: &VirtualMachine,
211+
vm: &VirtualMachine,
208212
_source_file: &SourceFile,
209-
_object: PyObjectRef,
213+
object: PyObjectRef,
210214
) -> PyResult<Self> {
211-
todo!()
215+
if vm.is_none(&object) {
216+
Ok(ruff::Singleton::None)
217+
} else if object.is(&vm.ctx.true_value) {
218+
Ok(ruff::Singleton::True)
219+
} else if object.is(&vm.ctx.false_value) {
220+
Ok(ruff::Singleton::False)
221+
} else {
222+
Err(vm.new_value_error(format!(
223+
"Expected None, True, or False, got {:?}",
224+
object.class().name()
225+
)))
226+
}
212227
}
213228
}
214229

@@ -372,57 +387,51 @@ impl Node for ruff::PatternMatchClass {
372387
}
373388
}
374389

375-
struct PatternMatchClassPatterns {
376-
pub _range: TextRange, // TODO: Use this
377-
}
390+
struct PatternMatchClassPatterns(Vec<ruff::Pattern>);
378391

379392
impl Node for PatternMatchClassPatterns {
380-
fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
381-
todo!()
393+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
394+
self.0.ast_to_object(vm, source_file)
382395
}
383396

384397
fn ast_from_object(
385-
_vm: &VirtualMachine,
386-
_source_file: &SourceFile,
387-
_object: PyObjectRef,
398+
vm: &VirtualMachine,
399+
source_file: &SourceFile,
400+
object: PyObjectRef,
388401
) -> PyResult<Self> {
389-
todo!()
402+
Ok(Self(Node::ast_from_object(vm, source_file, object)?))
390403
}
391404
}
392405

393-
struct PatternMatchClassKeywordAttributes {
394-
pub _range: TextRange, // TODO: Use this
395-
}
406+
struct PatternMatchClassKeywordAttributes(Vec<ruff::Identifier>);
396407

397408
impl Node for PatternMatchClassKeywordAttributes {
398-
fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
399-
todo!()
409+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
410+
self.0.ast_to_object(vm, source_file)
400411
}
401412

402413
fn ast_from_object(
403-
_vm: &VirtualMachine,
404-
_source_file: &SourceFile,
405-
_object: PyObjectRef,
414+
vm: &VirtualMachine,
415+
source_file: &SourceFile,
416+
object: PyObjectRef,
406417
) -> PyResult<Self> {
407-
todo!()
418+
Ok(Self(Node::ast_from_object(vm, source_file, object)?))
408419
}
409420
}
410421

411-
struct PatternMatchClassKeywordPatterns {
412-
pub _range: TextRange, // TODO: Use this
413-
}
422+
struct PatternMatchClassKeywordPatterns(Vec<ruff::Pattern>);
414423

415424
impl Node for PatternMatchClassKeywordPatterns {
416-
fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
417-
todo!()
425+
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
426+
self.0.ast_to_object(vm, source_file)
418427
}
419428

420429
fn ast_from_object(
421-
_vm: &VirtualMachine,
422-
_source_file: &SourceFile,
423-
_object: PyObjectRef,
430+
vm: &VirtualMachine,
431+
source_file: &SourceFile,
432+
object: PyObjectRef,
424433
) -> PyResult<Self> {
425-
todo!()
434+
Ok(Self(Node::ast_from_object(vm, source_file, object)?))
426435
}
427436
}
428437
// constructor
@@ -532,20 +541,38 @@ impl Node for ruff::PatternMatchOr {
532541
}
533542

534543
fn split_pattern_match_class(
535-
_arguments: ruff::PatternArguments,
544+
arguments: ruff::PatternArguments,
536545
) -> (
537546
PatternMatchClassPatterns,
538547
PatternMatchClassKeywordAttributes,
539548
PatternMatchClassKeywordPatterns,
540549
) {
541-
todo!()
550+
let patterns = PatternMatchClassPatterns(arguments.patterns);
551+
let kwd_attrs = PatternMatchClassKeywordAttributes(
552+
arguments.keywords.iter().map(|k| k.attr.clone()).collect(),
553+
);
554+
let kwd_patterns = PatternMatchClassKeywordPatterns(
555+
arguments.keywords.into_iter().map(|k| k.pattern).collect(),
556+
);
557+
(patterns, kwd_attrs, kwd_patterns)
542558
}
543559

544560
/// Merges the pattern match class attributes and patterns, opposite of [`split_pattern_match_class`].
545561
fn merge_pattern_match_class(
546-
_patterns: PatternMatchClassPatterns,
547-
_kwd_attrs: PatternMatchClassKeywordAttributes,
548-
_kwd_patterns: PatternMatchClassKeywordPatterns,
562+
patterns: PatternMatchClassPatterns,
563+
kwd_attrs: PatternMatchClassKeywordAttributes,
564+
kwd_patterns: PatternMatchClassKeywordPatterns,
549565
) -> (Vec<ruff::Pattern>, Vec<ruff::PatternKeyword>) {
550-
todo!()
566+
let keywords = kwd_attrs
567+
.0
568+
.into_iter()
569+
.zip(kwd_patterns.0)
570+
.map(|(attr, pattern)| ruff::PatternKeyword {
571+
range: Default::default(),
572+
node_index: Default::default(),
573+
attr,
574+
pattern,
575+
})
576+
.collect();
577+
(patterns.0, keywords)
551578
}

0 commit comments

Comments
 (0)