From a66902406ffc1a265322f004138edc36dbd8894b Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 22 Aug 2022 06:09:18 +0900 Subject: [PATCH] Refactor Mode and partial parser/codegen for eval/exec --- bytecode/src/lib.rs | 3 +++ bytecode/src/mode.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 bytecode/src/mode.rs diff --git a/bytecode/src/lib.rs b/bytecode/src/lib.rs index f4f3f30..c53697b 100644 --- a/bytecode/src/lib.rs +++ b/bytecode/src/lib.rs @@ -4,6 +4,9 @@ #![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")] #![doc(html_root_url = "https://docs.rs/rustpython-bytecode/")] +mod mode; +pub use mode::Mode; + use bitflags::bitflags; use bstr::ByteSlice; use itertools::Itertools; diff --git a/bytecode/src/mode.rs b/bytecode/src/mode.rs new file mode 100644 index 0000000..b56f226 --- /dev/null +++ b/bytecode/src/mode.rs @@ -0,0 +1,32 @@ +#[derive(Clone, Copy)] +pub enum Mode { + Exec, + Eval, + Single, + BlockExpr, +} + +impl std::str::FromStr for Mode { + type Err = ModeParseError; + + // To support `builtins.compile()` `mode` argument + fn from_str(s: &str) -> Result { + match s { + "exec" => Ok(Mode::Exec), + "eval" => Ok(Mode::Eval), + "single" => Ok(Mode::Single), + _ => Err(ModeParseError { _priv: () }), + } + } +} + +#[derive(Debug)] +pub struct ModeParseError { + _priv: (), +} + +impl std::fmt::Display for ModeParseError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, r#"mode should be "exec", "eval", or "single""#) + } +}