mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
* stage1 * compiler pass build * introduce rustpython-compiler-source * stage2 * fixup * pass compile * Fix hello world compiler test * Fix code generation for if-elif-else statement * Fix code generation for lambda expression * Fix code generation for integers * Fix code generation for fstrings * Fix code generation for if statement * Fix code generation for if statement * Fix code generation for if statement * Fix code generation for fstring * Fix code generation for class definition * Replace feature flags * Initialize frozen core modules * Allow __future__ import after module doc comment * Disable ast module * Commit remaining fixes for compile errors in examples * Fix some warnings * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Split ast stdlib module into files * Fix codegen for positional arguments with defaults * Update ast stdlib module * Update ast stdlib module * Extract string and constant handling from expression.rs * Always add required fields to AST nodes * Compile doc strings correctly again * Enable "ast" Cargo feature by default * Refactor compilation of big integer literal * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Reset barebones example * Fix some left-over warnings * Undo accidential change * Adapt shell to ruff parser * Pin parser to v0.4.10 * fix clippy * Add TODO about interactive mode * Fix compilation of complex number expression * Remove moved code * Update test case to ruff v0.4.10 * Apply suggestion Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> * Apply suggestion Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> * Fix compilation of fstring expression * Fix compilation of fstring expression * Fix wasm compile errors * Attach correct source locations to ast objects * Fix some more wasm compile errors * Consider compile mode and enable AST stdlib module again * Fix incorrect AST source location end column * Fix compile error if "compiler" feature is not enabled * Fix regrtests * Fix some test_ast tests * Add source range to type ignore * Fix incompatibility with Rust 2024 edition * Fix todos by implementing missing ast conversions and deleting unused code * Appease clippy * Fix remaining ast tests * Fix remaining ast tests * Mark/fix remaining tests * Fix more * Hacky windows fix --------- Co-authored-by: Kangzhi Shi <shikangzhi@gmail.com> Co-authored-by: Jeong YunWon <jeong@youknowone.org> Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> Co-authored-by: Noa <coolreader18@gmail.com>
91 lines
2.7 KiB
Rust
91 lines
2.7 KiB
Rust
/// This an example usage of the rustpython_compiler crate.
|
|
/// This program reads, parses, and compiles a file you provide
|
|
/// to RustPython bytecode, and then displays the output in the
|
|
/// `dis.dis` format.
|
|
///
|
|
/// example usage:
|
|
/// $ cargo run --release --example dis demo*.py
|
|
|
|
#[macro_use]
|
|
extern crate log;
|
|
|
|
use lexopt::ValueExt;
|
|
use rustpython_compiler as compiler;
|
|
use std::error::Error;
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
fn main() -> Result<(), lexopt::Error> {
|
|
env_logger::init();
|
|
|
|
let mut scripts = vec![];
|
|
let mut mode = compiler::Mode::Exec;
|
|
let mut expand_code_objects = true;
|
|
let mut optimize = 0;
|
|
|
|
let mut parser = lexopt::Parser::from_env();
|
|
while let Some(arg) = parser.next()? {
|
|
use lexopt::Arg::*;
|
|
match arg {
|
|
Long("help") | Short('h') => {
|
|
let bin_name = parser.bin_name().unwrap_or("dis");
|
|
println!(
|
|
"usage: {bin_name} <scripts...> [-m,--mode=exec|single|eval] [-x,--no-expand] [-O]"
|
|
);
|
|
println!(
|
|
"Compiles and disassembles python script files for viewing their bytecode."
|
|
);
|
|
return Ok(());
|
|
}
|
|
Value(x) => scripts.push(PathBuf::from(x)),
|
|
Long("mode") | Short('m') => {
|
|
mode = parser
|
|
.value()?
|
|
.parse_with(|s| s.parse::<compiler::Mode>().map_err(|e| e.to_string()))?
|
|
}
|
|
Long("no-expand") | Short('x') => expand_code_objects = false,
|
|
Short('O') => optimize += 1,
|
|
_ => return Err(arg.unexpected()),
|
|
}
|
|
}
|
|
|
|
if scripts.is_empty() {
|
|
return Err("expected at least one argument".into());
|
|
}
|
|
|
|
let opts = compiler::CompileOpts {
|
|
optimize,
|
|
..Default::default()
|
|
};
|
|
|
|
for script in &scripts {
|
|
if script.exists() && script.is_file() {
|
|
let res = display_script(script, mode, opts.clone(), expand_code_objects);
|
|
if let Err(e) = res {
|
|
error!("Error while compiling {:?}: {}", script, e);
|
|
}
|
|
} else {
|
|
eprintln!("{script:?} is not a file.");
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn display_script(
|
|
path: &Path,
|
|
mode: compiler::Mode,
|
|
opts: compiler::CompileOpts,
|
|
expand_code_objects: bool,
|
|
) -> Result<(), Box<dyn Error>> {
|
|
let source = fs::read_to_string(path)?;
|
|
let code = compiler::compile(&source, mode, &path.to_string_lossy(), opts)?;
|
|
println!("{}:", path.display());
|
|
if expand_code_objects {
|
|
println!("{}", code.display_expand_code_objects());
|
|
} else {
|
|
println!("{code}");
|
|
}
|
|
Ok(())
|
|
}
|