stuff around publishing crate successfuly done
This commit is contained in:
61
steel_decimal/examples/basic_usage.rs
Normal file
61
steel_decimal/examples/basic_usage.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
// examples/basic_usage.rs
|
||||
use steel_decimal::SteelDecimal;
|
||||
use steel::steel_vm::engine::Engine;
|
||||
|
||||
fn main() {
|
||||
// Create a new Steel Decimal engine
|
||||
let steel_decimal = SteelDecimal::new();
|
||||
|
||||
// Transform a simple math expression
|
||||
let script = "(+ 1.5 2.3)";
|
||||
let transformed = steel_decimal.transform(script);
|
||||
println!("Original: {}", script);
|
||||
println!("Transformed: {}", transformed);
|
||||
|
||||
// Create Steel VM and register functions
|
||||
let mut vm = Engine::new();
|
||||
steel_decimal.register_functions(&mut vm);
|
||||
|
||||
// Execute the transformed script
|
||||
match vm.compile_and_run_raw_program(transformed) {
|
||||
Ok(results) => {
|
||||
println!("Results: {:?}", results);
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Final result: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Try a more complex expression
|
||||
let complex_script = "(+ (* 2.5 3.0) (/ 15.0 3.0))";
|
||||
let complex_transformed = steel_decimal.transform(complex_script);
|
||||
println!("\nComplex original: {}", complex_script);
|
||||
println!("Complex transformed: {}", complex_transformed);
|
||||
|
||||
match vm.compile_and_run_raw_program(complex_transformed) {
|
||||
Ok(results) => {
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Complex result: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Using the convenience method
|
||||
println!("\nUsing convenience method:");
|
||||
match steel_decimal.parse_and_execute("(+ 10.5 20.3)") {
|
||||
Ok(results) => {
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Convenience result: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
131
steel_decimal/examples/selective_registration.rs
Normal file
131
steel_decimal/examples/selective_registration.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
// 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!();
|
||||
}
|
||||
73
steel_decimal/examples/with_variables.rs
Normal file
73
steel_decimal/examples/with_variables.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// examples/with_variables.rs
|
||||
use steel_decimal::SteelDecimal;
|
||||
use steel::steel_vm::engine::Engine;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
// Create variables
|
||||
let mut variables = HashMap::new();
|
||||
variables.insert("price".to_string(), "29.99".to_string());
|
||||
variables.insert("quantity".to_string(), "5".to_string());
|
||||
variables.insert("tax_rate".to_string(), "0.08".to_string());
|
||||
|
||||
// Create Steel Decimal with variables
|
||||
let steel_decimal = SteelDecimal::with_variables(variables);
|
||||
|
||||
// Script using variables
|
||||
let script = "(+ (* $price $quantity) (* (* $price $quantity) $tax_rate))";
|
||||
let transformed = steel_decimal.transform(script);
|
||||
|
||||
println!("Original script: {}", script);
|
||||
println!("Transformed script: {}", transformed);
|
||||
|
||||
// Create VM and register functions
|
||||
let mut vm = Engine::new();
|
||||
steel_decimal.register_functions(&mut vm);
|
||||
|
||||
// Execute the script
|
||||
match vm.compile_and_run_raw_program(transformed) {
|
||||
Ok(results) => {
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Total with tax: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Demonstrate adding variables dynamically
|
||||
let mut steel_decimal = SteelDecimal::new();
|
||||
steel_decimal.add_variable("x".to_string(), "10.5".to_string());
|
||||
steel_decimal.add_variable("y".to_string(), "20.3".to_string());
|
||||
|
||||
let simple_script = "(+ $x $y)";
|
||||
println!("\nSimple script: {}", simple_script);
|
||||
|
||||
match steel_decimal.parse_and_execute(simple_script) {
|
||||
Ok(results) => {
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Simple result: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Show variable validation
|
||||
println!("\nVariable validation:");
|
||||
match steel_decimal.validate_script("(+ $x $y)") {
|
||||
Ok(()) => println!("Script is valid"),
|
||||
Err(e) => println!("Script error: {}", e),
|
||||
}
|
||||
|
||||
match steel_decimal.validate_script("(+ $x $undefined_var)") {
|
||||
Ok(()) => println!("Script is valid"),
|
||||
Err(e) => println!("Script error: {}", e),
|
||||
}
|
||||
|
||||
// Extract dependencies
|
||||
let dependencies = steel_decimal.extract_dependencies("(+ $x $y $z)");
|
||||
println!("Dependencies: {:?}", dependencies);
|
||||
}
|
||||
Reference in New Issue
Block a user