mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Create vm::compiler to handle CompileError variations
This commit is contained in:
1 parent
d876673546
commit
07e339a7fb
10 files changed
+41
-34
No files matched your search
+2
-2
@@ -3,7 +3,7 @@ mod helper;
|
||||
use rustpython_parser::error::{LexicalErrorType, ParseErrorType};
|
||||
use rustpython_vm::{
|
||||
builtins::PyBaseExceptionRef,
|
||||
compile::{self, CompileError, CompileErrorBody, CompileErrorType},
|
||||
compiler::{self, CompileError, CompileErrorBody, CompileErrorType},
|
||||
readline::{Readline, ReadlineResult},
|
||||
scope::Scope,
|
||||
AsObject, PyResult, VirtualMachine,
|
||||
@@ -16,7 +16,7 @@ enum ShellExecResult {
|
||||
}
|
||||
|
||||
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResult {
|
||||
match vm.compile(source, compile::Mode::Single, "<stdin>".to_owned()) {
|
||||
match vm.compile(source, compiler::Mode::Single, "<stdin>".to_owned()) {
|
||||
Ok(code) => match vm.run_code_obj(code, scope) {
|
||||
Ok(_val) => ShellExecResult::Ok,
|
||||
Err(err) => ShellExecResult::PyErr(err),
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ mod decl {
|
||||
use crate::vm::{
|
||||
builtins::{PyCode, PyDictRef, PyStrRef},
|
||||
bytecode::CodeFlags,
|
||||
compile, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
|
||||
compiler, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
|
||||
};
|
||||
|
||||
#[pyfunction]
|
||||
@@ -15,7 +15,7 @@ mod decl {
|
||||
PyRef::try_from_object(vm, co)?
|
||||
} else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) {
|
||||
// String:
|
||||
vm.compile(co_str.as_str(), compile::Mode::Exec, "<dis>".to_owned())
|
||||
vm.compile(co_str.as_str(), compiler::Mode::Exec, "<dis>".to_owned())
|
||||
.map_err(|err| vm.new_syntax_error(&err))?
|
||||
} else {
|
||||
PyRef::try_from_object(vm, obj)?
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#[cfg(feature = "rustpython-codegen")]
|
||||
pub use rustpython_codegen::CompileOpts;
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
pub use rustpython_compiler::*;
|
||||
pub use rustpython_compiler_core::Mode;
|
||||
|
||||
#[cfg(not(feature = "rustpython-compiler"))]
|
||||
mod error {
|
||||
#[cfg(all(feature = "rustpython-parser", feature = "rustpython-codegen"))]
|
||||
panic!("Use --features=compiler to enable both parser and codegen");
|
||||
#[cfg(feature = "rustpython-parser")]
|
||||
pub type CompileError = rustpython_compiler_core::CompileError<rustpython_parser::ParseError>;
|
||||
#[cfg(feature = "rustpython-codegen")]
|
||||
pub type CompileError =
|
||||
rustpython_compiler_core::CompileError<rustpython_codegen::CodegenError>;
|
||||
}
|
||||
+2
-5
@@ -1,10 +1,7 @@
|
||||
use crate::compile;
|
||||
use crate::scope::Scope;
|
||||
use crate::PyResult;
|
||||
use crate::VirtualMachine;
|
||||
use crate::{compiler, scope::Scope, PyResult, VirtualMachine};
|
||||
|
||||
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
|
||||
match vm.compile(source, compile::Mode::Eval, source_path.to_owned()) {
|
||||
match vm.compile(source, compiler::Mode::Eval, source_path.to_owned()) {
|
||||
Ok(bytecode) => {
|
||||
debug!("Code object: {:?}", bytecode);
|
||||
vm.run_code_obj(bytecode, scope)
|
||||
|
||||
+1
-1
@@ -111,7 +111,7 @@ pub fn import_file(
|
||||
let code = vm
|
||||
.compile_with_opts(
|
||||
&content,
|
||||
crate::compile::Mode::Exec,
|
||||
crate::compiler::Mode::Exec,
|
||||
file_path,
|
||||
vm.compile_opts(),
|
||||
)
|
||||
|
||||
+1
-7
@@ -87,13 +87,7 @@ pub use self::vm::{Context, Interpreter, Settings, VirtualMachine};
|
||||
pub use rustpython_common as common;
|
||||
pub use rustpython_compiler_core as bytecode;
|
||||
|
||||
pub mod compile {
|
||||
#[cfg(feature = "rustpython-codegen")]
|
||||
pub use rustpython_codegen::CompileOpts;
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
pub use rustpython_compiler::*;
|
||||
pub use rustpython_compiler_core::Mode;
|
||||
}
|
||||
pub mod compiler;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod __exports {
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::{class::PyClassImpl, PyObjectRef, VirtualMachine};
|
||||
#[pymodule]
|
||||
mod builtins {
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
use crate::compile;
|
||||
use crate::{
|
||||
builtins::{
|
||||
asyncgenerator::PyAsyncGen,
|
||||
@@ -21,6 +20,7 @@ mod builtins {
|
||||
PyByteArray, PyBytes, PyDictRef, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType,
|
||||
},
|
||||
common::{hash::PyHash, str::to_ascii},
|
||||
compiler,
|
||||
format::call_object_format,
|
||||
function::Either,
|
||||
function::{
|
||||
@@ -144,7 +144,7 @@ mod builtins {
|
||||
#[cfg(feature = "rustpython-codegen")]
|
||||
{
|
||||
let mode = mode_str
|
||||
.parse::<compile::Mode>()
|
||||
.parse::<compiler::Mode>()
|
||||
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
||||
return ast::compile(vm, args.source, args.filename.as_str(), mode);
|
||||
}
|
||||
@@ -161,7 +161,7 @@ mod builtins {
|
||||
use rustpython_parser::parser;
|
||||
|
||||
let source = Either::<PyStrRef, PyBytesRef>::try_from_object(vm, args.source)?;
|
||||
// TODO: compile::compile should probably get bytes
|
||||
// TODO: compiler::compile should probably get bytes
|
||||
let source = match &source {
|
||||
Either::A(string) => string.as_str(),
|
||||
Either::B(bytes) => std::str::from_utf8(bytes)
|
||||
@@ -178,7 +178,7 @@ mod builtins {
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
{
|
||||
let mode = mode_str
|
||||
.parse::<compile::Mode>()
|
||||
.parse::<compiler::Mode>()
|
||||
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
||||
let code = vm
|
||||
.compile(source, mode, args.filename.as_str().to_owned())
|
||||
@@ -279,7 +279,7 @@ mod builtins {
|
||||
}
|
||||
Either::B(code) => Ok(Either::B(code)),
|
||||
}?;
|
||||
run_code(vm, code, scope, crate::compile::Mode::Eval, "eval")
|
||||
run_code(vm, code, scope, crate::compiler::Mode::Eval, "eval")
|
||||
}
|
||||
|
||||
/// Implements `exec`
|
||||
@@ -290,14 +290,14 @@ mod builtins {
|
||||
scope: ScopeArgs,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
run_code(vm, source, scope, crate::compile::Mode::Exec, "exec")
|
||||
run_code(vm, source, scope, crate::compiler::Mode::Exec, "exec")
|
||||
}
|
||||
|
||||
fn run_code(
|
||||
vm: &VirtualMachine,
|
||||
source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>,
|
||||
scope: ScopeArgs,
|
||||
#[allow(unused_variables)] mode: crate::compile::Mode,
|
||||
#[allow(unused_variables)] mode: crate::compiler::Mode,
|
||||
func: &str,
|
||||
) -> PyResult {
|
||||
let scope = scope.make_scope(vm)?;
|
||||
|
||||
@@ -3,7 +3,7 @@ pub(crate) use symtable::make_module;
|
||||
#[pymodule]
|
||||
mod symtable {
|
||||
use crate::{
|
||||
builtins::PyStrRef, compile, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
|
||||
builtins::PyStrRef, compiler, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
|
||||
};
|
||||
use rustpython_codegen::symboltable::{Symbol, SymbolScope, SymbolTable, SymbolTableType};
|
||||
use std::fmt;
|
||||
@@ -19,10 +19,10 @@ mod symtable {
|
||||
) -> PyResult<PySymbolTableRef> {
|
||||
let mode = mode
|
||||
.as_str()
|
||||
.parse::<compile::Mode>()
|
||||
.parse::<compiler::Mode>()
|
||||
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
||||
|
||||
let symtable = compile::compile_symtable(source.as_str(), mode, filename.as_str())
|
||||
let symtable = compiler::compile_symtable(source.as_str(), mode, filename.as_str())
|
||||
.map_err(|err| vm.new_syntax_error(&err))?;
|
||||
|
||||
let py_symbol_table = to_py_symbol_table(symtable);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
builtins::{PyCode, PyDictRef},
|
||||
compile::{self, CompileError, CompileOpts},
|
||||
compiler::{self, CompileError, CompileOpts},
|
||||
convert::TryFromObject,
|
||||
scope::Scope,
|
||||
AsObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
|
||||
@@ -18,7 +18,7 @@ impl VirtualMachine {
|
||||
pub fn compile(
|
||||
&self,
|
||||
source: &str,
|
||||
mode: compile::Mode,
|
||||
mode: compiler::Mode,
|
||||
source_path: String,
|
||||
) -> Result<PyRef<PyCode>, CompileError> {
|
||||
self.compile_with_opts(source, mode, source_path, self.compile_opts())
|
||||
@@ -27,11 +27,11 @@ impl VirtualMachine {
|
||||
pub fn compile_with_opts(
|
||||
&self,
|
||||
source: &str,
|
||||
mode: compile::Mode,
|
||||
mode: compiler::Mode,
|
||||
source_path: String,
|
||||
opts: CompileOpts,
|
||||
) -> Result<PyRef<PyCode>, CompileError> {
|
||||
compile::compile(source, mode, source_path, opts).map(|code| self.ctx.new_code(code))
|
||||
compiler::compile(source, mode, source_path, opts).map(|code| self.ctx.new_code(code))
|
||||
}
|
||||
|
||||
pub fn run_script(&self, scope: Scope, path: &str) -> PyResult<()> {
|
||||
@@ -68,7 +68,7 @@ impl VirtualMachine {
|
||||
|
||||
pub fn run_code_string(&self, scope: Scope, source: &str, source_path: String) -> PyResult {
|
||||
let code_obj = self
|
||||
.compile(source, compile::Mode::Exec, source_path.clone())
|
||||
.compile(source, compiler::Mode::Exec, source_path.clone())
|
||||
.map_err(|err| self.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
scope.globals.set_item(
|
||||
@@ -81,7 +81,7 @@ impl VirtualMachine {
|
||||
|
||||
pub fn run_block_expr(&self, scope: Scope, source: &str) -> PyResult {
|
||||
let code_obj = self
|
||||
.compile(source, compile::Mode::BlockExpr, "<embedded>".to_owned())
|
||||
.compile(source, compiler::Mode::BlockExpr, "<embedded>".to_owned())
|
||||
.map_err(|err| self.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
self.run_code_obj(code_obj, scope)
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
use crate::compile::{CompileError, CompileErrorType};
|
||||
use crate::compiler::{CompileError, CompileErrorType};
|
||||
use crate::{
|
||||
builtins::{
|
||||
pystr::IntoPyStrRef,
|
||||
|
||||
Reference in new issue
Block a user