Skip to content

Commit 45bb2bd

Browse files
committed
Split off bytecode compilation into a separate crate
1 parent 3f343af commit 45bb2bd

17 files changed

Lines changed: 63 additions & 38 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors = ["Windel Bouwman", "Shing Lyu <shing.lyu@gmail.com>"]
55
edition = "2018"
66

77
[workspace]
8-
members = [".", "derive", "vm", "wasm/lib", "parser"]
8+
members = [".", "derive", "vm", "wasm/lib", "parser", "compiler"]
99

1010
[[bench]]
1111
name = "bench"

compiler/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "rustpython_compiler"
3+
version = "0.1.0"
4+
authors = ["coolreader18 <33094578+coolreader18@users.noreply.github.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
bitflags = "1.1"
9+
rustpython_parser = { path = "../parser" }
10+
serde = { version = "1.0", features = ["derive"] }
11+
num-complex = { version = "0.2", features = ["serde"] }
12+
num-bigint = { version = "0.2", features = ["serde"] }
13+
log = "0.3"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Primitive instruction type, which can be encoded and decoded.
66
*/
77

8+
use bitflags::bitflags;
89
use num_bigint::BigInt;
910
use num_complex::Complex64;
1011
use rustpython_parser::ast;
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
88
use crate::bytecode::{self, CallType, CodeObject, Instruction, Varargs};
99
use crate::error::{CompileError, CompileErrorType};
10-
use crate::obj::objcode;
11-
use crate::obj::objcode::PyCodeRef;
12-
use crate::pyobject::PyValue;
1310
use crate::symboltable::{make_symbol_table, statements_to_symbol_table, SymbolRole, SymbolScope};
14-
use crate::VirtualMachine;
1511
use num_complex::Complex64;
1612
use rustpython_parser::{ast, parser};
1713

@@ -27,12 +23,7 @@ struct Compiler {
2723
}
2824

2925
/// Compile a given sourcecode into a bytecode object.
30-
pub fn compile(
31-
vm: &VirtualMachine,
32-
source: &str,
33-
mode: &Mode,
34-
source_path: String,
35-
) -> Result<PyCodeRef, CompileError> {
26+
pub fn compile(source: &str, mode: &Mode, source_path: String) -> Result<CodeObject, CompileError> {
3627
let mut compiler = Compiler::new();
3728
compiler.source_path = Some(source_path);
3829
compiler.push_new_code_object("<module>".to_string());
@@ -57,7 +48,7 @@ pub fn compile(
5748

5849
let code = compiler.pop_code_object();
5950
trace!("Compilation completed: {:?}", code);
60-
Ok(objcode::PyCode::new(code).into_ref(vm))
51+
Ok(code)
6152
}
6253

6354
pub enum Mode {
File renamed without changes.

compiler/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_use]
2+
extern crate log;
3+
4+
pub mod bytecode;
5+
pub mod compile;
6+
pub mod error;
7+
mod symboltable;

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ fn main() {
7575
}
7676

7777
fn _run_string(vm: &VirtualMachine, source: &str, source_path: String) -> PyResult {
78-
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path.clone())
78+
let code_obj = vm
79+
.compile(source, &compile::Mode::Exec, source_path.clone())
7980
.map_err(|err| vm.new_syntax_error(&err))?;
8081
// trace!("Code object: {:?}", code_obj.borrow());
8182
let attrs = vm.ctx.new_dict();
@@ -160,7 +161,7 @@ fn test_run_script() {
160161
}
161162

162163
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> Result<(), CompileError> {
163-
match compile::compile(vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
164+
match vm.compile(source, &compile::Mode::Single, "<stdin>".to_string()) {
164165
Ok(code) => {
165166
match vm.run_code_obj(code, scope.clone()) {
166167
Ok(value) => {

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ authors = ["Shing Lyu <shing.lyu@gmail.com>"]
55
edition = "2018"
66

77
[dependencies]
8-
bitflags = "1.0.4"
98
num-complex = { version = "0.2", features = ["serde"] }
109
num-bigint = { version = "0.2.1", features = ["serde"] }
1110
num-traits = "0.2"
@@ -15,6 +14,7 @@ rand = "0.5"
1514
log = "0.3"
1615
rustpython_derive = {path = "../derive"}
1716
rustpython_parser = {path = "../parser"}
17+
rustpython_compiler = {path = "../compiler"}
1818
serde = { version = "1.0.66", features = ["derive"] }
1919
serde_json = "1.0.26"
2020
byteorder = "1.2.6"

0 commit comments

Comments
 (0)