r2rust_core/environment.rs
1/// Environment module for variable management.
2///
3/// This module provides a key-value store for variables and their values
4/// within the interpreter.
5
6use std::collections::HashMap;
7
8/// Represents the variable storage environment.
9pub struct Environment {
10 /// A mapping of variable names to their values.
11 variables: HashMap<String, f64>,
12}
13
14impl Default for Environment {
15 /// Provides a default implementation for creating an empty environment.
16 fn default() -> Self {
17 Self::new()
18 }
19}
20
21impl Environment {
22 /// Creates a new, empty environment.
23 pub fn new() -> Self {
24 Environment {
25 variables: HashMap::new(),
26 }
27 }
28
29 /// Retrieves the value of a variable.
30 ///
31 /// # Arguments
32 /// * `var` - The name of the variable.
33 ///
34 /// # Returns
35 /// An optional reference to the value of the variable.
36 pub fn get(&self, var: &str) -> Option<&f64> {
37 self.variables.get(var)
38 }
39
40 /// Sets a variable to a specific value.
41 ///
42 /// # Arguments
43 /// * `var` - The name of the variable.
44 /// * `value` - The value to assign.
45 pub fn set(&mut self, var: String, value: f64) {
46 self.variables.insert(var, value);
47 }
48}