132 lines
4.2 KiB
Rust
132 lines
4.2 KiB
Rust
// examples/selective_registration.rs
|
|
use steel_decimal::{SteelDecimal, FunctionRegistryBuilder};
|
|
use steel::steel_vm::engine::Engine;
|
|
use std::collections::HashMap;
|
|
|
|
fn main() {
|
|
let steel_decimal = SteelDecimal::new();
|
|
|
|
println!("=== Basic Arithmetic Only ===");
|
|
let mut vm1 = Engine::new();
|
|
FunctionRegistryBuilder::new()
|
|
.basic_arithmetic(true)
|
|
.advanced_math(false)
|
|
.trigonometric(false)
|
|
.comparison(false)
|
|
.utility(false)
|
|
.constants(false)
|
|
.financial(false)
|
|
.register(&mut vm1);
|
|
|
|
// Test basic arithmetic
|
|
let script = "(+ 10.5 20.3)";
|
|
let transformed = steel_decimal.transform(script);
|
|
match vm1.compile_and_run_raw_program(transformed) {
|
|
Ok(results) => {
|
|
if let Some(result) = results.last() {
|
|
println!("Basic arithmetic result: {:?}", result);
|
|
}
|
|
}
|
|
Err(e) => println!("Error: {}", e),
|
|
}
|
|
|
|
println!("\n=== With Advanced Math ===");
|
|
let mut vm2 = Engine::new();
|
|
FunctionRegistryBuilder::new()
|
|
.basic_arithmetic(true)
|
|
.advanced_math(true)
|
|
.trigonometric(false)
|
|
.register(&mut vm2);
|
|
|
|
// Test power function
|
|
let power_script = "(^ 2.0 3.0)";
|
|
let power_transformed = steel_decimal.transform(power_script);
|
|
match vm2.compile_and_run_raw_program(power_transformed) {
|
|
Ok(results) => {
|
|
if let Some(result) = results.last() {
|
|
println!("Power result: {:?}", result);
|
|
}
|
|
}
|
|
Err(e) => println!("Error: {}", e),
|
|
}
|
|
|
|
// Test square root
|
|
let sqrt_script = "(sqrt 16.0)";
|
|
let sqrt_transformed = steel_decimal.transform(sqrt_script);
|
|
match vm2.compile_and_run_raw_program(sqrt_transformed) {
|
|
Ok(results) => {
|
|
if let Some(result) = results.last() {
|
|
println!("Square root result: {:?}", result);
|
|
}
|
|
}
|
|
Err(e) => println!("Error: {}", e),
|
|
}
|
|
|
|
println!("\n=== With Variables ===");
|
|
let mut variables = HashMap::new();
|
|
variables.insert("radius".to_string(), "5.0".to_string());
|
|
variables.insert("pi".to_string(), "3.14159".to_string());
|
|
|
|
let mut vm3 = Engine::new();
|
|
FunctionRegistryBuilder::new()
|
|
.basic_arithmetic(true)
|
|
.advanced_math(true)
|
|
.constants(true)
|
|
.with_variables(variables)
|
|
.register(&mut vm3);
|
|
|
|
// Calculate area of circle using variables
|
|
let area_script = "(* $pi (* $radius $radius))";
|
|
let area_transformed = steel_decimal.transform(area_script);
|
|
println!("Area script: {}", area_script);
|
|
println!("Transformed: {}", area_transformed);
|
|
|
|
match vm3.compile_and_run_raw_program(area_transformed) {
|
|
Ok(results) => {
|
|
if let Some(result) = results.last() {
|
|
println!("Circle area result: {:?}", result);
|
|
}
|
|
}
|
|
Err(e) => println!("Error: {}", e),
|
|
}
|
|
|
|
println!("\n=== Financial Functions ===");
|
|
let mut vm4 = Engine::new();
|
|
FunctionRegistryBuilder::new()
|
|
.basic_arithmetic(true)
|
|
.financial(true)
|
|
.register(&mut vm4);
|
|
|
|
// Test percentage calculation
|
|
let percent_script = r#"(decimal-percentage "1000.00" "15.0")"#;
|
|
match vm4.compile_and_run_raw_program(percent_script.to_string()) {
|
|
Ok(results) => {
|
|
if let Some(result) = results.last() {
|
|
println!("15% of 1000: {:?}", result);
|
|
}
|
|
}
|
|
Err(e) => println!("Error: {}", e),
|
|
}
|
|
|
|
// Test compound interest
|
|
let compound_script = r#"(decimal-compound "1000.00" "0.05" "10.0")"#;
|
|
match vm4.compile_and_run_raw_program(compound_script.to_string()) {
|
|
Ok(results) => {
|
|
if let Some(result) = results.last() {
|
|
println!("Compound interest (1000 @ 5% for 10 years): {:?}", result);
|
|
}
|
|
}
|
|
Err(e) => println!("Error: {}", e),
|
|
}
|
|
|
|
println!("\n=== Available Functions ===");
|
|
let function_names = steel_decimal::FunctionRegistry::get_function_names();
|
|
for (i, name) in function_names.iter().enumerate() {
|
|
if i % 5 == 0 {
|
|
println!();
|
|
}
|
|
print!("{:<18}", name);
|
|
}
|
|
println!();
|
|
}
|