1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::function_table::FunctionTable;
use crate::variable_stack::VariableStack;
use crate::variable_storage::VariableStorage;
use crate::variables::Variable;
#[derive(Debug)]
pub struct Machine {
function_table: FunctionTable,
variable_storage: VariableStorage,
}
impl Machine {
pub fn new() -> Self {
Self {
function_table: FunctionTable::new(),
variable_storage: VariableStorage::new(),
}
}
pub fn load_variable_storage( &mut self, filename: &str ) -> Result< (), Box< dyn std::error::Error > >{
match VariableStorage::load( filename ) {
Ok( vs ) => {
self.variable_storage = vs;
Ok(())
},
Err( e ) => {
Err( e )
},
}
}
pub fn save_variable_storage( &mut self, filename: &str ) -> Result< (), Box< dyn std::error::Error > >{
match self.variable_storage.save( filename ) {
Ok( _ ) => {
Ok(())
},
Err( e ) => {
Err( e )
},
}
}
pub fn get_mut_variable_storage( &mut self ) -> &mut VariableStorage {
&mut self.variable_storage
}
pub fn get_variable_storage( &self ) -> &VariableStorage {
&self.variable_storage
}
pub fn get_mut_function_table( &mut self ) -> &mut FunctionTable {
&mut self.function_table
}
pub fn get_function_table( &self ) -> &FunctionTable {
&self.function_table
}
pub fn call_function( &mut self, name: &str, argc: u16, stack: &mut VariableStack ) -> bool {
if let Some( f ) = self.function_table.find( name ) {
f( argc, stack, &mut self.variable_storage )
} else {
println!("Function not found for CallFunction {} with {} arguments", name, argc );
for _ in 0..argc {
stack.pop();
};
stack.push( Variable::I32( argc as i32 ) );
false
}
}
}