@@ -27,6 +27,9 @@ pub mod transpile;
2727pub mod ty;
2828pub mod varinfo;
2929
30+ #[ allow( unused) ]
31+ use erg_common:: config:: Package ;
32+
3033pub use build_hir:: { GenericHIRBuilder , HIRBuilder } ;
3134pub use erg_parser:: build_ast:: ASTBuilder ;
3235pub use transpile:: Transpiler ;
@@ -36,16 +39,22 @@ use pyo3::prelude::*;
3639#[ cfg( feature = "pylib" ) ]
3740use pyo3:: types:: { IntoPyDict , PyBytes } ;
3841
39- /// compile (code: str, mode: str) -> code
42+ /// compile_with_dependencies (code: str, mode: str, pkgs: list[Package] ) -> code
4043/// --
4144///
4245/// compile an Erg code as a module at runtime
4346#[ cfg( feature = "pylib" ) ]
4447#[ pyfunction]
45- #[ pyo3( name = "compile" ) ]
46- fn _compile ( py : Python < ' _ > , code : String , mode : & str ) -> Result < PyObject , error:: CompileErrors > {
48+ #[ pyo3( name = "compile_with_dependencies" ) ]
49+ fn _compile_with_dependencies (
50+ py : Python < ' _ > ,
51+ code : String ,
52+ mode : & str ,
53+ pkgs : Vec < Package > ,
54+ ) -> Result < PyObject , error:: CompileErrors > {
4755 use erg_common:: { config:: ErgConfig , traits:: Runnable } ;
48- let cfg = ErgConfig :: string ( code) ;
56+ let mut cfg = ErgConfig :: string ( code) ;
57+ cfg. packages = pkgs;
4958 let mut compiler = Compiler :: new ( cfg) ;
5059 let src = compiler. cfg_mut ( ) . input . read ( ) ;
5160 let code = compiler
@@ -59,6 +68,17 @@ fn _compile(py: Python<'_>, code: String, mode: &str) -> Result<PyObject, error:
5968 Ok ( code. into ( ) )
6069}
6170
71+ /// compile(code: str, mode: str) -> code
72+ /// --
73+ ///
74+ /// compile an Erg code as a module at runtime
75+ #[ cfg( feature = "pylib" ) ]
76+ #[ pyfunction]
77+ #[ pyo3( name = "compile" ) ]
78+ fn _compile ( py : Python < ' _ > , code : String , mode : & str ) -> Result < PyObject , error:: CompileErrors > {
79+ _compile_with_dependencies ( py, code, mode, vec ! [ ] )
80+ }
81+
6282/// compile_ast(ast: erg_parser.AST, mode: str) -> code
6383/// --
6484///
@@ -99,6 +119,22 @@ fn _compile_file(py: Python<'_>, path: String) -> Result<PyObject, error::Compil
99119 _compile ( py, code, "exec" )
100120}
101121
122+ /// compile_file_with_dependencies(path: str, pkgs: list[Package]) -> code
123+ /// --
124+ ///
125+ /// compile an Erg file as a module at runtime
126+ #[ cfg( feature = "pylib" ) ]
127+ #[ pyfunction]
128+ #[ pyo3( name = "compile_file_with_dependencies" ) ]
129+ fn _compile_file_with_dependencies (
130+ py : Python < ' _ > ,
131+ path : String ,
132+ pkgs : Vec < Package > ,
133+ ) -> Result < PyObject , error:: CompileErrors > {
134+ let code = std:: fs:: read_to_string ( & path) . unwrap_or_else ( |err| panic ! ( "{err}, path: {path}" ) ) ;
135+ _compile_with_dependencies ( py, code, "exec" , pkgs)
136+ }
137+
102138/// exec(code: str) -> module
103139/// --
104140///
@@ -145,9 +181,12 @@ fn _import(py: Python<'_>, name: String) -> Result<PyObject, error::CompileError
145181#[ cfg( feature = "pylib" ) ]
146182#[ pymodule]
147183fn erg_compiler ( py : Python < ' _ > , m : & PyModule ) -> PyResult < ( ) > {
184+ m. add_class :: < Package > ( ) ?;
148185 m. add_function ( wrap_pyfunction ! ( _compile, m) ?) ?;
186+ m. add_function ( wrap_pyfunction ! ( _compile_with_dependencies, m) ?) ?;
149187 m. add_function ( wrap_pyfunction ! ( _compile_ast, m) ?) ?;
150188 m. add_function ( wrap_pyfunction ! ( _compile_file, m) ?) ?;
189+ m. add_function ( wrap_pyfunction ! ( _compile_file_with_dependencies, m) ?) ?;
151190 m. add_function ( wrap_pyfunction ! ( _exec, m) ?) ?;
152191 m. add_function ( wrap_pyfunction ! ( _exec_ast, m) ?) ?;
153192 m. add_function ( wrap_pyfunction ! ( _import, m) ?) ?;
0 commit comments