MCPcopy Create free account
hub / github.com/Notgnoshi/generative / build_expression

Function build_expression

tools/bitwise.rs:58–82  ·  view source on GitHub ↗
(expr: &str)

Source from the content-addressed store, hash-verified

56type BitwiseExpression = Box<dyn Fn(i64, i64) -> i64>;
57
58fn build_expression(expr: &str) -> eyre::Result<BitwiseExpression> {
59 // Ok(Box::new(|x, y| (x & y) & ((x ^ y) % 13)))
60
61 let context = rune::Context::with_default_modules()?;
62 let runtime = rune::sync::Arc::try_new(context.runtime()?)?;
63 let mut script = rune::Sources::new();
64 script.insert(build_function(expr)?)?;
65 let mut diagnostics = rune::Diagnostics::new();
66 let maybe_unit = rune::prepare(&mut script)
67 .with_context(&context)
68 .with_diagnostics(&mut diagnostics)
69 .build();
70 if !diagnostics.is_empty() {
71 let mut writer =
72 rune::termcolor::StandardStream::stderr(rune::termcolor::ColorChoice::Always);
73 diagnostics.emit(&mut writer, &script)?;
74 }
75 let unit = rune::sync::Arc::try_new(maybe_unit?)?;
76 let vm = rune::Vm::new(runtime, unit);
77 let eval_expr = vm.lookup_function(["eval_expr"])?;
78 let eval_expr = move |x: i64, y: i64| -> i64 {
79 eval_expr.call((x, y)).expect("Failed to call eval_expr()")
80 };
81 Ok(Box::new(eval_expr))
82}
83
84fn build_function(expr: &str) -> eyre::Result<rune::Source> {
85 let lines = ["pub fn eval_expr(x, y) {", expr, "}"];

Callers 1

mainFunction · 0.85

Calls 2

build_functionFunction · 0.85
buildMethod · 0.80

Tested by

no test coverage detected