Repository navigation
Expand file tree
/
Copy pathmatch_control.rs
More file actions
55 lines (51 loc) · 1.53 KB
/
Copy pathmatch_control.rs
File metadata and controls
55 lines (51 loc) · 1.53 KB
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
// File Version: 1.1.0
// /src/match_control.rs
use crate::types::{Program, Statement};
use crate::data_types::Value;
use crate::operators;
use std::collections::HashMap;
fn resolve(token: &str, globals: &HashMap<String, Value>, locals: &HashMap<String, Value>) -> Result<Value, String> {
if let Some(val) = locals.get(token) {
return Ok(val.clone());
}
if let Some(val) = globals.get(token) {
return Ok(val.clone());
}
Value::infer(token)
}
pub fn execute(
pc: &mut usize,
var_name: &str,
program: &Program,
globals: &HashMap<String, Value>,
locals: &HashMap<String, Value>
) -> Result<(), String> {
let val = resolve(var_name, globals, locals)?;
let mut default_pc: Option<usize> = None;
let mut scan_pc = *pc + 1;
while scan_pc < program.statements.len() {
match &program.statements[scan_pc] {
Statement::Case { value } => {
let case_val = Value::infer(value)?;
if operators::perform_comparison(&val, "==", &case_val)? {
*pc = scan_pc + 1;
return Ok(());
}
},
Statement::Default => {
default_pc = Some(scan_pc + 1);
},
Statement::EndMatch => {
if let Some(def) = default_pc {
*pc = def;
} else {
*pc = scan_pc;
}
return Ok(());
}
_ => {}
}
scan_pc += 1;
}
Ok(())
}