Skip to main content

mumuastar/
lib.rs

1// FILE: astar/src/lib.rs
2
3use mumu::{
4    parser::interpreter::{Interpreter, DynamicFnInfo},
5    parser::types::{FunctionValue, Value},
6};
7use std::{
8    ffi::{c_void, CStr},
9    sync::{Arc, Mutex},
10};
11
12mod astar;     // existing A* logic
13mod maze;      // astar:maze & astar:maze_png
14mod path_png;  // astar:path_png
15mod png;       // astar:png
16mod path;      // astar:path
17mod boulders;  // astar:boulders (NEW)
18
19#[export_name = "Cargo_lock"]
20pub unsafe extern "C" fn cargo_lock(
21    interp_ptr: *mut c_void,
22    extra_str: *const c_void,
23) -> i32 {
24    if interp_ptr.is_null() {
25        eprintln!("(astar) cargo_lock => null interp ptr");
26        return 1;
27    }
28    let interp = &mut *(interp_ptr as *mut Interpreter);
29
30    if !extra_str.is_null() && interp.is_verbose() {
31        let c_s = CStr::from_ptr(extra_str as *const i8);
32        eprintln!("(astar) cargo_lock => extra arg='{}'", c_s.to_string_lossy());
33    }
34
35    // 1) "astar:astar" bridging
36    {
37        let bridging = Arc::new(Mutex::new(move | _i: &mut Interpreter, args: Vec<Value> | {
38            astar::astar_bridge(args)
39        }));
40        let info = DynamicFnInfo::new(bridging, false);
41        interp.register_dynamic_function_ex("astar:astar", info);
42        interp.set_variable(
43            "astar:astar",
44            Value::Function(Box::new(FunctionValue::Named("astar:astar".to_string())))
45        );
46    }
47
48    // 2) "astar:maze"
49    {
50        let bridging = Arc::new(Mutex::new(maze::astar_maze_bridge));
51        let info = DynamicFnInfo::new(bridging, false);
52        interp.register_dynamic_function_ex("astar:maze", info);
53        interp.set_variable(
54            "astar:maze",
55            Value::Function(Box::new(FunctionValue::Named("astar:maze".to_string())))
56        );
57    }
58
59    // 3) "astar:maze_png"
60    {
61        let bridging = Arc::new(Mutex::new(maze::astar_maze_png_bridge));
62        let info = DynamicFnInfo::new(bridging, false);
63        interp.register_dynamic_function_ex("astar:maze_png", info);
64        interp.set_variable(
65            "astar:maze_png",
66            Value::Function(Box::new(FunctionValue::Named("astar:maze_png".to_string())))
67        );
68    }
69
70    // 4) "astar:path_png"
71    {
72        let bridging = Arc::new(Mutex::new(path_png::astar_path_png_bridge));
73        let info = DynamicFnInfo::new(bridging, false);
74        interp.register_dynamic_function_ex("astar:path_png", info);
75        interp.set_variable(
76            "astar:path_png",
77            Value::Function(Box::new(FunctionValue::Named("astar:path_png".to_string())))
78        );
79    }
80
81    // 5) "astar:png"
82    {
83        let bridging = Arc::new(Mutex::new(png::astar_png_bridge));
84        let info = DynamicFnInfo::new(bridging, false);
85        interp.register_dynamic_function_ex("astar:png", info);
86        interp.set_variable(
87            "astar:png",
88            Value::Function(Box::new(FunctionValue::Named("astar:png".to_string())))
89        );
90    }
91
92    // 6) "astar:path"
93    {
94        let bridging = Arc::new(Mutex::new(path::astar_path_bridge));
95        let info = DynamicFnInfo::new(bridging, false);
96        interp.register_dynamic_function_ex("astar:path", info);
97        interp.set_variable(
98            "astar:path",
99            Value::Function(Box::new(FunctionValue::Named("astar:path".to_string())))
100        );
101    }
102
103    // 7) "astar:boulders" (NEW)
104    {
105        let bridging = Arc::new(Mutex::new(boulders::astar_boulders_bridge));
106        let info = DynamicFnInfo::new(bridging, false);
107        interp.register_dynamic_function_ex("astar:boulders", info);
108        interp.set_variable(
109            "astar:boulders",
110            Value::Function(Box::new(FunctionValue::Named("astar:boulders".to_string())))
111        );
112    }
113
114    if interp.is_verbose() {
115        eprintln!(
116            "(astar) bridging => astar:astar, astar:maze, astar:maze_png, astar:path_png, astar:png, astar:path, astar:boulders"
117        );
118    }
119
120    0
121}