Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion symbiont-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["agent", "dylib", "dynamic", "harness", "llm"]
license = "MPL-2.0"
name = "symbiont-macros"
repository = "https://github.com/MathisWellmann/symbiont"
version = "0.3.0"
version = "0.4.0"

[lints]
workspace = true
Expand All @@ -16,6 +16,7 @@ workspace = true
proc-macro = true

[dependencies]
prettyplease = "0.2"
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["extra-traits", "full"] }
66 changes: 66 additions & 0 deletions symbiont-macros/src/evolvable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use syn::{
ForeignItemFn,
ItemFn,
Signature,
Visibility,
parse::{
Parse,
ParseStream,
},
};

/// A single function declaration inside `evolvable! { ... }`.
///
/// Supports two forms:
/// - With body: `fn step(counter: &mut usize) { *counter += 1; }`
/// - Without body: `fn step(counter: &mut usize);`
pub(crate) enum EvolvableFn {
WithBody(ItemFn),
WithoutBody(ForeignItemFn),
}

impl EvolvableFn {
pub(crate) fn sig(&self) -> &Signature {
match self {
EvolvableFn::WithBody(f) => &f.sig,
EvolvableFn::WithoutBody(f) => &f.sig,
}
}

pub(crate) fn vis(&self) -> &Visibility {
match self {
EvolvableFn::WithBody(f) => &f.vis,
EvolvableFn::WithoutBody(f) => &f.vis,
}
}

pub(crate) fn attrs(&self) -> &[syn::Attribute] {
match self {
EvolvableFn::WithBody(f) => &f.attrs,
EvolvableFn::WithoutBody(f) => &f.attrs,
}
}
}

/// The contents of an `evolvable! { ... }` block: zero or more function declarations.
#[expect(clippy::field_scoped_visibility_modifiers, reason = "Good here")]
pub(crate) struct EvolvableBlock {
pub(crate) functions: Vec<EvolvableFn>,
}

impl Parse for EvolvableBlock {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut functions = Vec::new();
while !input.is_empty() {
// Try parsing as a full function (with body) first
let fork = input.fork();
if fork.parse::<ItemFn>().is_ok() {
functions.push(EvolvableFn::WithBody(input.parse::<ItemFn>()?));
} else {
// Fall back to bodyless (foreign-style) declaration
functions.push(EvolvableFn::WithoutBody(input.parse::<ForeignItemFn>()?));
}
}
Ok(EvolvableBlock { functions })
}
}
90 changes: 90 additions & 0 deletions symbiont-macros/src/full_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use quote::quote;

use crate::evolvable::EvolvableFn;

/// Build the `full_source` string for the dylib from a function declaration.
///
/// Forces `pub` visibility and prepends `#[unsafe(no_mangle)]`.
pub(crate) fn build_full_source(func: &EvolvableFn) -> String {
let sig = func.sig();

// Keep the body as a TokenStream so `quote!` splices it as code, not as a string literal.
let body_tokens: proc_macro2::TokenStream = match func {
EvolvableFn::WithBody(f) => {
let block = &f.block;
quote!(#block)
}
EvolvableFn::WithoutBody(_) => quote!({ todo!() }),
};

let inputs = &sig.inputs;
let output = &sig.output;
let ident = &sig.ident;

// Preserve doc comments on the generated function so they are available in the
// dylib's source for tooling and documentation purposes. Render them as `///`
// line comments rather than `#[doc = "..."]` attributes for readability.
use std::fmt::Write as _;
let doc_lines: String = func
.attrs()
.iter()
.filter(|attr| attr.path().is_ident("doc"))
.filter_map(extract_doc_string)
.fold(String::new(), |mut acc, line| {
let _ = writeln!(acc, "///{line}");
acc
});

let fn_tokens = quote! {
#[unsafe(no_mangle)]
pub fn #ident(#inputs) #output #body_tokens
};

// `quote!` above always produces a valid `fn` item, so parsing as a
// `syn::File` is infallible. Run it through `prettyplease` so the emitted
// source reads like hand-written Rust rather than a single token-spaced line.
let file = syn::parse2::<syn::File>(fn_tokens).expect("generated fn is valid Rust");
let formatted = prettyplease::unparse(&file);

format!("{doc_lines}{formatted}")
}

/// Extract the string value from a `#[doc = "..."]` attribute.
fn extract_doc_string(attr: &syn::Attribute) -> Option<String> {
if let syn::Meta::NameValue(nv) = &attr.meta
&& let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(s),
..
}) = &nv.value
{
return Some(s.value());
}
None
}

#[cfg(test)]
mod tests {
use super::*;
use crate::EvolvableBlock;

#[test]
fn test_build_full_source() {
let func = quote!(
/// Should increment the counter by a value in the range 5..20
fn step(counter: &mut usize) {
*counter += 1;
println!("doing stuff in iteration {}", counter);
}
);
let block: EvolvableBlock = syn::parse2(func).expect("parse evolvable block");
assert_eq!(
build_full_source(&block.functions[0]),
"/// Should increment the counter by a value in the range 5..20\n\
#[unsafe(no_mangle)]\n\
pub fn step(counter: &mut usize) {\n \
*counter += 1;\n \
println!(\"doing stuff in iteration {}\", counter);\n\
}\n",
);
}
}
122 changes: 7 additions & 115 deletions symbiont-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
//! Provides the [`evolvable!`] function-like macro that declares
//! hot-reloadable functions and generates dispatch wrappers.

mod evolvable;
mod full_source;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{
Expand All @@ -12,71 +15,14 @@ use quote::{
};
use syn::{
FnArg,
ForeignItemFn,
ItemFn,
ReturnType,
Signature,
Visibility,
parse::{
Parse,
ParseStream,
},
};

/// A single function declaration inside `evolvable! { ... }`.
///
/// Supports two forms:
/// - With body: `fn step(counter: &mut usize) { *counter += 1; }`
/// - Without body: `fn step(counter: &mut usize);`
enum EvolvableFn {
WithBody(ItemFn),
WithoutBody(ForeignItemFn),
}

impl EvolvableFn {
fn sig(&self) -> &Signature {
match self {
EvolvableFn::WithBody(f) => &f.sig,
EvolvableFn::WithoutBody(f) => &f.sig,
}
}

fn vis(&self) -> &Visibility {
match self {
EvolvableFn::WithBody(f) => &f.vis,
EvolvableFn::WithoutBody(f) => &f.vis,
}
}

fn attrs(&self) -> &[syn::Attribute] {
match self {
EvolvableFn::WithBody(f) => &f.attrs,
EvolvableFn::WithoutBody(f) => &f.attrs,
}
}
}

/// The contents of an `evolvable! { ... }` block: zero or more function declarations.
struct EvolvableBlock {
functions: Vec<EvolvableFn>,
}

impl Parse for EvolvableBlock {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut functions = Vec::new();
while !input.is_empty() {
// Try parsing as a full function (with body) first
let fork = input.fork();
if fork.parse::<ItemFn>().is_ok() {
functions.push(EvolvableFn::WithBody(input.parse::<ItemFn>()?));
} else {
// Fall back to bodyless (foreign-style) declaration
functions.push(EvolvableFn::WithoutBody(input.parse::<ForeignItemFn>()?));
}
}
Ok(EvolvableBlock { functions })
}
}
use crate::{
evolvable::EvolvableBlock,
full_source::build_full_source,
};

/// Format a `syn::Signature` into a human-readable string like `"fn step(counter: &mut usize)"`.
///
Expand Down Expand Up @@ -124,60 +70,6 @@ fn normalize_tokens(mut s: String) -> String {
s
}

/// Extract the string value from a `#[doc = "..."]` attribute.
fn extract_doc_string(attr: &syn::Attribute) -> Option<String> {
if let syn::Meta::NameValue(nv) = &attr.meta
&& let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(s),
..
}) = &nv.value
{
return Some(s.value());
}
None
}

/// Build the `full_source` string for the dylib from a function declaration.
///
/// Forces `pub` visibility and prepends `#[unsafe(no_mangle)]`.
fn build_full_source(func: &EvolvableFn) -> String {
let sig = func.sig();

// Keep the body as a TokenStream so `quote!` splices it as code, not as a string literal.
let body_tokens: proc_macro2::TokenStream = match func {
EvolvableFn::WithBody(f) => {
let block = &f.block;
quote!(#block)
}
EvolvableFn::WithoutBody(_) => quote!({ todo!() }),
};

let inputs = &sig.inputs;
let output = &sig.output;
let ident = &sig.ident;

// Preserve doc comments on the generated function so they are available in the
// dylib's source for tooling and documentation purposes. Render them as `///`
// line comments rather than `#[doc = "..."]` attributes for readability.
use std::fmt::Write as _;
let doc_lines: String = func
.attrs()
.iter()
.filter(|attr| attr.path().is_ident("doc"))
.filter_map(extract_doc_string)
.fold(String::new(), |mut acc, line| {
let _ = writeln!(acc, "///{line}");
acc
});

let fn_body = quote! {
#[unsafe(no_mangle)]
pub fn #ident(#inputs) #output #body_tokens
};

format!("{doc_lines}{fn_body}\n").trim_start().to_string()
}

/// Declare hot-reloadable functions that are compiled into a temporary dylib and loaded at runtime.
///
/// # Examples
Expand Down
4 changes: 2 additions & 2 deletions symbiont/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ license = "MPL-2.0"
name = "symbiont"
readme = "README.md"
repository = "https://github.com/MathisWellmann/symbiont"
version = "0.3.0"
version = "0.4.0"

[lints]
workspace = true

[dependencies]
symbiont-macros = { version = "0.3.0", path = "../symbiont-macros" }
symbiont-macros = { version = "0.4.0", path = "../symbiont-macros" }

rig-core.workspace = true
tokio.workspace = true
Expand Down
Loading
Loading