Struct erg_compiler::artifact::CompleteArtifact
source · pub struct CompleteArtifact<Inner = HIR> {
pub object: Inner,
pub warns: CompileErrors,
}
Fields§
§object: Inner
§warns: CompileErrors
Implementations§
source§impl<Inner> CompleteArtifact<Inner>
impl<Inner> CompleteArtifact<Inner>
sourcepub const fn new(object: Inner, warns: CompileErrors) -> Self
pub const fn new(object: Inner, warns: CompileErrors) -> Self
Examples found in repository?
More examples
compile.rs (line 213)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
pub fn eval_compile_and_dump_as_pyc<P: AsRef<Path>>(
&mut self,
pyc_path: P,
src: String,
mode: &str,
) -> Result<CompleteArtifact<Option<Expr>>, ErrorArtifact> {
let arti = self.eval_compile(src, mode)?;
let (code, last) = arti.object;
code.dump_as_pyc(pyc_path, self.cfg.py_magic_num)
.expect("failed to dump a .pyc file (maybe permission denied)");
Ok(CompleteArtifact::new(last, arti.warns))
}
pub fn compile(
&mut self,
src: String,
mode: &str,
) -> Result<CompleteArtifact<CodeObj>, ErrorArtifact> {
log!(info "the compiling process has started.");
let arti = self.build_link_desugar(src, mode)?;
let codeobj = self.code_generator.emit(arti.object);
log!(info "code object:\n{}", codeobj.code_info(Some(self.code_generator.py_version)));
log!(info "the compiling process has completed");
Ok(CompleteArtifact::new(codeobj, arti.warns))
}
pub fn eval_compile(
&mut self,
src: String,
mode: &str,
) -> Result<CompleteArtifact<(CodeObj, Option<Expr>)>, ErrorArtifact> {
log!(info "the compiling process has started.");
let arti = self.build_link_desugar(src, mode)?;
let last = arti.object.module.last().cloned();
let codeobj = self.code_generator.emit(arti.object);
log!(info "code object:\n{}", codeobj.code_info(Some(self.code_generator.py_version)));
log!(info "the compiling process has completed");
Ok(CompleteArtifact::new((codeobj, last), arti.warns))
}
fn build_link_desugar(
&mut self,
src: String,
mode: &str,
) -> Result<CompleteArtifact, ErrorArtifact> {
let artifact = self.builder.build(src, mode)?;
let linker = Linker::new(&self.cfg, &self.mod_cache);
let hir = linker.link(artifact.object);
let desugared = HIRDesugarer::desugar(hir);
Ok(CompleteArtifact::new(desugared, artifact.warns))
}
transpile.rs (line 213)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
pub fn transpile(
&mut self,
src: String,
mode: &str,
) -> Result<CompleteArtifact<PyScript>, ErrorArtifact> {
log!(info "the transpiling process has started.");
let artifact = self.build_link_desugar(src, mode)?;
let script = self.script_generator.transpile(artifact.object);
log!(info "code:\n{}", script.code);
log!(info "the transpiling process has completed");
Ok(CompleteArtifact::new(script, artifact.warns))
}
fn build_link_desugar(
&mut self,
src: String,
mode: &str,
) -> Result<CompleteArtifact, ErrorArtifact> {
let artifact = self.builder.build(src, mode)?;
let linker = Linker::new(&self.cfg, &self.mod_cache);
let hir = linker.link(artifact.object);
let desugared = HIRDesugarer::desugar(hir);
Ok(CompleteArtifact::new(desugared, artifact.warns))
}
build_hir.rs (line 133)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
pub fn check(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {
let artifact = self.lowerer.lower(ast, mode)?;
let effect_checker = SideEffectChecker::new(self.cfg().clone());
let hir = effect_checker
.check(artifact.object)
.map_err(|(hir, errs)| {
self.lowerer.ctx.clear_invalid_vars();
IncompleteArtifact::new(Some(hir), errs, artifact.warns.clone())
})?;
let hir = self.ownership_checker.check(hir).map_err(|(hir, errs)| {
self.lowerer.ctx.clear_invalid_vars();
IncompleteArtifact::new(Some(hir), errs, artifact.warns.clone())
})?;
Ok(CompleteArtifact::new(hir, artifact.warns))
}
lower.rs (lines 1909-1912)
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
pub fn lower(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {
log!(info "the AST lowering process has started.");
log!(info "the type-checking process has started.");
let ast = Reorderer::new(self.cfg.clone())
.reorder(ast)
.map_err(|errs| {
IncompleteArtifact::new(None, errs, LowerWarnings::from(self.warns.take_all()))
})?;
if mode == "declare" {
let hir = self.declare_module(ast);
if self.errs.is_empty() {
log!(info "HIR:\n{hir}");
log!(info "the declaring process has completed.");
return Ok(CompleteArtifact::new(
hir,
LowerWarnings::from(self.warns.take_all()),
));
} else {
log!(err "the declaring process has failed.");
return Err(self.return_incomplete_artifact(hir));
}
}
let mut module = hir::Module::with_capacity(ast.module.len());
if let Err(errs) = self.ctx.preregister(ast.module.block()) {
self.errs.extend(errs);
}
for chunk in ast.module.into_iter() {
match self.lower_expr(chunk) {
Ok(chunk) => {
module.push(chunk);
}
Err(errs) => {
self.errs.extend(errs);
}
}
}
self.ctx.clear_invalid_vars();
self.ctx.check_decls().unwrap_or_else(|mut errs| {
self.errs.append(&mut errs);
});
let hir = HIR::new(ast.name, module);
log!(info "HIR (not resolved, current errs: {}):\n{hir}", self.errs.len());
let hir = match self.ctx.resolve(hir) {
Ok(hir) => {
log!(info "HIR (resolved):\n{hir}");
hir
}
Err((hir, errs)) => {
self.errs.extend(errs);
log!(err "the resolving process has failed. errs: {}", self.errs.len());
return Err(self.return_incomplete_artifact(hir));
}
};
// TODO: recursive check
for chunk in hir.module.iter() {
if let Err(warns) = self.use_check(chunk, mode) {
self.warns.extend(warns);
}
}
if self.errs.is_empty() {
log!(info "the AST lowering process has completed.");
Ok(CompleteArtifact::new(
hir,
LowerWarnings::from(self.warns.take_all()),
))
} else {
log!(err "the AST lowering process has failed. errs: {}", self.errs.len());
Err(self.return_incomplete_artifact(hir))
}
}