Skip to main content

zoisite/
scope.rs

1use std::collections::HashMap;
2
3use ecow::EcoString;
4
5use crate::resolve_context::Place;
6
7pub struct Scope {
8    variables: HashMap<EcoString, VarId>,
9    functions: HashMap<(EcoString, usize), FnId>,
10    pub place: Place,
11}
12
13impl Scope {
14    pub fn new(place: Place) -> Self {
15        Scope {
16            variables: HashMap::new(),
17            functions: HashMap::new(),
18            place,
19        }
20    }
21    pub fn define_var(&mut self, name: EcoString, var_id: VarId) {
22        self.variables.insert(name, var_id);
23    }
24    pub fn define_fn(&mut self, name: EcoString, num_params: usize, fn_id: FnId) {
25        self.functions.insert((name, num_params), fn_id);
26    }
27    pub fn resolve_var(&self, name: &EcoString) -> Option<VarId> {
28        self.variables.get(name).copied()
29    }
30    pub fn resolve_fn(&self, name: &EcoString, num_params: usize) -> Option<FnId> {
31        self.functions.get(&(name.clone(), num_params)).copied()
32    }
33}
34
35#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
36pub struct VarId(pub usize);
37
38#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
39pub struct FnId(pub usize);