astar-mumu 0.2.0-rc.4

A* algorithm plugin for the Lava language
Documentation
// FILE: astar/src/lib.rs

use mumu::{
    parser::interpreter::{Interpreter, DynamicFnInfo},
    parser::types::{FunctionValue, Value},
};
use std::{
    ffi::{c_void, CStr},
    sync::{Arc, Mutex},
};

mod astar;     // existing A* logic
mod maze;      // astar:maze & astar:maze_png
mod path_png;  // astar:path_png
mod png;       // astar:png
mod path;      // astar:path
mod boulders;  // astar:boulders (NEW)

#[export_name = "Cargo_lock"]
pub unsafe extern "C" fn cargo_lock(
    interp_ptr: *mut c_void,
    extra_str: *const c_void,
) -> i32 {
    if interp_ptr.is_null() {
        eprintln!("(astar) cargo_lock => null interp ptr");
        return 1;
    }
    let interp = &mut *(interp_ptr as *mut Interpreter);

    if !extra_str.is_null() && interp.is_verbose() {
        let c_s = CStr::from_ptr(extra_str as *const i8);
        eprintln!("(astar) cargo_lock => extra arg='{}'", c_s.to_string_lossy());
    }

    // 1) "astar:astar" bridging
    {
        let bridging = Arc::new(Mutex::new(move | _i: &mut Interpreter, args: Vec<Value> | {
            astar::astar_bridge(args)
        }));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:astar", info);
        interp.set_variable(
            "astar:astar",
            Value::Function(Box::new(FunctionValue::Named("astar:astar".to_string())))
        );
    }

    // 2) "astar:maze"
    {
        let bridging = Arc::new(Mutex::new(maze::astar_maze_bridge));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:maze", info);
        interp.set_variable(
            "astar:maze",
            Value::Function(Box::new(FunctionValue::Named("astar:maze".to_string())))
        );
    }

    // 3) "astar:maze_png"
    {
        let bridging = Arc::new(Mutex::new(maze::astar_maze_png_bridge));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:maze_png", info);
        interp.set_variable(
            "astar:maze_png",
            Value::Function(Box::new(FunctionValue::Named("astar:maze_png".to_string())))
        );
    }

    // 4) "astar:path_png"
    {
        let bridging = Arc::new(Mutex::new(path_png::astar_path_png_bridge));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:path_png", info);
        interp.set_variable(
            "astar:path_png",
            Value::Function(Box::new(FunctionValue::Named("astar:path_png".to_string())))
        );
    }

    // 5) "astar:png"
    {
        let bridging = Arc::new(Mutex::new(png::astar_png_bridge));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:png", info);
        interp.set_variable(
            "astar:png",
            Value::Function(Box::new(FunctionValue::Named("astar:png".to_string())))
        );
    }

    // 6) "astar:path"
    {
        let bridging = Arc::new(Mutex::new(path::astar_path_bridge));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:path", info);
        interp.set_variable(
            "astar:path",
            Value::Function(Box::new(FunctionValue::Named("astar:path".to_string())))
        );
    }

    // 7) "astar:boulders" (NEW)
    {
        let bridging = Arc::new(Mutex::new(boulders::astar_boulders_bridge));
        let info = DynamicFnInfo::new(bridging, false);
        interp.register_dynamic_function_ex("astar:boulders", info);
        interp.set_variable(
            "astar:boulders",
            Value::Function(Box::new(FunctionValue::Named("astar:boulders".to_string())))
        );
    }

    if interp.is_verbose() {
        eprintln!(
            "(astar) bridging => astar:astar, astar:maze, astar:maze_png, astar:path_png, astar:png, astar:path, astar:boulders"
        );
    }

    0
}