summaryrefslogtreecommitdiff
path: root/mods/python-rust/src/lib.rs
blob: c056c172fe7d7df564e54962023f0c70b43367cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::env;

use rustpython::vm;

fn main() {
    let mut setting: vm::Settings = Default::default();
    setting.path_list.push("".to_string());
    let inter = vm::Interpreter::without_stdlib(setting);
    let ret: vm::PyResult<()> = inter.enter(|vm| {
        let scope = vm.new_scope_with_builtins();
        let source = r#"print("Hello World from python!")"#;
        let code_obj = vm
            .compile(source, vm::compiler::Mode::Exec, "<embedded>".to_owned())
            .map_err(|err| vm.new_syntax_error(&err, Some(source)))?;

        vm.unwrap_pyresult(vm.run_code_obj(code_obj, scope));

        Ok(())
    });
}

#[no_mangle]
pub fn fib(n: u32) -> u32 {
    if n <= 1 {
        main();
        return n;
    }
    return fib(n - 1) + fib(n - 2);
}