summaryrefslogtreecommitdiff
path: root/src/guile
diff options
context:
space:
mode:
Diffstat (limited to 'src/guile')
-rw-r--r--src/guile/guile.rs75
-rw-r--r--src/guile/guiledefs.c1
2 files changed, 70 insertions, 6 deletions
diff --git a/src/guile/guile.rs b/src/guile/guile.rs
index 36df62a..a459a64 100644
--- a/src/guile/guile.rs
+++ b/src/guile/guile.rs
@@ -1,13 +1,41 @@
+use crate::macro_processor::macro_processor::MacroProcessor;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
+use std::sync::mpsc;
+use std::thread;
+
+macro_rules! dprint {
+ ($($x:tt)*) => {
+ #[cfg(debug_assertions)]
+ print!($($x)*)
+ }
+}
+
#[link(name = "guile-3.0")]
extern "C" {
fn scm_init_guile();
fn scm_c_eval_string(expr: *const c_char) -> *mut c_void;
+ fn scm_from_utf8_string(expr: *const c_char) -> *mut c_void;
fn scm_to_locale_string(scm_obj: *mut c_void) -> *const c_char;
fn scm_is_string(scm_obj: *mut c_void) -> c_int;
fn scm_object_to_string(scm_obj: *mut c_void, printer: *mut c_void) -> *mut c_void;
+ fn scm_to_pointer(scm_obj: *mut c_void) -> *mut c_void;
+ fn scm_c_define_gsubr(
+ name: *const c_char,
+ req: c_int,
+ opt: c_int,
+ rst: c_int,
+ func: extern "C" fn(scm_obj: *mut c_void) -> *mut c_void,
+ );
+ fn scm_c_define(name: *const c_char, value: *mut c_void);
+ fn scm_from_pointer(value: *mut c_void) -> *mut c_void;
+ //fn scm_with_guile(func: extern "C" fn(data: *mut c_void) -> *mut c_void, data: *mut c_void) -> *mut c_void;
+ //fn scm_is_number(scm_obj: *mut c_void) -> c_int;
+ //fn scm_without_guile(func: extern "C" fn(data: *mut c_void) -> *mut c_void, data: *mut c_void) -> *mut c_void;
+ //fn scm_c_write(scm_obj: *mut c_void, port: *mut c_void);
+ //fn scm_get_output_string(port: *mut c_void) -> *mut c_void;
+ //fn scm_open_output_string() -> *mut c_void;
}
#[link(name = "guiledefs")]
@@ -44,7 +72,30 @@ fn string_from_scm(scm_obj: *mut c_void) -> Result<String, ()> {
}
}
-#[derive(Clone)]
+extern "C" fn scm_smp_macro(arg: *mut c_void) -> *mut c_void {
+ let arg_str;
+ unsafe {
+ arg_str = CStr::from_ptr(scm_to_locale_string(arg))
+ .to_string_lossy()
+ .into_owned();
+ }
+ dprint!("ARG {:#?}\n", arg_str);
+ let c_smp_state_ptr = CString::new("smp_state_ptr").expect("CString::new() failed");
+ unsafe {
+ let smp_state_scm = scm_c_eval_string(c_smp_state_ptr.as_ptr());
+ let smp_state_ptr = scm_to_pointer(smp_state_scm);
+ let smp: &mut MacroProcessor = &mut *(smp_state_ptr as *mut MacroProcessor);
+ if let Some(macro_value) = smp.macros.get(&arg_str) {
+ let r = CString::new(macro_value.to_string()).expect("CString::new() failed");
+ scm_from_utf8_string(r.as_ptr())
+ } else {
+ let r = CString::new("Macro not found").expect("CString::new() failed");
+ scm_from_utf8_string(r.as_ptr())
+ }
+ }
+}
+
+#[derive(Debug)]
pub struct Guile {}
impl Guile {
@@ -52,10 +103,16 @@ impl Guile {
unsafe {
scm_init_guile();
}
- Guile {}
+ let guile = Guile {};
+ let func_name = CString::new("smp_get").unwrap();
+ unsafe {
+ scm_c_define_gsubr(func_name.as_ptr(), 1, 0, 0, scm_smp_macro);
+ }
+ guile
}
pub fn evaluate_expression(&self, expr: &str) -> Result<String, ()> {
+ dprint!("(eval \"{}\")\n", expr);
unsafe {
let c_expr = CString::new(expr).map_err(|_| ())?;
let result = scm_c_eval_string(c_expr.as_ptr());
@@ -70,13 +127,19 @@ impl Guile {
defs_scm_define_string(c_name.as_ptr(), c_value.as_ptr());
}
}
+
+ pub fn define(&self, name: &str, value: *mut c_void) {
+ let c_name = CString::new(name).expect("CString::new failed");
+ unsafe {
+ scm_c_define(c_name.as_ptr(), scm_from_pointer(value));
+ }
+ }
}
impl Drop for Guile {
fn drop(&mut self) {
- if let Err(e) = self.evaluate_expression("(exit)") {
- panic!("Error while exiting {:#?}", e);
- }
+ //if let Err(e) = self.evaluate_expression("(variable-unset! \"smp_state_ptr\")") {
+ // panic!("Error while exiting {:#?}", e);
+ //}
}
}
-
diff --git a/src/guile/guiledefs.c b/src/guile/guiledefs.c
index a0e9bd2..bf050af 100644
--- a/src/guile/guiledefs.c
+++ b/src/guile/guiledefs.c
@@ -10,3 +10,4 @@ int defs_scm_is_eq(SCM x, SCM y) {
void defs_scm_define_string(const char* name, const char* value) {
scm_c_define(name, scm_from_utf8_string(value));
}
+