diff --git a/Cargo.lock b/Cargo.lock index 04729d30a..55872f1d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2324,7 +2324,6 @@ dependencies = [ "num-traits", "ruff_python_ast", "ruff_python_codegen", - "ruff_python_parser", "ruff_source_file", "ruff_text_size", "rustpython-compiler-core", @@ -2386,8 +2385,6 @@ dependencies = [ "lz4_flex", "malachite-bigint", "num-complex", - "ruff_python_ast", - "ruff_python_parser", "ruff_source_file", "rustpython-wtf8", "serde", diff --git a/compiler/codegen/Cargo.toml b/compiler/codegen/Cargo.toml index 2ee927709..f54d6476a 100644 --- a/compiler/codegen/Cargo.toml +++ b/compiler/codegen/Cargo.toml @@ -15,7 +15,6 @@ license.workspace = true rustpython-compiler-core = { workspace = true } rustpython-compiler-source = {workspace = true } rustpython-wtf8 = { workspace = true } -ruff_python_parser = { workspace = true } ruff_python_ast = { workspace = true } ruff_text_size = { workspace = true } ruff_source_file = { workspace = true } diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 6140d354a..03eff74ba 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -3458,12 +3458,13 @@ impl Compiler<'_> { self.compile_expression(&fstring_expr.expression)?; - emit!( - self, - Instruction::FormatValue { - conversion: conversion - } - ); + let conversion = match conversion { + ConversionFlag::None => bytecode::ConversionFlag::None, + ConversionFlag::Str => bytecode::ConversionFlag::Str, + ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii, + ConversionFlag::Repr => bytecode::ConversionFlag::Repr, + }; + emit!(self, Instruction::FormatValue { conversion }); // concatenate formatted string and debug text (if present) if debug_text_count > 0 { diff --git a/compiler/core/Cargo.toml b/compiler/core/Cargo.toml index e79d93768..837f9e486 100644 --- a/compiler/core/Cargo.toml +++ b/compiler/core/Cargo.toml @@ -10,8 +10,6 @@ license.workspace = true [dependencies] # rustpython-parser-core = { workspace = true, features=["location"] } -ruff_python_ast = { workspace = true } -ruff_python_parser = { workspace = true } ruff_source_file = { workspace = true } rustpython-wtf8 = { workspace = true } diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs index 2745a5e9f..2e8ff2901 100644 --- a/compiler/core/src/bytecode.rs +++ b/compiler/core/src/bytecode.rs @@ -5,14 +5,24 @@ use bitflags::bitflags; use itertools::Itertools; use malachite_bigint::BigInt; use num_complex::Complex64; -pub use ruff_python_ast::ConversionFlag; -// use rustpython_parser_core::source_code::{OneIndexed, SourceLocation}; use ruff_source_file::{OneIndexed, SourceLocation}; use rustpython_wtf8::{Wtf8, Wtf8Buf}; use std::marker::PhantomData; use std::{collections::BTreeSet, fmt, hash, mem}; -// pub use rustpython_parser_core::ConversionFlag; +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +#[repr(i8)] +#[allow(clippy::cast_possible_wrap)] +pub enum ConversionFlag { + /// No conversion + None = -1, // CPython uses -1 + /// Converts by calling `str()`. + Str = b's' as i8, + /// Converts by calling `ascii()`. + Ascii = b'a' as i8, + /// Converts by calling `repr()`. + Repr = b'r' as i8, +} pub trait Constant: Sized { type Name: AsRef; diff --git a/compiler/core/src/mode.rs b/compiler/core/src/mode.rs index 13cea42b1..0c14c3c49 100644 --- a/compiler/core/src/mode.rs +++ b/compiler/core/src/mode.rs @@ -1,10 +1,9 @@ -pub use ruff_python_parser::ModeParseError; - #[derive(Clone, Copy)] pub enum Mode { Exec, Eval, Single, + /// Returns the value of the last statement in the statement list. BlockExpr, } @@ -22,14 +21,12 @@ impl std::str::FromStr for Mode { } } -impl From for ruff_python_parser::Mode { - fn from(mode: Mode) -> Self { - match mode { - Mode::Exec => Self::Module, - Mode::Eval => Self::Expression, - // TODO: Improve ruff API - // ruff does not have an interactive mode - Mode::Single | Mode::BlockExpr => Self::Ipython, - } +/// Returned when a given mode is not valid. +#[derive(Debug)] +pub struct ModeParseError; + +impl std::fmt::Display for ModeParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, r#"mode must be "exec", "eval", or "single""#) } } diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 0ebfbbf41..390a2d566 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -119,7 +119,14 @@ fn _compile( mode: Mode, opts: CompileOpts, ) -> Result { - let parsed = parser::parse(source_code.text, parser::Mode::from(mode).into()) + let parser_mode = match mode { + Mode::Exec => parser::Mode::Module, + Mode::Eval => parser::Mode::Expression, + // ruff does not have an interactive mode, which is fine, + // since these are only different in terms of compilation + Mode::Single | Mode::BlockExpr => parser::Mode::Module, + }; + let parsed = parser::parse(source_code.text, parser_mode.into()) .map_err(|err| CompileError::from_ruff_parse_error(err, &source_code))?; let ast = parsed.into_syntax(); compile::compile_top(ast, source_code, mode, opts).map_err(|e| e.into()) diff --git a/vm/src/stdlib/ast/other.rs b/vm/src/stdlib/ast/other.rs index 2b9d292c1..f7d698133 100644 --- a/vm/src/stdlib/ast/other.rs +++ b/vm/src/stdlib/ast/other.rs @@ -1,5 +1,6 @@ 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 { @@ -13,7 +14,13 @@ impl Node for ruff::ConversionFlag { ) -> PyResult { i32::try_from_object(vm, object)? .to_u32() - .and_then(ruff::ConversionFlag::from_op_arg) + .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())) } }