Skip to content

Commit 4528f17

Browse files
committed
compiler/porcelain wrapper
1 parent b3851c8 commit 4528f17

File tree

16 files changed

+58
-60
lines changed

16 files changed

+58
-60
lines changed

Cargo.lock

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
1010

1111
[workspace]
1212
members = [
13-
".", "ast", "bytecode", "common", "compiler", "derive",
14-
"jit", "parser", "vm", "vm/pylib-crate", "wasm/lib",
13+
".", "ast", "bytecode", "common", "compiler", "compiler/porcelain",
14+
"derive", "jit", "parser", "vm", "vm/pylib-crate", "wasm/lib",
1515
]
1616

1717
[[bench]]
@@ -31,7 +31,6 @@ ssl = ["rustpython-vm/ssl"]
3131
log = "0.4"
3232
env_logger = "0.7"
3333
clap = "2.33"
34-
rustpython-compiler = { path = "compiler", version = "0.1.1" }
3534
rustpython-parser = { path = "parser", version = "0.1.1" }
3635
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["compile-parse"] }
3736
pylib = { package = "rustpython-pylib", path = "vm/pylib-crate", version = "0.1.0", default-features = false, optional = true }

ast/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rustpython-ast"
33
version = "0.1.0"
4-
authors = ["Noah <33094578+coolreader18@users.noreply.github.com>"]
4+
authors = ["RustPython Team"]
55
edition = "2018"
66

77
[dependencies]

compiler/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "rustpython-compiler"
2+
name = "rustpython-compiler-core"
33
version = "0.1.2"
44
description = "Compiler for python code into bytecode for the rustpython VM."
55
authors = ["RustPython Team"]

compiler/porcelain/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "rustpython-compiler"
3+
version = "0.1.2"
4+
description = "A usability wrapper around rustpython-parser and rustpython-compiler-core"
5+
authors = ["RustPython Team"]
6+
edition = "2018"
7+
8+
[dependencies]
9+
thiserror = "1.0"
10+
rustpython-compiler-core = { path = ".." }
11+
rustpython-parser = { path = "../../parser" }
12+
rustpython-bytecode = { path = "../../bytecode" }
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use rustpython_bytecode::bytecode::CodeObject;
2-
use rustpython_compiler::{compile, symboltable};
2+
use rustpython_compiler_core::{compile, symboltable};
33
use rustpython_parser::{ast::Location, parser};
44
use std::fmt;
55

66
pub use compile::{CompileOpts, Mode};
7+
pub use symboltable::{Symbol, SymbolScope, SymbolTable, SymbolTableType};
78

89
#[derive(Debug, thiserror::Error)]
910
pub enum CompileErrorType {
1011
#[error(transparent)]
11-
Compile(#[from] rustpython_compiler::error::CompileErrorType),
12+
Compile(#[from] rustpython_compiler_core::error::CompileErrorType),
1213
#[error(transparent)]
1314
Parse(#[from] rustpython_parser::error::ParseErrorType),
1415
}
@@ -38,7 +39,7 @@ impl fmt::Display for CompileError {
3839
}
3940

4041
impl CompileError {
41-
fn from_compile(error: rustpython_compiler::error::CompileError, source: &str) -> Self {
42+
fn from_compile(error: rustpython_compiler_core::error::CompileError, source: &str) -> Self {
4243
CompileError {
4344
error: error.error.into(),
4445
location: error.location,
@@ -99,7 +100,7 @@ pub fn compile(
99100
res.map_err(|e| CompileError::from_compile(e, source))
100101
}
101102

102-
pub(crate) fn compile_symtable(
103+
pub fn compile_symtable(
103104
source: &str,
104105
mode: compile::Mode,
105106
source_path: &str,

derive/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ syn = { version = "1.0", features = ["full"] }
1515
syn-ext = { version = "0.2.1", features = ["full"] }
1616
quote = "1.0"
1717
proc-macro2 = "1.0"
18-
rustpython-compiler = { path = "../compiler", version = "0.1.1" }
19-
rustpython-parser = { path = "../parser", version = "0.1.1" }
18+
rustpython-compiler = { path = "../compiler/porcelain", version = "0.1.1" }
2019
rustpython-bytecode = { path = "../bytecode", version = "0.1.1" }
2120
maplit = "1.0"
2221
once_cell = "1.3.1"

derive/src/compile_bytecode.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use once_cell::sync::Lazy;
1818
use proc_macro2::{Span, TokenStream as TokenStream2};
1919
use quote::quote;
2020
use rustpython_bytecode::bytecode::{CodeObject, FrozenModule};
21-
use rustpython_compiler::compile;
22-
use rustpython_parser::parser;
21+
use rustpython_compiler as compile;
2322
use std::collections::HashMap;
2423
use std::env;
2524
use std::fs;
@@ -44,40 +43,21 @@ struct CompilationSource {
4443
}
4544

4645
impl CompilationSource {
47-
fn compile_string<D: std::fmt::Display, F: Fn() -> D>(
46+
fn compile_string<D: std::fmt::Display, F: FnOnce() -> D>(
4847
&self,
4948
source: &str,
5049
mode: compile::Mode,
5150
module_name: String,
5251
origin: F,
5352
) -> Result<CodeObject, Diagnostic> {
54-
let parse_err = |err| {
55-
Diagnostic::spans_error(
56-
self.span,
57-
format!("Python parse error from {}: {}", origin(), err),
58-
)
59-
};
60-
let opts = compile::CompileOpts::default();
61-
let res = match mode {
62-
compile::Mode::Exec => {
63-
let ast = parser::parse_program(source).map_err(parse_err)?;
64-
compile::compile_program(ast, module_name, opts)
65-
}
66-
compile::Mode::Eval => {
67-
let statement = parser::parse_statement(source).map_err(parse_err)?;
68-
compile::compile_statement_eval(statement, module_name, opts)
69-
}
70-
compile::Mode::Single => {
71-
let ast = parser::parse_program(source).map_err(parse_err)?;
72-
compile::compile_program_single(ast, module_name, opts)
73-
}
74-
};
75-
res.map_err(|err| {
76-
Diagnostic::spans_error(
77-
self.span,
78-
format!("Python compile error from {}: {}", origin(), err),
79-
)
80-
})
53+
compile::compile(source, mode, module_name, compile::CompileOpts::default()).map_err(
54+
|err| {
55+
Diagnostic::spans_error(
56+
self.span,
57+
format!("Python compile error from {}: {}", origin(), err),
58+
)
59+
},
60+
)
8161
}
8262

8363
fn compile(

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ extern crate env_logger;
55
extern crate log;
66

77
use clap::{App, AppSettings, Arg, ArgMatches};
8-
use rustpython_compiler::compile;
98
use rustpython_vm::{
109
builtins::PyInt,
10+
compile,
1111
exceptions::print_exception,
1212
match_class,
1313
pyobject::{BorrowValue, ItemProtocol, PyResult, TypeProtocol},

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mt19937 = "1.0"
4141
log = "0.4"
4242
rustpython-derive = { path = "../derive", version = "0.1.2" }
4343
rustpython-parser = { path = "../parser", optional = true, version = "0.1.2" }
44-
rustpython-compiler = { path = "../compiler", optional = true, version = "0.1.2" }
44+
rustpython-compiler = { path = "../compiler/porcelain", optional = true, version = "0.1.2" }
4545
rustpython-bytecode = { path = "../bytecode", version = "0.1.2" }
4646
rustpython-jit = { path = "../jit", optional = true, version = "0.1.2" }
4747
rustpython-pylib = { path = "pylib-crate", optional = true, version = "0.1.0" }

0 commit comments

Comments
 (0)