Skip to content

Commit

Permalink
feat: add erg_compiler::compile_with_dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Feb 14, 2024
1 parent 121738d commit 1762588
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/erg_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 19 additions & 0 deletions crates/erg_common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -92,6 +97,7 @@ impl From<&str> for TranspileTarget {
}
}

#[pyclass]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Package {
pub name: &'static str,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}

Expand Down
47 changes: 43 additions & 4 deletions crates/erg_compiler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
/// --
///
Expand Down Expand Up @@ -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
/// --
///
Expand Down Expand Up @@ -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)?)?;
Expand Down
1 change: 0 additions & 1 deletion crates/erg_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 6 additions & 0 deletions crates/erg_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 1762588

Please sign in to comment.