feat: add erg_compiler::compile_with_dependencies

This commit is contained in:
Shunsuke Shibayama 2024-02-14 15:26:14 +09:00
parent 121738da74
commit 17625884b9
7 changed files with 71 additions and 7 deletions

2
Cargo.lock generated
View file

@ -124,6 +124,7 @@ version = "0.6.31-nightly.0"
dependencies = [ dependencies = [
"backtrace-on-stack-overflow", "backtrace-on-stack-overflow",
"crossterm", "crossterm",
"erg_proc_macros",
"parking_lot", "parking_lot",
"pyo3", "pyo3",
"thread_local", "thread_local",
@ -152,7 +153,6 @@ dependencies = [
name = "erg_proc_macros" name = "erg_proc_macros"
version = "0.6.31-nightly.0" version = "0.6.31-nightly.0"
dependencies = [ dependencies = [
"erg_common",
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
] ]

View file

@ -34,6 +34,7 @@ crossterm = { optional = true, version = "0.25.0" }
parking_lot = "0.12" parking_lot = "0.12"
thread_local = "1.1" thread_local = "1.1"
pyo3 = { workspace = true, optional = true } pyo3 = { workspace = true, optional = true }
erg_proc_macros = { workspace = true }
[lib] [lib]
path = "lib.rs" path = "lib.rs"

View file

@ -15,6 +15,11 @@ use crate::normalize_path;
use crate::python_util::{detect_magic_number, get_python_version, PythonVersion}; use crate::python_util::{detect_magic_number, get_python_version, PythonVersion};
use crate::serialize::{get_magic_num_from_bytes, get_ver_from_magic_num}; use crate::serialize::{get_magic_num_from_bytes, get_ver_from_magic_num};
#[cfg(not(feature = "pylib"))]
use erg_proc_macros::{new, pyclass, pymethods};
#[cfg(feature = "pylib")]
use pyo3::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErgMode { pub enum ErgMode {
Lex, Lex,
@ -92,6 +97,7 @@ impl From<&str> for TranspileTarget {
} }
} }
#[pyclass]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Package { pub struct Package {
pub name: &'static str, pub name: &'static str,
@ -116,6 +122,19 @@ impl Package {
} }
} }
#[pymethods]
impl Package {
#[new]
fn _new(name: String, as_name: String, version: String, path: Option<String>) -> Self {
Self {
name: Box::leak(name.into_boxed_str()),
as_name: Box::leak(as_name.into_boxed_str()),
version: Box::leak(version.into_boxed_str()),
path: path.map(|s| Box::leak(s.into_boxed_str()) as &'static str),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ErgConfig { pub struct ErgConfig {
pub mode: ErgMode, pub mode: ErgMode,

View file

@ -614,7 +614,7 @@ impl From<CompileErrors> for ParseErrors {
#[cfg(feature = "pylib")] #[cfg(feature = "pylib")]
impl std::convert::From<CompileErrors> for pyo3::PyErr { impl std::convert::From<CompileErrors> for pyo3::PyErr {
fn from(errs: CompileErrors) -> pyo3::PyErr { fn from(errs: CompileErrors) -> pyo3::PyErr {
pyo3::exceptions::PyOSError::new_err(errs[0].to_string()) pyo3::exceptions::PyOSError::new_err(format!("{} errors occurred\n{errs}", errs.len()))
} }
} }

View file

@ -27,6 +27,9 @@ pub mod transpile;
pub mod ty; pub mod ty;
pub mod varinfo; pub mod varinfo;
#[allow(unused)]
use erg_common::config::Package;
pub use build_hir::{GenericHIRBuilder, HIRBuilder}; pub use build_hir::{GenericHIRBuilder, HIRBuilder};
pub use erg_parser::build_ast::ASTBuilder; pub use erg_parser::build_ast::ASTBuilder;
pub use transpile::Transpiler; pub use transpile::Transpiler;
@ -36,16 +39,22 @@ use pyo3::prelude::*;
#[cfg(feature = "pylib")] #[cfg(feature = "pylib")]
use pyo3::types::{IntoPyDict, PyBytes}; use pyo3::types::{IntoPyDict, PyBytes};
/// compile(code: str, mode: str) -> code /// compile_with_dependencies(code: str, mode: str, pkgs: list[Package]) -> code
/// -- /// --
/// ///
/// compile an Erg code as a module at runtime /// compile an Erg code as a module at runtime
#[cfg(feature = "pylib")] #[cfg(feature = "pylib")]
#[pyfunction] #[pyfunction]
#[pyo3(name = "compile")] #[pyo3(name = "compile_with_dependencies")]
fn _compile(py: Python<'_>, code: String, mode: &str) -> Result<PyObject, error::CompileErrors> { fn _compile_with_dependencies(
py: Python<'_>,
code: String,
mode: &str,
pkgs: Vec<Package>,
) -> Result<PyObject, error::CompileErrors> {
use erg_common::{config::ErgConfig, traits::Runnable}; use erg_common::{config::ErgConfig, traits::Runnable};
let cfg = ErgConfig::string(code); let mut cfg = ErgConfig::string(code);
cfg.packages = pkgs;
let mut compiler = Compiler::new(cfg); let mut compiler = Compiler::new(cfg);
let src = compiler.cfg_mut().input.read(); let src = compiler.cfg_mut().input.read();
let code = compiler let code = compiler
@ -59,6 +68,17 @@ fn _compile(py: Python<'_>, code: String, mode: &str) -> Result<PyObject, error:
Ok(code.into()) Ok(code.into())
} }
/// compile(code: str, mode: str) -> code
/// --
///
/// compile an Erg code as a module at runtime
#[cfg(feature = "pylib")]
#[pyfunction]
#[pyo3(name = "compile")]
fn _compile(py: Python<'_>, code: String, mode: &str) -> Result<PyObject, error::CompileErrors> {
_compile_with_dependencies(py, code, mode, vec![])
}
/// compile_ast(ast: erg_parser.AST, mode: str) -> code /// compile_ast(ast: erg_parser.AST, mode: str) -> code
/// -- /// --
/// ///
@ -99,6 +119,22 @@ fn _compile_file(py: Python<'_>, path: String) -> Result<PyObject, error::Compil
_compile(py, code, "exec") _compile(py, code, "exec")
} }
/// compile_file_with_dependencies(path: str, pkgs: list[Package]) -> code
/// --
///
/// compile an Erg file as a module at runtime
#[cfg(feature = "pylib")]
#[pyfunction]
#[pyo3(name = "compile_file_with_dependencies")]
fn _compile_file_with_dependencies(
py: Python<'_>,
path: String,
pkgs: Vec<Package>,
) -> Result<PyObject, error::CompileErrors> {
let code = std::fs::read_to_string(&path).unwrap_or_else(|err| panic!("{err}, path: {path}"));
_compile_with_dependencies(py, code, "exec", pkgs)
}
/// exec(code: str) -> module /// exec(code: str) -> module
/// -- /// --
/// ///
@ -145,9 +181,12 @@ fn _import(py: Python<'_>, name: String) -> Result<PyObject, error::CompileError
#[cfg(feature = "pylib")] #[cfg(feature = "pylib")]
#[pymodule] #[pymodule]
fn erg_compiler(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn erg_compiler(py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<Package>()?;
m.add_function(wrap_pyfunction!(_compile, m)?)?; m.add_function(wrap_pyfunction!(_compile, m)?)?;
m.add_function(wrap_pyfunction!(_compile_with_dependencies, m)?)?;
m.add_function(wrap_pyfunction!(_compile_ast, m)?)?; m.add_function(wrap_pyfunction!(_compile_ast, m)?)?;
m.add_function(wrap_pyfunction!(_compile_file, m)?)?; m.add_function(wrap_pyfunction!(_compile_file, m)?)?;
m.add_function(wrap_pyfunction!(_compile_file_with_dependencies, m)?)?;
m.add_function(wrap_pyfunction!(_exec, m)?)?; m.add_function(wrap_pyfunction!(_exec, m)?)?;
m.add_function(wrap_pyfunction!(_exec_ast, m)?)?; m.add_function(wrap_pyfunction!(_exec_ast, m)?)?;
m.add_function(wrap_pyfunction!(_import, m)?)?; m.add_function(wrap_pyfunction!(_import, m)?)?;

View file

@ -11,7 +11,6 @@ homepage.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
erg_common = { workspace = true }
syn = { version = "1.0", features = ["full"] } syn = { version = "1.0", features = ["full"] }
quote = "1.0" quote = "1.0"

View file

@ -100,6 +100,12 @@ pub fn setter(_attr: TokenStream, item: TokenStream) -> TokenStream {
item item
} }
/// dummy attribute
#[proc_macro_attribute]
pub fn new(_attr: TokenStream, item: TokenStream) -> TokenStream {
item
}
fn args_to_owned(args: &PathArguments) -> PathArguments { fn args_to_owned(args: &PathArguments) -> PathArguments {
match args { match args {
PathArguments::AngleBracketed(args) => { PathArguments::AngleBracketed(args) => {