Skip to content

Commit a669024

Browse files
committed
Refactor Mode and partial parser/codegen for eval/exec
1 parent 53c48bf commit a669024

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎bytecode/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
55
#![doc(html_root_url = "https://docs.rs/rustpython-bytecode/")]
66

7+
mod mode;
8+
pub use mode::Mode;
9+
710
use bitflags::bitflags;
811
use bstr::ByteSlice;
912
use itertools::Itertools;

‎bytecode/src/mode.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[derive(Clone, Copy)]
2+
pub enum Mode {
3+
Exec,
4+
Eval,
5+
Single,
6+
BlockExpr,
7+
}
8+
9+
impl std::str::FromStr for Mode {
10+
type Err = ModeParseError;
11+
12+
// To support `builtins.compile()` `mode` argument
13+
fn from_str(s: &str) -> Result<Self, ModeParseError> {
14+
match s {
15+
"exec" => Ok(Mode::Exec),
16+
"eval" => Ok(Mode::Eval),
17+
"single" => Ok(Mode::Single),
18+
_ => Err(ModeParseError { _priv: () }),
19+
}
20+
}
21+
}
22+
23+
#[derive(Debug)]
24+
pub struct ModeParseError {
25+
_priv: (),
26+
}
27+
28+
impl std::fmt::Display for ModeParseError {
29+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30+
write!(f, r#"mode should be "exec", "eval", or "single""#)
31+
}
32+
}

0 commit comments

Comments
 (0)