Skip to main content

rustpython_codegen/
lib.rs

1//! Compile a Python AST or source code into bytecode consumable by RustPython.
2#![cfg_attr(not(feature = "std"), no_std)]
3#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
4#![doc(html_root_url = "https://docs.rs/rustpython-compiler/")]
5
6#[macro_use]
7extern crate log;
8
9extern crate alloc;
10
11type IndexMap<K, V> = indexmap::IndexMap<K, V, ahash::RandomState>;
12type IndexSet<T> = indexmap::IndexSet<T, ahash::RandomState>;
13
14pub mod compile;
15pub mod error;
16pub mod ir;
17mod string_parser;
18pub mod symboltable;
19mod unparse;
20
21pub use compile::CompileOpts;
22use ruff_python_ast as ast;
23
24pub(crate) use compile::InternalResult;
25
26pub trait ToPythonName {
27    /// Returns a short name for the node suitable for use in error messages.
28    fn python_name(&self) -> &'static str;
29}
30
31impl ToPythonName for ast::Expr {
32    fn python_name(&self) -> &'static str {
33        match self {
34            Self::BoolOp { .. } | Self::BinOp { .. } | Self::UnaryOp { .. } => "operator",
35            Self::Subscript { .. } => "subscript",
36            Self::Await { .. } => "await expression",
37            Self::Yield { .. } | Self::YieldFrom { .. } => "yield expression",
38            Self::Compare { .. } => "comparison",
39            Self::Attribute { .. } => "attribute",
40            Self::Call { .. } => "function call",
41            Self::BooleanLiteral(b) => {
42                if b.value {
43                    "True"
44                } else {
45                    "False"
46                }
47            }
48            Self::EllipsisLiteral(_) => "ellipsis",
49            Self::NoneLiteral(_) => "None",
50            Self::NumberLiteral(_) | Self::BytesLiteral(_) | Self::StringLiteral(_) => "literal",
51            Self::Tuple(_) => "tuple",
52            Self::List { .. } => "list",
53            Self::Dict { .. } => "dict display",
54            Self::Set { .. } => "set display",
55            Self::ListComp { .. } => "list comprehension",
56            Self::DictComp { .. } => "dict comprehension",
57            Self::SetComp { .. } => "set comprehension",
58            Self::Generator { .. } => "generator expression",
59            Self::Starred { .. } => "starred",
60            Self::Slice { .. } => "slice",
61            Self::FString { .. } => "f-string expression",
62            Self::TString { .. } => "t-string expression",
63            Self::Name { .. } => "name",
64            Self::Lambda { .. } => "lambda",
65            Self::If { .. } => "conditional expression",
66            Self::Named { .. } => "named expression",
67            Self::IpyEscapeCommand(_) => todo!(),
68        }
69    }
70}