1
0
forked from M-Labs/nac3

nac3embedded: compilation demo

This commit is contained in:
Sebastien Bourdeauducq 2020-12-19 16:23:12 +08:00
parent 75bd9b9a15
commit dd29ae251a
4 changed files with 16 additions and 3 deletions

2
Cargo.lock generated
View File

@ -459,6 +459,8 @@ dependencies = [
name = "nac3embedded"
version = "0.1.0"
dependencies = [
"inkwell",
"nac3core",
"pyo3",
"rustpython-parser",
]

View File

@ -10,4 +10,6 @@ crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.12.4", features = ["extension-module"] }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] }
rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" }
nac3core = { path = "../nac3core" }

View File

@ -3,8 +3,8 @@ from language import *
class Demo:
@kernel
def run(self):
pass
def run(self: bool) -> bool:
return False
if __name__ == "__main__":

View File

@ -4,6 +4,10 @@ use std::collections::hash_map::Entry;
use pyo3::prelude::*;
use pyo3::exceptions;
use rustpython_parser::{ast, parser};
use inkwell::context::Context;
use inkwell::targets::*;
use nac3core::CodeGen;
fn runs_on_core(decorator_list: &[ast::Expression]) -> bool {
for decorator in decorator_list.iter() {
@ -87,7 +91,11 @@ impl Nac3 {
decorator_list,
returns: _ } = &statement.node {
if runs_on_core(decorator_list) && funcdef_name == &name {
println!("found: {:?}", &statement.node);
let context = Context::create();
let mut codegen = CodeGen::new(&context);
codegen.compile_toplevel(&body[0]).map_err(|e|
exceptions::PyRuntimeError::new_err(format!("compilation failed: {}", e)))?;
codegen.print_ir();
}
}
}
@ -102,6 +110,7 @@ impl Nac3 {
#[pymodule]
fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> {
Target::initialize_all(&InitializationConfig::default());
m.add_class::<Nac3>()?;
Ok(())
}