Skip to content

Commit d375710

Browse files
committed
Make compiler no_std compatible
1 parent c49cc8f commit d375710

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ repository = "https://github.com/RustPython/RustPython"
77
license = "MIT"
88
edition = "2018"
99

10+
[features]
11+
std = ["rustpython-bytecode/std", "itertools/use_std"]
12+
default = ["std"]
13+
1014
[dependencies]
1115
indexmap = "1.0"
12-
itertools = "0.9"
13-
rustpython-bytecode = { path = "../bytecode", version = "0.1.1" }
16+
itertools = { version = "0.9", default-features = false }
17+
rustpython-bytecode = { path = "../bytecode", version = "0.1.1", default-features = false }
1418
rustpython-ast = { path = "../ast" }
1519
num-complex = { version = "0.3", features = ["serde"] }
1620
num-traits = "0.2"
1721
log = "0.4"
1822
ahash = "0.6"
23+
scopeguard = "1.1"
1924

2025
[dev-dependencies]
2126
rustpython-parser = { path = "../parser" }

compiler/src/compile.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ use crate::ir::{self, CodeInfo};
1010
pub use crate::mode::Mode;
1111
use crate::symboltable::{make_symbol_table, statements_to_symbol_table, SymbolScope, SymbolTable};
1212
use crate::IndexSet;
13+
use alloc::{borrow::ToOwned, boxed::Box, format, string::String, vec, vec::Vec};
1314
use itertools::Itertools;
1415
use num_complex::Complex64;
1516
use num_traits::ToPrimitive;
1617
use rustpython_ast as ast;
1718
use rustpython_bytecode::{self as bytecode, CodeObject, ConstantData, Instruction};
1819

20+
pub use crate::mode::Mode;
21+
1922
type CompileResult<T> = Result<T, CompileError>;
2023

2124
enum NameUsage {

compiler/src/error.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustpython_ast::Location;
22

3+
use alloc::string::String;
4+
use core::fmt;
5+
#[cfg(feature = "std")]
36
use std::error::Error;
4-
use std::fmt;
57

68
#[derive(Debug)]
79
pub struct CompileError {
@@ -78,6 +80,7 @@ impl fmt::Display for CompileErrorType {
7880
}
7981
}
8082

83+
#[cfg(feature = "std")]
8184
impl Error for CompileErrorType {}
8285

8386
impl fmt::Display for CompileError {
@@ -86,8 +89,5 @@ impl fmt::Display for CompileError {
8689
}
8790
}
8891

89-
impl Error for CompileError {
90-
fn source(&self) -> Option<&(dyn Error + 'static)> {
91-
None
92-
}
93-
}
92+
#[cfg(feature = "std")]
93+
impl Error for CompileError {}

compiler/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Compile a Python AST or source code into bytecode consumable by RustPython.
22
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/master/logo.png")]
33
#![doc(html_root_url = "https://docs.rs/rustpython-compiler/")]
4+
#![cfg_attr(not(feature = "std"), no_std)]
5+
6+
extern crate alloc;
47

58
#[macro_use]
69
extern crate log;

compiler/src/mode.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use core::fmt;
2+
13
#[derive(Clone, Copy)]
24
pub enum Mode {
35
Exec,
46
Eval,
57
Single,
68
}
79

8-
impl std::str::FromStr for Mode {
10+
impl core::str::FromStr for Mode {
911
type Err = ModeParseError;
1012
fn from_str(s: &str) -> Result<Self, ModeParseError> {
1113
match s {
@@ -22,8 +24,8 @@ pub struct ModeParseError {
2224
_priv: (),
2325
}
2426

25-
impl std::fmt::Display for ModeParseError {
26-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
27+
impl fmt::Display for ModeParseError {
28+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2729
write!(f, r#"mode should be "exec", "eval", or "single""#)
2830
}
2931
}

compiler/src/symboltable.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Inspirational file: https://github.com/python/cpython/blob/master/Python/symtabl
99

1010
use crate::error::{CompileError, CompileErrorType};
1111
use crate::IndexMap;
12+
use alloc::{borrow::ToOwned, format, string::String, vec, vec::Vec};
13+
use core::fmt;
1214
use rustpython_ast::{self as ast, Location};
13-
use std::fmt;
1415

1516
pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolTable, SymbolTableError> {
1617
let mut builder = SymbolTableBuilder::default();
@@ -187,8 +188,8 @@ impl SymbolTable {
187188
}
188189
}
189190

190-
impl std::fmt::Debug for SymbolTable {
191-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
191+
impl fmt::Debug for SymbolTable {
192+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
192193
write!(
193194
f,
194195
"SymbolTable({:?} symbols, {:?} sub scopes)",
@@ -209,8 +210,9 @@ fn analyze_symbol_table(symbol_table: &mut SymbolTable) -> SymbolTableResult {
209210
type SymbolMap = IndexMap<String, Symbol>;
210211

211212
mod stack {
212-
use std::panic;
213-
use std::ptr::NonNull;
213+
use alloc::vec::Vec;
214+
use core::ptr::NonNull;
215+
214216
pub struct StackStack<T> {
215217
v: Vec<NonNull<T>>,
216218
}
@@ -227,9 +229,10 @@ mod stack {
227229
F: FnOnce(&mut Self) -> R,
228230
{
229231
self.v.push(x.into());
230-
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| f(self)));
231-
self.v.pop();
232-
res.unwrap_or_else(|x| panic::resume_unwind(x))
232+
let mut this = scopeguard::guard(self, |this| {
233+
this.v.pop();
234+
});
235+
f(&mut this)
233236
}
234237

235238
pub fn iter(&self) -> impl Iterator<Item = &T> + DoubleEndedIterator + '_ {
@@ -273,7 +276,7 @@ struct SymbolTableAnalyzer {
273276

274277
impl SymbolTableAnalyzer {
275278
fn analyze_symbol_table(&mut self, symbol_table: &mut SymbolTable) -> SymbolTableResult {
276-
let symbols = std::mem::take(&mut symbol_table.symbols);
279+
let symbols = core::mem::take(&mut symbol_table.symbols);
277280
let sub_tables = &mut *symbol_table.sub_tables;
278281

279282
let mut info = (symbols, symbol_table.typ);
@@ -469,7 +472,7 @@ impl SymbolTableAnalyzer {
469472
SymbolTableType::Class => {
470473
// named expressions are forbidden in comprehensions on class scope
471474
return Err(SymbolTableError {
472-
error: "assignment expression within a comprehension cannot be used in a class body".to_string(),
475+
error: "assignment expression within a comprehension cannot be used in a class body".to_owned(),
473476
// TODO: accurate location info, somehow
474477
location: Location::default(),
475478
});
@@ -999,7 +1002,7 @@ impl SymbolTableBuilder {
9991002
// comprehension iterator definitions
10001003
if let ExpressionContext::IterDefinitionExp = context {
10011004
return Err(SymbolTableError {
1002-
error: "assignment expression cannot be used in a comprehension iterable expression".to_string(),
1005+
error: "assignment expression cannot be used in a comprehension iterable expression".to_owned(),
10031006
// TODO: accurate location info, somehow
10041007
location: Location::default(),
10051008
});
@@ -1218,7 +1221,7 @@ impl SymbolTableBuilder {
12181221
return Err(SymbolTableError {
12191222
error:
12201223
"assignment expression cannot be used in a comprehension iterable expression"
1221-
.to_string(),
1224+
.to_owned(),
12221225
location,
12231226
});
12241227
}

0 commit comments

Comments
 (0)