sample of using closure subroutines

This commit is contained in:
Viv Lim 2022-12-11 02:07:18 -08:00
parent a6ca0b7508
commit 7afab7df0a
2 changed files with 13 additions and 1 deletions

7
Cargo.lock generated
View File

@ -9,6 +9,12 @@ dependencies = [
"rust-guile",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "pkg-config"
version = "0.3.26"
@ -37,6 +43,7 @@ dependencies = [
name = "rust-guile"
version = "0.1.6"
dependencies = [
"lazy_static",
"pkg-config",
"thiserror",
]

View File

@ -1,11 +1,16 @@
use std::{sync::{atomic::{AtomicU8, Ordering}, Arc}};
use guile_goblins_rs::start_scm;
use rust_guile::api::GuileApi;
use rust_guile::api::{GuileApi, subroutines::register_subroutine};
fn main() {
let counter = Box::leak(Box::new(AtomicU8::new(0)));
// We want to run the contents of repl-init.scm before starting the repl, mostly to activate readline.
let to_run = |api: GuileApi| {
api.load_script("./repl-init.scm");
register_subroutine("idk", |a| {
println!("i'm a closure subroutine that's been called {} times.", counter.fetch_add(1, Ordering::Relaxed));
a
});
};
start_scm(vec![], to_run); // start guile with no args = repl
// note execution stops here, because guile exits the process.