mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 10:49:54 +00:00
feat: add erg_compiler::compile_with_dependencies
This commit is contained in:
parent
13f303fbfb
commit
dabdc8eaa5
7 changed files with 71 additions and 7 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -124,6 +124,7 @@ version = "0.6.31-nightly.0"
|
|||
dependencies = [
|
||||
"backtrace-on-stack-overflow",
|
||||
"crossterm",
|
||||
"erg_proc_macros",
|
||||
"parking_lot",
|
||||
"pyo3",
|
||||
"thread_local",
|
||||
|
@ -152,7 +153,6 @@ dependencies = [
|
|||
name = "erg_proc_macros"
|
||||
version = "0.6.31-nightly.0"
|
||||
dependencies = [
|
||||
"erg_common",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
|
|
@ -34,6 +34,7 @@ crossterm = { optional = true, version = "0.25.0" }
|
|||
parking_lot = "0.12"
|
||||
thread_local = "1.1"
|
||||
pyo3 = { workspace = true, optional = true }
|
||||
erg_proc_macros = { workspace = true }
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
|
|
|
@ -15,6 +15,11 @@ use crate::normalize_path;
|
|||
use crate::python_util::{detect_magic_number, get_python_version, PythonVersion};
|
||||
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)]
|
||||
pub enum ErgMode {
|
||||
Lex,
|
||||
|
@ -92,6 +97,7 @@ impl From<&str> for TranspileTarget {
|
|||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct Package {
|
||||
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)]
|
||||
pub struct ErgConfig {
|
||||
pub mode: ErgMode,
|
||||
|
|
|
@ -614,7 +614,7 @@ impl From<CompileErrors> for ParseErrors {
|
|||
#[cfg(feature = "pylib")]
|
||||
impl std::convert::From<CompileErrors> for 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()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,9 @@ pub mod transpile;
|
|||
pub mod ty;
|
||||
pub mod varinfo;
|
||||
|
||||
#[allow(unused)]
|
||||
use erg_common::config::Package;
|
||||
|
||||
pub use build_hir::{GenericHIRBuilder, HIRBuilder};
|
||||
pub use erg_parser::build_ast::ASTBuilder;
|
||||
pub use transpile::Transpiler;
|
||||
|
@ -36,16 +39,22 @@ use pyo3::prelude::*;
|
|||
#[cfg(feature = "pylib")]
|
||||
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
|
||||
#[cfg(feature = "pylib")]
|
||||
#[pyfunction]
|
||||
#[pyo3(name = "compile")]
|
||||
fn _compile(py: Python<'_>, code: String, mode: &str) -> Result<PyObject, error::CompileErrors> {
|
||||
#[pyo3(name = "compile_with_dependencies")]
|
||||
fn _compile_with_dependencies(
|
||||
py: Python<'_>,
|
||||
code: String,
|
||||
mode: &str,
|
||||
pkgs: Vec<Package>,
|
||||
) -> Result<PyObject, error::CompileErrors> {
|
||||
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 src = compiler.cfg_mut().input.read();
|
||||
let code = compiler
|
||||
|
@ -59,6 +68,17 @@ fn _compile(py: Python<'_>, code: String, mode: &str) -> Result<PyObject, error:
|
|||
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
|
||||
/// --
|
||||
///
|
||||
|
@ -99,6 +119,22 @@ fn _compile_file(py: Python<'_>, path: String) -> Result<PyObject, error::Compil
|
|||
_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
|
||||
/// --
|
||||
///
|
||||
|
@ -145,9 +181,12 @@ fn _import(py: Python<'_>, name: String) -> Result<PyObject, error::CompileError
|
|||
#[cfg(feature = "pylib")]
|
||||
#[pymodule]
|
||||
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_with_dependencies, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(_compile_ast, 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_ast, m)?)?;
|
||||
m.add_function(wrap_pyfunction!(_import, m)?)?;
|
||||
|
|
|
@ -11,7 +11,6 @@ homepage.workspace = true
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
erg_common = { workspace = true }
|
||||
syn = { version = "1.0", features = ["full"] }
|
||||
quote = "1.0"
|
||||
|
||||
|
|
|
@ -100,6 +100,12 @@ pub fn setter(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
|||
item
|
||||
}
|
||||
|
||||
/// dummy attribute
|
||||
#[proc_macro_attribute]
|
||||
pub fn new(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
item
|
||||
}
|
||||
|
||||
fn args_to_owned(args: &PathArguments) -> PathArguments {
|
||||
match args {
|
||||
PathArguments::AngleBracketed(args) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue