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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! The AmFn symbol table.
// Copyright (c) 2021 ShiftLeft Software
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::collections::HashMap;
use super::ElemSymbol;
pub struct MapSymbol {
/// The symbol table object.
symbol: HashMap<String, ElemSymbol>,
}
/// The AmFn symbol table default implementation.
impl Default for MapSymbol {
/// Create and return a new symbol table.
///
/// # Return
///
/// * See description.
fn default() -> Self {
MapSymbol::new()
}
}
/// The AmFn symbol table implementation.
impl MapSymbol {
/// Create and return a new symbol table.
///
/// # Return
///
/// * See description.
pub fn new() -> MapSymbol {
MapSymbol {
symbol: HashMap::new(),
}
}
/// Copy and return the new symbol table.
///
/// # Return
///
/// * See description.
pub fn copy(&self) -> MapSymbol {
MapSymbol {
symbol: self.symbol.clone(),
}
}
/// Clear all symbols from the symbol table.
pub fn clear(&mut self) {
self.symbol.clear();
}
/// Find and return the symbol element by name.
///
/// # Arguments
///
/// * `name` - The name of the symbol to find.
///
/// # Return
///
/// * The found symbol, otherwise None.
pub fn get_symbol(&self, name: &str) -> Option<&ElemSymbol> {
self.symbol.get(name)
}
/// Find and return the mut symbol element by name.
///
/// # Arguments
///
/// * `name` - The name of the symbol to find.
///
/// # Return
///
/// * The found symbol, otherwise None.
pub fn get_symbol_mut(&mut self, name: &str) -> Option<&mut ElemSymbol> {
self.symbol.get_mut(name)
}
/// Add the symbol to the symbol table.
///
/// # Arguments
///
/// * `name` - The symbol name.
/// * `elem_symbol` - The symbol element.
pub fn add_symbol(&mut self, name: &str, elem_symbol: ElemSymbol) {
self.symbol.insert(String::from(name), elem_symbol);
}
}